├── .clang-format
├── .clang-tidy
├── .dockerignore
├── .gitattributes
├── .github
├── PULL_REQUEST_TEMPLATE.md
└── issue-close-app.yml
├── .gitignore
├── .gitlab-ci.yml
├── .travis.yml
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── docker
├── Dockerfile.test
└── platformio.ini
├── examples
├── custom-bmp180-sensor
│ ├── custom-bmp180-sensor.cpp
│ └── custom-bmp180-sensor.ino
├── fastled
│ ├── fastled.cpp
│ └── fastled.ino
├── livingroom
│ ├── livingroom.cpp
│ └── livingroom.ino
└── livingroom8266
│ ├── livingroom8266.cpp
│ └── livingroom8266.ino
├── lib
└── readme.txt
├── library.json
├── library.properties
├── platformio.ini
├── src
├── esphome.h
├── esphome
│ ├── api
│ │ ├── api.proto
│ │ ├── api_message.cpp
│ │ ├── api_message.h
│ │ ├── api_server.cpp
│ │ ├── api_server.h
│ │ ├── basic_messages.cpp
│ │ ├── basic_messages.h
│ │ ├── command_messages.cpp
│ │ ├── command_messages.h
│ │ ├── list_entities.cpp
│ │ ├── list_entities.h
│ │ ├── service_call_message.cpp
│ │ ├── service_call_message.h
│ │ ├── subscribe_logs.cpp
│ │ ├── subscribe_logs.h
│ │ ├── subscribe_state.cpp
│ │ ├── subscribe_state.h
│ │ ├── user_services.cpp
│ │ ├── user_services.h
│ │ ├── util.cpp
│ │ └── util.h
│ ├── application.cpp
│ ├── application.h
│ ├── automation.cpp
│ ├── automation.h
│ ├── automation.tcc
│ ├── binary_sensor
│ │ ├── binary_sensor.cpp
│ │ ├── binary_sensor.h
│ │ ├── custom_binary_sensor.cpp
│ │ ├── custom_binary_sensor.h
│ │ ├── esp32_touch_binary_sensor.cpp
│ │ ├── esp32_touch_binary_sensor.h
│ │ ├── filter.cpp
│ │ ├── filter.h
│ │ ├── gpio_binary_sensor_component.cpp
│ │ ├── gpio_binary_sensor_component.h
│ │ ├── homeassistant_binary_sensor.cpp
│ │ ├── homeassistant_binary_sensor.h
│ │ ├── mpr121_sensor.cpp
│ │ ├── mpr121_sensor.h
│ │ ├── mqtt_binary_sensor_component.cpp
│ │ ├── mqtt_binary_sensor_component.h
│ │ ├── pn532_component.cpp
│ │ ├── pn532_component.h
│ │ ├── rdm6300.cpp
│ │ ├── rdm6300.h
│ │ ├── status_binary_sensor.cpp
│ │ ├── status_binary_sensor.h
│ │ ├── template_binary_sensor.cpp
│ │ ├── template_binary_sensor.h
│ │ ├── ttp229_lsf_sensor.cpp
│ │ └── ttp229_lsf_sensor.h
│ ├── climate
│ │ ├── bang_bang_climate.cpp
│ │ ├── bang_bang_climate.h
│ │ ├── climate_automation.h
│ │ ├── climate_device.cpp
│ │ ├── climate_device.h
│ │ ├── climate_mode.cpp
│ │ ├── climate_mode.h
│ │ ├── climate_traits.cpp
│ │ ├── climate_traits.h
│ │ ├── mqtt_climate_component.cpp
│ │ └── mqtt_climate_component.h
│ ├── component.cpp
│ ├── component.h
│ ├── controller.cpp
│ ├── controller.h
│ ├── cover
│ │ ├── cover.cpp
│ │ ├── cover.h
│ │ ├── cover_automation.h
│ │ ├── cover_traits.cpp
│ │ ├── cover_traits.h
│ │ ├── endstop_cover.cpp
│ │ ├── endstop_cover.h
│ │ ├── mqtt_cover_component.cpp
│ │ ├── mqtt_cover_component.h
│ │ ├── template_cover.cpp
│ │ ├── template_cover.h
│ │ ├── time_based_cover.cpp
│ │ └── time_based_cover.h
│ ├── custom_component.cpp
│ ├── custom_component.h
│ ├── debug_component.cpp
│ ├── debug_component.h
│ ├── deep_sleep_component.cpp
│ ├── deep_sleep_component.h
│ ├── defines.h
│ ├── display
│ │ ├── display.cpp
│ │ ├── display.h
│ │ ├── lcd_display.cpp
│ │ ├── lcd_display.h
│ │ ├── max7219.cpp
│ │ ├── max7219.h
│ │ ├── nextion.cpp
│ │ ├── nextion.h
│ │ ├── ssd1306.cpp
│ │ ├── ssd1306.h
│ │ ├── waveshare_epaper.cpp
│ │ └── waveshare_epaper.h
│ ├── esp32_ble_beacon.cpp
│ ├── esp32_ble_beacon.h
│ ├── esp32_ble_tracker.cpp
│ ├── esp32_ble_tracker.h
│ ├── esp32_camera.cpp
│ ├── esp32_camera.h
│ ├── esp_one_wire.cpp
│ ├── esp_one_wire.h
│ ├── esphal.cpp
│ ├── esphal.h
│ ├── espmath.h
│ ├── esppreferences.cpp
│ ├── esppreferences.h
│ ├── ethernet_component.cpp
│ ├── ethernet_component.h
│ ├── fan
│ │ ├── basic_fan_component.cpp
│ │ ├── basic_fan_component.h
│ │ ├── fan_state.cpp
│ │ ├── fan_state.h
│ │ ├── fan_traits.cpp
│ │ ├── fan_traits.h
│ │ ├── mqtt_fan_component.cpp
│ │ └── mqtt_fan_component.h
│ ├── helpers.cpp
│ ├── helpers.h
│ ├── i2c_component.cpp
│ ├── i2c_component.h
│ ├── io
│ │ ├── mcp23017.cpp
│ │ ├── mcp23017.h
│ │ ├── pcf8574_component.cpp
│ │ └── pcf8574_component.h
│ ├── light
│ │ ├── addressable_light.cpp
│ │ ├── addressable_light.h
│ │ ├── addressable_light.tcc
│ │ ├── addressable_light_effect.cpp
│ │ ├── addressable_light_effect.h
│ │ ├── fast_led_light_output.cpp
│ │ ├── fast_led_light_output.h
│ │ ├── light_automation.h
│ │ ├── light_color_values.cpp
│ │ ├── light_color_values.h
│ │ ├── light_effect.cpp
│ │ ├── light_effect.h
│ │ ├── light_output_component.cpp
│ │ ├── light_output_component.h
│ │ ├── light_state.cpp
│ │ ├── light_state.h
│ │ ├── light_traits.cpp
│ │ ├── light_traits.h
│ │ ├── light_transformer.cpp
│ │ ├── light_transformer.h
│ │ ├── mqtt_json_light_component.cpp
│ │ ├── mqtt_json_light_component.h
│ │ ├── neo_pixel_bus_light_output.h
│ │ └── neo_pixel_bus_light_output.tcc
│ ├── log.cpp
│ ├── log.h
│ ├── log_component.cpp
│ ├── log_component.h
│ ├── mqtt
│ │ ├── custom_mqtt_device.cpp
│ │ ├── custom_mqtt_device.h
│ │ ├── mqtt_client_component.cpp
│ │ ├── mqtt_client_component.h
│ │ ├── mqtt_component.cpp
│ │ └── mqtt_component.h
│ ├── optional.h
│ ├── ota_component.cpp
│ ├── ota_component.h
│ ├── output
│ │ ├── binary_output.cpp
│ │ ├── binary_output.h
│ │ ├── copy_output.cpp
│ │ ├── copy_output.h
│ │ ├── custom_output.cpp
│ │ ├── custom_output.h
│ │ ├── esp8266_pwm_output.cpp
│ │ ├── esp8266_pwm_output.h
│ │ ├── float_output.cpp
│ │ ├── float_output.h
│ │ ├── gpio_binary_output_component.cpp
│ │ ├── gpio_binary_output_component.h
│ │ ├── ledc_output_component.cpp
│ │ ├── ledc_output_component.h
│ │ ├── my9231_output_component.cpp
│ │ ├── my9231_output_component.h
│ │ ├── pca9685_output_component.cpp
│ │ └── pca9685_output_component.h
│ ├── power_supply_component.cpp
│ ├── power_supply_component.h
│ ├── remote
│ │ ├── jvc.cpp
│ │ ├── jvc.h
│ │ ├── lg.cpp
│ │ ├── lg.h
│ │ ├── nec.cpp
│ │ ├── nec.h
│ │ ├── panasonic.cpp
│ │ ├── panasonic.h
│ │ ├── raw.cpp
│ │ ├── raw.h
│ │ ├── rc5.cpp
│ │ ├── rc5.h
│ │ ├── rc_switch.cpp
│ │ ├── rc_switch.h
│ │ ├── rc_switch_protocol.cpp
│ │ ├── rc_switch_protocol.h
│ │ ├── remote_protocol.cpp
│ │ ├── remote_protocol.h
│ │ ├── remote_receiver.cpp
│ │ ├── remote_receiver.h
│ │ ├── remote_transmitter.cpp
│ │ ├── remote_transmitter.h
│ │ ├── samsung.cpp
│ │ ├── samsung.h
│ │ ├── sony.cpp
│ │ └── sony.h
│ ├── sensor
│ │ ├── adc.cpp
│ │ ├── adc.h
│ │ ├── ads1115_component.cpp
│ │ ├── ads1115_component.h
│ │ ├── apds9960.cpp
│ │ ├── apds9960.h
│ │ ├── bh1750_sensor.cpp
│ │ ├── bh1750_sensor.h
│ │ ├── bme280_component.cpp
│ │ ├── bme280_component.h
│ │ ├── bme680_component.cpp
│ │ ├── bme680_component.h
│ │ ├── bmp085_component.cpp
│ │ ├── bmp085_component.h
│ │ ├── bmp280_component.cpp
│ │ ├── bmp280_component.h
│ │ ├── cse7766.cpp
│ │ ├── cse7766.h
│ │ ├── custom_sensor.cpp
│ │ ├── custom_sensor.h
│ │ ├── dallas_component.cpp
│ │ ├── dallas_component.h
│ │ ├── dht12_component.cpp
│ │ ├── dht12_component.h
│ │ ├── dht_component.cpp
│ │ ├── dht_component.h
│ │ ├── duty_cycle_sensor.cpp
│ │ ├── duty_cycle_sensor.h
│ │ ├── esp32_hall_sensor.cpp
│ │ ├── esp32_hall_sensor.h
│ │ ├── filter.cpp
│ │ ├── filter.h
│ │ ├── hdc1080_component.cpp
│ │ ├── hdc1080_component.h
│ │ ├── hlw8012.cpp
│ │ ├── hlw8012.h
│ │ ├── hmc5883l.cpp
│ │ ├── hmc5883l.h
│ │ ├── homeassistant_sensor.cpp
│ │ ├── homeassistant_sensor.h
│ │ ├── htu21d_component.cpp
│ │ ├── htu21d_component.h
│ │ ├── hx711.cpp
│ │ ├── hx711.h
│ │ ├── ina219.cpp
│ │ ├── ina219.h
│ │ ├── ina3221.cpp
│ │ ├── ina3221.h
│ │ ├── max31855_sensor.cpp
│ │ ├── max31855_sensor.h
│ │ ├── max6675_sensor.cpp
│ │ ├── max6675_sensor.h
│ │ ├── mhz19_component.cpp
│ │ ├── mhz19_component.h
│ │ ├── mpu6050_component.cpp
│ │ ├── mpu6050_component.h
│ │ ├── mqtt_sensor_component.cpp
│ │ ├── mqtt_sensor_component.h
│ │ ├── mqtt_subscribe_sensor.cpp
│ │ ├── mqtt_subscribe_sensor.h
│ │ ├── ms5611.cpp
│ │ ├── ms5611.h
│ │ ├── pmsx003.cpp
│ │ ├── pmsx003.h
│ │ ├── pulse_counter.cpp
│ │ ├── pulse_counter.h
│ │ ├── rotary_encoder.cpp
│ │ ├── rotary_encoder.h
│ │ ├── sds011_component.cpp
│ │ ├── sds011_component.h
│ │ ├── sensor.cpp
│ │ ├── sensor.h
│ │ ├── sht3xd_component.cpp
│ │ ├── sht3xd_component.h
│ │ ├── tcs34725.cpp
│ │ ├── tcs34725.h
│ │ ├── template_sensor.cpp
│ │ ├── template_sensor.h
│ │ ├── total_daily_energy.cpp
│ │ ├── total_daily_energy.h
│ │ ├── tsl2561_sensor.cpp
│ │ ├── tsl2561_sensor.h
│ │ ├── ultrasonic_sensor.cpp
│ │ ├── ultrasonic_sensor.h
│ │ ├── uptime_sensor.cpp
│ │ ├── uptime_sensor.h
│ │ ├── wifi_signal_sensor.cpp
│ │ └── wifi_signal_sensor.h
│ ├── servo.cpp
│ ├── servo.h
│ ├── spi_component.cpp
│ ├── spi_component.h
│ ├── status_led.cpp
│ ├── status_led.h
│ ├── stepper
│ │ ├── a4988.cpp
│ │ ├── a4988.h
│ │ ├── stepper.cpp
│ │ ├── stepper.h
│ │ ├── uln2003.cpp
│ │ └── uln2003.h
│ ├── switch_
│ │ ├── custom_switch.cpp
│ │ ├── custom_switch.h
│ │ ├── gpio_switch.cpp
│ │ ├── gpio_switch.h
│ │ ├── mqtt_switch_component.cpp
│ │ ├── mqtt_switch_component.h
│ │ ├── output_switch.cpp
│ │ ├── output_switch.h
│ │ ├── restart_switch.cpp
│ │ ├── restart_switch.h
│ │ ├── shutdown_switch.cpp
│ │ ├── shutdown_switch.h
│ │ ├── switch.cpp
│ │ ├── switch.h
│ │ ├── template_switch.cpp
│ │ ├── template_switch.h
│ │ ├── uart_switch.cpp
│ │ └── uart_switch.h
│ ├── text_sensor
│ │ ├── custom_text_sensor.cpp
│ │ ├── custom_text_sensor.h
│ │ ├── homeassistant_text_sensor.cpp
│ │ ├── homeassistant_text_sensor.h
│ │ ├── mqtt_subscribe_text_sensor.cpp
│ │ ├── mqtt_subscribe_text_sensor.h
│ │ ├── mqtt_text_sensor.cpp
│ │ ├── mqtt_text_sensor.h
│ │ ├── template_text_sensor.cpp
│ │ ├── template_text_sensor.h
│ │ ├── text_sensor.cpp
│ │ ├── text_sensor.h
│ │ ├── version_text_sensor.cpp
│ │ ├── version_text_sensor.h
│ │ ├── wifi_info.cpp
│ │ └── wifi_info.h
│ ├── time
│ │ ├── homeassistant_time.cpp
│ │ ├── homeassistant_time.h
│ │ ├── rtc_component.cpp
│ │ ├── rtc_component.h
│ │ ├── sntp_component.cpp
│ │ └── sntp_component.h
│ ├── uart_component.cpp
│ ├── uart_component.h
│ ├── util.cpp
│ ├── util.h
│ ├── web_server.cpp
│ ├── web_server.h
│ ├── wifi_component.cpp
│ ├── wifi_component.h
│ ├── wifi_component_esp32.cpp
│ └── wifi_component_esp8266.cpp
└── esphomelib.h
└── travis
├── neopixelbus.patch
├── run-clang-format.py
├── run-clang-tidy.py
├── suggest-changes.sh
└── travis.py
/.dockerignore:
--------------------------------------------------------------------------------
1 | .pioenvs
2 | .piolibdeps
3 | CMakeListsPrivate.txt
4 |
5 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
6 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
7 |
8 | # User-specific stuff:
9 | .idea/**/workspace.xml
10 | .idea/**/tasks.xml
11 | .idea/dictionaries
12 |
13 | # Sensitive or high-churn files:
14 | .idea/**/dataSources/
15 | .idea/**/dataSources.ids
16 | .idea/**/dataSources.xml
17 | .idea/**/dataSources.local.xml
18 | .idea/**/dynamic.xml
19 |
20 | # CMake
21 | cmake-build-debug/
22 | cmake-build-release/
23 |
24 | # pyenv
25 | .python-version
26 | .clang_complete
27 | .gcc-flags.json
28 | .git/
29 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | * text eol=lf
2 |
3 | *.ico binary
4 | *.jpg binary
5 | *.png binary
6 | *.zip binary
7 | *.mp3 binary
8 | *.gif binary
9 |
--------------------------------------------------------------------------------
/.github/PULL_REQUEST_TEMPLATE.md:
--------------------------------------------------------------------------------
1 | ## Description:
2 |
3 |
4 | **Related issue (if applicable):** fixes
5 |
6 | **Pull request in [esphome](https://github.com/esphome/esphome) with python changes (if applicable):** esphome/esphome#
7 | **Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#
8 |
9 | ## Checklist:
10 |
11 | - [ ] The code change is tested and works locally.
12 | - [ ] The code change follows the [standards](https://esphome.io/guides/contributing.html#contributing-to-esphome-core)
13 |
14 | If user exposed functionality or configuration variables are added/changed:
15 | - [ ] Documentation added/updated in [esphome-docs](https://github.com/esphome/esphome-docs).
16 |
--------------------------------------------------------------------------------
/.github/issue-close-app.yml:
--------------------------------------------------------------------------------
1 | comment: >-
2 | https://github.com/esphome/esphome-core/issues/499
3 | issueConfigs:
4 | - content:
5 | - "OTHERWISE THE ISSUE WILL BE CLOSED AUTOMATICALLY"
6 |
7 | caseInsensitive: false
8 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .pioenvs
2 | .piolibdeps
3 | .vscode
4 | CMakeListsPrivate.txt
5 | CMakeLists.txt
6 |
7 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
8 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
9 |
10 | # User-specific stuff:
11 | .idea/**/workspace.xml
12 | .idea/**/tasks.xml
13 | .idea/dictionaries
14 |
15 | # Sensitive or high-churn files:
16 | .idea/**/dataSources/
17 | .idea/**/dataSources.ids
18 | .idea/**/dataSources.xml
19 | .idea/**/dataSources.local.xml
20 | .idea/**/dynamic.xml
21 |
22 | # CMake
23 | cmake-build-debug/
24 | cmake-build-release/
25 |
26 | # pyenv
27 | .python-version
28 | .clang_complete
29 | .gcc-flags.json
30 |
31 | CMakeCache.txt
32 | CMakeFiles
33 | CMakeScripts
34 | Testing
35 | Makefile
36 | cmake_install.cmake
37 | install_manifest.txt
38 | compile_commands.json
39 | CTestTestfile.cmake
40 | /*.cbp
41 |
--------------------------------------------------------------------------------
/.gitlab-ci.yml:
--------------------------------------------------------------------------------
1 | ---
2 | # Gitlab CI configuration for my custom build server. For contributors,
3 | # please use .travis.yml
4 |
5 | stages:
6 | # - lint
7 | - test
8 |
9 | .test: &test
10 | tags:
11 | - python2.7
12 | - esphomelib-test
13 | stage: test
14 | cache:
15 | paths:
16 | - /root/.platformio
17 | - .piolibdeps
18 |
19 | test-basic:
20 | <<: *test
21 | script:
22 | - |
23 | platformio run \
24 | -e livingroom \
25 | -e livingroom8266
26 |
27 | test-advanced:
28 | <<: *test
29 | script:
30 | - |
31 | platformio run \
32 | -e custombmp180
33 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: python
2 | python:
3 | - "2.7"
4 | install:
5 | - pip install -U platformio
6 | cache:
7 | directories:
8 | - "~/.platformio"
9 | - "$TRAVIS_BUILD_DIR/.piolibdeps"
10 |
11 | matrix:
12 | fast_finish: true
13 | include:
14 | - env: TARGET=lint
15 | dist: trusty
16 | sudo: required
17 | addons:
18 | apt:
19 | sources:
20 | - ubuntu-toolchain-r-test
21 | - llvm-toolchain-trusty-7
22 | packages:
23 | - cmake
24 | - clang-tidy-7
25 | - clang-format-7
26 | before_script:
27 | - pio init --ide clion
28 | - |
29 | if ! patch -R -p0 -s -f --dry-run 🚨 This repository is no longer used 🚨
6 | >
7 | > Starting with v1.13.0 of ESPHome, the core codebase has merged into the python codebase.
8 | >
9 | > Please create all patches against https://github.com/esphome/esphome.
10 | > See https://github.com/esphome/feature-requests/issues/97 for more info.
11 |
12 | This is the source of the C++ library that powers [ESPHome](https://esphome.io/)
13 |
14 | For issues, please go to [the issue tracker](https://github.com/esphome/issues/issues).
15 |
16 | For feature requests, please see [feature requests](https://github.com/esphome/feature-requests/issues).
17 |
18 | > Note: Starting with 1.10.0 using ESPHome-Core directly through C++ is no longer officially
19 | > supported. Please use the YAML syntax with ESPHome.
20 |
--------------------------------------------------------------------------------
/docker/Dockerfile.test:
--------------------------------------------------------------------------------
1 | FROM python:2.7
2 | MAINTAINER Otto Winter
3 |
4 | RUN pip install --no-cache-dir platformio && \
5 | platformio settings set enable_telemetry No
6 |
7 | COPY docker/platformio.ini /
8 | RUN platformio run -e espressif32 -e espressif8266; exit 0
9 |
--------------------------------------------------------------------------------
/docker/platformio.ini:
--------------------------------------------------------------------------------
1 | ; This file allows the docker build file to install the required platformio
2 | ; platforms
3 |
4 | [env:espressif8266]
5 | platform = espressif8266
6 | board = nodemcuv2
7 | framework = arduino
8 |
9 | [env:espressif32]
10 | platform = espressif32
11 | board = nodemcu-32s
12 | framework = arduino
13 |
--------------------------------------------------------------------------------
/examples/custom-bmp180-sensor/custom-bmp180-sensor.ino:
--------------------------------------------------------------------------------
1 | // This file intentionally left blank
2 | // See other tab for the code
3 |
--------------------------------------------------------------------------------
/examples/fastled/fastled.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using namespace esphome;
5 | using namespace esphome::light;
6 |
7 | /// Custom FastLED effect - Note: this will only work with FastLED lights
8 | class CustomLightEffect : public AddressableLightEffect {
9 | public:
10 | CustomLightEffect(const std::string &name) : AddressableLightEffect(name) {}
11 |
12 | void apply(AddressableLight &it, const ESPColor ¤t_color) override {
13 | ESPHSVColor hsv;
14 | hsv.value = 255; // brightness
15 | hsv.saturation = 240; // saturation
16 | hsv.hue = millis() / 70;
17 | for (int i = 0; i < it.size(); i++) {
18 | it[i] = hsv;
19 | hsv.hue += 10;
20 | }
21 | }
22 | };
23 |
24 | void setup() {
25 | App.set_name("fastled");
26 | App.init_log();
27 |
28 | App.init_wifi("YOUR_SSID", "YOUR_PASSWORD");
29 | App.init_mqtt("MQTT_HOST", "USERNAME", "PASSWORD");
30 | App.init_ota()->start_safe_mode();
31 |
32 | auto fast_led = App.make_fast_led_light("Fast LED Light");
33 | // 60 NEOPIXEL LEDS on pin GPIO23
34 | fast_led.fast_led->add_leds(60);
35 |
36 | // Register our custom effect
37 | fast_led.state->add_effects({new CustomLightEffect("My Custom Effect")});
38 |
39 | App.setup();
40 | }
41 |
42 | void loop() {
43 | App.loop();
44 | // Make sure this is not too low, some FastLED lights do not like
45 | // getting updated too often
46 | delay(16);
47 | }
48 |
--------------------------------------------------------------------------------
/examples/fastled/fastled.ino:
--------------------------------------------------------------------------------
1 | // This file intentionally left blank
2 | // See other tab for the code
3 |
--------------------------------------------------------------------------------
/examples/livingroom/livingroom.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace esphome;
4 |
5 | void setup() {
6 | App.set_name("livingroom");
7 | App.init_log();
8 |
9 | App.init_wifi("YOUR_SSID", "YOUR_PASSWORD");
10 | App.init_mqtt("MQTT_HOST", "USERNAME", "PASSWORD");
11 | App.init_ota()->start_safe_mode();
12 |
13 | // LEDC is only available on ESP32! for the ESP8266, take a look at App.make_esp8266_pwm_output().
14 | auto *red = App.make_ledc_output(32); // on pin 32
15 | auto *green = App.make_ledc_output(33);
16 | auto *blue = App.make_ledc_output(34);
17 | App.make_rgb_light("Livingroom Light", red, green, blue);
18 |
19 | App.make_dht_sensor("Livingroom Temperature", "Livingroom Humidity", 12);
20 | App.make_status_binary_sensor("Livingroom Node Status");
21 | App.make_restart_switch("Livingroom Restart");
22 |
23 | App.setup();
24 | }
25 |
26 | void loop() {
27 | App.loop();
28 | }
29 |
--------------------------------------------------------------------------------
/examples/livingroom/livingroom.ino:
--------------------------------------------------------------------------------
1 | // This file intentionally left blank
2 | // See other tab for the code
3 |
--------------------------------------------------------------------------------
/examples/livingroom8266/livingroom8266.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace esphome;
4 |
5 | void setup() {
6 | App.set_name("livingroom");
7 | App.init_log();
8 |
9 | App.init_wifi("YOUR_SSID", "YOUR_PASSWORD");
10 | App.init_mqtt("MQTT_HOST", "USERNAME", "PASSWORD");
11 | App.init_ota()->start_safe_mode();
12 |
13 | auto *lamp = App.make_gpio_output(32); // on pin 32
14 | App.make_binary_light("Standing Lamp", lamp);
15 |
16 | App.make_dht_sensor("Livingroom Temperature", "Livingroom Humidity", 4);
17 |
18 | App.make_monochromatic_light("Desk Lamp", App.make_esp8266_pwm_output(D2));
19 |
20 | App.setup();
21 | }
22 |
23 | void loop() {
24 | App.loop();
25 | }
26 |
--------------------------------------------------------------------------------
/examples/livingroom8266/livingroom8266.ino:
--------------------------------------------------------------------------------
1 | // This file intentionally left blank
2 | // See other tab for the code
3 |
--------------------------------------------------------------------------------
/lib/readme.txt:
--------------------------------------------------------------------------------
1 |
2 | This directory is intended for the project specific (private) libraries.
3 | PlatformIO will compile them to static libraries and link to executable file.
4 |
5 | The source code of each library should be placed in separate directory, like
6 | "lib/private_lib/[here are source files]".
7 |
8 | For example, see how can be organized `Foo` and `Bar` libraries:
9 |
10 | |--lib
11 | | |--Bar
12 | | | |--docs
13 | | | |--examples
14 | | | |--src
15 | | | |- Bar.c
16 | | | |- Bar.h
17 | | |--Foo
18 | | | |- Foo.c
19 | | | |- Foo.h
20 | | |- readme.txt --> THIS FILE
21 | |- platformio.ini
22 | |--src
23 | |- main.c
24 |
25 | Then in `src/main.c` you should use:
26 |
27 | #include
28 | #include
29 |
30 | // rest H/C/CPP code
31 |
32 | PlatformIO will find your libraries automatically, configure preprocessor's
33 | include paths and build them.
34 |
35 | More information about PlatformIO Library Dependency Finder
36 | - http://docs.platformio.org/page/librarymanager/ldf.html
37 |
--------------------------------------------------------------------------------
/library.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "esphome-core",
3 | "keywords": "esp32, home-assistant, ir, ledc, mqtt, iot",
4 | "description": "ESPHome - Reimagining DIY Home Automation.",
5 | "repository": {
6 | "type": "git",
7 | "url": "https://github.com/esphome/esphome-core.git"
8 | },
9 | "version": "1.13.0-dev",
10 | "authors": [
11 | {
12 | "name": "Otto Winter",
13 | "email": "contact@otto-winter.com",
14 | "url": "https://otto-winter.com",
15 | "maintainer": true
16 | }
17 | ],
18 | "dependencies": [
19 | {
20 | "name": "ArduinoJson-esphomelib",
21 | "version": "5.13.3"
22 | }
23 | ],
24 | "build": {
25 | "flags": "-Wno-reorder"
26 | },
27 | "license": "GPL-3.0",
28 | "frameworks": "arduino",
29 | "platforms": [
30 | "espressif32",
31 | "espressif8266"
32 | ]
33 | }
34 |
--------------------------------------------------------------------------------
/library.properties:
--------------------------------------------------------------------------------
1 | name=esphome-core
2 | version=1.13.0-dev
3 | author=Otto Winter
4 | maintainer=Otto Winter
5 | sentence=ESPHome - Reimagining DIY Home Automation.
6 | paragraph=
7 | category=Other
8 | url=https://github.com/esphome/esphome-core
9 | architectures=esp32,esp8266
10 |
--------------------------------------------------------------------------------
/platformio.ini:
--------------------------------------------------------------------------------
1 | ; This file is here for developing esphome-core
2 |
3 | [platformio]
4 | env_default = livingroom8266
5 | src_dir = .
6 | include_dir = src
7 |
8 | [common]
9 | lib_deps =
10 | AsyncTCP@1.0.3
11 | AsyncMqttClient@0.8.2
12 | ArduinoJson-esphomelib@5.13.3
13 | ESP Async WebServer@1.1.1
14 | FastLED@3.2.0
15 | NeoPixelBus@2.4.1
16 | ESPAsyncTCP@1.2.0
17 | build_flags =
18 | -Wno-reorder
19 | -DUSE_WEB_SERVER
20 | -DUSE_FAST_LED_LIGHT
21 | -DUSE_NEO_PIXEL_BUS_LIGHT
22 | -DDONT_STORE_LOG_STR_IN_FLASH
23 | ; Don't use FlashStringHelper for debug builds because CLion freaks out for all
24 | ; log messages
25 | src_filter = +
26 |
27 | [env:livingroom]
28 | platform = espressif32@1.6.0
29 | board = nodemcu-32s
30 | framework = arduino
31 | lib_deps = ${common.lib_deps}
32 | build_flags = ${common.build_flags} -DUSE_ETHERNET
33 | src_filter = ${common.src_filter} +
34 |
35 | [env:livingroom8266]
36 | platform = espressif8266@1.8.0
37 | board = nodemcuv2
38 | framework = arduino
39 | lib_deps = ${common.lib_deps}
40 | build_flags = ${common.build_flags} -DESPHOME_LOG_LEVEL=6
41 | src_filter = ${common.src_filter} +
42 |
43 | [env:custombmp180]
44 | platform = espressif8266@1.8.0
45 | board = nodemcuv2
46 | framework = arduino
47 | lib_deps =
48 | ${common.lib_deps}
49 | Adafruit BMP085 Library
50 | build_flags = ${common.build_flags}
51 | src_filter = ${common.src_filter} +
52 |
53 | [env:fastled]
54 | platform = espressif32@1.6.0
55 | board = nodemcu-32s
56 | framework = arduino
57 | lib_deps = ${common.lib_deps}
58 | build_flags = ${common.build_flags}
59 | src_filter = ${common.src_filter} +
60 |
--------------------------------------------------------------------------------
/src/esphome.h:
--------------------------------------------------------------------------------
1 | #include "esphome/application.h"
2 |
--------------------------------------------------------------------------------
/src/esphome/api/basic_messages.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_API_BASIC_MESSAGES_H
2 | #define ESPHOME_API_BASIC_MESSAGES_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_API
7 |
8 | #include "esphome/api/api_message.h"
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | namespace api {
13 |
14 | class HelloRequest : public APIMessage {
15 | public:
16 | bool decode_length_delimited(uint32_t field_id, const uint8_t *value, size_t len) override;
17 | const std::string &get_client_info() const;
18 | void set_client_info(const std::string &client_info);
19 | APIMessageType message_type() const override;
20 |
21 | protected:
22 | std::string client_info_;
23 | };
24 |
25 | class ConnectRequest : public APIMessage {
26 | public:
27 | bool decode_length_delimited(uint32_t field_id, const uint8_t *value, size_t len) override;
28 | const std::string &get_password() const;
29 | void set_password(const std::string &password);
30 | APIMessageType message_type() const override;
31 |
32 | protected:
33 | std::string password_;
34 | };
35 |
36 | class DeviceInfoRequest : public APIMessage {
37 | public:
38 | APIMessageType message_type() const override;
39 | };
40 |
41 | class DisconnectRequest : public APIMessage {
42 | public:
43 | bool decode_length_delimited(uint32_t field_id, const uint8_t *value, size_t len) override;
44 | void encode(APIBuffer &buffer) override;
45 | APIMessageType message_type() const override;
46 | const std::string &get_reason() const;
47 | void set_reason(const std::string &reason);
48 |
49 | protected:
50 | std::string reason_;
51 | };
52 |
53 | class DisconnectResponse : public APIMessage {
54 | public:
55 | APIMessageType message_type() const override;
56 | };
57 |
58 | class PingRequest : public APIMessage {
59 | public:
60 | APIMessageType message_type() const override;
61 | };
62 |
63 | class PingResponse : public APIMessage {
64 | public:
65 | APIMessageType message_type() const override;
66 | };
67 |
68 | } // namespace api
69 |
70 | ESPHOME_NAMESPACE_END
71 |
72 | #endif // USE_API
73 |
74 | #endif // ESPHOME_API_BASIC_MESSAGES_H
75 |
--------------------------------------------------------------------------------
/src/esphome/api/list_entities.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_LIST_ENTITIES_H
2 | #define ESPHOME_LIST_ENTITIES_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_API
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/api/api_message.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace api {
14 |
15 | class ListEntitiesRequest : public APIMessage {
16 | public:
17 | APIMessageType message_type() const override;
18 | };
19 |
20 | class APIConnection;
21 |
22 | class ListEntitiesIterator : public ComponentIterator {
23 | public:
24 | ListEntitiesIterator(APIServer *server, APIConnection *client);
25 | #ifdef USE_BINARY_SENSOR
26 | bool on_binary_sensor(binary_sensor::BinarySensor *binary_sensor) override;
27 | #endif
28 | #ifdef USE_COVER
29 | bool on_cover(cover::Cover *cover) override;
30 | #endif
31 | #ifdef USE_FAN
32 | bool on_fan(fan::FanState *fan) override;
33 | #endif
34 | #ifdef USE_LIGHT
35 | bool on_light(light::LightState *light) override;
36 | #endif
37 | #ifdef USE_SENSOR
38 | bool on_sensor(sensor::Sensor *sensor) override;
39 | #endif
40 | #ifdef USE_SWITCH
41 | bool on_switch(switch_::Switch *a_switch) override;
42 | #endif
43 | #ifdef USE_TEXT_SENSOR
44 | bool on_text_sensor(text_sensor::TextSensor *text_sensor) override;
45 | #endif
46 | bool on_service(UserServiceDescriptor *service) override;
47 | #ifdef USE_ESP32_CAMERA
48 | bool on_camera(ESP32Camera *camera) override;
49 | #endif
50 | #ifdef USE_CLIMATE
51 | bool on_climate(climate::ClimateDevice *climate) override;
52 | #endif
53 | bool on_end() override;
54 |
55 | protected:
56 | APIConnection *client_;
57 | };
58 |
59 | } // namespace api
60 |
61 | ESPHOME_NAMESPACE_END
62 |
63 | #include "esphome/api/api_server.h"
64 |
65 | #endif // USE_API
66 |
67 | #endif // ESPHOME_LIST_ENTITIES_H
68 |
--------------------------------------------------------------------------------
/src/esphome/api/service_call_message.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_API
4 |
5 | #include "esphome/api/service_call_message.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace api {
11 |
12 | APIMessageType SubscribeServiceCallsRequest::message_type() const {
13 | return APIMessageType::SUBSCRIBE_SERVICE_CALLS_REQUEST;
14 | }
15 |
16 | APIMessageType ServiceCallResponse::message_type() const { return APIMessageType::SERVICE_CALL_RESPONSE; }
17 | void ServiceCallResponse::encode(APIBuffer &buffer) {
18 | // string service = 1;
19 | buffer.encode_string(1, this->service_);
20 | // map data = 2;
21 | for (auto &it : this->data_) {
22 | auto nested = buffer.begin_nested(2);
23 | buffer.encode_string(1, it.key);
24 | buffer.encode_string(2, it.value);
25 | buffer.end_nested(nested);
26 | }
27 | // map data_template = 3;
28 | for (auto &it : this->data_template_) {
29 | auto nested = buffer.begin_nested(3);
30 | buffer.encode_string(1, it.key);
31 | buffer.encode_string(2, it.value);
32 | buffer.end_nested(nested);
33 | }
34 | // map variables = 4;
35 | for (auto &it : this->variables_) {
36 | auto nested = buffer.begin_nested(4);
37 | buffer.encode_string(1, it.key);
38 | buffer.encode_string(2, it.value());
39 | buffer.end_nested(nested);
40 | }
41 | }
42 | void ServiceCallResponse::set_service(const std::string &service) { this->service_ = service; }
43 | void ServiceCallResponse::set_data(const std::vector &data) { this->data_ = data; }
44 | void ServiceCallResponse::set_data_template(const std::vector &data_template) {
45 | this->data_template_ = data_template;
46 | }
47 | void ServiceCallResponse::set_variables(const std::vector &variables) {
48 | this->variables_ = variables;
49 | }
50 |
51 | KeyValuePair::KeyValuePair(const std::string &key, const std::string &value) : key(key), value(value) {}
52 | } // namespace api
53 |
54 | ESPHOME_NAMESPACE_END
55 |
56 | #endif // USE_API
57 |
--------------------------------------------------------------------------------
/src/esphome/api/service_call_message.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_API_SERVICE_CALL_MESSAGE_H
2 | #define ESPHOME_API_SERVICE_CALL_MESSAGE_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_API
7 |
8 | #include "esphome/helpers.h"
9 | #include "esphome/automation.h"
10 | #include "esphome/api/api_message.h"
11 |
12 | ESPHOME_NAMESPACE_BEGIN
13 |
14 | namespace api {
15 |
16 | class SubscribeServiceCallsRequest : public APIMessage {
17 | public:
18 | APIMessageType message_type() const override;
19 | };
20 |
21 | class KeyValuePair {
22 | public:
23 | KeyValuePair(const std::string &key, const std::string &value);
24 |
25 | std::string key;
26 | std::string value;
27 | };
28 |
29 | class TemplatableKeyValuePair {
30 | public:
31 | template TemplatableKeyValuePair(std::string key, T func);
32 |
33 | std::string key;
34 | std::function value;
35 | };
36 | template TemplatableKeyValuePair::TemplatableKeyValuePair(std::string key, T func) : key(key) {
37 | this->value = [func]() -> std::string { return to_string(func()); };
38 | }
39 |
40 | class ServiceCallResponse : public APIMessage {
41 | public:
42 | APIMessageType message_type() const override;
43 |
44 | void encode(APIBuffer &buffer) override;
45 |
46 | void set_service(const std::string &service);
47 | void set_data(const std::vector &data);
48 | void set_data_template(const std::vector &data_template);
49 | void set_variables(const std::vector &variables);
50 |
51 | protected:
52 | std::string service_;
53 | std::vector data_;
54 | std::vector data_template_;
55 | std::vector variables_;
56 | };
57 |
58 | } // namespace api
59 |
60 | ESPHOME_NAMESPACE_END
61 |
62 | #endif // USE_API
63 |
64 | #endif // ESPHOME_API_SERVICE_CALL_MESSAGE_H
65 |
--------------------------------------------------------------------------------
/src/esphome/api/subscribe_logs.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_API
4 |
5 | #include "esphome/api/subscribe_logs.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace api {
11 |
12 | APIMessageType SubscribeLogsRequest::message_type() const { return APIMessageType::SUBSCRIBE_LOGS_REQUEST; }
13 | bool SubscribeLogsRequest::decode_varint(uint32_t field_id, uint32_t value) {
14 | switch (field_id) {
15 | case 1: // LogLevel level = 1;
16 | this->level_ = value;
17 | return true;
18 | case 2: // bool dump_config = 2;
19 | this->dump_config_ = value;
20 | return true;
21 | default:
22 | return false;
23 | }
24 | }
25 | uint32_t SubscribeLogsRequest::get_level() const { return this->level_; }
26 | void SubscribeLogsRequest::set_level(uint32_t level) { this->level_ = level; }
27 | bool SubscribeLogsRequest::get_dump_config() const { return this->dump_config_; }
28 | void SubscribeLogsRequest::set_dump_config(bool dump_config) { this->dump_config_ = dump_config; }
29 | } // namespace api
30 |
31 | ESPHOME_NAMESPACE_END
32 |
33 | #endif // USE_API
34 |
--------------------------------------------------------------------------------
/src/esphome/api/subscribe_logs.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_API_SUBSCRIBE_LOGS_H
2 | #define ESPHOME_API_SUBSCRIBE_LOGS_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_API
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/api/api_message.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace api {
14 |
15 | class SubscribeLogsRequest : public APIMessage {
16 | public:
17 | bool decode_varint(uint32_t field_id, uint32_t value) override;
18 | APIMessageType message_type() const override;
19 | uint32_t get_level() const;
20 | void set_level(uint32_t level);
21 | bool get_dump_config() const;
22 | void set_dump_config(bool dump_config);
23 |
24 | protected:
25 | uint32_t level_{6};
26 | bool dump_config_{false};
27 | };
28 |
29 | } // namespace api
30 |
31 | ESPHOME_NAMESPACE_END
32 |
33 | #endif // USE_API
34 |
35 | #endif // ESPHOME_API_SUBSCRIBE_LOGS_H
36 |
--------------------------------------------------------------------------------
/src/esphome/api/subscribe_state.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SUBSCRIBE_STATE_H
2 | #define ESPHOME_SUBSCRIBE_STATE_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_API
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/controller.h"
10 | #include "esphome/api/util.h"
11 | #include "esphome/api/api_message.h"
12 |
13 | ESPHOME_NAMESPACE_BEGIN
14 |
15 | namespace api {
16 |
17 | class SubscribeStatesRequest : public APIMessage {
18 | public:
19 | APIMessageType message_type() const override;
20 | };
21 |
22 | class APIConnection;
23 |
24 | class InitialStateIterator : public ComponentIterator {
25 | public:
26 | InitialStateIterator(APIServer *server, APIConnection *client);
27 | #ifdef USE_BINARY_SENSOR
28 | bool on_binary_sensor(binary_sensor::BinarySensor *binary_sensor) override;
29 | #endif
30 | #ifdef USE_COVER
31 | bool on_cover(cover::Cover *cover) override;
32 | #endif
33 | #ifdef USE_FAN
34 | bool on_fan(fan::FanState *fan) override;
35 | #endif
36 | #ifdef USE_LIGHT
37 | bool on_light(light::LightState *light) override;
38 | #endif
39 | #ifdef USE_SENSOR
40 | bool on_sensor(sensor::Sensor *sensor) override;
41 | #endif
42 | #ifdef USE_SWITCH
43 | bool on_switch(switch_::Switch *a_switch) override;
44 | #endif
45 | #ifdef USE_TEXT_SENSOR
46 | bool on_text_sensor(text_sensor::TextSensor *text_sensor) override;
47 | #endif
48 | #ifdef USE_CLIMATE
49 | bool on_climate(climate::ClimateDevice *climate) override;
50 | #endif
51 | protected:
52 | APIConnection *client_;
53 | };
54 |
55 | class SubscribeHomeAssistantStatesRequest : public APIMessage {
56 | public:
57 | APIMessageType message_type() const override;
58 | };
59 |
60 | class HomeAssistantStateResponse : public APIMessage {
61 | public:
62 | bool decode_length_delimited(uint32_t field_id, const uint8_t *value, size_t len) override;
63 | APIMessageType message_type() const override;
64 | const std::string &get_entity_id() const;
65 | const std::string &get_state() const;
66 |
67 | protected:
68 | std::string entity_id_;
69 | std::string state_;
70 | };
71 |
72 | } // namespace api
73 |
74 | ESPHOME_NAMESPACE_END
75 |
76 | #include "esphome/api/api_server.h"
77 |
78 | #endif // USE_API
79 |
80 | #endif // ESPHOME_SUBSCRIBE_STATE_H
81 |
--------------------------------------------------------------------------------
/src/esphome/automation.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/automation.h"
2 | #include "esphome/espmath.h"
3 |
4 | ESPHOME_NAMESPACE_BEGIN
5 |
6 | void StartupTrigger::setup() { this->trigger(); }
7 | float StartupTrigger::get_setup_priority() const {
8 | // Run after everything is set up
9 | return this->setup_priority_;
10 | }
11 | StartupTrigger::StartupTrigger(float setup_priority) : setup_priority_(setup_priority) {}
12 |
13 | ShutdownTrigger::ShutdownTrigger() {
14 | add_shutdown_hook([this](const char *cause) { this->trigger(cause); });
15 | }
16 |
17 | void LoopTrigger::loop() { this->trigger(); }
18 | float LoopTrigger::get_setup_priority() const { return setup_priority::HARDWARE_LATE; }
19 |
20 | void IntervalTrigger::update() { this->trigger(); }
21 | float IntervalTrigger::get_setup_priority() const { return setup_priority::HARDWARE_LATE; }
22 |
23 | IntervalTrigger::IntervalTrigger(uint32_t update_interval) : PollingComponent(update_interval) {}
24 |
25 | RangeCondition::RangeCondition() = default;
26 |
27 | bool RangeCondition::check(float x) {
28 | float min = this->min_.value(x);
29 | float max = this->max_.value(x);
30 | if (isnan(min)) {
31 | return x >= max;
32 | } else if (isnan(max)) {
33 | return x >= min;
34 | } else {
35 | return min <= x && x <= max;
36 | }
37 | }
38 |
39 | void Script::execute() { this->trigger(); }
40 | void Script::stop() { this->parent_->stop(); }
41 |
42 | ESPHOME_NAMESPACE_END
43 |
--------------------------------------------------------------------------------
/src/esphome/binary_sensor/custom_binary_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_CUSTOM_BINARY_SENSOR
4 |
5 | #include "esphome/binary_sensor/custom_binary_sensor.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace binary_sensor {
11 |
12 | static const char *TAG = "binary_sensor.custom";
13 |
14 | CustomBinarySensorConstructor::CustomBinarySensorConstructor(const std::function()> &init) {
15 | this->binary_sensors_ = init();
16 | }
17 | BinarySensor *CustomBinarySensorConstructor::get_binary_sensor(int i) { return this->binary_sensors_[i]; }
18 | void CustomBinarySensorConstructor::dump_config() {
19 | for (auto *child : this->binary_sensors_) {
20 | LOG_BINARY_SENSOR("", "Custom Binary Sensor", child);
21 | }
22 | }
23 |
24 | } // namespace binary_sensor
25 |
26 | ESPHOME_NAMESPACE_END
27 |
28 | #endif // USE_CUSTOM_BINARY_SENSOR
29 |
--------------------------------------------------------------------------------
/src/esphome/binary_sensor/custom_binary_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_BINARY_SENSOR_CUSTOM_BINARY_SENSOR_H
2 | #define ESPHOME_BINARY_SENSOR_CUSTOM_BINARY_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_CUSTOM_BINARY_SENSOR
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/binary_sensor/binary_sensor.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace binary_sensor {
14 |
15 | class CustomBinarySensorConstructor : public Component {
16 | public:
17 | CustomBinarySensorConstructor(const std::function()> &init);
18 |
19 | BinarySensor *get_binary_sensor(int i);
20 |
21 | void dump_config() override;
22 |
23 | protected:
24 | std::vector binary_sensors_;
25 | };
26 |
27 | } // namespace binary_sensor
28 |
29 | ESPHOME_NAMESPACE_END
30 |
31 | #endif // USE_CUSTOM_BINARY_SENSOR
32 |
33 | #endif // ESPHOME_BINARY_SENSOR_CUSTOM_BINARY_SENSOR_H
34 |
--------------------------------------------------------------------------------
/src/esphome/binary_sensor/filter.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_BINARY_SENSOR
4 |
5 | #include "esphome/binary_sensor/filter.h"
6 |
7 | ESPHOME_NAMESPACE_BEGIN
8 |
9 | namespace binary_sensor {
10 |
11 | void Filter::output(bool value, bool is_initial) {
12 | if (!this->dedup_.next(value))
13 | return;
14 |
15 | if (this->next_ == nullptr) {
16 | this->parent_->send_state_internal(value, is_initial);
17 | } else {
18 | this->next_->input(value, is_initial);
19 | }
20 | }
21 | void Filter::input(bool value, bool is_initial) {
22 | auto b = this->new_value(value, is_initial);
23 | if (b.has_value()) {
24 | this->output(*b, is_initial);
25 | }
26 | }
27 | DelayedOnFilter::DelayedOnFilter(uint32_t delay) : delay_(delay) {}
28 | optional DelayedOnFilter::new_value(bool value, bool is_initial) {
29 | if (value) {
30 | this->set_timeout("ON", this->delay_, [this, is_initial]() { this->output(true, is_initial); });
31 | return {};
32 | } else {
33 | this->cancel_timeout("ON");
34 | return false;
35 | }
36 | }
37 |
38 | float DelayedOnFilter::get_setup_priority() const { return setup_priority::HARDWARE; }
39 |
40 | DelayedOffFilter::DelayedOffFilter(uint32_t delay) : delay_(delay) {}
41 | optional DelayedOffFilter::new_value(bool value, bool is_initial) {
42 | if (!value) {
43 | this->set_timeout("OFF", this->delay_, [this, is_initial]() { this->output(false, is_initial); });
44 | return {};
45 | } else {
46 | this->cancel_timeout("OFF");
47 | return true;
48 | }
49 | }
50 | float DelayedOffFilter::get_setup_priority() const { return setup_priority::HARDWARE; }
51 |
52 | optional InvertFilter::new_value(bool value, bool is_initial) { return !value; }
53 |
54 | LambdaFilter::LambdaFilter(const std::function(bool)> &f) : f_(f) {}
55 |
56 | optional LambdaFilter::new_value(bool value, bool is_initial) { return this->f_(value); }
57 |
58 | optional UniqueFilter::new_value(bool value, bool is_initial) {
59 | if (this->last_value_.has_value() && *this->last_value_ == value) {
60 | return {};
61 | } else {
62 | this->last_value_ = value;
63 | return value;
64 | }
65 | }
66 | } // namespace binary_sensor
67 |
68 | ESPHOME_NAMESPACE_END
69 |
70 | #endif // USE_BINARY_SENSOR
71 |
--------------------------------------------------------------------------------
/src/esphome/binary_sensor/filter.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_FILTER_H
2 | #define ESPHOME_FILTER_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_BINARY_SENSOR
7 |
8 | #include "esphome/binary_sensor/binary_sensor.h"
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | namespace binary_sensor {
13 |
14 | class BinarySensor;
15 |
16 | class Filter {
17 | public:
18 | virtual optional new_value(bool value, bool is_initial) = 0;
19 |
20 | void input(bool value, bool is_initial);
21 |
22 | void output(bool value, bool is_initial);
23 |
24 | protected:
25 | friend BinarySensor;
26 |
27 | Filter *next_{nullptr};
28 | BinarySensor *parent_{nullptr};
29 | Deduplicator dedup_;
30 | };
31 |
32 | class DelayedOnFilter : public Filter, public Component {
33 | public:
34 | explicit DelayedOnFilter(uint32_t delay);
35 |
36 | optional new_value(bool value, bool is_initial) override;
37 |
38 | float get_setup_priority() const override;
39 |
40 | protected:
41 | uint32_t delay_;
42 | };
43 |
44 | class DelayedOffFilter : public Filter, public Component {
45 | public:
46 | explicit DelayedOffFilter(uint32_t delay);
47 |
48 | optional new_value(bool value, bool is_initial) override;
49 |
50 | float get_setup_priority() const override;
51 |
52 | protected:
53 | uint32_t delay_;
54 | };
55 |
56 | class InvertFilter : public Filter {
57 | public:
58 | optional new_value(bool value, bool is_initial) override;
59 | };
60 |
61 | class LambdaFilter : public Filter {
62 | public:
63 | explicit LambdaFilter(const std::function(bool)> &f);
64 |
65 | optional new_value(bool value, bool is_initial) override;
66 |
67 | protected:
68 | std::function(bool)> f_;
69 | };
70 |
71 | class UniqueFilter : public Filter {
72 | public:
73 | optional new_value(bool value, bool is_initial) override;
74 |
75 | protected:
76 | optional last_value_{};
77 | };
78 |
79 | } // namespace binary_sensor
80 |
81 | ESPHOME_NAMESPACE_END
82 |
83 | #endif // USE_BINARY_SENSOR
84 |
85 | #endif // ESPHOME_FILTER_H
86 |
--------------------------------------------------------------------------------
/src/esphome/binary_sensor/gpio_binary_sensor_component.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_GPIO_BINARY_SENSOR
4 |
5 | #include "esphome/binary_sensor/gpio_binary_sensor_component.h"
6 | #include "esphome/esphal.h"
7 | #include "esphome/log.h"
8 |
9 | ESPHOME_NAMESPACE_BEGIN
10 |
11 | namespace binary_sensor {
12 |
13 | static const char *TAG = "binary_sensor.gpio";
14 |
15 | void GPIOBinarySensorComponent::setup() {
16 | ESP_LOGCONFIG(TAG, "Setting up GPIO binary sensor '%s'...", this->name_.c_str());
17 | this->pin_->setup();
18 | this->publish_initial_state(this->pin_->digital_read());
19 | }
20 |
21 | void GPIOBinarySensorComponent::dump_config() {
22 | LOG_BINARY_SENSOR("", "GPIO Binary Sensor", this);
23 | LOG_PIN(" Pin: ", this->pin_);
24 | }
25 |
26 | void GPIOBinarySensorComponent::loop() { this->publish_state(this->pin_->digital_read()); }
27 |
28 | float GPIOBinarySensorComponent::get_setup_priority() const { return setup_priority::HARDWARE; }
29 | GPIOBinarySensorComponent::GPIOBinarySensorComponent(const std::string &name, GPIOPin *pin)
30 | : BinarySensor(name), pin_(pin) {}
31 |
32 | } // namespace binary_sensor
33 |
34 | ESPHOME_NAMESPACE_END
35 |
36 | #endif // USE_GPIO_BINARY_SENSOR
37 |
--------------------------------------------------------------------------------
/src/esphome/binary_sensor/gpio_binary_sensor_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_BINARY_SENSOR_GPIO_BINARY_SENSOR_COMPONENT_H
2 | #define ESPHOME_BINARY_SENSOR_GPIO_BINARY_SENSOR_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_GPIO_BINARY_SENSOR
7 |
8 | #include "esphome/binary_sensor/binary_sensor.h"
9 | #include "esphome/esphal.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace binary_sensor {
14 |
15 | /** Simple binary_sensor component for a GPIO pin.
16 | *
17 | * This class allows you to observe the digital state of a certain GPIO pin.
18 | */
19 | class GPIOBinarySensorComponent : public BinarySensor, public Component {
20 | public:
21 | /** Construct a GPIOBinarySensorComponent.
22 | *
23 | * @param name The name for this binary sensor.
24 | * @param pin The input pin, can either be an integer or GPIOInputPin.
25 | */
26 | explicit GPIOBinarySensorComponent(const std::string &name, GPIOPin *pin);
27 |
28 | // ========== INTERNAL METHODS ==========
29 | // (In most use cases you won't need these)
30 | /// Setup pin
31 | void setup() override;
32 | void dump_config() override;
33 | /// Hardware priority
34 | float get_setup_priority() const override;
35 | /// Check sensor
36 | void loop() override;
37 |
38 | protected:
39 | GPIOPin *pin_;
40 | };
41 |
42 | } // namespace binary_sensor
43 |
44 | ESPHOME_NAMESPACE_END
45 |
46 | #endif // USE_GPIO_BINARY_SENSOR
47 |
48 | #endif // ESPHOME_BINARY_SENSOR_GPIO_BINARY_SENSOR_COMPONENT_H
49 |
--------------------------------------------------------------------------------
/src/esphome/binary_sensor/homeassistant_binary_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_HOMEASSISTANT_BINARY_SENSOR
4 |
5 | #include "esphome/binary_sensor/homeassistant_binary_sensor.h"
6 | #include "esphome/api/api_server.h"
7 | #include "esphome/log.h"
8 |
9 | ESPHOME_NAMESPACE_BEGIN
10 |
11 | namespace binary_sensor {
12 |
13 | static const char *TAG = "binary_sensor.homeassistant";
14 |
15 | void HomeassistantBinarySensor::setup() {
16 | api::global_api_server->subscribe_home_assistant_state(this->entity_id_, [this](std::string state) {
17 | auto val = parse_on_off(state.c_str());
18 | switch (val) {
19 | case PARSE_NONE:
20 | case PARSE_TOGGLE:
21 | ESP_LOGW(TAG, "Can't convert '%s' to binary state!", state.c_str());
22 | break;
23 | case PARSE_ON:
24 | ESP_LOGD(TAG, "'%s': Got state ON", this->entity_id_.c_str());
25 | this->publish_state(true);
26 | break;
27 | case PARSE_OFF:
28 | ESP_LOGD(TAG, "'%s': Got state OFF", this->entity_id_.c_str());
29 | this->publish_state(false);
30 | break;
31 | }
32 | });
33 | }
34 | void HomeassistantBinarySensor::dump_config() {
35 | LOG_BINARY_SENSOR("", "Homeassistant Binary Sensor", this);
36 | ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_.c_str());
37 | }
38 | float HomeassistantBinarySensor::get_setup_priority() const { return setup_priority::WIFI; }
39 | HomeassistantBinarySensor::HomeassistantBinarySensor(const std::string &name, const std::string &entity_id)
40 | : BinarySensor(name), entity_id_(entity_id) {}
41 |
42 | } // namespace binary_sensor
43 |
44 | ESPHOME_NAMESPACE_END
45 |
46 | #endif // USE_HOMEASSISTANT_BINARY_SENSOR
47 |
--------------------------------------------------------------------------------
/src/esphome/binary_sensor/homeassistant_binary_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_HOMEASSISTANT_BINARY_SENSOR_H
2 | #define ESPHOME_HOMEASSISTANT_BINARY_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_HOMEASSISTANT_BINARY_SENSOR
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/binary_sensor/binary_sensor.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace binary_sensor {
14 |
15 | class HomeassistantBinarySensor : public BinarySensor, public Component {
16 | public:
17 | HomeassistantBinarySensor(const std::string &name, const std::string &entity_id);
18 | void setup() override;
19 | void dump_config() override;
20 | float get_setup_priority() const override;
21 |
22 | protected:
23 | std::string entity_id_;
24 | };
25 |
26 | } // namespace binary_sensor
27 |
28 | ESPHOME_NAMESPACE_END
29 |
30 | #endif // USE_HOMEASSISTANT_BINARY_SENSOR
31 |
32 | #endif // ESPHOME_HOMEASSISTANT_BINARY_SENSOR_H
33 |
--------------------------------------------------------------------------------
/src/esphome/binary_sensor/mqtt_binary_sensor_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_BINARY_SENSOR_MQTT_BINARY_SENSOR_COMPONENT_H
2 | #define ESPHOME_BINARY_SENSOR_MQTT_BINARY_SENSOR_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_MQTT_BINARY_SENSOR
7 |
8 | #include "esphome/binary_sensor/binary_sensor.h"
9 | #include "esphome/mqtt/mqtt_component.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace binary_sensor {
14 |
15 | /** Simple MQTT front-end component for a binary_sensor.
16 | *
17 | * After construction of this class, it should be connected to the BinarySensor by setting the callback returned
18 | * by create_on_new_state_callback() in BinarySensor::on_new_state().
19 | */
20 | class MQTTBinarySensorComponent : public mqtt::MQTTComponent {
21 | public:
22 | /** Construct a MQTTBinarySensorComponent.
23 | *
24 | * @param binary_sensor The binary sensor.
25 | */
26 | explicit MQTTBinarySensorComponent(BinarySensor *binary_sensor);
27 |
28 | // ========== INTERNAL METHODS ==========
29 | // (In most use cases you won't need these)
30 | /// Send discovery.
31 | void setup() override;
32 |
33 | void dump_config() override;
34 |
35 | /// Send Home Assistant discovery info
36 | void send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) override;
37 |
38 | void set_is_status(bool status);
39 |
40 | bool send_initial_state() override;
41 | bool publish_state(bool state);
42 | bool is_internal() override;
43 |
44 | protected:
45 | /// Return the friendly name of this binary sensor.
46 | std::string friendly_name() const override;
47 | /// "binary_sensor" component type.
48 | std::string component_type() const override;
49 |
50 | BinarySensor *binary_sensor_;
51 | bool is_status_{false};
52 | };
53 |
54 | } // namespace binary_sensor
55 |
56 | ESPHOME_NAMESPACE_END
57 |
58 | #endif // USE_MQTT_BINARY_SENSOR
59 |
60 | #endif // ESPHOME_BINARY_SENSOR_MQTT_BINARY_SENSOR_COMPONENT_H
61 |
--------------------------------------------------------------------------------
/src/esphome/binary_sensor/rdm6300.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_RDM_6300_H
2 | #define ESPHOME_RDM_6300_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_RDM6300
7 |
8 | #include "esphome/uart_component.h"
9 | #include "esphome/binary_sensor/binary_sensor.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace binary_sensor {
14 |
15 | class RDM6300BinarySensor;
16 |
17 | class RDM6300Component : public Component, public UARTDevice {
18 | public:
19 | RDM6300Component(UARTComponent *parent);
20 |
21 | void loop() override;
22 |
23 | RDM6300BinarySensor *make_card(const std::string &name, uint32_t id);
24 |
25 | float get_setup_priority() const override;
26 |
27 | protected:
28 | int8_t read_state_{-1};
29 | uint8_t buffer_[6];
30 | std::vector cards_;
31 | uint32_t last_id_{0};
32 | };
33 |
34 | class RDM6300BinarySensor : public BinarySensor {
35 | public:
36 | RDM6300BinarySensor(const std::string &name, uint32_t id);
37 |
38 | bool process(uint32_t id);
39 |
40 | protected:
41 | uint32_t id_;
42 | };
43 |
44 | } // namespace binary_sensor
45 |
46 | ESPHOME_NAMESPACE_END
47 |
48 | #endif // USE_RDM6300
49 |
50 | #endif // ESPHOME_RDM_6300_H
51 |
--------------------------------------------------------------------------------
/src/esphome/binary_sensor/status_binary_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_STATUS_BINARY_SENSOR
4 |
5 | #include "esphome/binary_sensor/status_binary_sensor.h"
6 | #include "esphome/mqtt/mqtt_client_component.h"
7 | #include "esphome/wifi_component.h"
8 | #include "esphome/util.h"
9 | #include "esphome/log.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace binary_sensor {
14 |
15 | static const char *TAG = "binary_sensor.status";
16 |
17 | std::string StatusBinarySensor::device_class() { return "connectivity"; }
18 | StatusBinarySensor::StatusBinarySensor(const std::string &name) : BinarySensor(name) {}
19 | void StatusBinarySensor::loop() {
20 | bool status = network_is_connected();
21 | #ifdef USE_MQTT
22 | if (mqtt::global_mqtt_client != nullptr) {
23 | status = mqtt::global_mqtt_client->is_connected();
24 | }
25 | #endif
26 |
27 | if (this->last_status_ != status) {
28 | this->publish_state(status);
29 | this->last_status_ = status;
30 | }
31 | }
32 | void StatusBinarySensor::setup() { this->publish_state(false); }
33 | float StatusBinarySensor::get_setup_priority() const { return setup_priority::HARDWARE; }
34 | bool StatusBinarySensor::is_status_binary_sensor() const { return true; }
35 | void StatusBinarySensor::dump_config() { LOG_BINARY_SENSOR("", "Status Binary Sensor", this); }
36 |
37 | } // namespace binary_sensor
38 |
39 | ESPHOME_NAMESPACE_END
40 |
41 | #endif // USE_STATUS_BINARY_SENSOR
42 |
--------------------------------------------------------------------------------
/src/esphome/binary_sensor/status_binary_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_STATUS_BINARY_SENSOR_H
2 | #define ESPHOME_STATUS_BINARY_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_STATUS_BINARY_SENSOR
7 |
8 | #include "esphome/binary_sensor/binary_sensor.h"
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | namespace binary_sensor {
13 |
14 | /** Simple binary sensor that reports the online/offline state of the node using MQTT
15 | *
16 | * Most of the magic doesn't happen here, but in Application.make_status_binary_sensor.
17 | */
18 | class StatusBinarySensor : public BinarySensor, public Component {
19 | public:
20 | /// Construct the status binary sensor
21 | explicit StatusBinarySensor(const std::string &name);
22 |
23 | void loop() override;
24 |
25 | void setup() override;
26 | void dump_config() override;
27 |
28 | float get_setup_priority() const override;
29 |
30 | bool is_status_binary_sensor() const override;
31 |
32 | protected:
33 | /// "connectivity" device class.
34 | std::string device_class() override;
35 | bool last_status_{false};
36 | };
37 |
38 | } // namespace binary_sensor
39 |
40 | ESPHOME_NAMESPACE_END
41 |
42 | #endif // USE_STATUS_BINARY_SENSOR
43 |
44 | #endif // ESPHOME_STATUS_BINARY_SENSOR_H
45 |
--------------------------------------------------------------------------------
/src/esphome/binary_sensor/template_binary_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_TEMPLATE_BINARY_SENSOR
4 |
5 | #include "esphome/binary_sensor/template_binary_sensor.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace binary_sensor {
11 |
12 | static const char *TAG = "binary_sensor.template";
13 |
14 | TemplateBinarySensor::TemplateBinarySensor(const std::string &name) : BinarySensor(name) {}
15 | void TemplateBinarySensor::loop() {
16 | if (!this->f_.has_value())
17 | return;
18 |
19 | auto s = (*this->f_)();
20 | if (s.has_value()) {
21 | this->publish_state(*s);
22 | }
23 | }
24 | float TemplateBinarySensor::get_setup_priority() const { return setup_priority::HARDWARE; }
25 | void TemplateBinarySensor::set_template(std::function()> &&f) { this->f_ = f; }
26 | void TemplateBinarySensor::dump_config() { LOG_BINARY_SENSOR("", "Template Binary Sensor", this); }
27 |
28 | } // namespace binary_sensor
29 |
30 | ESPHOME_NAMESPACE_END
31 |
32 | #endif // USE_TEMPLATE_BINARY_SENSOR
33 |
--------------------------------------------------------------------------------
/src/esphome/binary_sensor/template_binary_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_BINARY_SENSOR_TEMPLATE_BINARY_SENSOR_H
2 | #define ESPHOME_BINARY_SENSOR_TEMPLATE_BINARY_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_TEMPLATE_BINARY_SENSOR
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/binary_sensor/binary_sensor.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace binary_sensor {
14 |
15 | class TemplateBinarySensor : public Component, public BinarySensor {
16 | public:
17 | explicit TemplateBinarySensor(const std::string &name);
18 |
19 | void set_template(std::function()> &&f);
20 |
21 | void loop() override;
22 | void dump_config() override;
23 |
24 | float get_setup_priority() const override;
25 |
26 | protected:
27 | optional()>> f_;
28 | };
29 |
30 | } // namespace binary_sensor
31 |
32 | ESPHOME_NAMESPACE_END
33 |
34 | #endif // USE_TEMPLATE_BINARY_SENSOR
35 |
36 | #endif // ESPHOME_BINARY_SENSOR_TEMPLATE_BINARY_SENSOR_H
37 |
--------------------------------------------------------------------------------
/src/esphome/binary_sensor/ttp229_lsf_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 | #ifdef USE_TTP229_LSF
3 |
4 | #include "esphome/binary_sensor/ttp229_lsf_sensor.h"
5 | #include "esphome/log.h"
6 | #include "esphome/helpers.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace binary_sensor {
11 |
12 | static const char *TAG = "binary_sensor.ttp229";
13 |
14 | TTP229Channel::TTP229Channel(const std::string &name, int channel_num) : BinarySensor(name), channel_(channel_num) {}
15 |
16 | void TTP229Channel::process(uint16_t data) { this->publish_state(data & (1 << this->channel_)); }
17 |
18 | TTP229LSFComponent::TTP229LSFComponent(I2CComponent *parent, uint8_t address) : I2CDevice(parent, address) {}
19 |
20 | void TTP229LSFComponent::setup() {
21 | ESP_LOGCONFIG(TAG, "Setting up ttp229...");
22 | if (!this->parent_->raw_request_from(this->address_, 2)) {
23 | this->error_code_ = COMMUNICATION_FAILED;
24 | this->mark_failed();
25 | return;
26 | }
27 | }
28 |
29 | void TTP229LSFComponent::dump_config() {
30 | ESP_LOGCONFIG(TAG, "ttp229:");
31 | LOG_I2C_DEVICE(this);
32 | switch (this->error_code_) {
33 | case COMMUNICATION_FAILED:
34 | ESP_LOGE(TAG, "Communication with TTP229 failed!");
35 | break;
36 | case NONE:
37 | default:
38 | break;
39 | }
40 | }
41 |
42 | float TTP229LSFComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; }
43 |
44 | TTP229Channel *TTP229LSFComponent::add_channel(binary_sensor::TTP229Channel *channel) {
45 | this->channels_.push_back(channel);
46 | return channel;
47 | }
48 |
49 | void TTP229LSFComponent::loop() {
50 | uint16_t touched = 0;
51 | if (!this->parent_->raw_receive_16(this->address_, &touched, 1)) {
52 | this->status_set_warning();
53 | return;
54 | }
55 | this->status_clear_warning();
56 | touched = reverse_bits_16(touched);
57 | for (auto *channel : this->channels_) {
58 | channel->process(touched);
59 | }
60 | }
61 |
62 | } // namespace binary_sensor
63 |
64 | ESPHOME_NAMESPACE_END
65 |
66 | #endif // USE_TTP229_LSF
67 |
--------------------------------------------------------------------------------
/src/esphome/binary_sensor/ttp229_lsf_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_BINARY_SENSOR_TTP229_LSF_H
2 | #define ESPHOME_BINARY_SENSOR_TTP229_LSF_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_TTP229_LSF
7 |
8 | #include "esphome/binary_sensor/binary_sensor.h"
9 | #include "esphome/i2c_component.h"
10 | #include "esphome/component.h"
11 |
12 | ESPHOME_NAMESPACE_BEGIN
13 |
14 | namespace binary_sensor {
15 |
16 | class TTP229Channel : public binary_sensor::BinarySensor {
17 | public:
18 | TTP229Channel(const std::string &name, int channel_num);
19 | void process(uint16_t data);
20 |
21 | protected:
22 | int channel_;
23 | };
24 |
25 | class TTP229LSFComponent : public Component, public I2CDevice {
26 | public:
27 | TTP229LSFComponent(I2CComponent *parent, uint8_t address);
28 | binary_sensor::TTP229Channel *add_channel(binary_sensor::TTP229Channel *channel);
29 | void setup() override;
30 | void dump_config() override;
31 | float get_setup_priority() const override;
32 | void loop() override;
33 |
34 | protected:
35 | std::vector channels_{};
36 | enum ErrorCode {
37 | NONE = 0,
38 | COMMUNICATION_FAILED,
39 | } error_code_{NONE};
40 | };
41 |
42 | } // namespace binary_sensor
43 |
44 | ESPHOME_NAMESPACE_END
45 |
46 | #endif // USE_TTP229_LSF
47 |
48 | #endif // ESPHOME_BINARY_SENSOR_TTP229_LSF_H
49 |
--------------------------------------------------------------------------------
/src/esphome/climate/climate_automation.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_CORE_CLIMATE_AUTOMATION_H
2 | #define ESPHOME_CORE_CLIMATE_AUTOMATION_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_CLIMATE
7 |
8 | #include "esphome/climate/climate_mode.h"
9 | #include "esphome/climate/climate_device.h"
10 | #include "esphome/automation.h"
11 |
12 | ESPHOME_NAMESPACE_BEGIN
13 |
14 | namespace climate {
15 |
16 | class ClimateDevice;
17 |
18 | template class ControlAction : public Action {
19 | public:
20 | explicit ControlAction(ClimateDevice *climate) : climate_(climate) {}
21 |
22 | TEMPLATABLE_VALUE(ClimateMode, mode)
23 | TEMPLATABLE_VALUE(float, target_temperature)
24 | TEMPLATABLE_VALUE(float, target_temperature_low)
25 | TEMPLATABLE_VALUE(float, target_temperature_high)
26 | TEMPLATABLE_VALUE(bool, away)
27 |
28 | void play(Ts... x) override {
29 | auto call = this->climate_->make_call();
30 | call.set_target_temperature(this->mode_.optional_value(x...));
31 | call.set_target_temperature_low(this->target_temperature_low_.optional_value(x...));
32 | call.set_target_temperature_high(this->target_temperature_high_.optional_value(x...));
33 | call.set_away(this->away_.optional_value(x...));
34 | call.perform();
35 | }
36 |
37 | protected:
38 | ClimateDevice *climate_;
39 | };
40 |
41 | } // namespace climate
42 |
43 | ESPHOME_NAMESPACE_END
44 |
45 | #endif // USE_CLIMATE
46 |
47 | #endif // ESPHOME_CORE_CLIMATE_AUTOMATION_H
48 |
--------------------------------------------------------------------------------
/src/esphome/climate/climate_mode.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_CLIMATE
4 |
5 | #include "esphome/climate/climate_mode.h"
6 |
7 | ESPHOME_NAMESPACE_BEGIN
8 |
9 | namespace climate {
10 |
11 | const char *climate_mode_to_string(ClimateMode mode) {
12 | switch (mode) {
13 | case CLIMATE_MODE_OFF:
14 | return "OFF";
15 | case CLIMATE_MODE_AUTO:
16 | return "AUTO";
17 | case CLIMATE_MODE_COOL:
18 | return "COOL";
19 | case CLIMATE_MODE_HEAT:
20 | return "HEAT";
21 | default:
22 | return "UNKNOWN";
23 | }
24 | }
25 |
26 | } // namespace climate
27 |
28 | ESPHOME_NAMESPACE_END
29 |
30 | #endif // USE_CLIMATE
31 |
--------------------------------------------------------------------------------
/src/esphome/climate/climate_mode.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_CORE_CLIMATE_MODE_H
2 | #define ESPHOME_CORE_CLIMATE_MODE_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_CLIMATE
7 |
8 | #include
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | namespace climate {
13 |
14 | /// Enum for all modes a climate device can be in.
15 | enum ClimateMode : uint8_t {
16 | /// The climate device is off (not in auto, heat or cool mode)
17 | CLIMATE_MODE_OFF = 0,
18 | /// The climate device is set to automatically change the heating/cooling cycle
19 | CLIMATE_MODE_AUTO = 1,
20 | /// The climate device is manually set to cool mode (not in auto mode!)
21 | CLIMATE_MODE_COOL = 2,
22 | /// The climate device is manually set to heat mode (not in auto mode!)
23 | CLIMATE_MODE_HEAT = 3,
24 | };
25 |
26 | /// Convert the given ClimateMode to a human-readable string.
27 | const char *climate_mode_to_string(ClimateMode mode);
28 |
29 | } // namespace climate
30 |
31 | ESPHOME_NAMESPACE_END
32 |
33 | #endif // USE_CLIMATE
34 |
35 | #endif // ESPHOME_CORE_CLIMATE_MODE_H
36 |
--------------------------------------------------------------------------------
/src/esphome/climate/mqtt_climate_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_CORE_MQTT_CLIMATE_COMPONENT_H
2 | #define ESPHOME_CORE_MQTT_CLIMATE_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_MQTT_CLIMATE
7 |
8 | #include "esphome/mqtt/mqtt_component.h"
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | namespace climate {
13 |
14 | class ClimateDevice;
15 |
16 | class MQTTClimateComponent : public mqtt::MQTTComponent {
17 | public:
18 | MQTTClimateComponent(ClimateDevice *device);
19 | void send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) override;
20 | bool send_initial_state() override;
21 | bool is_internal() override;
22 | std::string component_type() const override;
23 | void setup() override;
24 |
25 | MQTT_COMPONENT_CUSTOM_TOPIC(current_temperature, state)
26 | MQTT_COMPONENT_CUSTOM_TOPIC(mode, state)
27 | MQTT_COMPONENT_CUSTOM_TOPIC(mode, command)
28 | MQTT_COMPONENT_CUSTOM_TOPIC(target_temperature, state)
29 | MQTT_COMPONENT_CUSTOM_TOPIC(target_temperature, command)
30 | MQTT_COMPONENT_CUSTOM_TOPIC(target_temperature_low, state)
31 | MQTT_COMPONENT_CUSTOM_TOPIC(target_temperature_low, command)
32 | MQTT_COMPONENT_CUSTOM_TOPIC(target_temperature_high, state)
33 | MQTT_COMPONENT_CUSTOM_TOPIC(target_temperature_high, command)
34 | MQTT_COMPONENT_CUSTOM_TOPIC(away, state)
35 | MQTT_COMPONENT_CUSTOM_TOPIC(away, command)
36 |
37 | protected:
38 | std::string friendly_name() const override;
39 |
40 | bool publish_state_();
41 |
42 | ClimateDevice *device_;
43 | };
44 |
45 | } // namespace climate
46 |
47 | ESPHOME_NAMESPACE_END
48 |
49 | #endif // USE_MQTT_CLIMATE
50 |
51 | #endif // ESPHOME_CORE_MQTT_CLIMATE_COMPONENT_H
52 |
--------------------------------------------------------------------------------
/src/esphome/cover/cover_traits.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_COVER
4 |
5 | #include "esphome/cover/cover_traits.h"
6 |
7 | ESPHOME_NAMESPACE_BEGIN
8 |
9 | namespace cover {
10 |
11 | bool CoverTraits::get_is_assumed_state() const { return this->is_assumed_state_; }
12 | void CoverTraits::set_is_assumed_state(bool is_assumed_state) { this->is_assumed_state_ = is_assumed_state; }
13 | bool CoverTraits::get_supports_position() const { return this->supports_position_; }
14 | void CoverTraits::set_supports_position(bool supports_position) { this->supports_position_ = supports_position; }
15 | bool CoverTraits::get_supports_tilt() const { return this->supports_tilt_; }
16 | void CoverTraits::set_supports_tilt(bool supports_tilt) { this->supports_tilt_ = supports_tilt; }
17 |
18 | } // namespace cover
19 |
20 | ESPHOME_NAMESPACE_END
21 |
22 | #endif // USE_COVER
23 |
--------------------------------------------------------------------------------
/src/esphome/cover/cover_traits.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_CORE_COVER_TRAITS_H
2 | #define ESPHOME_CORE_COVER_TRAITS_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_COVER
7 |
8 | #include
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | namespace cover {
13 |
14 | class CoverTraits {
15 | public:
16 | CoverTraits() {}
17 | bool get_is_assumed_state() const;
18 | void set_is_assumed_state(bool is_assumed_state);
19 | bool get_supports_position() const;
20 | void set_supports_position(bool supports_position);
21 | bool get_supports_tilt() const;
22 | void set_supports_tilt(bool supports_tilt);
23 |
24 | protected:
25 | bool is_assumed_state_{false};
26 | bool supports_position_{false};
27 | bool supports_tilt_{false};
28 | };
29 |
30 | } // namespace cover
31 |
32 | ESPHOME_NAMESPACE_END
33 |
34 | #endif // USE_COVER
35 |
36 | #endif // ESPHOME_CORE_COVER_TRAITS_H
37 |
--------------------------------------------------------------------------------
/src/esphome/cover/mqtt_cover_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_COVER_MQTT_COVER_COMPONENT_H
2 | #define ESPHOME_COVER_MQTT_COVER_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_MQTT_COVER
7 |
8 | #include "esphome/mqtt/mqtt_component.h"
9 | #include "esphome/cover/cover.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace cover {
14 |
15 | class MQTTCoverComponent : public mqtt::MQTTComponent {
16 | public:
17 | explicit MQTTCoverComponent(Cover *cover);
18 |
19 | void setup() override;
20 | void send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) override;
21 |
22 | MQTT_COMPONENT_CUSTOM_TOPIC(position, command)
23 | MQTT_COMPONENT_CUSTOM_TOPIC(position, state)
24 | MQTT_COMPONENT_CUSTOM_TOPIC(tilt, command)
25 | MQTT_COMPONENT_CUSTOM_TOPIC(tilt, state)
26 |
27 | bool send_initial_state() override;
28 | bool is_internal() override;
29 |
30 | bool publish_state();
31 |
32 | void dump_config() override;
33 |
34 | protected:
35 | std::string component_type() const override;
36 | std::string friendly_name() const override;
37 |
38 | Cover *cover_;
39 | };
40 |
41 | } // namespace cover
42 |
43 | ESPHOME_NAMESPACE_END
44 |
45 | #endif // USE_MQTT_COVER
46 |
47 | #endif // ESPHOME_COVER_MQTT_COVER_COMPONENT_H
48 |
--------------------------------------------------------------------------------
/src/esphome/cover/template_cover.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_COVER_TEMPLATE_COVER_H
2 | #define ESPHOME_COVER_TEMPLATE_COVER_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_TEMPLATE_COVER
7 |
8 | #include "esphome/cover/cover.h"
9 | #include "esphome/automation.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace cover {
14 |
15 | enum class TemplateCoverRestoreMode {
16 | NO_RESTORE,
17 | RESTORE,
18 | RESTORE_AND_CALL,
19 | };
20 |
21 | class TemplateCover : public Cover, public Component {
22 | public:
23 | explicit TemplateCover(const std::string &name);
24 |
25 | void set_state_lambda(std::function()> &&f);
26 | Trigger<> *get_open_trigger() const;
27 | Trigger<> *get_close_trigger() const;
28 | Trigger<> *get_stop_trigger() const;
29 | Trigger *get_position_trigger() const;
30 | Trigger *get_tilt_trigger() const;
31 | void set_optimistic(bool optimistic);
32 | void set_assumed_state(bool assumed_state);
33 | void set_tilt_lambda(std::function()> &&tilt_f);
34 | void set_has_position(bool has_position);
35 | void set_has_tilt(bool has_tilt);
36 |
37 | void setup() override;
38 | void loop() override;
39 | void dump_config() override;
40 |
41 | float get_setup_priority() const override;
42 |
43 | protected:
44 | void control(const CoverCall &call) override;
45 | CoverTraits get_traits() override;
46 | void stop_prev_trigger_();
47 |
48 | TemplateCoverRestoreMode restore_mode_{TemplateCoverRestoreMode::RESTORE};
49 | optional()>> state_f_;
50 | optional()>> tilt_f_;
51 | bool assumed_state_{false};
52 | bool optimistic_{false};
53 | Trigger<> *open_trigger_;
54 | Trigger<> *close_trigger_;
55 | Trigger<> *stop_trigger_;
56 | Trigger<> *prev_command_trigger_{nullptr};
57 | Trigger *position_trigger_;
58 | bool has_position_{false};
59 | Trigger *tilt_trigger_;
60 | bool has_tilt_{false};
61 | };
62 |
63 | } // namespace cover
64 |
65 | ESPHOME_NAMESPACE_END
66 |
67 | #endif // USE_TEMPLATE_COVER
68 |
69 | #endif // ESPHOME_COVER_TEMPLATE_COVER_H
70 |
--------------------------------------------------------------------------------
/src/esphome/cover/time_based_cover.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_CORE_TIME_BASED_COVER_H
2 | #define ESPHOME_CORE_TIME_BASED_COVER_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_TIME_BASED_COVER
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/cover/cover.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace cover {
14 |
15 | class TimeBasedCover : public Cover, public Component {
16 | public:
17 | TimeBasedCover(const std::string &name) : Cover(name) {}
18 | void setup() override;
19 | void loop() override;
20 | void dump_config() override;
21 | float get_setup_priority() const override;
22 |
23 | Trigger<> *get_open_trigger() const { return this->open_trigger_; }
24 | Trigger<> *get_close_trigger() const { return this->close_trigger_; }
25 | Trigger<> *get_stop_trigger() const { return this->stop_trigger_; }
26 | void set_open_duration(uint32_t open_duration) { this->open_duration_ = open_duration; }
27 | void set_close_duration(uint32_t close_duration) { this->close_duration_ = close_duration; }
28 | CoverTraits get_traits() override;
29 |
30 | protected:
31 | void control(const CoverCall &call) override;
32 | void stop_prev_trigger_();
33 | bool is_at_target_() const;
34 |
35 | void start_direction_(CoverOperation dir);
36 |
37 | void recompute_position_();
38 |
39 | Trigger<> *open_trigger_{new Trigger<>()};
40 | uint32_t open_duration_;
41 | Trigger<> *close_trigger_{new Trigger<>()};
42 | uint32_t close_duration_;
43 | Trigger<> *stop_trigger_{new Trigger<>()};
44 |
45 | Trigger<> *prev_command_trigger_{nullptr};
46 | uint32_t last_recompute_time_{0};
47 | uint32_t start_dir_time_{0};
48 | uint32_t last_publish_time_{0};
49 | float target_position_{0};
50 | };
51 |
52 | } // namespace cover
53 |
54 | ESPHOME_NAMESPACE_END
55 |
56 | #endif // USE_TIME_BASED_COVER
57 |
58 | #endif // ESPHOME_CORE_TIME_BASED_COVER_H
59 |
--------------------------------------------------------------------------------
/src/esphome/custom_component.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_CUSTOM_COMPONENT
4 |
5 | #include "esphome/custom_component.h"
6 | #include "esphome/application.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | CustomComponentConstructor::CustomComponentConstructor(const std::function()> &init) {
11 | this->components_ = init();
12 |
13 | for (auto *comp : this->components_) {
14 | App.register_component(comp);
15 | }
16 | }
17 |
18 | Component *CustomComponentConstructor::get_component(int i) { return this->components_[i]; }
19 |
20 | ESPHOME_NAMESPACE_END
21 |
22 | #endif // USE_CUSTOM_COMPONENT
23 |
--------------------------------------------------------------------------------
/src/esphome/custom_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_CUSTOM_COMPONENT_H
2 | #define ESPHOME_CUSTOM_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_CUSTOM_COMPONENT
7 |
8 | #include "esphome/component.h"
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | class CustomComponentConstructor {
13 | public:
14 | CustomComponentConstructor(const std::function()> &init);
15 |
16 | Component *get_component(int i);
17 |
18 | protected:
19 | std::vector components_;
20 | };
21 |
22 | ESPHOME_NAMESPACE_END
23 |
24 | #endif // USE_CUSTOM_COMPONENT
25 |
26 | #endif // ESPHOME_CUSTOM_COMPONENT_H
27 |
--------------------------------------------------------------------------------
/src/esphome/debug_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_DEBUG_COMPONENT_H
2 | #define ESPHOME_DEBUG_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_DEBUG_COMPONENT
7 |
8 | #include "esphome/component.h"
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | /// The debug component prints out debug information like free heap size on startup.
13 | class DebugComponent : public Component {
14 | public:
15 | void setup() override;
16 | void loop() override;
17 | float get_setup_priority() const override;
18 | void dump_config() override;
19 |
20 | protected:
21 | uint32_t free_heap_{};
22 | };
23 |
24 | ESPHOME_NAMESPACE_END
25 |
26 | #endif // USE_DEBUG_COMPONENT
27 |
28 | #endif // ESPHOME_DEBUG_COMPONENT_H
29 |
--------------------------------------------------------------------------------
/src/esphome/esp32_ble_beacon.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_ESP_32_BLE_BEACON_H
2 | #define ESPHOME_ESP_32_BLE_BEACON_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_ESP32_BLE_BEACON
7 |
8 | #include "esphome/component.h"
9 | #include
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | typedef struct {
14 | uint8_t flags[3];
15 | uint8_t length;
16 | uint8_t type;
17 | uint16_t company_id;
18 | uint16_t beacon_type;
19 | } __attribute__((packed)) esp_ble_ibeacon_head_t;
20 |
21 | typedef struct {
22 | uint8_t proximity_uuid[16];
23 | uint16_t major;
24 | uint16_t minor;
25 | uint8_t measured_power;
26 | } __attribute__((packed)) esp_ble_ibeacon_vendor_t;
27 |
28 | typedef struct {
29 | esp_ble_ibeacon_head_t ibeacon_head;
30 | esp_ble_ibeacon_vendor_t ibeacon_vendor;
31 | } __attribute__((packed)) esp_ble_ibeacon_t;
32 |
33 | class ESP32BLEBeacon : public Component {
34 | public:
35 | explicit ESP32BLEBeacon(const std::array &uuid);
36 |
37 | void setup() override;
38 | float get_setup_priority() const override;
39 |
40 | void set_major(uint16_t major);
41 | void set_minor(uint16_t minor);
42 |
43 | protected:
44 | static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
45 | static void ble_core_task(void *params);
46 | static void ble_setup();
47 |
48 | std::array uuid;
49 | uint16_t major{10167};
50 | uint16_t minor{61958};
51 | };
52 |
53 | extern ESP32BLEBeacon *global_esp32_ble_beacon;
54 |
55 | ESPHOME_NAMESPACE_END
56 |
57 | #endif // USE_ESP32_BLE_BEACON
58 |
59 | #endif // ESPHOME_ESP_32_BLE_BEACON_H
60 |
--------------------------------------------------------------------------------
/src/esphome/espmath.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_ESPMATH_H
2 | #define ESPHOME_ESPMATH_H
3 |
4 | #include "Arduino.h"
5 |
6 | #ifdef round
7 | #undef round
8 | #endif
9 |
10 | #ifdef abs
11 | #undef abs
12 | #endif
13 |
14 | #ifdef min
15 | #undef min
16 | #endif
17 |
18 | #ifdef max
19 | #undef max
20 | #endif
21 |
22 | #include "math.h" // NOLINT
23 |
24 | #endif // ESPHOME_ESPMATH_H
25 |
--------------------------------------------------------------------------------
/src/esphome/ethernet_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_ETHERNET_COMPONENT_H
2 | #define ESPHOME_ETHERNET_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_ETHERNET
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/wifi_component.h"
10 | #include "esp_eth.h"
11 |
12 | ESPHOME_NAMESPACE_BEGIN
13 |
14 | enum EthernetType {
15 | ETHERNET_TYPE_LAN8720 = 0,
16 | ETHERNET_TYPE_TLK110,
17 | };
18 |
19 | class EthernetComponent : public Component {
20 | public:
21 | EthernetComponent();
22 | void setup() override;
23 | void loop() override;
24 | void dump_config() override;
25 | float get_setup_priority() const override;
26 | bool can_proceed() override;
27 | bool is_connected();
28 |
29 | void set_phy_addr(uint8_t phy_addr);
30 | void set_power_pin(const GPIOOutputPin &power_pin);
31 | void set_mdc_pin(uint8_t mdc_pin);
32 | void set_mdio_pin(uint8_t mdio_pin);
33 | void set_type(EthernetType type);
34 | void set_clk_mode(eth_clock_mode_t clk_mode);
35 | void set_manual_ip(ManualIP manual_ip);
36 |
37 | IPAddress get_ip_address();
38 | std::string get_use_address() const;
39 | void set_use_address(const std::string &use_address);
40 |
41 | protected:
42 | void on_wifi_event_(system_event_id_t event, system_event_info_t info);
43 | void start_connect_();
44 | void dump_connect_params_();
45 |
46 | static void eth_phy_config_gpio_();
47 | static void eth_phy_power_enable_(bool enable);
48 |
49 | std::string use_address_;
50 | uint8_t phy_addr_{0};
51 | GPIOPin *power_pin_{nullptr};
52 | uint8_t mdc_pin_{23};
53 | uint8_t mdio_pin_{18};
54 | EthernetType type_{ETHERNET_TYPE_LAN8720};
55 | eth_clock_mode_t clk_mode_{ETH_CLOCK_GPIO0_IN};
56 | optional manual_ip_{};
57 |
58 | bool initialized_{false};
59 | bool connected_{false};
60 | bool last_connected_{false};
61 | uint32_t connect_begin_;
62 | eth_config_t eth_config;
63 | eth_phy_power_enable_func orig_power_enable_fun_;
64 | };
65 |
66 | extern EthernetComponent *global_eth_component;
67 |
68 | ESPHOME_NAMESPACE_END
69 |
70 | #endif // USE_ETHERNET
71 |
72 | #endif // ESPHOME_ETHERNET_COMPONENT_H
73 |
--------------------------------------------------------------------------------
/src/esphome/fan/fan_traits.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_FAN
4 |
5 | #include "esphome/fan/fan_traits.h"
6 |
7 | ESPHOME_NAMESPACE_BEGIN
8 |
9 | namespace fan {
10 |
11 | FanTraits::FanTraits() : oscillation_(false), speed_(false) {}
12 |
13 | FanTraits::FanTraits(bool oscillation, bool speed) : oscillation_(oscillation), speed_(speed) {}
14 | bool FanTraits::supports_oscillation() const { return this->oscillation_; }
15 | bool FanTraits::supports_speed() const { return this->speed_; }
16 | void FanTraits::set_oscillation(bool oscillation) { this->oscillation_ = oscillation; }
17 | void FanTraits::set_speed(bool speed) { this->speed_ = speed; }
18 |
19 | } // namespace fan
20 |
21 | ESPHOME_NAMESPACE_END
22 |
23 | #endif // USE_FAN
24 |
--------------------------------------------------------------------------------
/src/esphome/fan/fan_traits.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_FAN_FAN_TRAITS_H
2 | #define ESPHOME_FAN_FAN_TRAITS_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_FAN
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace fan {
11 |
12 | /// This class represents the capabilities/feature set of a fan.
13 | class FanTraits {
14 | public:
15 | /// Construct an empty FanTraits object. All features will be marked unsupported.
16 | FanTraits();
17 | /// Construct a FanTraits object with the provided oscillation and speed support.
18 | FanTraits(bool oscillation, bool speed);
19 |
20 | /// Return if this fan supports oscillation.
21 | bool supports_oscillation() const;
22 | /// Set whether this fan supports oscillation.
23 | void set_oscillation(bool oscillation);
24 | /// Return if this fan supports speed modes.
25 | bool supports_speed() const;
26 | /// Set whether this fan supports speed modes.
27 | void set_speed(bool speed);
28 |
29 | protected:
30 | bool oscillation_;
31 | bool speed_;
32 | };
33 |
34 | } // namespace fan
35 |
36 | ESPHOME_NAMESPACE_END
37 |
38 | #endif // USE_FAN
39 |
40 | #endif // ESPHOME_FAN_FAN_TRAITS_H
41 |
--------------------------------------------------------------------------------
/src/esphome/fan/mqtt_fan_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_FAN_MQTT_FAN_COMPONENT_H
2 | #define ESPHOME_FAN_MQTT_FAN_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_MQTT_FAN
7 |
8 | #include "esphome/mqtt/mqtt_component.h"
9 | #include "esphome/fan/fan_state.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace fan {
14 |
15 | class MQTTFanComponent : public mqtt::MQTTComponent {
16 | public:
17 | explicit MQTTFanComponent(FanState *state);
18 |
19 | MQTT_COMPONENT_CUSTOM_TOPIC(oscillation, command)
20 | MQTT_COMPONENT_CUSTOM_TOPIC(oscillation, state)
21 | MQTT_COMPONENT_CUSTOM_TOPIC(speed, command)
22 | MQTT_COMPONENT_CUSTOM_TOPIC(speed, state)
23 |
24 | void send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) override;
25 |
26 | // ========== INTERNAL METHODS ==========
27 | // (In most use cases you won't need these)
28 | /// Setup the fan subscriptions and discovery.
29 | void setup() override;
30 | /// Send the full current state to MQTT.
31 | bool send_initial_state() override;
32 | bool publish_state();
33 | /// 'fan' component type for discovery.
34 | std::string component_type() const override;
35 |
36 | FanState *get_state() const;
37 |
38 | bool is_internal() override;
39 |
40 | protected:
41 | std::string friendly_name() const override;
42 |
43 | FanState *state_;
44 | };
45 |
46 | } // namespace fan
47 |
48 | ESPHOME_NAMESPACE_END
49 |
50 | #endif // USE_MQTT_FAN
51 |
52 | #endif // ESPHOME_FAN_MQTT_FAN_COMPONENT_H
53 |
--------------------------------------------------------------------------------
/src/esphome/light/light_traits.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_LIGHT
4 |
5 | #include "esphome/light/light_traits.h"
6 |
7 | ESPHOME_NAMESPACE_BEGIN
8 |
9 | namespace light {
10 |
11 | LightTraits::LightTraits() : brightness_(false), rgb_(false), rgb_white_value_(false), color_temperature_(false) {}
12 |
13 | LightTraits::LightTraits(bool brightness, bool rgb, bool rgb_white_value, bool color_temperature)
14 | : brightness_(brightness), rgb_(rgb), rgb_white_value_(rgb_white_value), color_temperature_(color_temperature) {}
15 |
16 | bool LightTraits::has_brightness() const { return this->brightness_; }
17 | bool LightTraits::has_rgb() const { return this->rgb_; }
18 | bool LightTraits::has_rgb_white_value() const { return this->rgb_white_value_; }
19 | bool LightTraits::has_color_temperature() const { return this->color_temperature_; }
20 | float LightTraits::get_min_mireds() const { return this->min_mireds_; }
21 | float LightTraits::get_max_mireds() const { return this->max_mireds_; }
22 | void LightTraits::set_min_mireds(float min_mireds) { this->min_mireds_ = min_mireds; }
23 | void LightTraits::set_max_mireds(float max_mireds) { this->max_mireds_ = max_mireds; }
24 |
25 | } // namespace light
26 |
27 | ESPHOME_NAMESPACE_END
28 |
29 | #endif // USE_LIGHT
30 |
--------------------------------------------------------------------------------
/src/esphome/light/light_traits.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_LIGHT_LIGHT_TRAITS_H
2 | #define ESPHOME_LIGHT_LIGHT_TRAITS_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_LIGHT
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace light {
11 |
12 | /// This class is used to represent the capabilities of a light.
13 | class LightTraits {
14 | public:
15 | LightTraits();
16 |
17 | LightTraits(bool brightness, bool rgb, bool rgb_white_value, bool color_temperature = false);
18 |
19 | bool has_brightness() const;
20 | bool has_rgb() const;
21 | bool has_rgb_white_value() const;
22 | bool has_color_temperature() const;
23 | float get_min_mireds() const;
24 | float get_max_mireds() const;
25 | void set_min_mireds(float min_mireds);
26 | void set_max_mireds(float max_mireds);
27 |
28 | protected:
29 | bool brightness_{false};
30 | bool rgb_{false};
31 | bool rgb_white_value_{false};
32 | bool color_temperature_{false};
33 | float min_mireds_{0};
34 | float max_mireds_{0};
35 | };
36 |
37 | } // namespace light
38 |
39 | ESPHOME_NAMESPACE_END
40 |
41 | #endif // USE_LIGHT
42 |
43 | #endif // ESPHOME_LIGHT_LIGHT_TRAITS_H
44 |
--------------------------------------------------------------------------------
/src/esphome/light/mqtt_json_light_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_LIGHT_MQTT_JSON_LIGHT_COMPONENT
2 | #define ESPHOME_LIGHT_MQTT_JSON_LIGHT_COMPONENT
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_MQTT_LIGHT
7 |
8 | #include "esphome/mqtt/mqtt_component.h"
9 | #include "esphome/light/light_state.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace light {
14 |
15 | class MQTTJSONLightComponent : public mqtt::MQTTComponent {
16 | public:
17 | explicit MQTTJSONLightComponent(LightState *state);
18 |
19 | LightState *get_state() const;
20 |
21 | void setup() override;
22 |
23 | void dump_config() override;
24 |
25 | void send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) override;
26 |
27 | bool send_initial_state() override;
28 |
29 | bool is_internal() override;
30 |
31 | protected:
32 | std::string friendly_name() const override;
33 | std::string component_type() const override;
34 |
35 | bool publish_state_();
36 |
37 | LightState *state_;
38 | };
39 |
40 | } // namespace light
41 |
42 | ESPHOME_NAMESPACE_END
43 |
44 | #endif // USE_MQTT_LIGHT
45 |
46 | #endif // ESPHOME_LIGHT_MQTT_JSON_LIGHT_COMPONENT
47 |
--------------------------------------------------------------------------------
/src/esphome/log.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/log.h"
2 |
3 | #include "esphome/log_component.h"
4 |
5 | int HOT esp_log_printf_(int level, const char *tag, const char *format, ...) { // NOLINT
6 | va_list arg;
7 | va_start(arg, format);
8 | int ret = esp_log_vprintf_(level, tag, format, arg);
9 | va_end(arg);
10 | return ret;
11 | }
12 | #ifdef USE_STORE_LOG_STR_IN_FLASH
13 | int HOT esp_log_printf_(int level, const char *tag, const __FlashStringHelper *format, ...) {
14 | va_list arg;
15 | va_start(arg, format);
16 | int ret = esp_log_vprintf_(level, tag, format, arg);
17 | va_end(arg);
18 | return ret;
19 | }
20 | #endif
21 |
22 | int HOT esp_log_vprintf_(int level, const char *tag, const char *format, va_list args) { // NOLINT
23 | auto *log = esphome::global_log_component;
24 | if (log == nullptr)
25 | return 0;
26 |
27 | return log->log_vprintf_(level, tag, format, args);
28 | }
29 |
30 | #ifdef USE_STORE_LOG_STR_IN_FLASH
31 | int HOT esp_log_vprintf_(int level, const char *tag, const __FlashStringHelper *format, va_list args) { // NOLINT
32 | auto *log = esphome::global_log_component;
33 | if (log == nullptr)
34 | return 0;
35 |
36 | return log->log_vprintf_(level, tag, format, args);
37 | }
38 | #endif
39 |
40 | int HOT esp_idf_log_vprintf_(const char *format, va_list args) { // NOLINT
41 | auto *log = esphome::global_log_component;
42 | if (log == nullptr)
43 | return 0;
44 |
45 | return log->log_vprintf_(log->get_global_log_level(), "", format, args);
46 | }
47 |
--------------------------------------------------------------------------------
/src/esphome/mqtt/custom_mqtt_device.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_MQTT_CUSTOM_COMPONENT
4 |
5 | #include "esphome/mqtt/custom_mqtt_device.h"
6 |
7 | ESPHOME_NAMESPACE_BEGIN
8 |
9 | namespace mqtt {
10 |
11 | bool CustomMQTTDevice::publish(const std::string &topic, const std::string &payload, uint8_t qos, bool retain) {
12 | return global_mqtt_client->publish(topic, payload, qos, retain);
13 | }
14 | bool CustomMQTTDevice::publish(const std::string &topic, float value, int8_t number_decimals) {
15 | auto str = value_accuracy_to_string(value, number_decimals);
16 | return this->publish(topic, str);
17 | }
18 | bool CustomMQTTDevice::publish(const std::string &topic, int value) {
19 | char buffer[24];
20 | sprintf(buffer, "%d", value);
21 | return this->publish(topic, buffer);
22 | }
23 | bool CustomMQTTDevice::publish_json(const std::string &topic, const json_build_t &f, uint8_t qos, bool retain) {
24 | return global_mqtt_client->publish_json(topic, f, qos, retain);
25 | }
26 | bool CustomMQTTDevice::publish_json(const std::string &topic, const json_build_t &f) {
27 | return this->publish_json(topic, f, 0, false);
28 | }
29 | bool CustomMQTTDevice::is_connected() { return global_mqtt_client != nullptr && global_mqtt_client->is_connected(); }
30 |
31 | } // namespace mqtt
32 |
33 | ESPHOME_NAMESPACE_END
34 |
35 | #endif // USE_MQTT_CUSTOM_COMPONENT
36 |
--------------------------------------------------------------------------------
/src/esphome/output/binary_output.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_OUTPUT
4 |
5 | #include "esphome/output/binary_output.h"
6 |
7 | ESPHOME_NAMESPACE_BEGIN
8 |
9 | namespace output {
10 |
11 | bool BinaryOutput::is_inverted() const { return this->inverted_; }
12 | void BinaryOutput::set_inverted(bool inverted) { this->inverted_ = inverted; }
13 | PowerSupplyComponent *BinaryOutput::get_power_supply() const { return this->power_supply_; }
14 | void BinaryOutput::set_power_supply(PowerSupplyComponent *power_supply) { this->power_supply_ = power_supply; }
15 | void BinaryOutput::turn_on() {
16 | if (this->power_supply_ != nullptr && !this->has_requested_high_power_) {
17 | this->power_supply_->request_high_power();
18 | this->has_requested_high_power_ = true;
19 | }
20 | this->write_state(!this->inverted_);
21 | }
22 | void BinaryOutput::turn_off() {
23 | if (this->power_supply_ != nullptr && this->has_requested_high_power_) {
24 | this->power_supply_->unrequest_high_power();
25 | this->has_requested_high_power_ = false;
26 | }
27 | this->write_state(this->inverted_);
28 | }
29 |
30 | } // namespace output
31 |
32 | ESPHOME_NAMESPACE_END
33 |
34 | #endif // USE_OUTPUT
35 |
--------------------------------------------------------------------------------
/src/esphome/output/copy_output.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_COPY_OUTPUT
4 |
5 | #include "esphome/output/copy_output.h"
6 |
7 | ESPHOME_NAMESPACE_BEGIN
8 |
9 | namespace output {
10 |
11 | BinaryCopyOutput::BinaryCopyOutput(const std::vector &outputs) : outputs_(outputs) {}
12 | void BinaryCopyOutput::write_state(bool state) {
13 | for (auto *out : this->outputs_) {
14 | if (state)
15 | out->turn_on();
16 | else
17 | out->turn_off();
18 | }
19 | }
20 | FloatCopyOutput::FloatCopyOutput(const std::vector &outputs) : outputs_(outputs) {}
21 | void FloatCopyOutput::write_state(float state) {
22 | for (auto *out : this->outputs_) {
23 | out->set_level(state);
24 | }
25 | }
26 |
27 | } // namespace output
28 |
29 | ESPHOME_NAMESPACE_END
30 |
31 | #endif // USE_COPY_OUTPUT
32 |
--------------------------------------------------------------------------------
/src/esphome/output/copy_output.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_CORE_OUTPUT_COPY_OUTPUT_H
2 | #define ESPHOME_CORE_OUTPUT_COPY_OUTPUT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_COPY_OUTPUT
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/output/binary_output.h"
10 | #include "esphome/output/float_output.h"
11 |
12 | ESPHOME_NAMESPACE_BEGIN
13 |
14 | namespace output {
15 |
16 | class BinaryCopyOutput : public BinaryOutput {
17 | public:
18 | BinaryCopyOutput(const std::vector &outputs);
19 |
20 | protected:
21 | std::vector outputs_;
22 |
23 | void write_state(bool state) override;
24 | };
25 |
26 | class FloatCopyOutput : public FloatOutput {
27 | public:
28 | FloatCopyOutput(const std::vector &outputs);
29 |
30 | protected:
31 | std::vector outputs_;
32 |
33 | void write_state(float state) override;
34 | };
35 |
36 | } // namespace output
37 |
38 | ESPHOME_NAMESPACE_END
39 |
40 | #endif // USE_COPY_OUTPUT
41 |
42 | #endif // ESPHOME_CORE_OUTPUT_COPY_OUTPUT_H
43 |
--------------------------------------------------------------------------------
/src/esphome/output/custom_output.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_CUSTOM_OUTPUT
4 |
5 | #include "esphome/output/custom_output.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace output {
11 |
12 | CustomBinaryOutputConstructor::CustomBinaryOutputConstructor(std::function()> init) {
13 | this->outputs_ = init();
14 | }
15 | BinaryOutput *CustomBinaryOutputConstructor::get_output(int i) { return this->outputs_[i]; }
16 | CustomFloatOutputConstructor::CustomFloatOutputConstructor(std::function()> init) {
17 | this->outputs_ = init();
18 | }
19 | FloatOutput *CustomFloatOutputConstructor::get_output(int i) { return this->outputs_[i]; }
20 | } // namespace output
21 |
22 | ESPHOME_NAMESPACE_END
23 |
24 | #endif // USE_CUSTOM_OUTPUT
25 |
--------------------------------------------------------------------------------
/src/esphome/output/custom_output.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_OUTPUT_CUSTOM_OUTPUT_H
2 | #define ESPHOME_OUTPUT_CUSTOM_OUTPUT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_CUSTOM_OUTPUT
7 |
8 | #include "esphome/output/binary_output.h"
9 | #include "esphome/output/float_output.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace output {
14 |
15 | class CustomBinaryOutputConstructor {
16 | public:
17 | CustomBinaryOutputConstructor(std::function()> init);
18 |
19 | BinaryOutput *get_output(int i);
20 |
21 | protected:
22 | std::vector outputs_;
23 | };
24 |
25 | class CustomFloatOutputConstructor {
26 | public:
27 | CustomFloatOutputConstructor(std::function()> init);
28 |
29 | FloatOutput *get_output(int i);
30 |
31 | protected:
32 | std::vector outputs_;
33 | };
34 |
35 | } // namespace output
36 |
37 | ESPHOME_NAMESPACE_END
38 |
39 | #endif // USE_CUSTOM_OUTPUT
40 |
41 | #endif // ESPHOME_OUTPUT_CUSTOM_OUTPUT_H
42 |
--------------------------------------------------------------------------------
/src/esphome/output/esp8266_pwm_output.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_ESP8266_PWM_OUTPUT
4 |
5 | #ifdef ARDUINO_ESP8266_RELEASE_2_3_0
6 | #error ESP8266 PWM requires at least arduino_core_version 2.4.0
7 | #endif
8 |
9 | #include "esphome/output/esp8266_pwm_output.h"
10 | #include "esphome/espmath.h"
11 | #include "esphome/log.h"
12 |
13 | #include
14 |
15 | ESPHOME_NAMESPACE_BEGIN
16 |
17 | namespace output {
18 |
19 | static const char *TAG = "output.esp8266_pwm";
20 |
21 | ESP8266PWMOutput::ESP8266PWMOutput(const GPIOOutputPin &pin) : pin_(pin) {}
22 |
23 | void ESP8266PWMOutput::setup() {
24 | ESP_LOGCONFIG(TAG, "Setting up ESP8266 PWM Output...");
25 | this->pin_.setup();
26 | this->turn_off();
27 | }
28 | void ESP8266PWMOutput::dump_config() {
29 | ESP_LOGCONFIG(TAG, "ESP8266 PWM:");
30 | LOG_PIN(" Pin: ", &this->pin_);
31 | ESP_LOGCONFIG(TAG, " Frequency: %.1f Hz", this->frequency_);
32 | LOG_FLOAT_OUTPUT(this);
33 | }
34 | void HOT ESP8266PWMOutput::write_state(float state) {
35 | // Also check pin inversion
36 | if (this->pin_.is_inverted()) {
37 | state = 1.0f - state;
38 | }
39 |
40 | auto total_time_us = static_cast(roundf(1e6f / this->frequency_));
41 | auto duty_on = static_cast(roundf(total_time_us * state));
42 | uint32_t duty_off = total_time_us - duty_on;
43 |
44 | if (duty_on == 0) {
45 | stopWaveform(this->pin_.get_pin());
46 | this->pin_.digital_write(this->pin_.is_inverted());
47 | } else if (duty_off == 0) {
48 | stopWaveform(this->pin_.get_pin());
49 | this->pin_.digital_write(!this->pin_.is_inverted());
50 | } else {
51 | startWaveform(this->pin_.get_pin(), duty_on, duty_off, 0);
52 | }
53 | }
54 | float ESP8266PWMOutput::get_setup_priority() const { return setup_priority::HARDWARE; }
55 | void ESP8266PWMOutput::set_frequency(float frequency) { this->frequency_ = frequency; }
56 |
57 | } // namespace output
58 |
59 | ESPHOME_NAMESPACE_END
60 |
61 | #endif // USE_ESP8266_PWM_OUTPUT
62 |
--------------------------------------------------------------------------------
/src/esphome/output/esp8266_pwm_output.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_ESP_8266_PWM_OUTPUT_H
2 | #define ESPHOME_ESP_8266_PWM_OUTPUT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_ESP8266_PWM_OUTPUT
7 |
8 | #include "esphome/output/float_output.h"
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | namespace output {
13 |
14 | /** Software PWM output component for ESP8266.
15 | *
16 | * Supported pins are 0-16. By default, this uses a frequency of 1000Hz, and this can only
17 | * be changed globally for all software PWM pins. To change the PWM frequency, do the following:
18 | *
19 | * ```cpp
20 | * analogWriteFreq(500); // 500Hz frequency for all pins.
21 | * ```
22 | *
23 | * Note that this is a software PWM and can have noticeable flickering because of other
24 | * interrupts on the ESP8266 (like WiFi). Additionally, this PWM output can only be 80%
25 | * on at max. That is a known limitation, if it's a deal breaker, consider using the ESP32
26 | * instead - it up to 16 integrated hardware PWM channels.
27 | */
28 | class ESP8266PWMOutput : public FloatOutput, public Component {
29 | public:
30 | /// Construct the Software PWM output.
31 | explicit ESP8266PWMOutput(const GPIOOutputPin &pin);
32 |
33 | void set_frequency(float frequency);
34 |
35 | // ========== INTERNAL METHODS ==========
36 | // (In most use cases you won't need these)
37 |
38 | /// Initialize pin
39 | void setup() override;
40 | void dump_config() override;
41 | /// HARDWARE setup_priority
42 | float get_setup_priority() const override;
43 | /// Override FloatOutput's write_state for analogWrite
44 |
45 | protected:
46 | void write_state(float state) override;
47 |
48 | GPIOOutputPin pin_;
49 | float frequency_{1000.0};
50 | };
51 |
52 | } // namespace output
53 |
54 | ESPHOME_NAMESPACE_END
55 |
56 | #endif // USE_ESP8266_PWM_OUTPUT
57 |
58 | #endif // ESPHOME_ESP_8266_PWM_OUTPUT_H
59 |
--------------------------------------------------------------------------------
/src/esphome/output/float_output.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_OUTPUT
4 |
5 | #include "esphome/output/float_output.h"
6 |
7 | #include "esphome/helpers.h"
8 | #include "esphome/log.h"
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | namespace output {
13 |
14 | void FloatOutput::set_max_power(float max_power) {
15 | this->max_power_ = clamp(this->min_power_, 1.0f, max_power); // Clamp to MIN>=MAX>=1.0
16 | }
17 |
18 | float FloatOutput::get_max_power() const { return this->max_power_; }
19 |
20 | void FloatOutput::set_min_power(float min_power) {
21 | this->min_power_ = clamp(0.0f, this->max_power_, min_power); // Clamp to 0.0>=MIN>=MAX
22 | }
23 |
24 | float FloatOutput::get_min_power() const { return this->min_power_; }
25 |
26 | void FloatOutput::set_level(float state) {
27 | state = clamp(0.0f, 1.0f, state);
28 |
29 | if (state > 0.0f) { // ON
30 | // maybe refactor this
31 | if (this->power_supply_ != nullptr && !this->has_requested_high_power_) {
32 | this->power_supply_->request_high_power();
33 | this->has_requested_high_power_ = true;
34 | }
35 | } else { // OFF
36 | if (this->power_supply_ != nullptr && this->has_requested_high_power_) {
37 | this->power_supply_->unrequest_high_power();
38 | this->has_requested_high_power_ = false;
39 | }
40 | }
41 |
42 | float adjusted_value = (state * (this->max_power_ - this->min_power_)) + this->min_power_;
43 | if (this->is_inverted())
44 | adjusted_value = 1.0f - adjusted_value;
45 | this->write_state(adjusted_value);
46 | }
47 |
48 | void FloatOutput::write_state(bool state) { this->set_level(state != this->inverted_ ? 1.0f : 0.0f); }
49 |
50 | } // namespace output
51 |
52 | ESPHOME_NAMESPACE_END
53 |
54 | #endif // USE_OUTPUT
55 |
--------------------------------------------------------------------------------
/src/esphome/output/gpio_binary_output_component.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_GPIO_OUTPUT
4 |
5 | #include "esphome/output/gpio_binary_output_component.h"
6 |
7 | #include "esphome/esphal.h"
8 | #include "esphome/log.h"
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | namespace output {
13 |
14 | static const char *TAG = "output.gpio";
15 |
16 | void GPIOBinaryOutputComponent::write_state(bool state) { this->pin_->digital_write(state); }
17 |
18 | void GPIOBinaryOutputComponent::setup() {
19 | ESP_LOGCONFIG(TAG, "Setting up GPIO Binary Output...");
20 | this->turn_off();
21 | this->pin_->setup();
22 | this->turn_off();
23 | }
24 | void GPIOBinaryOutputComponent::dump_config() {
25 | ESP_LOGCONFIG(TAG, "GPIO Binary Output:");
26 | LOG_PIN(" Pin: ", this->pin_);
27 | LOG_BINARY_OUTPUT(this);
28 | }
29 |
30 | float GPIOBinaryOutputComponent::get_setup_priority() const { return setup_priority::HARDWARE; }
31 | GPIOBinaryOutputComponent::GPIOBinaryOutputComponent(GPIOPin *pin) : pin_(pin) {}
32 |
33 | } // namespace output
34 |
35 | ESPHOME_NAMESPACE_END
36 |
37 | #endif // USE_GPIO_OUTPUT
38 |
--------------------------------------------------------------------------------
/src/esphome/output/gpio_binary_output_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_OUTPUT_GPIO_BINARY_OUTPUT_COMPONENT_H
2 | #define ESPHOME_OUTPUT_GPIO_BINARY_OUTPUT_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_GPIO_OUTPUT
7 |
8 | #include "esphome/output/binary_output.h"
9 | #include "esphome/esphal.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace output {
14 |
15 | /** Simple binary output component for a GPIO pin.
16 | *
17 | * This component allows you to control a GPIO pin as a switch.
18 | *
19 | * Note that with this output component you actually have two ways of inverting the output:
20 | * either through the GPIOOutputPin, or the BinaryOutput API. You can use either one of these.
21 | *
22 | * This is only an *output component*, not a *switch*, if what you want is a switch, take a look
23 | * at App.make_gpio_switch();
24 | */
25 | class GPIOBinaryOutputComponent : public BinaryOutput, public Component {
26 | public:
27 | /** Construct the GPIO binary output.
28 | *
29 | * @param pin The output pin to use for this output, can be integer or GPIOOutputPin.
30 | */
31 | explicit GPIOBinaryOutputComponent(GPIOPin *pin);
32 |
33 | // ========== INTERNAL METHODS ==========
34 | // (In most use cases you won't need these)
35 |
36 | /// Set pin mode.
37 | void setup() override;
38 | void dump_config() override;
39 | /// Hardware setup priority.
40 | float get_setup_priority() const override;
41 |
42 | protected:
43 | /// Override the BinaryOutput method for writing values to HW.
44 | void write_state(bool state) override;
45 |
46 | GPIOPin *pin_;
47 | };
48 |
49 | } // namespace output
50 |
51 | ESPHOME_NAMESPACE_END
52 |
53 | #endif // USE_GPIO_OUTPUT
54 |
55 | #endif // ESPHOME_OUTPUT_GPIO_BINARY_OUTPUT_COMPONENT_H
56 |
--------------------------------------------------------------------------------
/src/esphome/output/ledc_output_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_OUTPUT_LEDC_OUTPUT_COMPONENT_H
2 | #define ESPHOME_OUTPUT_LEDC_OUTPUT_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_LEDC_OUTPUT
7 |
8 | #include "esphome/output/float_output.h"
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | namespace output {
13 |
14 | /// ESP32 LEDC output component.
15 | class LEDCOutputComponent : public FloatOutput, public Component {
16 | public:
17 | /// Construct a LEDCOutputComponent. The channel will be set using the next_ledc_channel global variable.
18 | explicit LEDCOutputComponent(uint8_t pin, float frequency = 1000.0f, uint8_t bit_depth = 12);
19 |
20 | /// Manually set the pin used for this output.
21 | void set_pin(uint8_t pin);
22 | /// Manually set the ledc_channel used for this component.
23 | void set_channel(uint8_t channel);
24 | /// Manually set the bit depth. Defaults to 12.
25 | void set_bit_depth(uint8_t bit_depth);
26 |
27 | /** Manually set frequency the LEDC timer should operate on.
28 | *
29 | * Two adjacent LEDC channels will usually receive the same timer and so can only have the same
30 | * frequency.
31 | *
32 | * @param frequency The frequency in Hz.
33 | */
34 | void set_frequency(float frequency);
35 |
36 | // ========== INTERNAL METHODS ==========
37 | // (In most use cases you won't need these)
38 | /// Setup LEDC.
39 | void setup() override;
40 | void dump_config() override;
41 | /// HARDWARE setup priority
42 | float get_setup_priority() const override;
43 |
44 | /// Override FloatOutput's write_state.
45 | void write_state(float adjusted_value) override;
46 |
47 | float get_frequency() const;
48 | uint8_t get_bit_depth() const;
49 | uint8_t get_channel() const;
50 | uint8_t get_pin() const;
51 |
52 | protected:
53 | uint8_t pin_;
54 | uint8_t channel_;
55 | uint8_t bit_depth_;
56 | float frequency_;
57 | };
58 |
59 | extern uint8_t next_ledc_channel;
60 |
61 | } // namespace output
62 |
63 | ESPHOME_NAMESPACE_END
64 |
65 | #endif // USE_LEDC_OUTPUT
66 |
67 | #endif // ESPHOME_OUTPUT_LEDC_OUTPUT_COMPONENT_H
68 |
--------------------------------------------------------------------------------
/src/esphome/remote/jvc.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_REMOTE_JVC_H
2 | #define ESPHOME_REMOTE_JVC_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_REMOTE
7 |
8 | #include "esphome/remote/remote_receiver.h"
9 | #include "esphome/remote/remote_transmitter.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace remote {
14 |
15 | #ifdef USE_REMOTE_TRANSMITTER
16 | class JVCTransmitter : public RemoteTransmitter {
17 | public:
18 | JVCTransmitter(const std::string &name, uint32_t data);
19 |
20 | void to_data(RemoteTransmitData *data) override;
21 |
22 | protected:
23 | uint32_t data_;
24 | };
25 |
26 | void encode_jvc(RemoteTransmitData *data, uint32_t jvc_data);
27 | #endif
28 |
29 | #ifdef USE_REMOTE_RECEIVER
30 | JVCDecodeData decode_jvc(RemoteReceiveData *data);
31 |
32 | class JVCReceiver : public RemoteReceiver {
33 | public:
34 | JVCReceiver(const std::string &name, uint32_t data);
35 |
36 | protected:
37 | bool matches(RemoteReceiveData *data) override;
38 |
39 | uint32_t data_;
40 | };
41 |
42 | class JVCDumper : public RemoteReceiveDumper {
43 | public:
44 | bool dump(RemoteReceiveData *data) override;
45 | };
46 | #endif
47 |
48 | } // namespace remote
49 |
50 | ESPHOME_NAMESPACE_END
51 |
52 | #endif // USE_REMOTE
53 |
54 | #endif // ESPHOME_REMOTE_JVC_H
55 |
--------------------------------------------------------------------------------
/src/esphome/remote/lg.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_REMOTE_LG_H
2 | #define ESPHOME_REMOTE_LG_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_REMOTE
7 |
8 | #include "esphome/remote/remote_receiver.h"
9 | #include "esphome/remote/remote_transmitter.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace remote {
14 |
15 | #ifdef USE_REMOTE_TRANSMITTER
16 | class LGTransmitter : public RemoteTransmitter {
17 | public:
18 | LGTransmitter(const std::string &name, uint32_t data, uint8_t nbits);
19 |
20 | void to_data(RemoteTransmitData *data) override;
21 |
22 | protected:
23 | uint32_t data_;
24 | uint8_t nbits_;
25 | };
26 |
27 | void encode_lg(RemoteTransmitData *data, uint32_t lg_data, uint8_t nbits);
28 | #endif
29 |
30 | #ifdef USE_REMOTE_RECEIVER
31 | LGDecodeData decode_lg(RemoteReceiveData *data);
32 |
33 | class LGReceiver : public RemoteReceiver {
34 | public:
35 | LGReceiver(const std::string &name, uint32_t data, uint8_t nbits);
36 |
37 | protected:
38 | bool matches(RemoteReceiveData *data) override;
39 |
40 | uint32_t data_;
41 | uint8_t nbits_;
42 | };
43 |
44 | class LGDumper : public RemoteReceiveDumper {
45 | public:
46 | bool dump(RemoteReceiveData *data) override;
47 | };
48 | #endif
49 |
50 | } // namespace remote
51 |
52 | ESPHOME_NAMESPACE_END
53 |
54 | #endif // USE_REMOTE
55 |
56 | #endif // ESPHOME_REMOTE_LG_H
57 |
--------------------------------------------------------------------------------
/src/esphome/remote/nec.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_REMOTE_NEC_H
2 | #define ESPHOME_REMOTE_NEC_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_REMOTE
7 |
8 | #include "esphome/remote/remote_receiver.h"
9 | #include "esphome/remote/remote_transmitter.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace remote {
14 |
15 | #ifdef USE_REMOTE_TRANSMITTER
16 | class NECTransmitter : public RemoteTransmitter {
17 | public:
18 | NECTransmitter(const std::string &name, uint16_t address, uint16_t command);
19 |
20 | void to_data(RemoteTransmitData *data) override;
21 |
22 | protected:
23 | uint16_t address_;
24 | uint16_t command_;
25 | };
26 |
27 | void encode_nec(RemoteTransmitData *data, uint16_t address, uint16_t command);
28 | #endif
29 |
30 | #ifdef USE_REMOTE_RECEIVER
31 | NECDecodeData decode_nec(RemoteReceiveData *data);
32 |
33 | class NECReceiver : public RemoteReceiver {
34 | public:
35 | NECReceiver(const std::string &name, uint16_t address, uint16_t command);
36 |
37 | protected:
38 | bool matches(RemoteReceiveData *data) override;
39 |
40 | uint16_t address_;
41 | uint16_t command_;
42 | };
43 |
44 | class NECDumper : public RemoteReceiveDumper {
45 | public:
46 | bool dump(RemoteReceiveData *data) override;
47 | };
48 | #endif
49 |
50 | } // namespace remote
51 |
52 | ESPHOME_NAMESPACE_END
53 |
54 | #endif // USE_REMOTE
55 |
56 | #endif // ESPHOME_REMOTE_NEC_H
57 |
--------------------------------------------------------------------------------
/src/esphome/remote/panasonic.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_REMOTE_PANASONIC_H
2 | #define ESPHOME_REMOTE_PANASONIC_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_REMOTE
7 |
8 | #include "esphome/remote/remote_receiver.h"
9 | #include "esphome/remote/remote_transmitter.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace remote {
14 |
15 | #ifdef USE_REMOTE_TRANSMITTER
16 | class PanasonicTransmitter : public RemoteTransmitter {
17 | public:
18 | PanasonicTransmitter(const std::string &name, uint16_t address, uint32_t command);
19 |
20 | void to_data(RemoteTransmitData *data) override;
21 |
22 | protected:
23 | uint16_t address_;
24 | uint32_t command_;
25 | };
26 |
27 | void encode_panasonic(RemoteTransmitData *data, uint16_t address, uint32_t command);
28 | #endif
29 |
30 | #ifdef USE_REMOTE_RECEIVER
31 | PanasonicDecodeData decode_panasonic(RemoteReceiveData *data);
32 |
33 | class PanasonicReceiver : public RemoteReceiver {
34 | public:
35 | PanasonicReceiver(const std::string &name, uint16_t address, uint32_t command);
36 |
37 | protected:
38 | bool matches(RemoteReceiveData *data) override;
39 |
40 | uint16_t address_;
41 | uint32_t command_;
42 | };
43 |
44 | class PanasonicDumper : public RemoteReceiveDumper {
45 | public:
46 | bool dump(RemoteReceiveData *data) override;
47 | };
48 | #endif
49 |
50 | } // namespace remote
51 |
52 | ESPHOME_NAMESPACE_END
53 |
54 | #endif // USE_REMOTE
55 |
56 | #endif // ESPHOME_REMOTE_PANASONIC_H
57 |
--------------------------------------------------------------------------------
/src/esphome/remote/raw.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_REMOTE_RAW_H
2 | #define ESPHOME_REMOTE_RAW_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_REMOTE
7 |
8 | #include "esphome/remote/remote_receiver.h"
9 | #include "esphome/remote/remote_transmitter.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace remote {
14 |
15 | #ifdef USE_REMOTE_TRANSMITTER
16 | class RawTransmitter : public RemoteTransmitter {
17 | public:
18 | RawTransmitter(const std::string &name, const int32_t *data, size_t len, uint32_t carrier_frequency = 0);
19 |
20 | void to_data(RemoteTransmitData *data) override;
21 |
22 | protected:
23 | const int32_t *data_;
24 | size_t len_;
25 | uint32_t carrier_frequency_{0};
26 | };
27 | #endif
28 |
29 | #ifdef USE_REMOTE_RECEIVER
30 | class RawReceiver : public RemoteReceiver {
31 | public:
32 | RawReceiver(const std::string &name, const int32_t *data, size_t len);
33 |
34 | protected:
35 | bool matches(RemoteReceiveData *data) override;
36 |
37 | const int32_t *data_;
38 | size_t len_;
39 | };
40 |
41 | class RawDumper : public RemoteReceiveDumper {
42 | public:
43 | bool dump(RemoteReceiveData *data) override;
44 |
45 | bool is_secondary() override;
46 | };
47 | #endif
48 |
49 | } // namespace remote
50 |
51 | ESPHOME_NAMESPACE_END
52 |
53 | #endif // USE_REMOTE
54 |
55 | #endif // ESPHOME_REMOTE_RAW_H
56 |
--------------------------------------------------------------------------------
/src/esphome/remote/rc5.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_REMOTE_RC5_H
2 | #define ESPHOME_REMOTE_RC5_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_REMOTE
7 |
8 | #include "esphome/remote/remote_receiver.h"
9 | #include "esphome/remote/remote_transmitter.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace remote {
14 |
15 | #ifdef USE_REMOTE_TRANSMITTER
16 | class RC5Transmitter : public RemoteTransmitter {
17 | public:
18 | RC5Transmitter(const std::string &name, uint8_t address, uint8_t command);
19 |
20 | void to_data(RemoteTransmitData *data) override;
21 |
22 | protected:
23 | uint8_t address_;
24 | uint8_t command_;
25 | bool toggle_{false};
26 | };
27 |
28 | void encode_rc5(RemoteTransmitData *data, uint8_t address, uint8_t command, bool toggle);
29 | #endif
30 |
31 | #ifdef USE_REMOTE_RECEIVER
32 | RC5DecodeData decode_rc5(RemoteReceiveData *data);
33 |
34 | class RC5Receiver : public RemoteReceiver {
35 | public:
36 | RC5Receiver(const std::string &name, uint8_t address, uint8_t command);
37 |
38 | protected:
39 | bool matches(RemoteReceiveData *data) override;
40 |
41 | uint8_t address_;
42 | uint8_t command_;
43 | };
44 |
45 | class RC5Dumper : public RemoteReceiveDumper {
46 | public:
47 | bool dump(RemoteReceiveData *data) override;
48 | };
49 | #endif
50 |
51 | } // namespace remote
52 |
53 | ESPHOME_NAMESPACE_END
54 |
55 | #endif // USE_REMOTE
56 |
57 | #endif // ESPHOME_REMOTE_RC5_H
58 |
--------------------------------------------------------------------------------
/src/esphome/remote/remote_protocol.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_REMOTE
4 |
5 | #include "esphome/remote/remote_protocol.h"
6 | #include "esphome/log.h"
7 | #include "esphome/espmath.h"
8 | #include "esphome/helpers.h"
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | namespace remote {
13 |
14 | RemoteControlComponentBase::RemoteControlComponentBase(GPIOPin *pin) : pin_(pin) {
15 | #ifdef ARDUINO_ARCH_ESP32
16 | this->channel_ = select_next_rmt_channel();
17 | #endif
18 | }
19 | #ifdef ARDUINO_ARCH_ESP32
20 | uint32_t RemoteControlComponentBase::from_microseconds(uint32_t us) {
21 | const uint32_t ticks_per_ten_us = 80000000u / this->clock_divider_ / 100000u;
22 | return us * ticks_per_ten_us / 10;
23 | }
24 | uint32_t RemoteControlComponentBase::to_microseconds(uint32_t ticks) {
25 | const uint32_t ticks_per_ten_us = 80000000u / this->clock_divider_ / 100000u;
26 | return (ticks * 10) / ticks_per_ten_us;
27 | }
28 | void RemoteControlComponentBase::set_channel(rmt_channel_t channel) { this->channel_ = channel; }
29 | void RemoteControlComponentBase::set_clock_divider(uint8_t clock_divider) { this->clock_divider_ = clock_divider; }
30 | #endif
31 |
32 | } // namespace remote
33 |
34 | ESPHOME_NAMESPACE_END
35 |
36 | #endif // USE_REMOTE
37 |
--------------------------------------------------------------------------------
/src/esphome/remote/remote_protocol.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_REMOTE_REMOTE_PROTOCOL_H
2 | #define ESPHOME_REMOTE_REMOTE_PROTOCOL_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_REMOTE
7 |
8 | #include
9 | #include "esphome/component.h"
10 | #include "esphome/esphal.h"
11 |
12 | #ifdef ARDUINO_ARCH_ESP32
13 | #include
14 | #endif
15 |
16 | ESPHOME_NAMESPACE_BEGIN
17 |
18 | namespace remote {
19 |
20 | class RemoteControlComponentBase {
21 | public:
22 | explicit RemoteControlComponentBase(GPIOPin *pin);
23 |
24 | #ifdef ARDUINO_ARCH_ESP32
25 | void set_channel(rmt_channel_t channel);
26 | void set_clock_divider(uint8_t clock_divider);
27 | #endif
28 |
29 | protected:
30 | #ifdef ARDUINO_ARCH_ESP32
31 | uint32_t from_microseconds(uint32_t us);
32 | uint32_t to_microseconds(uint32_t ticks);
33 | #endif
34 |
35 | GPIOPin *pin_;
36 | #ifdef ARDUINO_ARCH_ESP32
37 | rmt_channel_t channel_{RMT_CHANNEL_0};
38 | uint8_t clock_divider_{80};
39 | esp_err_t error_code_{ESP_OK};
40 | #endif
41 | };
42 |
43 | } // namespace remote
44 |
45 | ESPHOME_NAMESPACE_END
46 |
47 | #endif // USE_REMOTE
48 |
49 | #endif // ESPHOME_REMOTE_REMOTE_PROTOCOL_H
50 |
--------------------------------------------------------------------------------
/src/esphome/remote/samsung.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_REMOTE_SAMSUNG_H
2 | #define ESPHOME_REMOTE_SAMSUNG_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_REMOTE
7 |
8 | #include "esphome/remote/remote_receiver.h"
9 | #include "esphome/remote/remote_transmitter.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace remote {
14 |
15 | #ifdef USE_REMOTE_TRANSMITTER
16 | class SamsungTransmitter : public RemoteTransmitter {
17 | public:
18 | SamsungTransmitter(const std::string &name, uint32_t data);
19 |
20 | void to_data(RemoteTransmitData *data) override;
21 |
22 | protected:
23 | uint32_t data_;
24 | };
25 |
26 | void encode_samsung(RemoteTransmitData *data, uint32_t samsung_data);
27 | #endif
28 |
29 | #ifdef USE_REMOTE_RECEIVER
30 | SamsungDecodeData decode_samsung(RemoteReceiveData *data);
31 |
32 | class SamsungReceiver : public RemoteReceiver {
33 | public:
34 | SamsungReceiver(const std::string &name, uint32_t data);
35 |
36 | protected:
37 | bool matches(RemoteReceiveData *data) override;
38 |
39 | uint32_t data_;
40 | };
41 |
42 | class SamsungDumper : public RemoteReceiveDumper {
43 | public:
44 | bool dump(RemoteReceiveData *data) override;
45 | };
46 | #endif
47 |
48 | } // namespace remote
49 |
50 | ESPHOME_NAMESPACE_END
51 |
52 | #endif // USE_REMOTE
53 |
54 | #endif // ESPHOME_REMOTE_SAMSUNG_H
55 |
--------------------------------------------------------------------------------
/src/esphome/remote/sony.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_REMOTE_SONY_H
2 | #define ESPHOME_REMOTE_SONY_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_REMOTE
7 |
8 | #include "esphome/remote/remote_receiver.h"
9 | #include "esphome/remote/remote_transmitter.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace remote {
14 |
15 | #ifdef USE_REMOTE_TRANSMITTER
16 | class SonyTransmitter : public RemoteTransmitter {
17 | public:
18 | SonyTransmitter(const std::string &name, uint32_t data, uint8_t nbits);
19 |
20 | void to_data(RemoteTransmitData *data) override;
21 |
22 | protected:
23 | uint32_t data_;
24 | uint8_t nbits_;
25 | };
26 |
27 | void encode_sony(RemoteTransmitData *data, uint32_t sony_data, uint8_t nbits);
28 | #endif
29 |
30 | #ifdef USE_REMOTE_RECEIVER
31 | SonyDecodeData decode_sony(RemoteReceiveData *data);
32 |
33 | class SonyReceiver : public RemoteReceiver {
34 | public:
35 | SonyReceiver(const std::string &name, uint32_t data, uint8_t nbits);
36 |
37 | protected:
38 | bool matches(RemoteReceiveData *data) override;
39 |
40 | uint32_t data_;
41 | uint8_t nbits_;
42 | };
43 |
44 | class SonyDumper : public RemoteReceiveDumper {
45 | public:
46 | bool dump(RemoteReceiveData *data) override;
47 | };
48 | #endif
49 |
50 | } // namespace remote
51 |
52 | ESPHOME_NAMESPACE_END
53 |
54 | #endif // USE_REMOTE
55 |
56 | #endif // ESPHOME_REMOTE_SONY_H
57 |
--------------------------------------------------------------------------------
/src/esphome/sensor/bh1750_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SENSOR_BH1750_SENSOR_H
2 | #define ESPHOME_SENSOR_BH1750_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_BH1750
7 |
8 | #include "esphome/sensor/sensor.h"
9 | #include "esphome/i2c_component.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace sensor {
14 |
15 | /// Enum listing all resolutions that can be used with the BH1750
16 | enum BH1750Resolution {
17 | BH1750_RESOLUTION_4P0_LX = 0b00100011, // one-time low resolution mode
18 | BH1750_RESOLUTION_1P0_LX = 0b00100000, // one-time high resolution mode 1
19 | BH1750_RESOLUTION_0P5_LX = 0b00100001, // one-time high resolution mode 2
20 | };
21 |
22 | /// This class implements support for the i2c-based BH1750 ambient light sensor.
23 | class BH1750Sensor : public PollingSensorComponent, public I2CDevice {
24 | public:
25 | BH1750Sensor(I2CComponent *parent, const std::string &name, uint8_t address = 0x23, uint32_t update_interval = 60000);
26 |
27 | /** Set the resolution of this sensor.
28 | *
29 | * Possible values are:
30 | *
31 | * - `BH1750_RESOLUTION_4P0_LX`
32 | * - `BH1750_RESOLUTION_1P0_LX`
33 | * - `BH1750_RESOLUTION_0P5_LX` (default)
34 | *
35 | * @param resolution The new resolution of the sensor.
36 | */
37 | void set_resolution(BH1750Resolution resolution);
38 |
39 | // ========== INTERNAL METHODS ==========
40 | // (In most use cases you won't need these)
41 | void setup() override;
42 | void dump_config() override;
43 | void update() override;
44 | float get_setup_priority() const override;
45 | std::string unit_of_measurement() override;
46 | std::string icon() override;
47 | int8_t accuracy_decimals() override;
48 |
49 | protected:
50 | void read_data_();
51 |
52 | BH1750Resolution resolution_{BH1750_RESOLUTION_0P5_LX};
53 | };
54 |
55 | } // namespace sensor
56 |
57 | ESPHOME_NAMESPACE_END
58 |
59 | #endif // USE_BH1750
60 |
61 | #endif // ESPHOME_SENSOR_BH1750_SENSOR_H
62 |
--------------------------------------------------------------------------------
/src/esphome/sensor/cse7766.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SENSOR_CSE7766_H
2 | #define ESPHOME_SENSOR_CSE7766_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_CSE7766
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/uart_component.h"
10 | #include "esphome/helpers.h"
11 | #include "esphome/sensor/sensor.h"
12 |
13 | ESPHOME_NAMESPACE_BEGIN
14 |
15 | namespace sensor {
16 |
17 | using CSE7766VoltageSensor = EmptySensor<1, ICON_FLASH, UNIT_V>;
18 | using CSE7766CurrentSensor = EmptySensor<1, ICON_FLASH, UNIT_A>;
19 | using CSE7766PowerSensor = EmptySensor<1, ICON_FLASH, UNIT_W>;
20 |
21 | class CSE7766Component : public PollingComponent, public UARTDevice {
22 | public:
23 | CSE7766Component(UARTComponent *parent, uint32_t update_interval = 60000);
24 |
25 | CSE7766VoltageSensor *make_voltage_sensor(const std::string &name);
26 |
27 | CSE7766CurrentSensor *make_current_sensor(const std::string &name);
28 |
29 | CSE7766PowerSensor *make_power_sensor(const std::string &name);
30 |
31 | void loop() override;
32 | float get_setup_priority() const override;
33 | void update() override;
34 | void dump_config() override;
35 |
36 | protected:
37 | bool check_byte_();
38 | void parse_data_();
39 | uint32_t get_24_bit_uint_(uint8_t start_index);
40 |
41 | uint8_t raw_data_[24];
42 | uint8_t raw_data_index_{0};
43 | uint32_t last_transmission_{0};
44 | CSE7766VoltageSensor *voltage_sensor_{nullptr};
45 | CSE7766CurrentSensor *current_sensor_{nullptr};
46 | CSE7766PowerSensor *power_sensor_{nullptr};
47 | float voltage_acc_{0.0f};
48 | float current_acc_{0.0f};
49 | float power_acc_{0.0f};
50 | uint32_t voltage_counts_{0};
51 | uint32_t current_counts_{0};
52 | uint32_t power_counts_{0};
53 | };
54 |
55 | } // namespace sensor
56 |
57 | ESPHOME_NAMESPACE_END
58 |
59 | #endif // USE_CSE7766
60 |
61 | #endif // ESPHOME_SENSOR_CSE7766_H
62 |
--------------------------------------------------------------------------------
/src/esphome/sensor/custom_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_CUSTOM_SENSOR
4 |
5 | #include "esphome/sensor/custom_sensor.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace sensor {
11 |
12 | static const char *TAG = "sensor.custom";
13 |
14 | CustomSensorConstructor::CustomSensorConstructor(const std::function()> &init) {
15 | this->sensors_ = init();
16 | }
17 | Sensor *CustomSensorConstructor::get_sensor(int i) { return this->sensors_[i]; }
18 | void CustomSensorConstructor::dump_config() {
19 | for (auto *child : this->sensors_) {
20 | LOG_SENSOR("", "Custom Sensor", child);
21 | }
22 | }
23 |
24 | } // namespace sensor
25 |
26 | ESPHOME_NAMESPACE_END
27 |
28 | #endif // USE_CUSTOM_SENSOR
29 |
--------------------------------------------------------------------------------
/src/esphome/sensor/custom_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SENSOR_CUSTOM_SENSOR_H
2 | #define ESPHOME_SENSOR_CUSTOM_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_CUSTOM_SENSOR
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/sensor/sensor.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace sensor {
14 |
15 | class CustomSensorConstructor : public Component {
16 | public:
17 | CustomSensorConstructor(const std::function()> &init);
18 |
19 | Sensor *get_sensor(int i);
20 |
21 | void dump_config() override;
22 |
23 | protected:
24 | std::vector sensors_;
25 | };
26 |
27 | } // namespace sensor
28 |
29 | ESPHOME_NAMESPACE_END
30 |
31 | #endif // USE_CUSTOM_SENSOR
32 |
33 | #endif // ESPHOME_SENSOR_CUSTOM_SENSOR_H
34 |
--------------------------------------------------------------------------------
/src/esphome/sensor/dht12_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SENSOR_DHT12_COMPONENT_H
2 | #define ESPHOME_SENSOR_DHT12_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_DHT12_SENSOR
7 |
8 | #include "esphome/sensor/sensor.h"
9 | #include "esphome/i2c_component.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace sensor {
14 |
15 | using DHT12TemperatureSensor = EmptyPollingParentSensor<1, ICON_EMPTY, UNIT_C>;
16 | using DHT12HumiditySensor = EmptyPollingParentSensor<0, ICON_WATER_PERCENT, UNIT_PERCENT>;
17 |
18 | class DHT12Component : public PollingComponent, public I2CDevice {
19 | public:
20 | DHT12Component(I2CComponent *parent, const std::string &temperature_name, const std::string &humidity_name,
21 | uint32_t update_interval = 60000);
22 |
23 | // ========== INTERNAL METHODS ==========
24 | // (In most use cases you won't need these)
25 | void setup() override;
26 | void dump_config() override;
27 | float get_setup_priority() const override;
28 | void update() override;
29 | DHT12TemperatureSensor *get_temperature_sensor() const;
30 | DHT12HumiditySensor *get_humidity_sensor() const;
31 |
32 | protected:
33 | bool read_data_(uint8_t *data);
34 |
35 | DHT12TemperatureSensor *temperature_sensor_;
36 | DHT12HumiditySensor *humidity_sensor_;
37 | };
38 |
39 | } // namespace sensor
40 |
41 | ESPHOME_NAMESPACE_END
42 |
43 | #endif // USE_DHT12_SENSOR
44 |
45 | #endif // ESPHOME_SENSOR_DHT12_COMPONENT_H
46 |
--------------------------------------------------------------------------------
/src/esphome/sensor/duty_cycle_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_DUTY_CYCLE_SENSOR_H
2 | #define ESPHOME_DUTY_CYCLE_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_DUTY_CYCLE_SENSOR
7 |
8 | #include "esphome/sensor/sensor.h"
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | namespace sensor {
13 |
14 | /// Store data in a class that doesn't use multiple-inheritance (vtables in flash)
15 | struct DutyCycleSensorStore {
16 | volatile uint32_t last_interrupt{0};
17 | volatile uint32_t on_time{0};
18 | volatile bool last_level{false};
19 | ISRInternalGPIOPin *pin;
20 |
21 | static void gpio_intr(DutyCycleSensorStore *arg);
22 | };
23 |
24 | class DutyCycleSensor : public PollingSensorComponent {
25 | public:
26 | DutyCycleSensor(const std::string &name, GPIOPin *pin, uint32_t update_interval = 60000);
27 |
28 | void setup() override;
29 | float get_setup_priority() const override;
30 | void dump_config() override;
31 | void update() override;
32 | std::string unit_of_measurement() override;
33 | std::string icon() override;
34 | int8_t accuracy_decimals() override;
35 |
36 | protected:
37 | GPIOPin *pin_;
38 |
39 | DutyCycleSensorStore store_;
40 | uint32_t last_update_;
41 | };
42 |
43 | } // namespace sensor
44 |
45 | ESPHOME_NAMESPACE_END
46 |
47 | #endif // USE_DUTY_CYCLE_SENSOR
48 |
49 | #endif // ESPHOME_DUTY_CYCLE_SENSOR_H
50 |
--------------------------------------------------------------------------------
/src/esphome/sensor/esp32_hall_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_ESP32_HALL_SENSOR
4 |
5 | #include "esphome/sensor/esp32_hall_sensor.h"
6 | #include "esphome/esphal.h"
7 | #include "esphome/log.h"
8 |
9 | ESPHOME_NAMESPACE_BEGIN
10 |
11 | namespace sensor {
12 |
13 | static const char *TAG = "sensor.esp32_hall";
14 |
15 | ESP32HallSensor::ESP32HallSensor(const std::string &name, uint32_t update_interval)
16 | : PollingSensorComponent(name, update_interval) {}
17 | void ESP32HallSensor::update() {
18 | float value = (hallRead() / 4095.0f) * 10000.0f;
19 | ESP_LOGD(TAG, "'%s': Got reading %.0f µT", this->name_.c_str(), value);
20 | this->publish_state(value);
21 | }
22 | std::string ESP32HallSensor::unit_of_measurement() { return "µT"; }
23 | std::string ESP32HallSensor::icon() { return "mdi:magnet"; }
24 | int8_t ESP32HallSensor::accuracy_decimals() { return -1; }
25 | std::string ESP32HallSensor::unique_id() { return get_mac_address() + "-hall"; }
26 | void ESP32HallSensor::dump_config() { LOG_SENSOR("", "ESP32 Hall Sensor", this); }
27 |
28 | } // namespace sensor
29 |
30 | ESPHOME_NAMESPACE_END
31 |
32 | #endif // USE_ESP32_HALL_SENSOR
33 |
--------------------------------------------------------------------------------
/src/esphome/sensor/esp32_hall_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SENSOR_ESP32_HALL_SENSOR_H
2 | #define ESPHOME_SENSOR_ESP32_HALL_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_ESP32_HALL_SENSOR
7 |
8 | #include "esphome/sensor/sensor.h"
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | namespace sensor {
13 |
14 | class ESP32HallSensor : public PollingSensorComponent {
15 | public:
16 | explicit ESP32HallSensor(const std::string &name, uint32_t update_interval = 60000);
17 |
18 | void dump_config() override;
19 |
20 | void update() override;
21 |
22 | std::string unit_of_measurement() override;
23 | std::string icon() override;
24 | int8_t accuracy_decimals() override;
25 | std::string unique_id() override;
26 | };
27 |
28 | } // namespace sensor
29 |
30 | ESPHOME_NAMESPACE_END
31 |
32 | #endif // USE_ESP32_HALL_SENSOR
33 |
34 | #endif // ESPHOME_SENSOR_ESP32_HALL_SENSOR_H
35 |
--------------------------------------------------------------------------------
/src/esphome/sensor/hdc1080_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SENSOR_HDC1080_COMPONENT_H
2 | #define ESPHOME_SENSOR_HDC1080_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_HDC1080_SENSOR
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/sensor/sensor.h"
10 | #include "esphome/i2c_component.h"
11 |
12 | ESPHOME_NAMESPACE_BEGIN
13 |
14 | namespace sensor {
15 |
16 | using HDC1080TemperatureSensor = EmptyPollingParentSensor<1, ICON_EMPTY, UNIT_C>;
17 | using HDC1080HumiditySensor = EmptyPollingParentSensor<0, ICON_WATER_PERCENT, UNIT_PERCENT>;
18 |
19 | /** HDC1080 temperature+humidity i2c sensor integration.
20 | *
21 | * Based off of implementation by ClosedCube: https://github.com/closedcube/ClosedCube_HDC1080_Arduino
22 | */
23 | class HDC1080Component : public PollingComponent, public I2CDevice {
24 | public:
25 | /// Initialize the component with the provided update interval.
26 | explicit HDC1080Component(I2CComponent *parent, const std::string &temperature_name, const std::string &humidity_name,
27 | uint32_t update_interval);
28 |
29 | // ========== INTERNAL METHODS ==========
30 | // (In most use cases you won't need these)
31 | /// Setup the sensor and check for connection.
32 | void setup() override;
33 | void dump_config() override;
34 | /// Retrieve the latest sensor values. This operation takes approximately 16ms.
35 | void update() override;
36 |
37 | /// Get the internal temperature sensor.
38 | HDC1080TemperatureSensor *get_temperature_sensor() const;
39 | /// Get the internal humidity sensor.
40 | HDC1080HumiditySensor *get_humidity_sensor() const;
41 |
42 | float get_setup_priority() const override;
43 |
44 | protected:
45 | HDC1080TemperatureSensor *temperature_;
46 | HDC1080HumiditySensor *humidity_;
47 | };
48 |
49 | } // namespace sensor
50 |
51 | ESPHOME_NAMESPACE_END
52 |
53 | #endif // USE_HDC1080_SENSOR
54 |
55 | #endif // ESPHOME_SENSOR_HDC1080_COMPONENT_H
56 |
--------------------------------------------------------------------------------
/src/esphome/sensor/hmc5883l.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_HMC_5883_L_H
2 | #define ESPHOME_HMC_5883_L_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_HMC5883L
7 |
8 | #include "esphome/sensor/sensor.h"
9 | #include "esphome/i2c_component.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace sensor {
14 |
15 | enum HMC5883LRange {
16 | HMC5883L_RANGE_88_UT = 0b000,
17 | HMC5883L_RANGE_130_UT = 0b001,
18 | HMC5883L_RANGE_190_UT = 0b010,
19 | HMC5883L_RANGE_250_UT = 0b011,
20 | HMC5883L_RANGE_400_UT = 0b100,
21 | HMC5883L_RANGE_470_UT = 0b101,
22 | HMC5883L_RANGE_560_UT = 0b110,
23 | HMC5883L_RANGE_810_UT = 0b111,
24 | };
25 |
26 | using HMC5883LFieldStrengthSensor = EmptyPollingParentSensor<1, ICON_MAGNET, UNIT_UT>;
27 | using HMC5883LHeadingSensor = EmptyPollingParentSensor<1, ICON_SCREEN_ROTATION, UNIT_DEGREES>;
28 |
29 | class HMC5883LComponent : public PollingComponent, public I2CDevice {
30 | public:
31 | HMC5883LComponent(I2CComponent *parent, uint32_t update_interval = 60000);
32 |
33 | void setup() override;
34 | void dump_config() override;
35 | float get_setup_priority() const override;
36 | void update() override;
37 |
38 | HMC5883LFieldStrengthSensor *make_x_sensor(const std::string &name);
39 | HMC5883LFieldStrengthSensor *make_y_sensor(const std::string &name);
40 | HMC5883LFieldStrengthSensor *make_z_sensor(const std::string &name);
41 | HMC5883LHeadingSensor *make_heading_sensor(const std::string &name);
42 |
43 | void set_range(HMC5883LRange range);
44 |
45 | protected:
46 | HMC5883LRange range_{HMC5883L_RANGE_130_UT};
47 | HMC5883LFieldStrengthSensor *x_sensor_;
48 | HMC5883LFieldStrengthSensor *y_sensor_;
49 | HMC5883LFieldStrengthSensor *z_sensor_;
50 | HMC5883LHeadingSensor *heading_sensor_;
51 | enum ErrorCode {
52 | NONE = 0,
53 | COMMUNICATION_FAILED,
54 | ID_REGISTERS,
55 | } error_code_;
56 | };
57 |
58 | } // namespace sensor
59 |
60 | ESPHOME_NAMESPACE_END
61 |
62 | #endif // USE_HMC5883L
63 |
64 | #endif // ESPHOME_HMC_5883_L_H
65 |
--------------------------------------------------------------------------------
/src/esphome/sensor/homeassistant_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_HOMEASSISTANT_SENSOR
4 |
5 | #include "esphome/sensor/homeassistant_sensor.h"
6 | #include "esphome/api/api_server.h"
7 | #include "esphome/log.h"
8 |
9 | ESPHOME_NAMESPACE_BEGIN
10 |
11 | namespace sensor {
12 |
13 | static const char *TAG = "sensor.homeassistant";
14 |
15 | HomeassistantSensor::HomeassistantSensor(const std::string &name, const std::string &entity_id)
16 | : Sensor(name), entity_id_(entity_id) {}
17 | void HomeassistantSensor::setup() {
18 | api::global_api_server->subscribe_home_assistant_state(this->entity_id_, [this](std::string state) {
19 | auto val = parse_float(state);
20 | if (!val.has_value()) {
21 | ESP_LOGW(TAG, "Can't convert '%s' to number!", state.c_str());
22 | this->publish_state(NAN);
23 | return;
24 | }
25 |
26 | ESP_LOGD(TAG, "'%s': Got state %.2f", this->entity_id_.c_str(), *val);
27 | this->publish_state(*val);
28 | });
29 | }
30 | void HomeassistantSensor::dump_config() {
31 | LOG_SENSOR("", "Homeassistant Sensor", this);
32 | ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_.c_str());
33 | }
34 | float HomeassistantSensor::get_setup_priority() const { return setup_priority::WIFI; }
35 |
36 | } // namespace sensor
37 |
38 | ESPHOME_NAMESPACE_END
39 |
40 | #endif // USE_HOMEASSISTANT_SENSOR
41 |
--------------------------------------------------------------------------------
/src/esphome/sensor/homeassistant_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_HOMEASSISTANT_SENSOR_H
2 | #define ESPHOME_HOMEASSISTANT_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_HOMEASSISTANT_SENSOR
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/sensor/sensor.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace sensor {
14 |
15 | class HomeassistantSensor : public Sensor, public Component {
16 | public:
17 | HomeassistantSensor(const std::string &name, const std::string &entity_id);
18 | void setup() override;
19 | void dump_config() override;
20 | float get_setup_priority() const override;
21 |
22 | protected:
23 | std::string entity_id_;
24 | };
25 |
26 | } // namespace sensor
27 |
28 | ESPHOME_NAMESPACE_END
29 |
30 | #endif // USE_HOMEASSISTANT_SENSOR
31 |
32 | #endif // ESPHOME_HOMEASSISTANT_SENSOR_H
33 |
--------------------------------------------------------------------------------
/src/esphome/sensor/htu21d_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SENSOR_HTU21D_COMPONENT_H
2 | #define ESPHOME_SENSOR_HTU21D_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_HTU21D_SENSOR
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/sensor/sensor.h"
10 | #include "esphome/i2c_component.h"
11 |
12 | ESPHOME_NAMESPACE_BEGIN
13 |
14 | namespace sensor {
15 |
16 | using HTU21DTemperatureSensor = EmptyPollingParentSensor<1, ICON_EMPTY, UNIT_C>;
17 | using HTU21DHumiditySensor = EmptyPollingParentSensor<0, ICON_WATER_PERCENT, UNIT_PERCENT>;
18 |
19 | class HTU21DComponent : public PollingComponent, public I2CDevice {
20 | public:
21 | /// Construct the HTU21D with the given update interval.
22 | HTU21DComponent(I2CComponent *parent, const std::string &temperature_name, const std::string &humidity_name,
23 | uint32_t update_interval = 60000);
24 |
25 | // ========== INTERNAL METHODS ==========
26 | // (In most use cases you won't need these)
27 | /// Get a pointer to the temperature sensor object used to expose temperatures as a sensor.
28 | HTU21DTemperatureSensor *get_temperature_sensor() const;
29 | /// Get a pointer to the humidity sensor object used to expose humidities as a sensor.
30 | HTU21DHumiditySensor *get_humidity_sensor() const;
31 |
32 | /// Setup (reset) the sensor and check connection.
33 | void setup() override;
34 | void dump_config() override;
35 | /// Update the sensor values (temperature+humidity).
36 | void update() override;
37 |
38 | float get_setup_priority() const override;
39 |
40 | protected:
41 | HTU21DTemperatureSensor *temperature_{nullptr};
42 | HTU21DHumiditySensor *humidity_{nullptr};
43 | };
44 |
45 | } // namespace sensor
46 |
47 | ESPHOME_NAMESPACE_END
48 |
49 | #endif // USE_HTU21D_SENSOR
50 |
51 | #endif // ESPHOME_SENSOR_HTU21D_COMPONENT_H
52 |
--------------------------------------------------------------------------------
/src/esphome/sensor/hx711.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SENSOR_HX711_H
2 | #define ESPHOME_SENSOR_HX711_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_HX711
7 |
8 | #include "esphome/sensor/sensor.h"
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | namespace sensor {
13 |
14 | enum HX711Gain {
15 | HX711_GAIN_128 = 1,
16 | HX711_GAIN_32 = 2,
17 | HX711_GAIN_64 = 3,
18 | };
19 |
20 | class HX711Sensor : public PollingSensorComponent {
21 | public:
22 | HX711Sensor(const std::string &name, GPIOPin *dout, GPIOPin *sck, uint32_t update_interval = 60000);
23 |
24 | void setup() override;
25 | void dump_config() override;
26 | float get_setup_priority() const override;
27 | void update() override;
28 |
29 | void set_gain(HX711Gain gain);
30 |
31 | std::string unit_of_measurement() override;
32 | std::string icon() override;
33 | int8_t accuracy_decimals() override;
34 |
35 | protected:
36 | bool read_sensor_(uint32_t *result);
37 |
38 | GPIOPin *dout_pin_;
39 | GPIOPin *sck_pin_;
40 | HX711Gain gain_{HX711_GAIN_128};
41 | };
42 |
43 | } // namespace sensor
44 |
45 | ESPHOME_NAMESPACE_END
46 |
47 | #endif // USE_HX711
48 |
49 | #endif // ESPHOME_SENSOR_HX711_H
50 |
--------------------------------------------------------------------------------
/src/esphome/sensor/ina219.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_INA_219_H
2 | #define ESPHOME_INA_219_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_INA219
7 |
8 | #include "esphome/sensor/sensor.h"
9 | #include "esphome/component.h"
10 | #include "esphome/i2c_component.h"
11 |
12 | ESPHOME_NAMESPACE_BEGIN
13 |
14 | namespace sensor {
15 |
16 | using INA219VoltageSensor = EmptyPollingParentSensor<2, ICON_FLASH, UNIT_V>;
17 | using INA219CurrentSensor = EmptyPollingParentSensor<3, ICON_FLASH, UNIT_A>;
18 | using INA219PowerSensor = EmptyPollingParentSensor<2, ICON_FLASH, UNIT_W>;
19 |
20 | class INA219Component : public PollingComponent, public I2CDevice {
21 | public:
22 | INA219Component(I2CComponent *parent, float shunt_resistance_ohm, float max_current_a, float max_voltage_v,
23 | uint8_t address = 0x40, uint32_t update_interval = 60000);
24 | void setup() override;
25 | void dump_config() override;
26 | float get_setup_priority() const override;
27 | void update() override;
28 |
29 | INA219VoltageSensor *make_bus_voltage_sensor(const std::string &name);
30 | INA219VoltageSensor *make_shunt_voltage_sensor(const std::string &name);
31 | INA219CurrentSensor *make_current_sensor(const std::string &name);
32 | INA219PowerSensor *make_power_sensor(const std::string &name);
33 |
34 | protected:
35 | float shunt_resistance_ohm_;
36 | float max_current_a_;
37 | float max_voltage_v_;
38 | uint32_t calibration_lsb_;
39 | INA219VoltageSensor *bus_voltage_sensor_{nullptr};
40 | INA219VoltageSensor *shunt_voltage_sensor_{nullptr};
41 | INA219CurrentSensor *current_sensor_{nullptr};
42 | INA219PowerSensor *power_sensor_{nullptr};
43 | };
44 |
45 | } // namespace sensor
46 |
47 | ESPHOME_NAMESPACE_END
48 |
49 | #endif // USE_INA219
50 |
51 | #endif // ESPHOME_INA_219_H
52 |
--------------------------------------------------------------------------------
/src/esphome/sensor/ina3221.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_INA3221_H
2 | #define ESPHOME_INA3221_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_INA3221
7 |
8 | #include "esphome/sensor/sensor.h"
9 | #include "esphome/i2c_component.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace sensor {
14 |
15 | using INA3221VoltageSensor = EmptyPollingParentSensor<2, ICON_FLASH, UNIT_V>;
16 | using INA3221CurrentSensor = EmptyPollingParentSensor<2, ICON_FLASH, UNIT_A>;
17 | using INA3221PowerSensor = EmptyPollingParentSensor<2, ICON_FLASH, UNIT_W>;
18 |
19 | class INA3221Component : public PollingComponent, public I2CDevice {
20 | public:
21 | INA3221Component(I2CComponent *parent, uint8_t address = 0x40, uint32_t update_interval = 60000);
22 | void setup() override;
23 | void dump_config() override;
24 | void update() override;
25 | float get_setup_priority() const override;
26 |
27 | INA3221VoltageSensor *make_bus_voltage_sensor(int channel, const std::string &name);
28 | INA3221VoltageSensor *make_shunt_voltage_sensor(int channel, const std::string &name);
29 | INA3221CurrentSensor *make_current_sensor(int channel, const std::string &name);
30 | INA3221PowerSensor *make_power_sensor(int channel, const std::string &name);
31 | void set_shunt_resistance(int channel, float resistance_ohm);
32 |
33 | protected:
34 | struct INA3221Channel {
35 | float shunt_resistance_{0.1f};
36 | INA3221VoltageSensor *bus_voltage_sensor_{nullptr};
37 | INA3221VoltageSensor *shunt_voltage_sensor_{nullptr};
38 | INA3221CurrentSensor *current_sensor_{nullptr};
39 | INA3221PowerSensor *power_sensor_{nullptr};
40 |
41 | bool exists();
42 | bool should_measure_shunt_voltage();
43 | bool should_measure_bus_voltage();
44 | } channels_[3];
45 | };
46 |
47 | } // namespace sensor
48 |
49 | ESPHOME_NAMESPACE_END
50 |
51 | #endif // USE_INA3221
52 |
53 | #endif // ESPHOME_INA3221_H
54 |
--------------------------------------------------------------------------------
/src/esphome/sensor/max31855_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SENSOR_MAX31855_H
2 | #define ESPHOME_SENSOR_MAX31855_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_MAX31855_SENSOR
7 |
8 | #include "esphome/sensor/sensor.h"
9 | #include "esphome/spi_component.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace sensor {
14 |
15 | class MAX31855Sensor : public PollingSensorComponent, public SPIDevice {
16 | public:
17 | MAX31855Sensor(const std::string &name, SPIComponent *parent, GPIOPin *cs, uint32_t update_interval = 60000);
18 |
19 | void setup() override;
20 | void dump_config() override;
21 | float get_setup_priority() const override;
22 |
23 | void update() override;
24 |
25 | std::string unit_of_measurement() override;
26 | std::string icon() override;
27 | int8_t accuracy_decimals() override;
28 |
29 | protected:
30 | bool is_device_msb_first() override;
31 |
32 | void read_data_();
33 | };
34 |
35 | } // namespace sensor
36 |
37 | ESPHOME_NAMESPACE_END
38 |
39 | #endif // USE_MAX31855_SENSOR
40 |
41 | #endif // ESPHOME_SENSOR_MAX31855_H
42 |
--------------------------------------------------------------------------------
/src/esphome/sensor/max6675_sensor.cpp:
--------------------------------------------------------------------------------
1 | // Implementation based on https://github.com/adafruit/MAX6675-library/blob/master/max6675.cpp
2 |
3 | #include "esphome/defines.h"
4 |
5 | #ifdef USE_MAX6675_SENSOR
6 |
7 | #include "esphome/sensor/max6675_sensor.h"
8 | #include "esphome/log.h"
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | namespace sensor {
13 |
14 | static const char *TAG = "sensor.max6675";
15 |
16 | void MAX6675Sensor::update() {
17 | this->enable();
18 | delay(1);
19 | // conversion initiated by rising edge
20 | this->disable();
21 |
22 | // Conversion time typ: 170ms, max: 220ms
23 | auto f = std::bind(&MAX6675Sensor::read_data_, this);
24 | this->set_timeout("value", 220, f);
25 | }
26 |
27 | void MAX6675Sensor::setup() {
28 | ESP_LOGCONFIG(TAG, "Setting up MAX6675Sensor '%s'...", this->name_.c_str());
29 | this->spi_setup();
30 | }
31 | void MAX6675Sensor::dump_config() {
32 | LOG_SENSOR("", "MAX6675", this);
33 | LOG_PIN(" CS Pin: ", this->cs_);
34 | LOG_UPDATE_INTERVAL(this);
35 | }
36 | float MAX6675Sensor::get_setup_priority() const { return setup_priority::HARDWARE_LATE; }
37 | std::string MAX6675Sensor::unit_of_measurement() { return UNIT_C; }
38 | std::string MAX6675Sensor::icon() { return ICON_EMPTY; }
39 | int8_t MAX6675Sensor::accuracy_decimals() { return 1; }
40 | void MAX6675Sensor::read_data_() {
41 | this->enable();
42 | delay(1);
43 | uint8_t data[2];
44 | this->read_array(data, 2);
45 | uint16_t val = data[1] | (uint16_t(data[0]) << 8);
46 | this->disable();
47 |
48 | if ((val & 0x04) != 0) {
49 | // Thermocouple open
50 | ESP_LOGW(TAG, "Got invalid value from MAX6675Sensor (0x%04X)", val);
51 | this->status_set_warning();
52 | return;
53 | }
54 |
55 | float temperature = float(val >> 3) / 4.0f;
56 | ESP_LOGD(TAG, "'%s': Got temperature=%.1f°C", this->name_.c_str(), temperature);
57 | this->publish_state(temperature);
58 | this->status_clear_warning();
59 | }
60 |
61 | MAX6675Sensor::MAX6675Sensor(const std::string &name, SPIComponent *parent, GPIOPin *cs, uint32_t update_interval)
62 | : PollingSensorComponent(name, update_interval), SPIDevice(parent, cs) {}
63 |
64 | bool MAX6675Sensor::is_device_msb_first() { return true; }
65 |
66 | } // namespace sensor
67 |
68 | ESPHOME_NAMESPACE_END
69 |
70 | #endif // USE_MAX6675_SENSOR
71 |
--------------------------------------------------------------------------------
/src/esphome/sensor/max6675_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SENSOR_MAX6675_H
2 | #define ESPHOME_SENSOR_MAX6675_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_MAX6675_SENSOR
7 |
8 | #include "esphome/sensor/sensor.h"
9 | #include "esphome/spi_component.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace sensor {
14 |
15 | class MAX6675Sensor : public PollingSensorComponent, public SPIDevice {
16 | public:
17 | MAX6675Sensor(const std::string &name, SPIComponent *parent, GPIOPin *cs, uint32_t update_interval = 60000);
18 |
19 | void setup() override;
20 | void dump_config() override;
21 | float get_setup_priority() const override;
22 |
23 | void update() override;
24 |
25 | std::string unit_of_measurement() override;
26 | std::string icon() override;
27 | int8_t accuracy_decimals() override;
28 |
29 | protected:
30 | bool is_device_msb_first() override;
31 |
32 | void read_data_();
33 | };
34 |
35 | } // namespace sensor
36 |
37 | ESPHOME_NAMESPACE_END
38 |
39 | #endif // USE_MAX6675_SENSOR
40 |
41 | #endif // ESPHOME_SENSOR_MAX6675_H
42 |
--------------------------------------------------------------------------------
/src/esphome/sensor/mhz19_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_MHZ_19_COMPONENT_H
2 | #define ESPHOME_MHZ_19_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_MHZ19
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/uart_component.h"
10 | #include "esphome/sensor/sensor.h"
11 |
12 | ESPHOME_NAMESPACE_BEGIN
13 |
14 | namespace sensor {
15 |
16 | using MHZ19TemperatureSensor = sensor::EmptyPollingParentSensor<0, ICON_EMPTY, UNIT_C>;
17 | using MHZ19CO2Sensor = sensor::EmptyPollingParentSensor<0, ICON_PERIODIC_TABLE_CO2, UNIT_PPM>;
18 |
19 | class MHZ19Component : public PollingComponent, public UARTDevice {
20 | public:
21 | MHZ19Component(UARTComponent *parent, const std::string &name, uint32_t update_interval = 60000);
22 |
23 | float get_setup_priority() const override;
24 |
25 | void update() override;
26 | void dump_config() override;
27 |
28 | MHZ19TemperatureSensor *make_temperature_sensor(const std::string &name);
29 | MHZ19CO2Sensor *get_co2_sensor() const;
30 |
31 | protected:
32 | bool mhz19_write_command_(const uint8_t *command, uint8_t *response);
33 |
34 | MHZ19TemperatureSensor *temperature_sensor_{nullptr};
35 | MHZ19CO2Sensor *co2_sensor_;
36 | };
37 |
38 | } // namespace sensor
39 |
40 | ESPHOME_NAMESPACE_END
41 |
42 | #endif // USE_MHZ19
43 |
44 | #endif // ESPHOME_MHZ_19_COMPONENT_H
45 |
--------------------------------------------------------------------------------
/src/esphome/sensor/mpu6050_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_MPU_6050_COMPONENT_H
2 | #define ESPHOME_MPU_6050_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_MPU6050
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/sensor/sensor.h"
10 | #include "esphome/i2c_component.h"
11 |
12 | ESPHOME_NAMESPACE_BEGIN
13 |
14 | namespace sensor {
15 |
16 | using MPU6050AccelSensor = EmptyPollingParentSensor<2, ICON_BRIEFCASE_DOWNLOAD, UNIT_M_PER_S_SQUARED>;
17 | using MPU6050GyroSensor = EmptyPollingParentSensor<2, ICON_SCREEN_ROTATION, UNIT_DEGREES_PER_SECOND>;
18 | using MPU6050TemperatureSensor = EmptyPollingParentSensor<1, ICON_EMPTY, UNIT_C>;
19 |
20 | class MPU6050Component : public PollingComponent, public I2CDevice {
21 | public:
22 | explicit MPU6050Component(I2CComponent *parent, uint8_t address = 0x68, uint32_t update_interval = 60000);
23 |
24 | void setup() override;
25 | void dump_config() override;
26 |
27 | void update() override;
28 |
29 | float get_setup_priority() const override;
30 |
31 | MPU6050AccelSensor *make_accel_x_sensor(const std::string &name);
32 | MPU6050AccelSensor *make_accel_y_sensor(const std::string &name);
33 | MPU6050AccelSensor *make_accel_z_sensor(const std::string &name);
34 | MPU6050GyroSensor *make_gyro_x_sensor(const std::string &name);
35 | MPU6050GyroSensor *make_gyro_y_sensor(const std::string &name);
36 | MPU6050GyroSensor *make_gyro_z_sensor(const std::string &name);
37 | MPU6050TemperatureSensor *make_temperature_sensor(const std::string &name);
38 |
39 | protected:
40 | MPU6050AccelSensor *accel_x_sensor_{nullptr};
41 | MPU6050AccelSensor *accel_y_sensor_{nullptr};
42 | MPU6050AccelSensor *accel_z_sensor_{nullptr};
43 | MPU6050TemperatureSensor *temperature_sensor_{nullptr};
44 | MPU6050GyroSensor *gyro_x_sensor_{nullptr};
45 | MPU6050GyroSensor *gyro_y_sensor_{nullptr};
46 | MPU6050GyroSensor *gyro_z_sensor_{nullptr};
47 | };
48 |
49 | } // namespace sensor
50 |
51 | ESPHOME_NAMESPACE_END
52 |
53 | #endif // USE_MPU6050
54 |
55 | #endif // ESPHOME_MPU_6050_COMPONENT_H
56 |
--------------------------------------------------------------------------------
/src/esphome/sensor/mqtt_sensor_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SENSOR_MQTT_SENSOR_COMPONENT_H
2 | #define ESPHOME_SENSOR_MQTT_SENSOR_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_MQTT_SENSOR
7 |
8 | #include "esphome/sensor/sensor.h"
9 | #include "esphome/mqtt/mqtt_component.h"
10 | #include "esphome/helpers.h"
11 | #include "esphome/sensor/filter.h"
12 |
13 | ESPHOME_NAMESPACE_BEGIN
14 |
15 | namespace sensor {
16 |
17 | /// Class that exposes sensors to the MQTT frontend.
18 | class MQTTSensorComponent : public mqtt::MQTTComponent {
19 | public:
20 | /** Construct this MQTTSensorComponent instance with the provided friendly_name and sensor
21 | *
22 | * Note the sensor is never stored and is only used for initializing some values of this class.
23 | * If sensor is nullptr, then automatic initialization of these fields is disabled.
24 | *
25 | * @param sensor The sensor, this can be null to disable automatic setup.
26 | */
27 | explicit MQTTSensorComponent(Sensor *sensor);
28 |
29 | /// Setup an expiry, 0 disables it
30 | void set_expire_after(uint32_t expire_after);
31 | /// Disable Home Assistant value expiry.
32 | void disable_expire_after();
33 |
34 | void send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) override;
35 |
36 | // ========== INTERNAL METHODS ==========
37 | // (In most use cases you won't need these)
38 | /// Override setup.
39 | void setup() override;
40 |
41 | void dump_config() override;
42 |
43 | /// Get the expire_after in milliseconds used for Home Assistant discovery, first checks override.
44 | uint32_t get_expire_after() const;
45 |
46 | bool publish_state(float value);
47 | bool send_initial_state() override;
48 | bool is_internal() override;
49 |
50 | protected:
51 | /// Override for MQTTComponent, returns "sensor".
52 | std::string component_type() const override;
53 |
54 | std::string friendly_name() const override;
55 |
56 | std::string unique_id() override;
57 |
58 | Sensor *sensor_;
59 | optional expire_after_; // Override the expire after advertised to Home Assistant
60 | };
61 |
62 | } // namespace sensor
63 |
64 | ESPHOME_NAMESPACE_END
65 |
66 | #endif // USE_MQTT_SENSOR
67 |
68 | #endif // ESPHOME_SENSOR_MQTT_SENSOR_COMPONENT_H
69 |
--------------------------------------------------------------------------------
/src/esphome/sensor/mqtt_subscribe_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_MQTT_SUBSCRIBE_SENSOR
4 |
5 | #include "esphome/sensor/mqtt_subscribe_sensor.h"
6 | #include "esphome/mqtt/mqtt_client_component.h"
7 | #include "esphome/log.h"
8 | #include
9 | #include
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace sensor {
14 |
15 | static const char *TAG = "sensor.mqtt_subscribe";
16 |
17 | MQTTSubscribeSensor::MQTTSubscribeSensor(const std::string &name, std::string topic)
18 | : Sensor(name), topic_(std::move(topic)) {}
19 | void MQTTSubscribeSensor::setup() {
20 | mqtt::global_mqtt_client->subscribe(this->topic_,
21 | [this](const std::string &topic, std::string payload) {
22 | auto val = parse_float(payload);
23 | if (!val.has_value()) {
24 | ESP_LOGW(TAG, "Can't convert '%s' to number!", payload.c_str());
25 | this->publish_state(NAN);
26 | return;
27 | }
28 |
29 | this->publish_state(*val);
30 | },
31 | this->qos_);
32 | }
33 |
34 | float MQTTSubscribeSensor::get_setup_priority() const { return setup_priority::MQTT_CLIENT - 1.0f; }
35 | void MQTTSubscribeSensor::set_qos(uint8_t qos) { this->qos_ = qos; }
36 | void MQTTSubscribeSensor::dump_config() {
37 | LOG_SENSOR("", "MQTT Subscribe", this);
38 | ESP_LOGCONFIG(TAG, " Topic: %s", this->topic_.c_str());
39 | }
40 |
41 | } // namespace sensor
42 |
43 | ESPHOME_NAMESPACE_END
44 |
45 | #endif // USE_MQTT_SUBSCRIBE_SENSOR
46 |
--------------------------------------------------------------------------------
/src/esphome/sensor/mqtt_subscribe_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SENSOR_MQTT_SUBSCRIBE_SENSOR_H
2 | #define ESPHOME_SENSOR_MQTT_SUBSCRIBE_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_MQTT_SUBSCRIBE_SENSOR
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/sensor/sensor.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace sensor {
14 |
15 | class MQTTSubscribeSensor : public Sensor, public Component {
16 | public:
17 | MQTTSubscribeSensor(const std::string &name, std::string topic);
18 |
19 | void setup() override;
20 | void dump_config() override;
21 | float get_setup_priority() const override;
22 |
23 | void set_qos(uint8_t qos);
24 |
25 | protected:
26 | std::string topic_;
27 | uint8_t qos_{0};
28 | };
29 |
30 | } // namespace sensor
31 |
32 | ESPHOME_NAMESPACE_END
33 |
34 | #endif // USE_MQTT_SUBSCRIBE_SENSOR
35 |
36 | #endif // ESPHOME_SENSOR_MQTT_SUBSCRIBE_SENSOR_H
37 |
--------------------------------------------------------------------------------
/src/esphome/sensor/ms5611.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SENSOR_MS5611_H
2 | #define ESPHOME_SENSOR_MS5611_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_MS5611
7 |
8 | #include "esphome/sensor/sensor.h"
9 | #include "esphome/i2c_component.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace sensor {
14 |
15 | using MS5611TemperatureSensor = sensor::EmptyPollingParentSensor<1, ICON_EMPTY, UNIT_C>;
16 | using MS5611PressureSensor = sensor::EmptyPollingParentSensor<1, ICON_GAUGE, UNIT_HPA>;
17 |
18 | class MS5611Component : public PollingComponent, public I2CDevice {
19 | public:
20 | MS5611Component(I2CComponent *parent, const std::string &temperature_name, const std::string &pressure_name,
21 | uint32_t update_interval = 60000);
22 |
23 | void setup() override;
24 | void dump_config() override;
25 | float get_setup_priority() const override;
26 | void update() override;
27 |
28 | MS5611TemperatureSensor *get_temperature_sensor() const;
29 | MS5611PressureSensor *get_pressure_sensor() const;
30 |
31 | protected:
32 | void read_temperature_();
33 | void read_pressure_(uint32_t raw_temperature);
34 | void calculate_values_(uint32_t raw_temperature, uint32_t raw_pressure);
35 |
36 | MS5611TemperatureSensor *temperature_sensor_;
37 | MS5611PressureSensor *pressure_sensor_;
38 | uint16_t prom_[6];
39 | };
40 |
41 | } // namespace sensor
42 |
43 | ESPHOME_NAMESPACE_END
44 |
45 | #endif // USE_MS5611
46 |
47 | #endif // ESPHOME_SENSOR_MS5611_H
48 |
--------------------------------------------------------------------------------
/src/esphome/sensor/sds011_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SDS011_COMPONENT_H
2 | #define ESPHOME_SDS011_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_SDS011
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/sensor/sensor.h"
10 | #include "esphome/uart_component.h"
11 |
12 | ESPHOME_NAMESPACE_BEGIN
13 |
14 | namespace sensor {
15 |
16 | using SDS011Sensor = sensor::EmptySensor<1, ICON_CHEMICAL_WEAPON, UNIT_MICROGRAMS_PER_CUBIC_METER>;
17 |
18 | class SDS011Component : public Component, public UARTDevice {
19 | public:
20 | /** Construct the component.
21 | *
22 | * @param parent UARTComponent
23 | * @param update_interval_min The update interval in minutes.
24 | * @param rx_mode_only RX-only mode to avoid sending data to the sensor.
25 | */
26 | SDS011Component(UARTComponent *parent, uint8_t update_interval_min = 0, bool rx_mode_only = false);
27 |
28 | /// Manually set the rx-only mode. Defaults to false.
29 | void set_rx_mode_only(bool rx_mode_only);
30 |
31 | // ========== INTERNAL METHODS ==========
32 | // (In most use cases you won't need these)
33 | void setup() override;
34 | void dump_config() override;
35 | void loop() override;
36 |
37 | float get_setup_priority() const override;
38 | SDS011Sensor *make_pm_2_5_sensor(const std::string &name);
39 | SDS011Sensor *make_pm_10_0_sensor(const std::string &name);
40 | bool get_rx_mode_only() const;
41 |
42 | void set_update_interval_min(uint8_t update_interval_min);
43 |
44 | protected:
45 | void sds011_write_command_(const uint8_t *command);
46 | uint8_t sds011_checksum_(const uint8_t *command_data, uint8_t length) const;
47 | optional check_byte_() const;
48 | void parse_data_();
49 | uint16_t get_16_bit_uint_(uint8_t start_index) const;
50 |
51 | SDS011Sensor *pm_2_5_sensor_{nullptr};
52 | SDS011Sensor *pm_10_0_sensor_{nullptr};
53 |
54 | uint8_t data_[10];
55 | uint8_t data_index_{0};
56 | uint32_t last_transmission_{0};
57 | uint8_t update_interval_min_;
58 |
59 | bool rx_mode_only_;
60 | };
61 |
62 | } // namespace sensor
63 |
64 | ESPHOME_NAMESPACE_END
65 |
66 | #endif // USE_SDS011
67 |
68 | #endif // ESPHOME_SDS011_COMPONENT_H
69 |
--------------------------------------------------------------------------------
/src/esphome/sensor/template_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_TEMPLATE_SENSOR
4 |
5 | #include "esphome/sensor/template_sensor.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace sensor {
11 |
12 | static const char *TAG = "sensor.template";
13 |
14 | TemplateSensor::TemplateSensor(const std::string &name, uint32_t update_interval)
15 | : PollingSensorComponent(name, update_interval) {}
16 | void TemplateSensor::update() {
17 | if (!this->f_.has_value())
18 | return;
19 |
20 | auto val = (*this->f_)();
21 | if (val.has_value()) {
22 | this->publish_state(*val);
23 | }
24 | }
25 | float TemplateSensor::get_setup_priority() const { return setup_priority::HARDWARE; }
26 | void TemplateSensor::set_template(std::function()> &&f) { this->f_ = f; }
27 | void TemplateSensor::dump_config() {
28 | LOG_SENSOR("", "Template Sensor", this);
29 | LOG_UPDATE_INTERVAL(this);
30 | }
31 |
32 | } // namespace sensor
33 |
34 | ESPHOME_NAMESPACE_END
35 |
36 | #endif // USE_TEMPLATE_SENSOR
37 |
--------------------------------------------------------------------------------
/src/esphome/sensor/template_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SENSOR_TEMPLATE_SENSOR_H
2 | #define ESPHOME_SENSOR_TEMPLATE_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_TEMPLATE_SENSOR
7 |
8 | #include "esphome/sensor/sensor.h"
9 | #include "esphome/helpers.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace sensor {
14 |
15 | class TemplateSensor : public PollingSensorComponent {
16 | public:
17 | TemplateSensor(const std::string &name, uint32_t update_interval = 60000);
18 |
19 | void set_template(std::function()> &&f);
20 |
21 | void update() override;
22 |
23 | void dump_config() override;
24 |
25 | float get_setup_priority() const override;
26 |
27 | protected:
28 | optional()>> f_;
29 | };
30 |
31 | } // namespace sensor
32 |
33 | ESPHOME_NAMESPACE_END
34 |
35 | #endif // USE_TEMPLATE_SENSOR
36 |
37 | #endif // ESPHOME_SENSOR_TEMPLATE_SENSOR_H
38 |
--------------------------------------------------------------------------------
/src/esphome/sensor/total_daily_energy.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SENSOR_TOTAL_DAILY_ENERGY_H
2 | #define ESPHOME_SENSOR_TOTAL_DAILY_ENERGY_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_TOTAL_DAILY_ENERGY_SENSOR
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/sensor/sensor.h"
10 | #include "esphome/time/rtc_component.h"
11 | #include "esphome/esppreferences.h"
12 |
13 | ESPHOME_NAMESPACE_BEGIN
14 |
15 | namespace sensor {
16 |
17 | class TotalDailyEnergy : public Sensor, public Component {
18 | public:
19 | TotalDailyEnergy(const std::string &name, time::RealTimeClockComponent *time, Sensor *parent);
20 | void setup() override;
21 | void dump_config() override;
22 | float get_setup_priority() const override;
23 | uint32_t update_interval() override;
24 | std::string unit_of_measurement() override;
25 | std::string icon() override;
26 | int8_t accuracy_decimals() override;
27 | void loop() override;
28 |
29 | void publish_state_and_save(float state);
30 |
31 | protected:
32 | void process_new_state_(float state);
33 |
34 | ESPPreferenceObject pref_;
35 | time::RealTimeClockComponent *time_;
36 | Sensor *parent_;
37 | uint16_t last_day_of_year_{};
38 | uint32_t last_update_{0};
39 | float total_energy_{0.0f};
40 | };
41 |
42 | } // namespace sensor
43 |
44 | ESPHOME_NAMESPACE_END
45 |
46 | #endif // USE_TOTAL_DAILY_ENERGY_SENSOR
47 |
48 | #endif // ESPHOME_SENSOR_TOTAL_DAILY_ENERGY_H
49 |
--------------------------------------------------------------------------------
/src/esphome/sensor/ultrasonic_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SENSOR_ULTRASONIC_SENSOR_H
2 | #define ESPHOME_SENSOR_ULTRASONIC_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_ULTRASONIC_SENSOR
7 |
8 | #include "esphome/sensor/sensor.h"
9 | #include "esphome/esphal.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace sensor {
14 |
15 | class UltrasonicSensorComponent : public PollingSensorComponent {
16 | public:
17 | /** Construct the ultrasonic sensor with the specified trigger pin and echo pin.
18 | *
19 | * @param trigger_pin The trigger pin where pulses are sent to.
20 | * @param echo_pin The echo pin where the echo is listened for.
21 | * @param update_interval The interval in ms the sensor should check for new values.
22 | */
23 | UltrasonicSensorComponent(const std::string &name, GPIOPin *trigger_pin, GPIOPin *echo_pin,
24 | uint32_t update_interval = 60000);
25 |
26 | /// Set the timeout for waiting for the echo in µs.
27 | void set_timeout_us(uint32_t timeout_us);
28 |
29 | // ========== INTERNAL METHODS ==========
30 | // (In most use cases you won't need these)
31 | /// Set up pins and register interval.
32 | void setup() override;
33 | void dump_config() override;
34 |
35 | void update() override;
36 |
37 | std::string unit_of_measurement() override;
38 | std::string icon() override;
39 | int8_t accuracy_decimals() override;
40 |
41 | float get_setup_priority() const override;
42 |
43 | /// Set the time in µs the trigger pin should be enabled for in µs, defaults to 10µs (for HC-SR04)
44 | void set_pulse_time_us(uint32_t pulse_time_us);
45 |
46 | protected:
47 | /// Helper function to convert the specified echo duration in µs to meters.
48 | static float us_to_m(uint32_t us);
49 | /// Helper function to convert the specified distance in meters to the echo duration in µs.
50 |
51 | GPIOPin *trigger_pin_;
52 | GPIOPin *echo_pin_;
53 | uint32_t timeout_us_{11662}; /// 2 meters.
54 | uint32_t pulse_time_us_{10};
55 | };
56 |
57 | } // namespace sensor
58 |
59 | ESPHOME_NAMESPACE_END
60 |
61 | #endif // USE_ULTRASONIC_SENSOR
62 |
63 | #endif // ESPHOME_SENSOR_ULTRASONIC_SENSOR_H
64 |
--------------------------------------------------------------------------------
/src/esphome/sensor/uptime_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_UPTIME_SENSOR
4 |
5 | #include "esphome/sensor/uptime_sensor.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace sensor {
11 |
12 | static const char *TAG = "sensor.uptime";
13 |
14 | UptimeSensor::UptimeSensor(const std::string &name, uint32_t update_interval)
15 | : PollingSensorComponent(name, update_interval) {}
16 | void UptimeSensor::update() {
17 | const uint32_t ms = millis();
18 | const uint64_t ms_mask = (1ULL << 32) - 1ULL;
19 | const uint32_t last_ms = this->uptime_ & ms_mask;
20 | if (ms < last_ms) {
21 | this->uptime_ += ms_mask + 1ULL;
22 | ESP_LOGD(TAG, "Detected roll-over \xf0\x9f\xa6\x84");
23 | }
24 | this->uptime_ &= ~ms_mask;
25 | this->uptime_ |= ms;
26 |
27 | // Do separate second and milliseconds conversion to avoid floating point division errors
28 | // Probably some IEEE standard already guarantees this division can be done without loss
29 | // of precision in a single division, but let's do it like this to be sure.
30 | const uint64_t seconds_int = this->uptime_ / 1000ULL;
31 | const float seconds = float(seconds_int) + (this->uptime_ % 1000ULL) / 1000.0f;
32 | this->publish_state(seconds);
33 | }
34 | std::string UptimeSensor::unit_of_measurement() { return "s"; }
35 | std::string UptimeSensor::icon() { return "mdi:timer"; }
36 | int8_t UptimeSensor::accuracy_decimals() { return 0; }
37 | std::string UptimeSensor::unique_id() { return get_mac_address() + "-uptime"; }
38 | float UptimeSensor::get_setup_priority() const { return setup_priority::HARDWARE; }
39 |
40 | } // namespace sensor
41 |
42 | ESPHOME_NAMESPACE_END
43 |
44 | #endif // USE_UPTIME_SENSOR
45 |
--------------------------------------------------------------------------------
/src/esphome/sensor/uptime_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_UPTIME_SENSOR_H
2 | #define ESPHOME_UPTIME_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_UPTIME_SENSOR
7 |
8 | #include "esphome/sensor/sensor.h"
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | namespace sensor {
13 |
14 | class UptimeSensor : public PollingSensorComponent {
15 | public:
16 | explicit UptimeSensor(const std::string &name, uint32_t update_interval = 60000);
17 |
18 | void update() override;
19 |
20 | std::string unit_of_measurement() override;
21 | std::string icon() override;
22 | int8_t accuracy_decimals() override;
23 | std::string unique_id() override;
24 |
25 | float get_setup_priority() const override;
26 |
27 | protected:
28 | uint64_t uptime_{0};
29 | };
30 |
31 | } // namespace sensor
32 |
33 | ESPHOME_NAMESPACE_END
34 |
35 | #endif // USE_UPTIME_SENSOR
36 |
37 | #endif // ESPHOME_UPTIME_SENSOR_H
38 |
--------------------------------------------------------------------------------
/src/esphome/sensor/wifi_signal_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_WIFI_SIGNAL_SENSOR
4 |
5 | #include "esphome/sensor/wifi_signal_sensor.h"
6 | #ifdef ARDUINO_ARCH_ESP32
7 | #include
8 | #else
9 | #include
10 | #endif
11 | #include "esphome/log.h"
12 |
13 | ESPHOME_NAMESPACE_BEGIN
14 |
15 | namespace sensor {
16 |
17 | static const char *TAG = "sensor.wifi_signal";
18 |
19 | WiFiSignalSensor::WiFiSignalSensor(const std::string &name, uint32_t update_interval)
20 | : PollingSensorComponent(name, update_interval) {}
21 | void WiFiSignalSensor::update() { this->publish_state(WiFi.RSSI()); }
22 | std::string WiFiSignalSensor::unit_of_measurement() { return "dB"; }
23 | std::string WiFiSignalSensor::icon() { return "mdi:wifi"; }
24 | int8_t WiFiSignalSensor::accuracy_decimals() { return 0; }
25 | std::string WiFiSignalSensor::unique_id() { return get_mac_address() + "-wifisignal"; }
26 | float WiFiSignalSensor::get_setup_priority() const { return setup_priority::WIFI; }
27 | void WiFiSignalSensor::dump_config() { LOG_SENSOR("", "WiFi Signal", this); }
28 |
29 | } // namespace sensor
30 |
31 | ESPHOME_NAMESPACE_END
32 |
33 | #endif // USE_WIFI_SIGNAL_SENSOR
34 |
--------------------------------------------------------------------------------
/src/esphome/sensor/wifi_signal_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SENSOR_WIFI_SIGNAL_SENSOR_H
2 | #define ESPHOME_SENSOR_WIFI_SIGNAL_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_WIFI_SIGNAL_SENSOR
7 |
8 | #include "esphome/sensor/sensor.h"
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | namespace sensor {
13 |
14 | class WiFiSignalSensor : public PollingSensorComponent {
15 | public:
16 | explicit WiFiSignalSensor(const std::string &name, uint32_t update_interval = 60000);
17 |
18 | void update() override;
19 | void dump_config() override;
20 |
21 | std::string unit_of_measurement() override;
22 | std::string icon() override;
23 | int8_t accuracy_decimals() override;
24 | std::string unique_id() override;
25 | float get_setup_priority() const override;
26 | };
27 |
28 | } // namespace sensor
29 |
30 | ESPHOME_NAMESPACE_END
31 |
32 | #endif // USE_WIFI_SIGNAL_SENSOR
33 |
34 | #endif // ESPHOME_SENSOR_WIFI_SIGNAL_SENSOR_H
35 |
--------------------------------------------------------------------------------
/src/esphome/servo.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_SERVO
4 |
5 | #include "esphome/servo.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | static const char *TAG = "servo";
11 |
12 | void Servo::write(float value) {
13 | if (value < -1.0)
14 | value = -1.0;
15 | else if (value > 1.0)
16 | value = 1.0;
17 |
18 | float level;
19 | if (value < 0.0)
20 | level = lerp(this->idle_level_, this->min_level_, -value);
21 | else
22 | level = lerp(this->idle_level_, this->max_level_, value);
23 |
24 | this->output_->set_level(level);
25 | }
26 | void Servo::setup() { this->write(0.0f); }
27 | void Servo::dump_config() {
28 | ESP_LOGCONFIG(TAG, "Servo:");
29 | ESP_LOGCONFIG(TAG, " Idle Level: %.1f%%", this->idle_level_ * 100.0f);
30 | ESP_LOGCONFIG(TAG, " Min Level: %.1f%%", this->min_level_ * 100.0f);
31 | ESP_LOGCONFIG(TAG, " Max Level: %.1f%%", this->max_level_ * 100.0f);
32 | }
33 | float Servo::get_setup_priority() const { return setup_priority::HARDWARE_LATE; }
34 | void Servo::set_min_level(float min_level) { this->min_level_ = min_level; }
35 | void Servo::set_idle_level(float idle_level) { this->idle_level_ = idle_level; }
36 | void Servo::set_max_level(float max_level) { this->max_level_ = max_level; }
37 | Servo::Servo(output::FloatOutput *output) : output_(output) {}
38 |
39 | ESPHOME_NAMESPACE_END
40 |
41 | #endif // USE_SERVO
42 |
--------------------------------------------------------------------------------
/src/esphome/servo.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_CORE_SERVO_H
2 | #define ESPHOME_CORE_SERVO_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_SERVO
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/output/float_output.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | class Servo : public Component {
14 | public:
15 | Servo(output::FloatOutput *output);
16 | void write(float value);
17 | void setup() override;
18 | void dump_config() override;
19 | float get_setup_priority() const override;
20 | void set_min_level(float min_level);
21 | void set_idle_level(float idle_level);
22 | void set_max_level(float max_level);
23 |
24 | protected:
25 | output::FloatOutput *output_;
26 | float min_level_ = 0.0300f;
27 | float idle_level_ = 0.0750f;
28 | float max_level_ = 0.1200f;
29 | };
30 |
31 | template class ServoWriteAction : public Action {
32 | public:
33 | ServoWriteAction(Servo *servo);
34 | template void set_value(V val) { this->value_ = val; }
35 | void play(Ts... x) override;
36 |
37 | protected:
38 | Servo *servo_;
39 | TemplatableValue value_;
40 | };
41 |
42 | template ServoWriteAction::ServoWriteAction(Servo *servo) : servo_(servo) {}
43 | template void ServoWriteAction::play(Ts... x) {
44 | this->servo_->write(this->value_.value(x...));
45 | this->play_next(x...);
46 | }
47 |
48 | ESPHOME_NAMESPACE_END
49 |
50 | #endif // USE_SERVO
51 |
52 | #endif // ESPHOME_CORE_SERVO_H
53 |
--------------------------------------------------------------------------------
/src/esphome/spi_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SPI_COMPONENT_H
2 | #define ESPHOME_SPI_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_SPI
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/esphal.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | class SPIComponent : public Component {
14 | public:
15 | SPIComponent(GPIOPin *clk, GPIOPin *miso, GPIOPin *mosi);
16 |
17 | void setup() override;
18 |
19 | void dump_config() override;
20 |
21 | uint8_t read_byte();
22 |
23 | void read_array(uint8_t *data, size_t length);
24 |
25 | void write_byte(uint8_t data);
26 |
27 | void write_array(uint8_t *data, size_t length);
28 |
29 | void enable(GPIOPin *cs, bool msb_first, bool high_speed);
30 |
31 | void disable();
32 |
33 | float get_setup_priority() const override;
34 |
35 | void set_miso(const GPIOInputPin &miso);
36 |
37 | void set_mosi(const GPIOOutputPin &mosi);
38 |
39 | protected:
40 | GPIOPin *clk_;
41 | GPIOPin *miso_;
42 | GPIOPin *mosi_;
43 | GPIOPin *active_cs_{nullptr};
44 | bool msb_first_{true};
45 | bool high_speed_{false};
46 | };
47 |
48 | class SPIDevice {
49 | public:
50 | SPIDevice(SPIComponent *parent, GPIOPin *cs);
51 |
52 | void spi_setup();
53 |
54 | void enable();
55 |
56 | void disable();
57 |
58 | uint8_t read_byte();
59 |
60 | void read_array(uint8_t *data, size_t length);
61 |
62 | void write_byte(uint8_t data);
63 |
64 | void write_array(uint8_t *data, size_t length);
65 |
66 | protected:
67 | virtual bool is_device_msb_first() = 0;
68 |
69 | virtual bool is_device_high_speed();
70 |
71 | SPIComponent *parent_;
72 | GPIOPin *cs_;
73 | };
74 |
75 | ESPHOME_NAMESPACE_END
76 |
77 | #endif // USE_SPI
78 |
79 | #endif // ESPHOME_SPI_COMPONENT_H
80 |
--------------------------------------------------------------------------------
/src/esphome/status_led.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_STATUS_LED
4 |
5 | #include "esphome/status_led.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | static const char *TAG = "status_led";
11 |
12 | StatusLEDComponent *global_status_led = nullptr;
13 |
14 | StatusLEDComponent::StatusLEDComponent(GPIOPin *pin) : pin_(pin) { global_status_led = this; }
15 | void StatusLEDComponent::setup() {
16 | ESP_LOGCONFIG(TAG, "Setting up Status LED...");
17 | this->pin_->setup();
18 | this->pin_->digital_write(false);
19 | }
20 | void StatusLEDComponent::dump_config() {
21 | ESP_LOGCONFIG(TAG, "Status LED:");
22 | LOG_PIN(" Pin: ", this->pin_);
23 | }
24 | void StatusLEDComponent::loop() {
25 | if ((global_state & STATUS_LED_ERROR) != 0u) {
26 | this->pin_->digital_write(millis() % 250u < 150u);
27 | } else if ((global_state & STATUS_LED_WARNING) != 0u) {
28 | this->pin_->digital_write(millis() % 1500u < 250u);
29 | } else {
30 | this->pin_->digital_write(false);
31 | }
32 | }
33 | float StatusLEDComponent::get_setup_priority() const { return setup_priority::HARDWARE; }
34 | float StatusLEDComponent::get_loop_priority() const { return 50.0f; }
35 |
36 | ESPHOME_NAMESPACE_END
37 |
38 | #endif // USE_STATUS_LED
39 |
--------------------------------------------------------------------------------
/src/esphome/status_led.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_STATUS_LED_H
2 | #define ESPHOME_STATUS_LED_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_STATUS_LED
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/esphal.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | class StatusLEDComponent : public Component {
14 | public:
15 | explicit StatusLEDComponent(GPIOPin *pin);
16 |
17 | void setup() override;
18 | void dump_config() override;
19 | void loop() override;
20 | float get_setup_priority() const override;
21 | float get_loop_priority() const override;
22 |
23 | protected:
24 | GPIOPin *pin_;
25 | };
26 |
27 | extern StatusLEDComponent *global_status_led;
28 |
29 | ESPHOME_NAMESPACE_END
30 |
31 | #endif // USE_STATUS_LED
32 |
33 | #endif // ESPHOME_STATUS_LED_H
34 |
--------------------------------------------------------------------------------
/src/esphome/stepper/a4988.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_A4988
4 |
5 | #include "esphome/stepper/a4988.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace stepper {
11 |
12 | static const char *TAG = "stepper.a4988";
13 |
14 | void A4988::setup() {
15 | ESP_LOGCONFIG(TAG, "Setting up A4988...");
16 | if (this->sleep_pin_ != nullptr) {
17 | this->sleep_pin_->setup();
18 | this->sleep_pin_->digital_write(false);
19 | }
20 | this->step_pin_->setup();
21 | this->step_pin_->digital_write(false);
22 | this->dir_pin_->setup();
23 | this->dir_pin_->digital_write(false);
24 | }
25 | void A4988::dump_config() {
26 | ESP_LOGCONFIG(TAG, "A4988:");
27 | LOG_PIN(" Step Pin: ", this->step_pin_);
28 | LOG_PIN(" Dir Pin: ", this->dir_pin_);
29 | LOG_PIN(" Sleep Pin: ", this->sleep_pin_);
30 | LOG_STEPPER(this);
31 | }
32 | void A4988::loop() {
33 | bool at_target = this->has_reached_target();
34 | if (this->sleep_pin_ != nullptr) {
35 | this->sleep_pin_->digital_write(!at_target);
36 | }
37 | if (at_target) {
38 | this->high_freq_.stop();
39 | } else {
40 | this->high_freq_.start();
41 | }
42 |
43 | int32_t dir = this->should_step_();
44 | if (dir == 0)
45 | return;
46 |
47 | this->dir_pin_->digital_write(dir == 1);
48 | this->step_pin_->digital_write(true);
49 | delayMicroseconds(5);
50 | this->step_pin_->digital_write(false);
51 | }
52 | float A4988::get_setup_priority() const { return setup_priority::HARDWARE; }
53 | A4988::A4988(GPIOPin *step_pin, GPIOPin *dir_pin) : step_pin_(step_pin), dir_pin_(dir_pin) {}
54 | void A4988::set_sleep_pin(const GPIOOutputPin &sleep_pin) { this->sleep_pin_ = sleep_pin.copy(); }
55 |
56 | } // namespace stepper
57 |
58 | ESPHOME_NAMESPACE_END
59 |
60 | #endif // USE_A4988
61 |
--------------------------------------------------------------------------------
/src/esphome/stepper/a4988.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_STEPPER_A4988_H
2 | #define ESPHOME_STEPPER_A4988_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_A4988
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/stepper/stepper.h"
10 | #include "esphome/esphal.h"
11 |
12 | ESPHOME_NAMESPACE_BEGIN
13 |
14 | namespace stepper {
15 |
16 | class A4988 : public Stepper, public Component {
17 | public:
18 | A4988(GPIOPin *step_pin, GPIOPin *dir_pin);
19 | void set_sleep_pin(const GPIOOutputPin &sleep_pin);
20 | void setup() override;
21 | void dump_config() override;
22 | void loop() override;
23 | float get_setup_priority() const override;
24 |
25 | protected:
26 | GPIOPin *step_pin_;
27 | GPIOPin *dir_pin_;
28 | GPIOPin *sleep_pin_{nullptr};
29 | HighFrequencyLoopRequester high_freq_;
30 | };
31 |
32 | } // namespace stepper
33 |
34 | ESPHOME_NAMESPACE_END
35 |
36 | #endif // USE_A4988
37 |
38 | #endif // ESPHOME_STEPPER_A4988_H
39 |
--------------------------------------------------------------------------------
/src/esphome/stepper/uln2003.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_STEPPER_ULN2003_H
2 | #define ESPHOME_STEPPER_ULN2003_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_ULN2003
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/stepper/stepper.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace stepper {
14 |
15 | enum ULN2003StepMode {
16 | ULN2003_STEP_MODE_FULL_STEP,
17 | ULN2003_STEP_MODE_HALF_STEP,
18 | ULN2003_STEP_MODE_WAVE_DRIVE,
19 | };
20 |
21 | class ULN2003 : public Stepper, public Component {
22 | public:
23 | ULN2003(GPIOPin *a, GPIOPin *b, GPIOPin *c, GPIOPin *d);
24 |
25 | void setup() override;
26 | void loop() override;
27 | void dump_config() override;
28 | float get_setup_priority() const override;
29 | void set_sleep_when_done(bool sleep_when_done);
30 | void set_step_mode(ULN2003StepMode step_mode);
31 |
32 | protected:
33 | void write_step_(int32_t step);
34 |
35 | bool sleep_when_done_{false};
36 | GPIOPin *pin_a_;
37 | GPIOPin *pin_b_;
38 | GPIOPin *pin_c_;
39 | GPIOPin *pin_d_;
40 | ULN2003StepMode step_mode_{ULN2003_STEP_MODE_FULL_STEP};
41 | HighFrequencyLoopRequester high_freq_;
42 | int32_t current_uln_pos_{0};
43 | };
44 |
45 | } // namespace stepper
46 |
47 | ESPHOME_NAMESPACE_END
48 |
49 | #endif // USE_ULN2003
50 |
51 | #endif // ESPHOME_STEPPER_ULN2003_H
52 |
--------------------------------------------------------------------------------
/src/esphome/switch_/custom_switch.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_CUSTOM_SWITCH
4 |
5 | #include "esphome/switch_/custom_switch.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace switch_ {
11 |
12 | static const char *TAG = "switch.custom";
13 |
14 | CustomSwitchConstructor::CustomSwitchConstructor(std::function()> init) {
15 | this->switches_ = init();
16 | }
17 | Switch *CustomSwitchConstructor::get_switch(int i) { return this->switches_[i]; }
18 | void CustomSwitchConstructor::dump_config() {
19 | for (auto *child : this->switches_) {
20 | LOG_SWITCH("", "Custom Switch", child);
21 | }
22 | }
23 |
24 | } // namespace switch_
25 |
26 | ESPHOME_NAMESPACE_END
27 |
28 | #endif // USE_CUSTOM_SWITCH
29 |
--------------------------------------------------------------------------------
/src/esphome/switch_/custom_switch.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_CUSTOM_SWITCH_H
2 | #define ESPHOME_CUSTOM_SWITCH_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_CUSTOM_SWITCH
7 |
8 | #include "esphome/switch_/switch.h"
9 | #include "esphome/component.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace switch_ {
14 |
15 | class CustomSwitchConstructor : public Component {
16 | public:
17 | CustomSwitchConstructor(std::function()> init);
18 |
19 | Switch *get_switch(int i);
20 |
21 | void dump_config() override;
22 |
23 | protected:
24 | std::vector switches_;
25 | };
26 |
27 | } // namespace switch_
28 |
29 | ESPHOME_NAMESPACE_END
30 |
31 | #endif // USE_CUSTOM_SWITCH
32 |
33 | #endif // ESPHOME_CUSTOM_SWITCH_H
34 |
--------------------------------------------------------------------------------
/src/esphome/switch_/gpio_switch.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SWITCH_GPIO_SWITCH_H
2 | #define ESPHOME_SWITCH_GPIO_SWITCH_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_GPIO_SWITCH
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/esphal.h"
10 | #include "esphome/switch_/switch.h"
11 |
12 | ESPHOME_NAMESPACE_BEGIN
13 |
14 | namespace switch_ {
15 |
16 | enum GPIOSwitchRestoreMode {
17 | GPIO_SWITCH_RESTORE_DEFAULT_OFF,
18 | GPIO_SWITCH_RESTORE_DEFAULT_ON,
19 | GPIO_SWITCH_ALWAYS_OFF,
20 | GPIO_SWITCH_ALWAYS_ON,
21 | };
22 |
23 | class GPIOSwitch : public Switch, public Component {
24 | public:
25 | GPIOSwitch(const std::string &name, GPIOPin *pin);
26 |
27 | void set_restore_mode(GPIOSwitchRestoreMode restore_mode);
28 |
29 | // ========== INTERNAL METHODS ==========
30 | // (In most use cases you won't need these)
31 | float get_setup_priority() const override;
32 |
33 | void setup() override;
34 | void dump_config() override;
35 | void set_interlock(const std::vector &interlock);
36 |
37 | protected:
38 | void write_state(bool state) override;
39 |
40 | GPIOPin *const pin_;
41 | GPIOSwitchRestoreMode restore_mode_{GPIO_SWITCH_RESTORE_DEFAULT_OFF};
42 | std::vector interlock_;
43 | };
44 |
45 | } // namespace switch_
46 |
47 | ESPHOME_NAMESPACE_END
48 |
49 | #endif // USE_GPIO_SWITCH
50 |
51 | #endif // ESPHOME_SWITCH_GPIO_SWITCH_H
52 |
--------------------------------------------------------------------------------
/src/esphome/switch_/mqtt_switch_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SWITCH_MQTT_SWITCH_COMPONENT_H
2 | #define ESPHOME_SWITCH_MQTT_SWITCH_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_MQTT_SWITCH
7 |
8 | #include "esphome/binary_sensor/mqtt_binary_sensor_component.h"
9 | #include "esphome/mqtt/mqtt_component.h"
10 | #include "esphome/switch_/switch.h"
11 |
12 | ESPHOME_NAMESPACE_BEGIN
13 |
14 | namespace switch_ {
15 |
16 | /** MQTT representation of switches
17 | *
18 | * Overrides MQTTBinarySensorComponent with a callback that can write values to hardware.
19 | */
20 | class MQTTSwitchComponent : public mqtt::MQTTComponent {
21 | public:
22 | explicit MQTTSwitchComponent(switch_::Switch *a_switch);
23 |
24 | // ========== INTERNAL METHODS ==========
25 | // (In most use cases you won't need these)
26 | void setup() override;
27 | void dump_config() override;
28 |
29 | void send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) override;
30 |
31 | bool send_initial_state() override;
32 | bool is_internal() override;
33 |
34 | bool publish_state(bool state);
35 |
36 | protected:
37 | std::string friendly_name() const override;
38 |
39 | /// "switch" component type.
40 | std::string component_type() const override;
41 |
42 | Switch *switch_;
43 | };
44 |
45 | } // namespace switch_
46 |
47 | ESPHOME_NAMESPACE_END
48 |
49 | #endif // USE_MQTT_SWITCH
50 |
51 | #endif // ESPHOME_SWITCH_MQTT_SWITCH_COMPONENT_H
52 |
--------------------------------------------------------------------------------
/src/esphome/switch_/output_switch.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_OUTPUT_SWITCH
4 |
5 | #include "esphome/switch_/output_switch.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace switch_ {
11 |
12 | static const char *TAG = "switch.output";
13 |
14 | OutputSwitch::OutputSwitch(const std::string &name, output::BinaryOutput *output) : Switch(name), output_(output) {}
15 | void OutputSwitch::write_state(bool state) {
16 | if (state) {
17 | this->output_->turn_on();
18 | } else {
19 | this->output_->turn_off();
20 | }
21 | this->publish_state(state);
22 | }
23 | void OutputSwitch::setup() {
24 | auto restored = this->get_initial_state();
25 | if (!restored.has_value())
26 | return;
27 |
28 | if (*restored) {
29 | this->turn_on();
30 | } else {
31 | this->turn_off();
32 | }
33 | }
34 | float OutputSwitch::get_setup_priority() const { return setup_priority::HARDWARE; }
35 | void OutputSwitch::dump_config() { LOG_SWITCH("", "Output Switch", this); }
36 |
37 | } // namespace switch_
38 |
39 | ESPHOME_NAMESPACE_END
40 |
41 | #endif // USE_OUTPUT_SWITCH
42 |
--------------------------------------------------------------------------------
/src/esphome/switch_/output_switch.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SWITCH_OUTPUT_SWITCH
2 | #define ESPHOME_SWITCH_OUTPUT_SWITCH
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_OUTPUT_SWITCH
7 |
8 | #include "esphome/output/binary_output.h"
9 | #include "esphome/switch_/switch.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace switch_ {
14 |
15 | /// A simple switch that exposes a binary output as a switch.
16 | class OutputSwitch : public Switch, public Component {
17 | public:
18 | /// Construct this SimpleSwitch with the provided BinaryOutput.
19 | explicit OutputSwitch(const std::string &name, output::BinaryOutput *output);
20 |
21 | // ========== INTERNAL METHODS ==========
22 | // (In most use cases you won't need these)
23 |
24 | void setup() override;
25 | float get_setup_priority() const override;
26 | void dump_config() override;
27 |
28 | protected:
29 | void write_state(bool state) override;
30 |
31 | output::BinaryOutput *output_;
32 | };
33 |
34 | } // namespace switch_
35 |
36 | ESPHOME_NAMESPACE_END
37 |
38 | #endif // USE_OUTPUT_SWITCH
39 |
40 | #endif // ESPHOME_SWITCH_OUTPUT_SWITCH
41 |
--------------------------------------------------------------------------------
/src/esphome/switch_/restart_switch.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_RESTART_SWITCH
4 |
5 | #include "esphome/switch_/restart_switch.h"
6 |
7 | #include "esphome/log.h"
8 |
9 | ESPHOME_NAMESPACE_BEGIN
10 |
11 | namespace switch_ {
12 |
13 | static const char *TAG = "switch.restart";
14 |
15 | std::string RestartSwitch::icon() { return "mdi:restart"; }
16 | RestartSwitch::RestartSwitch(const std::string &name) : Switch(name) {}
17 | void RestartSwitch::write_state(bool state) {
18 | // Acknowledge
19 | this->publish_state(false);
20 |
21 | if (state) {
22 | ESP_LOGI(TAG, "Restarting device...");
23 | // then execute
24 | delay(100); // Let MQTT settle a bit
25 | safe_reboot("restart");
26 | }
27 | }
28 | void RestartSwitch::dump_config() { LOG_SWITCH("", "Restart Switch", this); }
29 |
30 | } // namespace switch_
31 |
32 | ESPHOME_NAMESPACE_END
33 |
34 | #endif // USE_RESTART_SWITCH
35 |
--------------------------------------------------------------------------------
/src/esphome/switch_/restart_switch.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_RESTART_SWITCH_H
2 | #define ESPHOME_RESTART_SWITCH_H
3 |
4 | #include "esphome/switch_/switch.h"
5 | #include "esphome/defines.h"
6 |
7 | #ifdef USE_RESTART_SWITCH
8 |
9 | ESPHOME_NAMESPACE_BEGIN
10 |
11 | namespace switch_ {
12 |
13 | /// A simple switch that restarts the device when triggered.
14 | class RestartSwitch : public Switch, public Component {
15 | public:
16 | explicit RestartSwitch(const std::string &name);
17 |
18 | std::string icon() override;
19 |
20 | void dump_config() override;
21 |
22 | protected:
23 | void write_state(bool state) override;
24 | };
25 |
26 | } // namespace switch_
27 |
28 | ESPHOME_NAMESPACE_END
29 |
30 | #endif // USE_RESTART_SWITCH
31 |
32 | #endif // ESPHOME_RESTART_SWITCH_H
33 |
--------------------------------------------------------------------------------
/src/esphome/switch_/shutdown_switch.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_SHUTDOWN_SWITCH
4 |
5 | #include "esphome/switch_/shutdown_switch.h"
6 | #include "esphome/log.h"
7 |
8 | #include
9 |
10 | ESPHOME_NAMESPACE_BEGIN
11 |
12 | namespace switch_ {
13 |
14 | static const char *TAG = "switch.shutdown";
15 |
16 | ShutdownSwitch::ShutdownSwitch(const std::string &name) : Switch(name) {}
17 |
18 | std::string ShutdownSwitch::icon() { return "mdi:power"; }
19 | void ShutdownSwitch::write_state(bool state) {
20 | // Acknowledge
21 | this->publish_state(false);
22 |
23 | if (state) {
24 | ESP_LOGI(TAG, "Shutting down...");
25 | delay(100); // Let MQTT settle a bit
26 |
27 | run_safe_shutdown_hooks("shutdown");
28 | #ifdef ARDUINO_ARCH_ESP8266
29 | ESP.deepSleep(0);
30 | #endif
31 | #ifdef ARDUINO_ARCH_ESP32
32 | esp_deep_sleep_start();
33 | #endif
34 | }
35 | }
36 | void ShutdownSwitch::dump_config() { LOG_SWITCH("", "Shutdown Switch", this); }
37 |
38 | } // namespace switch_
39 |
40 | ESPHOME_NAMESPACE_END
41 |
42 | #endif // USE_SHUTDOWN_SWITCH
43 |
--------------------------------------------------------------------------------
/src/esphome/switch_/shutdown_switch.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SWITCH_SHUTDOWN_SWITCH_H
2 | #define ESPHOME_SWITCH_SHUTDOWN_SWITCH_H
3 |
4 | #include "esphome/switch_/switch.h"
5 | #include "esphome/defines.h"
6 |
7 | #ifdef USE_SHUTDOWN_SWITCH
8 |
9 | ESPHOME_NAMESPACE_BEGIN
10 |
11 | namespace switch_ {
12 |
13 | /// A simple switch that will put the node into deep sleep indefinitely.
14 | class ShutdownSwitch : public Switch, public Component {
15 | public:
16 | explicit ShutdownSwitch(const std::string &name);
17 |
18 | std::string icon() override;
19 |
20 | void dump_config() override;
21 |
22 | protected:
23 | void write_state(bool state) override;
24 | };
25 |
26 | } // namespace switch_
27 |
28 | ESPHOME_NAMESPACE_END
29 |
30 | #endif // USE_RESTART_SWITCH
31 |
32 | #endif // ESPHOME_SWITCH_SHUTDOWN_SWITCH_H
33 |
--------------------------------------------------------------------------------
/src/esphome/switch_/template_switch.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SWITCH_TEMPLATE_SWITCH_H
2 | #define ESPHOME_SWITCH_TEMPLATE_SWITCH_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_TEMPLATE_SWITCH
7 |
8 | #include "esphome/switch_/switch.h"
9 | #include "esphome/helpers.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace switch_ {
14 |
15 | class TemplateSwitch : public Switch, public Component {
16 | public:
17 | explicit TemplateSwitch(const std::string &name);
18 |
19 | void setup() override;
20 | void dump_config() override;
21 |
22 | void set_state_lambda(std::function()> &&f);
23 | void set_restore_state(bool restore_state);
24 | Trigger<> *get_turn_on_trigger() const;
25 | Trigger<> *get_turn_off_trigger() const;
26 | void set_optimistic(bool optimistic);
27 | void set_assumed_state(bool assumed_state);
28 | void loop() override;
29 |
30 | float get_setup_priority() const override;
31 |
32 | protected:
33 | bool assumed_state() override;
34 |
35 | void write_state(bool state) override;
36 |
37 | optional()>> f_;
38 | bool optimistic_{false};
39 | bool assumed_state_{false};
40 | Trigger<> *turn_on_trigger_;
41 | Trigger<> *turn_off_trigger_;
42 | Trigger<> *prev_trigger_{nullptr};
43 | bool restore_state_{false};
44 | };
45 |
46 | } // namespace switch_
47 |
48 | ESPHOME_NAMESPACE_END
49 |
50 | #endif // USE_TEMPLATE_SWITCH
51 |
52 | #endif // ESPHOME_SWITCH_TEMPLATE_SWITCH_H
53 |
--------------------------------------------------------------------------------
/src/esphome/switch_/uart_switch.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_UART_SWITCH
4 |
5 | #include "esphome/switch_/uart_switch.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace switch_ {
11 |
12 | static const char *TAG = "switch.uart";
13 |
14 | UARTSwitch::UARTSwitch(UARTComponent *parent, const std::string &name, const std::vector &data)
15 | : Switch(name), UARTDevice(parent), data_(data) {}
16 |
17 | void UARTSwitch::write_state(bool state) {
18 | if (!state) {
19 | this->publish_state(false);
20 | return;
21 | }
22 |
23 | this->publish_state(true);
24 | ESP_LOGD(TAG, "'%s': Sending data...", this->get_name().c_str());
25 | this->write_array(this->data_.data(), this->data_.size());
26 | this->publish_state(false);
27 | }
28 | void UARTSwitch::dump_config() { LOG_SWITCH("", "UART Switch", this); }
29 |
30 | } // namespace switch_
31 |
32 | ESPHOME_NAMESPACE_END
33 |
34 | #endif // USE_UART_SWITCH
35 |
--------------------------------------------------------------------------------
/src/esphome/switch_/uart_switch.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_UART_SWITCH_H
2 | #define ESPHOME_UART_SWITCH_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_UART_SWITCH
7 |
8 | #include "esphome/switch_/switch.h"
9 | #include "esphome/uart_component.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace switch_ {
14 |
15 | class UARTSwitch : public Switch, public UARTDevice, public Component {
16 | public:
17 | UARTSwitch(UARTComponent *parent, const std::string &name, const std::vector &data);
18 |
19 | void dump_config() override;
20 |
21 | protected:
22 | void write_state(bool state) override;
23 | std::vector data_;
24 | };
25 |
26 | } // namespace switch_
27 |
28 | ESPHOME_NAMESPACE_END
29 |
30 | #endif // USE_UART_SWITCH
31 |
32 | #endif // ESPHOME_UART_SWITCH_H
33 |
--------------------------------------------------------------------------------
/src/esphome/text_sensor/custom_text_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_CUSTOM_TEXT_SENSOR
4 |
5 | #include "esphome/text_sensor/custom_text_sensor.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace text_sensor {
11 |
12 | static const char *TAG = "text_sensor.custom";
13 |
14 | CustomTextSensorConstructor::CustomTextSensorConstructor(std::function()> init) {
15 | this->text_sensors_ = init();
16 | }
17 | TextSensor *CustomTextSensorConstructor::get_text_sensor(int i) { return this->text_sensors_[i]; }
18 | void CustomTextSensorConstructor::dump_config() {
19 | for (auto *child : this->text_sensors_) {
20 | LOG_TEXT_SENSOR("", "Custom Text Sensor", child);
21 | }
22 | }
23 |
24 | } // namespace text_sensor
25 |
26 | ESPHOME_NAMESPACE_END
27 |
28 | #endif // USE_CUSTOM_TEXT_SENSOR
29 |
--------------------------------------------------------------------------------
/src/esphome/text_sensor/custom_text_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_TEXT_SENSOR_CUSTOM_TEXT_SENSOR_H
2 | #define ESPHOME_TEXT_SENSOR_CUSTOM_TEXT_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_CUSTOM_TEXT_SENSOR
7 |
8 | #include "esphome/text_sensor/text_sensor.h"
9 | #include "esphome/log.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace text_sensor {
14 |
15 | class CustomTextSensorConstructor : public Component {
16 | public:
17 | CustomTextSensorConstructor(std::function()> init);
18 |
19 | TextSensor *get_text_sensor(int i);
20 |
21 | void dump_config() override;
22 |
23 | protected:
24 | std::vector text_sensors_;
25 | };
26 |
27 | } // namespace text_sensor
28 |
29 | ESPHOME_NAMESPACE_END
30 |
31 | #endif // USE_CUSTOM_TEXT_SENSOR
32 |
33 | #endif // ESPHOME_TEXT_SENSOR_CUSTOM_TEXT_SENSOR_H
34 |
--------------------------------------------------------------------------------
/src/esphome/text_sensor/homeassistant_text_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_HOMEASSISTANT_TEXT_SENSOR
4 |
5 | #include "esphome/text_sensor/homeassistant_text_sensor.h"
6 | #include "esphome/api/api_server.h"
7 | #include "esphome/log.h"
8 |
9 | ESPHOME_NAMESPACE_BEGIN
10 |
11 | namespace text_sensor {
12 |
13 | static const char *TAG = "text_sensor.homeassistant";
14 |
15 | HomeassistantTextSensor::HomeassistantTextSensor(const std::string &name, const std::string &entity_id)
16 | : TextSensor(name), entity_id_(entity_id) {}
17 | void HomeassistantTextSensor::dump_config() {
18 | LOG_TEXT_SENSOR("", "Homeassistant Text Sensor", this);
19 | ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_.c_str());
20 | }
21 | void HomeassistantTextSensor::setup() {
22 | api::global_api_server->subscribe_home_assistant_state(this->entity_id_, [this](std::string state) {
23 | ESP_LOGD(TAG, "'%s': Got state '%s'", this->entity_id_.c_str(), state.c_str());
24 | this->publish_state(state);
25 | });
26 | }
27 |
28 | } // namespace text_sensor
29 |
30 | ESPHOME_NAMESPACE_END
31 |
32 | #endif // USE_HOMEASSISTANT_TEXT_SENSOR
33 |
--------------------------------------------------------------------------------
/src/esphome/text_sensor/homeassistant_text_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_HOMEASSISTANT_TEXT_SENSOR_H
2 | #define ESPHOME_HOMEASSISTANT_TEXT_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_HOMEASSISTANT_TEXT_SENSOR
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/text_sensor/text_sensor.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace text_sensor {
14 |
15 | class HomeassistantTextSensor : public TextSensor, public Component {
16 | public:
17 | HomeassistantTextSensor(const std::string &name, const std::string &entity_id);
18 | void dump_config() override;
19 | void setup() override;
20 |
21 | protected:
22 | std::string entity_id_;
23 | };
24 |
25 | } // namespace text_sensor
26 |
27 | ESPHOME_NAMESPACE_END
28 |
29 | #endif // USE_HOMEASSISTANT_TEXT_SENSOR
30 |
31 | #endif // ESPHOME_HOMEASSISTANT_TEXT_SENSOR_H
32 |
--------------------------------------------------------------------------------
/src/esphome/text_sensor/mqtt_subscribe_text_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_MQTT_SUBSCRIBE_TEXT_SENSOR
4 |
5 | #include "esphome/text_sensor/mqtt_subscribe_text_sensor.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace text_sensor {
11 |
12 | static const char *TAG = "text_sensor.mqtt_subscribe";
13 |
14 | MQTTSubscribeTextSensor::MQTTSubscribeTextSensor(const std::string &name, std::string topic)
15 | : TextSensor(name), topic_(std::move(topic)) {}
16 | void MQTTSubscribeTextSensor::setup() {
17 | mqtt::global_mqtt_client->subscribe(
18 | this->topic_, [this](const std::string &topic, std::string payload) { this->publish_state(payload); },
19 | this->qos_);
20 | }
21 | float MQTTSubscribeTextSensor::get_setup_priority() const { return setup_priority::MQTT_CLIENT - 1.0f; }
22 | void MQTTSubscribeTextSensor::set_qos(uint8_t qos) { this->qos_ = qos; }
23 | void MQTTSubscribeTextSensor::dump_config() {
24 | LOG_TEXT_SENSOR("", "MQTT Subscribe Text Sensor", this);
25 | ESP_LOGCONFIG(TAG, " Topic: %s", this->topic_.c_str());
26 | }
27 |
28 | } // namespace text_sensor
29 |
30 | ESPHOME_NAMESPACE_END
31 |
32 | #endif // USE_MQTT_SUBSCRIBE_TEXT_SENSOR
33 |
--------------------------------------------------------------------------------
/src/esphome/text_sensor/mqtt_subscribe_text_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_MQTT_SUBSCRIBE_TEXT_SENSOR_H
2 | #define ESPHOME_MQTT_SUBSCRIBE_TEXT_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_MQTT_SUBSCRIBE_TEXT_SENSOR
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/text_sensor/text_sensor.h"
10 | #include "esphome/mqtt/mqtt_client_component.h"
11 |
12 | ESPHOME_NAMESPACE_BEGIN
13 |
14 | namespace text_sensor {
15 |
16 | class MQTTSubscribeTextSensor : public TextSensor, public Component {
17 | public:
18 | MQTTSubscribeTextSensor(const std::string &name, std::string topic);
19 |
20 | void setup() override;
21 | void dump_config() override;
22 | float get_setup_priority() const override;
23 | void set_qos(uint8_t qos);
24 |
25 | protected:
26 | std::string topic_;
27 | uint8_t qos_{0};
28 | };
29 |
30 | } // namespace text_sensor
31 |
32 | ESPHOME_NAMESPACE_END
33 |
34 | #endif // USE_MQTT_SUBSCRIBE_TEXT_SENSOR
35 |
36 | #endif // ESPHOME_MQTT_SUBSCRIBE_TEXT_SENSOR_H
37 |
--------------------------------------------------------------------------------
/src/esphome/text_sensor/mqtt_text_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_MQTT_TEXT_SENSOR
4 |
5 | #include "esphome/text_sensor/mqtt_text_sensor.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace text_sensor {
11 |
12 | static const char *TAG = "text_sensor.mqtt";
13 |
14 | MQTTTextSensor::MQTTTextSensor(TextSensor *sensor) : MQTTComponent(), sensor_(sensor) {}
15 | void MQTTTextSensor::send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) {
16 | if (!this->sensor_->get_icon().empty())
17 | root["icon"] = this->sensor_->get_icon();
18 |
19 | if (!this->sensor_->unique_id().empty())
20 | root["unique_id"] = this->sensor_->unique_id();
21 |
22 | config.command_topic = false;
23 | }
24 | void MQTTTextSensor::setup() {
25 | this->sensor_->add_on_state_callback([this](const std::string &state) { this->publish_state(state); });
26 | }
27 |
28 | void MQTTTextSensor::dump_config() {
29 | ESP_LOGCONFIG(TAG, "MQTT Text Sensor '%s':", this->sensor_->get_name().c_str());
30 | LOG_MQTT_COMPONENT(true, false);
31 | }
32 |
33 | bool MQTTTextSensor::publish_state(const std::string &value) { return this->publish(this->get_state_topic_(), value); }
34 | bool MQTTTextSensor::send_initial_state() {
35 | if (this->sensor_->has_state()) {
36 | return this->publish_state(this->sensor_->state);
37 | } else {
38 | return true;
39 | }
40 | }
41 | bool MQTTTextSensor::is_internal() { return this->sensor_->is_internal(); }
42 | std::string MQTTTextSensor::component_type() const { return "sensor"; }
43 | std::string MQTTTextSensor::friendly_name() const { return this->sensor_->get_name(); }
44 |
45 | } // namespace text_sensor
46 |
47 | ESPHOME_NAMESPACE_END
48 |
49 | #endif // USE_MQTT_TEXT_SENSOR
50 |
--------------------------------------------------------------------------------
/src/esphome/text_sensor/mqtt_text_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_MQTT_TEXT_SENSOR_H
2 | #define ESPHOME_MQTT_TEXT_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_MQTT_TEXT_SENSOR
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/mqtt/mqtt_component.h"
10 | #include "esphome/text_sensor/text_sensor.h"
11 |
12 | ESPHOME_NAMESPACE_BEGIN
13 |
14 | namespace text_sensor {
15 |
16 | class MQTTTextSensor : public mqtt::MQTTComponent {
17 | public:
18 | explicit MQTTTextSensor(TextSensor *sensor);
19 |
20 | void send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) override;
21 |
22 | void setup() override;
23 |
24 | void dump_config() override;
25 |
26 | bool publish_state(const std::string &value);
27 |
28 | bool send_initial_state() override;
29 |
30 | bool is_internal() override;
31 |
32 | protected:
33 | std::string component_type() const override;
34 |
35 | std::string friendly_name() const override;
36 |
37 | TextSensor *sensor_;
38 | };
39 |
40 | } // namespace text_sensor
41 |
42 | ESPHOME_NAMESPACE_END
43 |
44 | #endif // USE_MQTT_TEXT_SENSOR
45 |
46 | #endif // ESPHOME_MQTT_TEXT_SENSOR_H
47 |
--------------------------------------------------------------------------------
/src/esphome/text_sensor/template_text_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_TEMPLATE_TEXT_SENSOR
4 |
5 | #include "esphome/text_sensor/template_text_sensor.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace text_sensor {
11 |
12 | static const char *TAG = "text_sensor.template";
13 |
14 | TemplateTextSensor::TemplateTextSensor(const std::string &name, uint32_t update_interval)
15 | : TextSensor(name), PollingComponent(update_interval) {}
16 | void TemplateTextSensor::update() {
17 | if (!this->f_.has_value())
18 | return;
19 |
20 | auto val = (*this->f_)();
21 | if (val.has_value()) {
22 | this->publish_state(*val);
23 | }
24 | }
25 | float TemplateTextSensor::get_setup_priority() const { return setup_priority::HARDWARE; }
26 | void TemplateTextSensor::set_template(std::function()> &&f) { this->f_ = f; }
27 | void TemplateTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Template Sensor", this); }
28 |
29 | } // namespace text_sensor
30 |
31 | ESPHOME_NAMESPACE_END
32 |
33 | #endif // USE_TEMPLATE_TEXT_SENSOR
34 |
--------------------------------------------------------------------------------
/src/esphome/text_sensor/template_text_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_TEXT_SENSOR_TEMPLATE_TEXT_SENSOR_H
2 | #define ESPHOME_TEXT_SENSOR_TEMPLATE_TEXT_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_TEMPLATE_TEXT_SENSOR
7 |
8 | #include "esphome/text_sensor/text_sensor.h"
9 | #include "esphome/helpers.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace text_sensor {
14 |
15 | class TemplateTextSensor : public TextSensor, public PollingComponent {
16 | public:
17 | TemplateTextSensor(const std::string &name, uint32_t update_interval = 60000);
18 |
19 | void set_template(std::function()> &&f);
20 |
21 | void update() override;
22 |
23 | float get_setup_priority() const override;
24 |
25 | void dump_config() override;
26 |
27 | protected:
28 | optional()>> f_{};
29 | };
30 |
31 | } // namespace text_sensor
32 |
33 | ESPHOME_NAMESPACE_END
34 |
35 | #endif // USE_TEMPLATE_TEXT_SENSOR
36 |
37 | #endif // ESPHOME_TEXT_SENSOR_TEMPLATE_TEXT_SENSOR_H
38 |
--------------------------------------------------------------------------------
/src/esphome/text_sensor/text_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_TEXT_SENSOR
4 |
5 | #include "esphome/text_sensor/text_sensor.h"
6 | #include "esphome/log.h"
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | namespace text_sensor {
11 |
12 | static const char *TAG = "text_sensor.text_sensor";
13 |
14 | TextSensor::TextSensor() : TextSensor("") {}
15 | TextSensor::TextSensor(const std::string &name) : Nameable(name) {}
16 |
17 | void TextSensor::publish_state(std::string state) {
18 | this->state = state;
19 | this->has_state_ = true;
20 | ESP_LOGD(TAG, "'%s': Sending state '%s'", this->name_.c_str(), state.c_str());
21 | this->callback_.call(state);
22 | }
23 | void TextSensor::set_icon(const std::string &icon) { this->icon_ = icon; }
24 | void TextSensor::add_on_state_callback(std::function callback) {
25 | this->callback_.add(std::move(callback));
26 | }
27 | std::string TextSensor::get_icon() {
28 | if (this->icon_.has_value())
29 | return *this->icon_;
30 | return this->icon();
31 | }
32 | std::string TextSensor::icon() { return ""; }
33 | std::string TextSensor::unique_id() { return ""; }
34 | TextSensorStateTrigger *TextSensor::make_state_trigger() { return new TextSensorStateTrigger(this); }
35 | bool TextSensor::has_state() { return this->has_state_; }
36 | uint32_t TextSensor::hash_base() { return 334300109UL; }
37 | #ifdef USE_MQTT_TEXT_SENSOR
38 | MQTTTextSensor *TextSensor::get_mqtt() const { return this->mqtt_; }
39 | void TextSensor::set_mqtt(MQTTTextSensor *mqtt) { this->mqtt_ = mqtt; }
40 | #endif
41 |
42 | TextSensorStateTrigger::TextSensorStateTrigger(TextSensor *parent) {
43 | parent->add_on_state_callback([this](std::string value) { this->trigger(value); });
44 | }
45 |
46 | } // namespace text_sensor
47 |
48 | ESPHOME_NAMESPACE_END
49 |
50 | #endif // USE_TEXT_SENSOR
51 |
--------------------------------------------------------------------------------
/src/esphome/text_sensor/version_text_sensor.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_VERSION_TEXT_SENSOR
4 |
5 | #include "esphome/text_sensor/version_text_sensor.h"
6 | #include "esphome/log.h"
7 | #include "esphome/util.h"
8 |
9 | ESPHOME_NAMESPACE_BEGIN
10 |
11 | namespace text_sensor {
12 |
13 | static const char *TAG = "text_sensor.version";
14 |
15 | void VersionTextSensor::setup() {
16 | if (get_app_compilation_time().empty()) {
17 | this->publish_state(ESPHOME_VERSION);
18 | } else {
19 | this->publish_state(ESPHOME_VERSION " " + get_app_compilation_time());
20 | }
21 | }
22 | float VersionTextSensor::get_setup_priority() const { return setup_priority::HARDWARE_LATE; }
23 | VersionTextSensor::VersionTextSensor(const std::string &name) : TextSensor(name) {}
24 |
25 | std::string VersionTextSensor::icon() { return "mdi:new-box"; }
26 | std::string VersionTextSensor::unique_id() { return get_mac_address() + "-version"; }
27 | void VersionTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Version Text Sensor", this); }
28 |
29 | } // namespace text_sensor
30 |
31 | ESPHOME_NAMESPACE_END
32 |
33 | #endif // USE_VERSION_TEXT_SENSOR
34 |
--------------------------------------------------------------------------------
/src/esphome/text_sensor/version_text_sensor.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_VERSION_TEXT_SENSOR_H
2 | #define ESPHOME_VERSION_TEXT_SENSOR_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_VERSION_TEXT_SENSOR
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/text_sensor/text_sensor.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace text_sensor {
14 |
15 | class VersionTextSensor : public TextSensor, public Component {
16 | public:
17 | explicit VersionTextSensor(const std::string &name);
18 | void setup() override;
19 | void dump_config() override;
20 | float get_setup_priority() const override;
21 | std::string icon() override;
22 | std::string unique_id() override;
23 | };
24 |
25 | } // namespace text_sensor
26 |
27 | ESPHOME_NAMESPACE_END
28 |
29 | #endif // USE_VERSION_TEXT_SENSOR
30 |
31 | #endif // ESPHOME_VERSION_TEXT_SENSOR_H
32 |
--------------------------------------------------------------------------------
/src/esphome/text_sensor/wifi_info.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_WIFI_INFO_TEXT_SENSOR
4 |
5 | #include "esphome/text_sensor/wifi_info.h"
6 | #include "esphome/wifi_component.h"
7 | #include "esphome/log.h"
8 |
9 | ESPHOME_NAMESPACE_BEGIN
10 |
11 | namespace text_sensor {
12 |
13 | static const char *TAG = "text_sensor.wifi_info";
14 |
15 | void IPAddressWiFiInfo::loop() {
16 | IPAddress ip = WiFi.localIP();
17 | if (ip != this->last_ip_) {
18 | this->last_ip_ = ip;
19 | this->publish_state(ip.toString().c_str());
20 | }
21 | }
22 | IPAddressWiFiInfo::IPAddressWiFiInfo(const std::string &name) : TextSensor(name) {}
23 | float IPAddressWiFiInfo::get_setup_priority() const { return setup_priority::WIFI - 1.0f; }
24 |
25 | void SSIDWiFiInfo::loop() {
26 | String ssid = WiFi.SSID();
27 | if (this->last_ssid_ != ssid.c_str()) {
28 | this->last_ssid_ = std::string(ssid.c_str());
29 | this->publish_state(this->last_ssid_);
30 | }
31 | }
32 | SSIDWiFiInfo::SSIDWiFiInfo(const std::string &name) : TextSensor(name) {}
33 | float SSIDWiFiInfo::get_setup_priority() const { return setup_priority::WIFI - 1.0f; }
34 |
35 | void BSSIDWiFiInfo::loop() {
36 | uint8_t *bssid = WiFi.BSSID();
37 | if (memcmp(bssid, this->last_bssid_.data(), 6) != 0) {
38 | std::copy(bssid, bssid + 6, this->last_bssid_.data());
39 | char buf[30];
40 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
41 | this->publish_state(buf);
42 | }
43 | }
44 | BSSIDWiFiInfo::BSSIDWiFiInfo(const std::string &name) : TextSensor(name) {}
45 | float BSSIDWiFiInfo::get_setup_priority() const { return setup_priority::WIFI - 1.0f; }
46 |
47 | } // namespace text_sensor
48 |
49 | ESPHOME_NAMESPACE_END
50 |
51 | #endif // USE_WIFI_INFO_TEXT_SENSOR
52 |
--------------------------------------------------------------------------------
/src/esphome/text_sensor/wifi_info.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_CORE_TEXT_SENSOR_WIFI_INFO_H
2 | #define ESPHOME_CORE_TEXT_SENSOR_WIFI_INFO_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_WIFI_INFO_TEXT_SENSOR
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/wifi_component.h"
10 | #include "esphome/text_sensor/text_sensor.h"
11 |
12 | ESPHOME_NAMESPACE_BEGIN
13 |
14 | namespace text_sensor {
15 |
16 | class IPAddressWiFiInfo : public Component, public TextSensor {
17 | public:
18 | IPAddressWiFiInfo(const std::string &name);
19 | void loop() override;
20 | float get_setup_priority() const override;
21 |
22 | protected:
23 | IPAddress last_ip_;
24 | };
25 |
26 | class SSIDWiFiInfo : public Component, public TextSensor {
27 | public:
28 | SSIDWiFiInfo(const std::string &name);
29 | void loop() override;
30 | float get_setup_priority() const override;
31 |
32 | protected:
33 | std::string last_ssid_;
34 | };
35 |
36 | class BSSIDWiFiInfo : public Component, public TextSensor {
37 | public:
38 | BSSIDWiFiInfo(const std::string &name);
39 | void loop() override;
40 | float get_setup_priority() const override;
41 |
42 | protected:
43 | bssid_t last_bssid_;
44 | };
45 |
46 | } // namespace text_sensor
47 |
48 | ESPHOME_NAMESPACE_END
49 |
50 | #endif // USE_WIFI_INFO_TEXT_SENSOR
51 |
52 | #endif // ESPHOME_CORE_TEXT_SENSOR_WIFI_INFO_H
53 |
--------------------------------------------------------------------------------
/src/esphome/time/homeassistant_time.cpp:
--------------------------------------------------------------------------------
1 | #include "esphome/defines.h"
2 |
3 | #ifdef USE_HOMEASSISTANT_TIME
4 |
5 | #include "esphome/time/homeassistant_time.h"
6 | #include "esphome/api/api_server.h"
7 | #include "esphome/log.h"
8 | #include "lwip/opt.h"
9 | #ifdef ARDUINO_ARCH_ESP8266
10 | #include "sys/time.h"
11 | #endif
12 |
13 | ESPHOME_NAMESPACE_BEGIN
14 |
15 | namespace time {
16 |
17 | static const char *TAG = "time.homeassistant";
18 |
19 | void HomeAssistantTime::set_epoch_time(uint32_t epoch) {
20 | struct timeval timev {
21 | .tv_sec = static_cast(epoch), .tv_usec = 0,
22 | };
23 | timezone tz = {0, 0};
24 | settimeofday(&timev, &tz);
25 |
26 | auto time = this->now();
27 | char buf[128];
28 | time.strftime(buf, sizeof(buf), "%c");
29 | ESP_LOGD(TAG, "Synchronized time: %s", buf);
30 | }
31 | void HomeAssistantTime::dump_config() {
32 | ESP_LOGCONFIG(TAG, "Home Assistant Time:");
33 | ESP_LOGCONFIG(TAG, " Timezone: '%s'", this->timezone_.c_str());
34 | }
35 | float HomeAssistantTime::get_setup_priority() const { return setup_priority::HARDWARE_LATE; }
36 | void HomeAssistantTime::setup() {
37 | global_homeassistant_time = this;
38 |
39 | this->set_interval(15 * 60 * 1000, [this]() {
40 | // re-request time every 15 minutes
41 | api::global_api_server->request_time();
42 | });
43 | }
44 |
45 | HomeAssistantTime *global_homeassistant_time = nullptr;
46 |
47 | bool GetTimeResponse::decode_32bit(uint32_t field_id, uint32_t value) {
48 | switch (field_id) {
49 | case 1:
50 | // fixed32 epoch_seconds = 1;
51 | if (global_homeassistant_time != nullptr) {
52 | global_homeassistant_time->set_epoch_time(value);
53 | }
54 | return true;
55 | default:
56 | return false;
57 | }
58 | }
59 | api::APIMessageType GetTimeResponse::message_type() const { return api::APIMessageType::GET_TIME_RESPONSE; }
60 |
61 | } // namespace time
62 |
63 | ESPHOME_NAMESPACE_END
64 |
65 | #endif // USE_HOMEASSISTANT_TIME
66 |
--------------------------------------------------------------------------------
/src/esphome/time/homeassistant_time.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_HOMEASSISTANT_TIME_H
2 | #define ESPHOME_HOMEASSISTANT_TIME_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_HOMEASSISTANT_TIME
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/time/rtc_component.h"
10 | #include "esphome/api/api_message.h"
11 |
12 | ESPHOME_NAMESPACE_BEGIN
13 |
14 | namespace time {
15 |
16 | class HomeAssistantTime : public RealTimeClockComponent {
17 | public:
18 | void setup() override;
19 | void dump_config() override;
20 | void set_epoch_time(uint32_t epoch);
21 | float get_setup_priority() const override;
22 |
23 | protected:
24 | };
25 |
26 | extern HomeAssistantTime *global_homeassistant_time;
27 |
28 | class GetTimeResponse : public api::APIMessage {
29 | public:
30 | bool decode_32bit(uint32_t field_id, uint32_t value) override;
31 | api::APIMessageType message_type() const override;
32 | };
33 |
34 | } // namespace time
35 |
36 | ESPHOME_NAMESPACE_END
37 |
38 | #endif // USE_HOMEASSISTANT_TIME
39 |
40 | #endif // ESPHOME_HOMEASSISTANT_TIME_H
41 |
--------------------------------------------------------------------------------
/src/esphome/time/sntp_component.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_SNTP_COMPONENT_H
2 | #define ESPHOME_SNTP_COMPONENT_H
3 |
4 | #include "esphome/defines.h"
5 |
6 | #ifdef USE_SNTP_COMPONENT
7 |
8 | #include "esphome/component.h"
9 | #include "esphome/time/rtc_component.h"
10 |
11 | ESPHOME_NAMESPACE_BEGIN
12 |
13 | namespace time {
14 |
15 | /// The SNTP component allows you to configure local timekeeping via Simple Network Time Protocol.
16 | ///
17 | /// \note
18 | /// The C library (newlib) available on ESPs only supports TZ strings that specify an offset and DST info;
19 | /// you cannot specify zone names or paths to zoneinfo files.
20 | /// \see https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
21 | class SNTPComponent : public RealTimeClockComponent {
22 | public:
23 | SNTPComponent();
24 |
25 | void setup() override;
26 | void dump_config() override;
27 | /// Change the servers used by SNTP for timekeeping
28 | void set_servers(const std::string &server_1, const std::string &server_2, const std::string &server_3);
29 | float get_setup_priority() const override;
30 |
31 | void loop() override;
32 |
33 | protected:
34 | std::string server_1_;
35 | std::string server_2_;
36 | std::string server_3_;
37 | bool has_time_{false};
38 | };
39 |
40 | } // namespace time
41 |
42 | ESPHOME_NAMESPACE_END
43 |
44 | #endif // USE_SNTP_COMPONENT
45 |
46 | #endif // ESPHOME_SNTP_COMPONENT_H
47 |
--------------------------------------------------------------------------------
/src/esphome/util.h:
--------------------------------------------------------------------------------
1 | #ifndef ESPHOME_UTIL_H
2 | #define ESPHOME_UTIL_H
3 |
4 | #include "esphome/defines.h"
5 | #include
6 | #include
7 |
8 | ESPHOME_NAMESPACE_BEGIN
9 |
10 | /// Return whether the node is connected to the network (through wifi, eth, ...)
11 | bool network_is_connected();
12 | /// Get the active network hostname
13 | std::string network_get_address();
14 |
15 | /// Manually set up the network stack (outside of the App.setup() loop, for example in OTA safe mode)
16 | void network_setup();
17 | void network_tick();
18 | void network_setup_mdns();
19 | void network_tick_mdns();
20 |
21 | std::string get_app_name();
22 | std::string get_app_compilation_time();
23 |
24 | ESPHOME_NAMESPACE_END
25 |
26 | #endif // ESPHOME_UTIL_H
27 |
--------------------------------------------------------------------------------
/src/esphomelib.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #warning "This header file is deprecated, please include esphome.h instead."
3 | #include "esphome/application.h"
4 |
--------------------------------------------------------------------------------
/travis/neopixelbus.patch:
--------------------------------------------------------------------------------
1 | --- .piolibdeps/NeoPixelBus_ID547/src/internal/NeoEsp8266DmaMethod.h 2018-12-25 06:37:53.000000000 +0100
2 | +++ .piolibdeps/NeoPixelBus_ID547/src/internal/NeoEsp8266DmaMethod.h.2 2019-03-01 22:18:10.000000000 +0100
3 | @@ -169,7 +169,7 @@
4 | _i2sBufDesc[indexDesc].sub_sof = 0;
5 | _i2sBufDesc[indexDesc].datalen = blockSize;
6 | _i2sBufDesc[indexDesc].blocksize = blockSize;
7 | - _i2sBufDesc[indexDesc].buf_ptr = (uint32_t)is2Buffer;
8 | + _i2sBufDesc[indexDesc].buf_ptr = is2Buffer;
9 | _i2sBufDesc[indexDesc].unused = 0;
10 | _i2sBufDesc[indexDesc].next_link_ptr = (uint32_t)&(_i2sBufDesc[indexDesc + 1]);
11 |
12 | @@ -329,11 +329,13 @@
13 | case NeoDmaState_Sending:
14 | {
15 | slc_queue_item* finished_item = (slc_queue_item*)SLCRXEDA;
16 | + uint32_t **ptr = reinterpret_cast(&finished_item);
17 | + uint32_t dat = *reinterpret_cast(ptr);
18 |
19 | // the data block had actual data sent
20 | // point last state block to first state block thus
21 | // just looping and not sending the data blocks
22 | - (finished_item + 1)->next_link_ptr = (uint32_t)(finished_item);
23 | + (finished_item + 1)->next_link_ptr = dat;
24 |
25 | s_this->_dmaState = NeoDmaState_Idle;
26 | }
27 |
--------------------------------------------------------------------------------
/travis/suggest-changes.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | set -e
4 |
5 | if git diff-index --quiet HEAD --; then
6 | echo "No changes detected, formatting is correct!"
7 | exit 0
8 | else
9 | echo "========================================================="
10 | echo "Your formatting is not correct, ESPHome uses clang-format"
11 | echo "The following files are outdated:"
12 | git diff HEAD --name-only | sed 's/^/ /'
13 | echo "To format them, please apply these changes:"
14 | echo "========================================================="
15 | echo
16 | git diff HEAD
17 | exit 1
18 | fi
19 |
--------------------------------------------------------------------------------
/travis/travis.py:
--------------------------------------------------------------------------------
1 | from __future__ import print_function
2 | import glob
3 | import sys
4 | import codecs
5 | import os.path
6 |
7 | errors = []
8 |
9 |
10 | def find_all(a_str, sub):
11 | for i, line in enumerate(a_str.splitlines()):
12 | column = 0
13 | while True:
14 | column = line.find(sub, column)
15 | if column == -1:
16 | break
17 | yield i, column
18 | column += len(sub)
19 |
20 |
21 | files = []
22 | for root, _, fs in os.walk('src'):
23 | for f in fs:
24 | _, ext = os.path.splitext(f)
25 | if ext in ('.h', '.c', '.cpp', '.tcc'):
26 | files.append(os.path.join(root, f))
27 | files.sort()
28 |
29 | for f in files:
30 | try:
31 | with codecs.open(f, 'r', encoding='utf-8') as f_handle:
32 | content = f_handle.read()
33 | except UnicodeDecodeError:
34 | errors.append("File {} is not readable as UTF-8. Please set your editor to UTF-8 mode."
35 | "".format(f))
36 | for line, col in find_all(content, '\t'):
37 | errors.append("File {} contains tab character on line {}:{}. "
38 | "Please convert tabs to spaces.".format(f, line, col))
39 | for line, col in find_all(content, '\r'):
40 | errors.append("File {} contains windows newline on line {}:{}. "
41 | "Please set your editor to unix newline mode.".format(f, line, col))
42 |
43 | for error in errors:
44 | print(error)
45 |
46 | sys.exit(len(errors))
47 |
--------------------------------------------------------------------------------