From 306ab0c56c34792df45c11b8cf0b4cdcfe6970c8 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Thu, 13 Jul 2023 09:50:48 +1200 Subject: [PATCH 01/84] Bump version to 2023.8.0-dev --- esphome/const.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/const.py b/esphome/const.py index 3442c392c7..1c4ccdf3a4 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -1,6 +1,6 @@ """Constants used by esphome.""" -__version__ = "2023.7.0-dev" +__version__ = "2023.8.0-dev" ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_" VALID_SUBSTITUTIONS_CHARACTERS = ( From ac0549578197ae4c6c7a83a53e1b17a82bcfdb61 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Thu, 13 Jul 2023 12:19:04 +1200 Subject: [PATCH 02/84] Dont do mqtt ip lookup if `use_address` has ip address (#5096) * Dont do mqtt ip lookup id `use_address` is in config * Fix after actually testing =) --- esphome/__main__.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/esphome/__main__.py b/esphome/__main__.py index c7c83ad83b..ecf0092b05 100644 --- a/esphome/__main__.py +++ b/esphome/__main__.py @@ -32,7 +32,7 @@ from esphome.const import ( SECRETS_FILES, ) from esphome.core import CORE, EsphomeError, coroutine -from esphome.helpers import indent +from esphome.helpers import indent, is_ip_address from esphome.util import ( run_external_command, run_external_process, @@ -308,8 +308,10 @@ def upload_program(config, args, host): password = ota_conf.get(CONF_PASSWORD, "") if ( - get_port_type(host) == "MQTT" or config[CONF_MDNS][CONF_DISABLED] - ) and CONF_MQTT in config: + not is_ip_address(CORE.address) + and (get_port_type(host) == "MQTT" or config[CONF_MDNS][CONF_DISABLED]) + and CONF_MQTT in config + ): from esphome import mqtt host = mqtt.get_esphome_device_ip( From f8df694aa302ba0f7f935241ea8f4e76c7ddd9f1 Mon Sep 17 00:00:00 2001 From: Jimmy Hedman Date: Thu, 13 Jul 2023 02:32:05 +0200 Subject: [PATCH 03/84] Mk2 to prepare color.h for idf >= 5 (#5070) --- esphome/core/color.h | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/esphome/core/color.h b/esphome/core/color.h index 7062a2a8c8..45b2d4c871 100644 --- a/esphome/core/color.h +++ b/esphome/core/color.h @@ -57,21 +57,6 @@ struct Color { inline bool operator!=(uint32_t colorcode) { // NOLINT return this->raw_32 != colorcode; } - - inline Color &operator=(const Color &rhs) ALWAYS_INLINE { // NOLINT - this->r = rhs.r; - this->g = rhs.g; - this->b = rhs.b; - this->w = rhs.w; - return *this; - } - inline Color &operator=(uint32_t colorcode) ALWAYS_INLINE { - this->w = (colorcode >> 24) & 0xFF; - this->r = (colorcode >> 16) & 0xFF; - this->g = (colorcode >> 8) & 0xFF; - this->b = (colorcode >> 0) & 0xFF; - return *this; - } inline uint8_t &operator[](uint8_t x) ALWAYS_INLINE { return this->raw[x]; } inline Color operator*(uint8_t scale) const ALWAYS_INLINE { return Color(esp_scale8(this->red, scale), esp_scale8(this->green, scale), esp_scale8(this->blue, scale), From 1691c13b477271c45dcb29037702bc4fcaaa7924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Sat, 15 Jul 2023 05:30:19 +0900 Subject: [PATCH 04/84] display: Add helper methods to `Display::clip` and `Display::clamp_x/y_` (#5003) * display: `Rect` make most of methods `const` * display: add `clip` and `clamp_x/y_` methods for clipping to `Display` --- esphome/components/display/display.cpp | 45 +++++++++++++++++++++++--- esphome/components/display/display.h | 9 +++++- esphome/components/display/rect.cpp | 6 ++-- esphome/components/display/rect.h | 12 +++---- 4 files changed, 57 insertions(+), 15 deletions(-) diff --git a/esphome/components/display/display.cpp b/esphome/components/display/display.cpp index 410ff58de3..22454aeddb 100644 --- a/esphome/components/display/display.cpp +++ b/esphome/components/display/display.cpp @@ -269,10 +269,7 @@ void Display::do_update_() { } else if (this->writer_.has_value()) { (*this->writer_)(*this); } - // remove all not ended clipping regions - while (is_clipping()) { - end_clipping(); - } + this->clear_clipping_(); } void DisplayOnPageChangeTrigger::process(DisplayPage *from, DisplayPage *to) { if ((this->from_ == nullptr || this->from_ == from) && (this->to_ == nullptr || this->to_ == to)) @@ -322,13 +319,51 @@ void Display::shrink_clipping(Rect add_rect) { this->clipping_rectangle_.back().shrink(add_rect); } } -Rect Display::get_clipping() { +Rect Display::get_clipping() const { if (this->clipping_rectangle_.empty()) { return Rect(); } else { return this->clipping_rectangle_.back(); } } +void Display::clear_clipping_() { this->clipping_rectangle_.clear(); } +bool Display::clip(int x, int y) { + if (x < 0 || x >= this->get_width() || y < 0 || y >= this->get_height()) + return false; + if (!this->get_clipping().inside(x, y)) + return false; + return true; +} +bool Display::clamp_x_(int x, int w, int &min_x, int &max_x) { + min_x = std::max(x, 0); + max_x = std::min(x + w, this->get_width()); + + if (!this->clipping_rectangle_.empty()) { + const auto &rect = this->clipping_rectangle_.back(); + if (!rect.is_set()) + return false; + + min_x = std::max(min_x, (int) rect.x); + max_x = std::min(max_x, (int) rect.x2()); + } + + return min_x < max_x; +} +bool Display::clamp_y_(int y, int h, int &min_y, int &max_y) { + min_y = std::max(y, 0); + max_y = std::min(y + h, this->get_height()); + + if (!this->clipping_rectangle_.empty()) { + const auto &rect = this->clipping_rectangle_.back(); + if (!rect.is_set()) + return false; + + min_y = std::max(min_y, (int) rect.y); + max_y = std::min(max_y, (int) rect.y2()); + } + + return min_y < max_y; +} DisplayPage::DisplayPage(display_writer_t writer) : writer_(std::move(writer)) {} void DisplayPage::show() { this->parent_->show_page(this); } diff --git a/esphome/components/display/display.h b/esphome/components/display/display.h index 08d8c70e0d..350fd40f26 100644 --- a/esphome/components/display/display.h +++ b/esphome/components/display/display.h @@ -472,14 +472,21 @@ class Display { * * return rect for active clipping region */ - Rect get_clipping(); + Rect get_clipping() const; bool is_clipping() const { return !this->clipping_rectangle_.empty(); } + /** Check if pixel is within region of display. + */ + bool clip(int x, int y); + protected: + bool clamp_x_(int x, int w, int &min_x, int &max_x); + bool clamp_y_(int y, int h, int &min_y, int &max_y); void vprintf_(int x, int y, BaseFont *font, Color color, TextAlign align, const char *format, va_list arg); void do_update_(); + void clear_clipping_(); DisplayRotation rotation_{DISPLAY_ROTATION_0_DEGREES}; optional writer_{}; diff --git a/esphome/components/display/rect.cpp b/esphome/components/display/rect.cpp index 6e91c86c4f..34b611191f 100644 --- a/esphome/components/display/rect.cpp +++ b/esphome/components/display/rect.cpp @@ -60,11 +60,11 @@ void Rect::shrink(Rect rect) { } } -bool Rect::equal(Rect rect) { +bool Rect::equal(Rect rect) const { return (rect.x == this->x) && (rect.w == this->w) && (rect.y == this->y) && (rect.h == this->h); } -bool Rect::inside(int16_t test_x, int16_t test_y, bool absolute) { // NOLINT +bool Rect::inside(int16_t test_x, int16_t test_y, bool absolute) const { // NOLINT if (!this->is_set()) { return true; } @@ -75,7 +75,7 @@ bool Rect::inside(int16_t test_x, int16_t test_y, bool absolute) { // NOLINT } } -bool Rect::inside(Rect rect, bool absolute) { +bool Rect::inside(Rect rect, bool absolute) const { if (!this->is_set() || !rect.is_set()) { return true; } diff --git a/esphome/components/display/rect.h b/esphome/components/display/rect.h index 867a9c67c7..a728ddd132 100644 --- a/esphome/components/display/rect.h +++ b/esphome/components/display/rect.h @@ -16,19 +16,19 @@ class Rect { Rect() : x(VALUE_NO_SET), y(VALUE_NO_SET), w(VALUE_NO_SET), h(VALUE_NO_SET) {} // NOLINT inline Rect(int16_t x, int16_t y, int16_t w, int16_t h) ALWAYS_INLINE : x(x), y(y), w(w), h(h) {} - inline int16_t x2() { return this->x + this->w; }; ///< X coordinate of corner - inline int16_t y2() { return this->y + this->h; }; ///< Y coordinate of corner + inline int16_t x2() const { return this->x + this->w; }; ///< X coordinate of corner + inline int16_t y2() const { return this->y + this->h; }; ///< Y coordinate of corner - inline bool is_set() ALWAYS_INLINE { return (this->h != VALUE_NO_SET) && (this->w != VALUE_NO_SET); } + inline bool is_set() const ALWAYS_INLINE { return (this->h != VALUE_NO_SET) && (this->w != VALUE_NO_SET); } void expand(int16_t horizontal, int16_t vertical); void extend(Rect rect); void shrink(Rect rect); - bool inside(Rect rect, bool absolute = true); - bool inside(int16_t test_x, int16_t test_y, bool absolute = true); - bool equal(Rect rect); + bool inside(Rect rect, bool absolute = true) const; + bool inside(int16_t test_x, int16_t test_y, bool absolute = true) const; + bool equal(Rect rect) const; void info(const std::string &prefix = "rect info:"); }; From 3ac0165f000265b1653a708c020a9e5074c97f91 Mon Sep 17 00:00:00 2001 From: Pierre-Alexis Ciavaldini Date: Sun, 16 Jul 2023 21:42:01 +0200 Subject: [PATCH 05/84] ESP32 enable ADC2 when wifi is disabled (#4381) Co-authored-by: Keith Burzinski --- esphome/components/adc/__init__.py | 59 +++++++++++++++-- esphome/components/adc/adc_sensor.cpp | 93 ++++++++++++++++++--------- esphome/components/adc/adc_sensor.h | 20 ++++-- esphome/components/adc/sensor.py | 40 ++++++++++-- 4 files changed, 166 insertions(+), 46 deletions(-) diff --git a/esphome/components/adc/__init__.py b/esphome/components/adc/__init__.py index cceaa594ef..99dad68501 100644 --- a/esphome/components/adc/__init__.py +++ b/esphome/components/adc/__init__.py @@ -24,6 +24,7 @@ ATTENUATION_MODES = { } adc1_channel_t = cg.global_ns.enum("adc1_channel_t") +adc2_channel_t = cg.global_ns.enum("adc2_channel_t") # From https://github.com/espressif/esp-idf/blob/master/components/driver/include/driver/adc_common.h # pin to adc1 channel mapping @@ -78,6 +79,49 @@ ESP32_VARIANT_ADC1_PIN_TO_CHANNEL = { }, } +ESP32_VARIANT_ADC2_PIN_TO_CHANNEL = { + # TODO: add other variants + VARIANT_ESP32: { + 4: adc2_channel_t.ADC2_CHANNEL_0, + 0: adc2_channel_t.ADC2_CHANNEL_1, + 2: adc2_channel_t.ADC2_CHANNEL_2, + 15: adc2_channel_t.ADC2_CHANNEL_3, + 13: adc2_channel_t.ADC2_CHANNEL_4, + 12: adc2_channel_t.ADC2_CHANNEL_5, + 14: adc2_channel_t.ADC2_CHANNEL_6, + 27: adc2_channel_t.ADC2_CHANNEL_7, + 25: adc2_channel_t.ADC2_CHANNEL_8, + 26: adc2_channel_t.ADC2_CHANNEL_9, + }, + VARIANT_ESP32S2: { + 11: adc2_channel_t.ADC2_CHANNEL_0, + 12: adc2_channel_t.ADC2_CHANNEL_1, + 13: adc2_channel_t.ADC2_CHANNEL_2, + 14: adc2_channel_t.ADC2_CHANNEL_3, + 15: adc2_channel_t.ADC2_CHANNEL_4, + 16: adc2_channel_t.ADC2_CHANNEL_5, + 17: adc2_channel_t.ADC2_CHANNEL_6, + 18: adc2_channel_t.ADC2_CHANNEL_7, + 19: adc2_channel_t.ADC2_CHANNEL_8, + 20: adc2_channel_t.ADC2_CHANNEL_9, + }, + VARIANT_ESP32S3: { + 11: adc2_channel_t.ADC2_CHANNEL_0, + 12: adc2_channel_t.ADC2_CHANNEL_1, + 13: adc2_channel_t.ADC2_CHANNEL_2, + 14: adc2_channel_t.ADC2_CHANNEL_3, + 15: adc2_channel_t.ADC2_CHANNEL_4, + 16: adc2_channel_t.ADC2_CHANNEL_5, + 17: adc2_channel_t.ADC2_CHANNEL_6, + 18: adc2_channel_t.ADC2_CHANNEL_7, + 19: adc2_channel_t.ADC2_CHANNEL_8, + 20: adc2_channel_t.ADC2_CHANNEL_9, + }, + VARIANT_ESP32C3: { + 5: adc2_channel_t.ADC2_CHANNEL_0, + }, +} + def validate_adc_pin(value): if str(value).upper() == "VCC": @@ -89,11 +133,18 @@ def validate_adc_pin(value): if CORE.is_esp32: value = pins.internal_gpio_input_pin_number(value) variant = get_esp32_variant() - if variant not in ESP32_VARIANT_ADC1_PIN_TO_CHANNEL: + if ( + variant not in ESP32_VARIANT_ADC1_PIN_TO_CHANNEL + and variant not in ESP32_VARIANT_ADC2_PIN_TO_CHANNEL + ): raise cv.Invalid(f"This ESP32 variant ({variant}) is not supported") - if value not in ESP32_VARIANT_ADC1_PIN_TO_CHANNEL[variant]: + if ( + value not in ESP32_VARIANT_ADC1_PIN_TO_CHANNEL[variant] + and value not in ESP32_VARIANT_ADC2_PIN_TO_CHANNEL[variant] + ): raise cv.Invalid(f"{variant} doesn't support ADC on this pin") + return pins.internal_gpio_input_pin_schema(value) if CORE.is_esp8266: @@ -104,7 +155,7 @@ def validate_adc_pin(value): ) if value != 17: # A0 - raise cv.Invalid("ESP8266: Only pin A0 (GPIO17) supports ADC.") + raise cv.Invalid("ESP8266: Only pin A0 (GPIO17) supports ADC") return pins.gpio_pin_schema( {CONF_ANALOG: True, CONF_INPUT: True}, internal=True )(value) @@ -112,7 +163,7 @@ def validate_adc_pin(value): if CORE.is_rp2040: value = pins.internal_gpio_input_pin_number(value) if value not in (26, 27, 28, 29): - raise cv.Invalid("RP2040: Only pins 26, 27, 28 and 29 support ADC.") + raise cv.Invalid("RP2040: Only pins 26, 27, 28 and 29 support ADC") return pins.internal_gpio_input_pin_schema(value) raise NotImplementedError diff --git a/esphome/components/adc/adc_sensor.cpp b/esphome/components/adc/adc_sensor.cpp index 9bfe0f5eed..bb6a7a8c85 100644 --- a/esphome/components/adc/adc_sensor.cpp +++ b/esphome/components/adc/adc_sensor.cpp @@ -20,20 +20,20 @@ namespace adc { static const char *const TAG = "adc"; -// 13bit for S2, and 12bit for all other esp32 variants +// 13-bit for S2, 12-bit for all other ESP32 variants #ifdef USE_ESP32 static const adc_bits_width_t ADC_WIDTH_MAX_SOC_BITS = static_cast(ADC_WIDTH_MAX - 1); #ifndef SOC_ADC_RTC_MAX_BITWIDTH #if USE_ESP32_VARIANT_ESP32S2 -static const int SOC_ADC_RTC_MAX_BITWIDTH = 13; +static const int32_t SOC_ADC_RTC_MAX_BITWIDTH = 13; #else -static const int SOC_ADC_RTC_MAX_BITWIDTH = 12; +static const int32_t SOC_ADC_RTC_MAX_BITWIDTH = 12; #endif #endif -static const int ADC_MAX = (1 << SOC_ADC_RTC_MAX_BITWIDTH) - 1; // 4095 (12 bit) or 8191 (13 bit) -static const int ADC_HALF = (1 << SOC_ADC_RTC_MAX_BITWIDTH) >> 1; // 2048 (12 bit) or 4096 (13 bit) +static const int32_t ADC_MAX = (1 << SOC_ADC_RTC_MAX_BITWIDTH) - 1; // 4095 (12 bit) or 8191 (13 bit) +static const int32_t ADC_HALF = (1 << SOC_ADC_RTC_MAX_BITWIDTH) >> 1; // 2048 (12 bit) or 4096 (13 bit) #endif #ifdef USE_RP2040 @@ -47,14 +47,21 @@ extern "C" #endif #ifdef USE_ESP32 - adc1_config_width(ADC_WIDTH_MAX_SOC_BITS); - if (!autorange_) { - adc1_config_channel_atten(channel_, attenuation_); + if (channel1_ != ADC1_CHANNEL_MAX) { + adc1_config_width(ADC_WIDTH_MAX_SOC_BITS); + if (!autorange_) { + adc1_config_channel_atten(channel1_, attenuation_); + } + } else if (channel2_ != ADC2_CHANNEL_MAX) { + if (!autorange_) { + adc2_config_channel_atten(channel2_, attenuation_); + } } // load characteristics for each attenuation - for (int i = 0; i < (int) ADC_ATTEN_MAX; i++) { - auto cal_value = esp_adc_cal_characterize(ADC_UNIT_1, (adc_atten_t) i, ADC_WIDTH_MAX_SOC_BITS, + for (int32_t i = 0; i < (int32_t) ADC_ATTEN_MAX; i++) { + auto adc_unit = channel1_ != ADC1_CHANNEL_MAX ? ADC_UNIT_1 : ADC_UNIT_2; + auto cal_value = esp_adc_cal_characterize(adc_unit, (adc_atten_t) i, ADC_WIDTH_MAX_SOC_BITS, 1100, // default vref &cal_characteristics_[i]); switch (cal_value) { @@ -136,9 +143,9 @@ void ADCSensor::update() { #ifdef USE_ESP8266 float ADCSensor::sample() { #ifdef USE_ADC_SENSOR_VCC - int raw = ESP.getVcc(); // NOLINT(readability-static-accessed-through-instance) + int32_t raw = ESP.getVcc(); // NOLINT(readability-static-accessed-through-instance) #else - int raw = analogRead(this->pin_->get_pin()); // NOLINT + int32_t raw = analogRead(this->pin_->get_pin()); // NOLINT #endif if (output_raw_) { return raw; @@ -150,29 +157,53 @@ float ADCSensor::sample() { #ifdef USE_ESP32 float ADCSensor::sample() { if (!autorange_) { - int raw = adc1_get_raw(channel_); + int32_t raw = -1; + if (channel1_ != ADC1_CHANNEL_MAX) { + raw = adc1_get_raw(channel1_); + } else if (channel2_ != ADC2_CHANNEL_MAX) { + adc2_get_raw(channel2_, ADC_WIDTH_MAX_SOC_BITS, &raw); + } + if (raw == -1) { return NAN; } if (output_raw_) { return raw; } - uint32_t mv = esp_adc_cal_raw_to_voltage(raw, &cal_characteristics_[(int) attenuation_]); + uint32_t mv = esp_adc_cal_raw_to_voltage(raw, &cal_characteristics_[(int32_t) attenuation_]); return mv / 1000.0f; } - int raw11, raw6 = ADC_MAX, raw2 = ADC_MAX, raw0 = ADC_MAX; - adc1_config_channel_atten(channel_, ADC_ATTEN_DB_11); - raw11 = adc1_get_raw(channel_); - if (raw11 < ADC_MAX) { - adc1_config_channel_atten(channel_, ADC_ATTEN_DB_6); - raw6 = adc1_get_raw(channel_); - if (raw6 < ADC_MAX) { - adc1_config_channel_atten(channel_, ADC_ATTEN_DB_2_5); - raw2 = adc1_get_raw(channel_); - if (raw2 < ADC_MAX) { - adc1_config_channel_atten(channel_, ADC_ATTEN_DB_0); - raw0 = adc1_get_raw(channel_); + int32_t raw11 = ADC_MAX, raw6 = ADC_MAX, raw2 = ADC_MAX, raw0 = ADC_MAX; + + if (channel1_ != ADC1_CHANNEL_MAX) { + adc1_config_channel_atten(channel1_, ADC_ATTEN_DB_11); + raw11 = adc1_get_raw(channel1_); + if (raw11 < ADC_MAX) { + adc1_config_channel_atten(channel1_, ADC_ATTEN_DB_6); + raw6 = adc1_get_raw(channel1_); + if (raw6 < ADC_MAX) { + adc1_config_channel_atten(channel1_, ADC_ATTEN_DB_2_5); + raw2 = adc1_get_raw(channel1_); + if (raw2 < ADC_MAX) { + adc1_config_channel_atten(channel1_, ADC_ATTEN_DB_0); + raw0 = adc1_get_raw(channel1_); + } + } + } + } else if (channel2_ != ADC2_CHANNEL_MAX) { + adc2_config_channel_atten(channel2_, ADC_ATTEN_DB_11); + adc2_get_raw(channel2_, ADC_WIDTH_MAX_SOC_BITS, &raw11); + if (raw11 < ADC_MAX) { + adc2_config_channel_atten(channel2_, ADC_ATTEN_DB_6); + adc2_get_raw(channel2_, ADC_WIDTH_MAX_SOC_BITS, &raw6); + if (raw6 < ADC_MAX) { + adc2_config_channel_atten(channel2_, ADC_ATTEN_DB_2_5); + adc2_get_raw(channel2_, ADC_WIDTH_MAX_SOC_BITS, &raw2); + if (raw2 < ADC_MAX) { + adc2_config_channel_atten(channel2_, ADC_ATTEN_DB_0); + adc2_get_raw(channel2_, ADC_WIDTH_MAX_SOC_BITS, &raw0); + } } } } @@ -181,10 +212,10 @@ float ADCSensor::sample() { return NAN; } - uint32_t mv11 = esp_adc_cal_raw_to_voltage(raw11, &cal_characteristics_[(int) ADC_ATTEN_DB_11]); - uint32_t mv6 = esp_adc_cal_raw_to_voltage(raw6, &cal_characteristics_[(int) ADC_ATTEN_DB_6]); - uint32_t mv2 = esp_adc_cal_raw_to_voltage(raw2, &cal_characteristics_[(int) ADC_ATTEN_DB_2_5]); - uint32_t mv0 = esp_adc_cal_raw_to_voltage(raw0, &cal_characteristics_[(int) ADC_ATTEN_DB_0]); + uint32_t mv11 = esp_adc_cal_raw_to_voltage(raw11, &cal_characteristics_[(int32_t) ADC_ATTEN_DB_11]); + uint32_t mv6 = esp_adc_cal_raw_to_voltage(raw6, &cal_characteristics_[(int32_t) ADC_ATTEN_DB_6]); + uint32_t mv2 = esp_adc_cal_raw_to_voltage(raw2, &cal_characteristics_[(int32_t) ADC_ATTEN_DB_2_5]); + uint32_t mv0 = esp_adc_cal_raw_to_voltage(raw0, &cal_characteristics_[(int32_t) ADC_ATTEN_DB_0]); // Contribution of each value, in range 0-2048 (12 bit ADC) or 0-4096 (13 bit ADC) uint32_t c11 = std::min(raw11, ADC_HALF); @@ -212,7 +243,7 @@ float ADCSensor::sample() { adc_select_input(pin - 26); } - int raw = adc_read(); + int32_t raw = adc_read(); if (this->is_temperature_) { adc_set_temp_sensor_enabled(false); } diff --git a/esphome/components/adc/adc_sensor.h b/esphome/components/adc/adc_sensor.h index 22cddde6f8..a905177790 100644 --- a/esphome/components/adc/adc_sensor.h +++ b/esphome/components/adc/adc_sensor.h @@ -19,16 +19,23 @@ class ADCSensor : public sensor::Sensor, public PollingComponent, public voltage #ifdef USE_ESP32 /// Set the attenuation for this pin. Only available on the ESP32. void set_attenuation(adc_atten_t attenuation) { attenuation_ = attenuation; } - void set_channel(adc1_channel_t channel) { channel_ = channel; } + void set_channel1(adc1_channel_t channel) { + channel1_ = channel; + channel2_ = ADC2_CHANNEL_MAX; + } + void set_channel2(adc2_channel_t channel) { + channel2_ = channel; + channel1_ = ADC1_CHANNEL_MAX; + } void set_autorange(bool autorange) { autorange_ = autorange; } #endif - /// Update adc values. + /// Update ADC values void update() override; - /// Setup ADc + /// Setup ADC void setup() override; void dump_config() override; - /// `HARDWARE_LATE` setup priority. + /// `HARDWARE_LATE` setup priority float get_setup_priority() const override; void set_pin(InternalGPIOPin *pin) { this->pin_ = pin; } void set_output_raw(bool output_raw) { output_raw_ = output_raw; } @@ -52,9 +59,10 @@ class ADCSensor : public sensor::Sensor, public PollingComponent, public voltage #ifdef USE_ESP32 adc_atten_t attenuation_{ADC_ATTEN_DB_0}; - adc1_channel_t channel_{}; + adc1_channel_t channel1_{ADC1_CHANNEL_MAX}; + adc2_channel_t channel2_{ADC2_CHANNEL_MAX}; bool autorange_{false}; - esp_adc_cal_characteristics_t cal_characteristics_[(int) ADC_ATTEN_MAX] = {}; + esp_adc_cal_characteristics_t cal_characteristics_[(int32_t) ADC_ATTEN_MAX] = {}; #endif }; diff --git a/esphome/components/adc/sensor.py b/esphome/components/adc/sensor.py index 4695e96570..a0eda1d659 100644 --- a/esphome/components/adc/sensor.py +++ b/esphome/components/adc/sensor.py @@ -1,5 +1,7 @@ import esphome.codegen as cg import esphome.config_validation as cv +import esphome.final_validate as fv +from esphome.core import CORE from esphome.components import sensor, voltage_sampler from esphome.components.esp32 import get_esp32_variant from esphome.const import ( @@ -8,15 +10,15 @@ from esphome.const import ( CONF_NUMBER, CONF_PIN, CONF_RAW, + CONF_WIFI, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT, UNIT_VOLT, ) -from esphome.core import CORE - from . import ( ATTENUATION_MODES, ESP32_VARIANT_ADC1_PIN_TO_CHANNEL, + ESP32_VARIANT_ADC2_PIN_TO_CHANNEL, validate_adc_pin, ) @@ -25,7 +27,23 @@ AUTO_LOAD = ["voltage_sampler"] def validate_config(config): if config[CONF_RAW] and config.get(CONF_ATTENUATION, None) == "auto": - raise cv.Invalid("Automatic attenuation cannot be used when raw output is set.") + raise cv.Invalid("Automatic attenuation cannot be used when raw output is set") + + return config + + +def final_validate_config(config): + if CORE.is_esp32: + variant = get_esp32_variant() + if ( + CONF_WIFI in fv.full_config.get() + and config[CONF_PIN][CONF_NUMBER] + in ESP32_VARIANT_ADC2_PIN_TO_CHANNEL[variant] + ): + raise cv.Invalid( + f"{variant} doesn't support ADC on this pin when Wi-Fi is configured" + ) + return config @@ -55,6 +73,8 @@ CONFIG_SCHEMA = cv.All( validate_config, ) +FINAL_VALIDATE_SCHEMA = final_validate_config + async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) @@ -81,5 +101,15 @@ async def to_code(config): if CORE.is_esp32: variant = get_esp32_variant() pin_num = config[CONF_PIN][CONF_NUMBER] - chan = ESP32_VARIANT_ADC1_PIN_TO_CHANNEL[variant][pin_num] - cg.add(var.set_channel(chan)) + if ( + variant in ESP32_VARIANT_ADC1_PIN_TO_CHANNEL + and pin_num in ESP32_VARIANT_ADC1_PIN_TO_CHANNEL[variant] + ): + chan = ESP32_VARIANT_ADC1_PIN_TO_CHANNEL[variant][pin_num] + cg.add(var.set_channel1(chan)) + elif ( + variant in ESP32_VARIANT_ADC2_PIN_TO_CHANNEL + and pin_num in ESP32_VARIANT_ADC2_PIN_TO_CHANNEL[variant] + ): + chan = ESP32_VARIANT_ADC2_PIN_TO_CHANNEL[variant][pin_num] + cg.add(var.set_channel2(chan)) From 508392db6e0daa3cd227cab59b38d35dc2ea4488 Mon Sep 17 00:00:00 2001 From: Ilia Sotnikov Date: Sun, 16 Jul 2023 23:28:31 +0300 Subject: [PATCH 06/84] [Sprinkler] Resume fixes (#5100) --- esphome/components/sprinkler/sprinkler.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/esphome/components/sprinkler/sprinkler.cpp b/esphome/components/sprinkler/sprinkler.cpp index 095884997c..8afafcb5ce 100644 --- a/esphome/components/sprinkler/sprinkler.cpp +++ b/esphome/components/sprinkler/sprinkler.cpp @@ -954,10 +954,18 @@ void Sprinkler::pause() { } void Sprinkler::resume() { + if (this->standby()) { + ESP_LOGD(TAG, "resume called but standby is enabled; no action taken"); + return; + } + if (this->paused_valve_.has_value() && (this->resume_duration_.has_value())) { - ESP_LOGD(TAG, "Resuming valve %u with %u seconds remaining", this->paused_valve_.value_or(0), - this->resume_duration_.value_or(0)); - this->fsm_request_(this->paused_valve_.value(), this->resume_duration_.value()); + // Resume only if valve has not been completed yet + if (!this->valve_cycle_complete_(this->paused_valve_.value())) { + ESP_LOGD(TAG, "Resuming valve %u with %u seconds remaining", this->paused_valve_.value_or(0), + this->resume_duration_.value_or(0)); + this->fsm_request_(this->paused_valve_.value(), this->resume_duration_.value()); + } this->reset_resume(); } else { ESP_LOGD(TAG, "No valve to resume!"); From 8c6cddf1bb58d86436539b6ec0bb33b7e4c4817c Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Mon, 17 Jul 2023 09:11:43 +1200 Subject: [PATCH 07/84] Remove template switch restore_state (#5106) --- esphome/components/template/switch/__init__.py | 5 +++-- esphome/components/template/switch/template_switch.cpp | 5 ----- esphome/components/template/switch/template_switch.h | 2 -- tests/test1.yaml | 2 -- 4 files changed, 3 insertions(+), 11 deletions(-) diff --git a/esphome/components/template/switch/__init__.py b/esphome/components/template/switch/__init__.py index e002c4e3d8..a221cbaa60 100644 --- a/esphome/components/template/switch/__init__.py +++ b/esphome/components/template/switch/__init__.py @@ -43,7 +43,9 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_TURN_ON_ACTION): automation.validate_automation( single=True ), - cv.Optional(CONF_RESTORE_STATE, default=False): cv.boolean, + cv.Optional(CONF_RESTORE_STATE): cv.invalid( + "The restore_state option has been removed in 2023.7.0. Use the restore_mode option instead" + ), } ) .extend(cv.COMPONENT_SCHEMA), @@ -70,7 +72,6 @@ async def to_code(config): ) cg.add(var.set_optimistic(config[CONF_OPTIMISTIC])) cg.add(var.set_assumed_state(config[CONF_ASSUMED_STATE])) - cg.add(var.set_restore_state(config[CONF_RESTORE_STATE])) @automation.register_action( diff --git a/esphome/components/template/switch/template_switch.cpp b/esphome/components/template/switch/template_switch.cpp index 5db346b99f..b2a221669e 100644 --- a/esphome/components/template/switch/template_switch.cpp +++ b/esphome/components/template/switch/template_switch.cpp @@ -40,9 +40,6 @@ float TemplateSwitch::get_setup_priority() const { return setup_priority::HARDWA Trigger<> *TemplateSwitch::get_turn_on_trigger() const { return this->turn_on_trigger_; } Trigger<> *TemplateSwitch::get_turn_off_trigger() const { return this->turn_off_trigger_; } void TemplateSwitch::setup() { - if (!this->restore_state_) - return; - optional initial_state = this->get_initial_state_with_restore_mode(); if (initial_state.has_value()) { @@ -57,10 +54,8 @@ void TemplateSwitch::setup() { } void TemplateSwitch::dump_config() { LOG_SWITCH("", "Template Switch", this); - ESP_LOGCONFIG(TAG, " Restore State: %s", YESNO(this->restore_state_)); ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_)); } -void TemplateSwitch::set_restore_state(bool restore_state) { this->restore_state_ = restore_state; } void TemplateSwitch::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; } } // namespace template_ diff --git a/esphome/components/template/switch/template_switch.h b/esphome/components/template/switch/template_switch.h index ef9b567451..bfe9ac25d6 100644 --- a/esphome/components/template/switch/template_switch.h +++ b/esphome/components/template/switch/template_switch.h @@ -15,7 +15,6 @@ class TemplateSwitch : public switch_::Switch, public Component { void dump_config() override; void set_state_lambda(std::function()> &&f); - void set_restore_state(bool restore_state); Trigger<> *get_turn_on_trigger() const; Trigger<> *get_turn_off_trigger() const; void set_optimistic(bool optimistic); @@ -35,7 +34,6 @@ class TemplateSwitch : public switch_::Switch, public Component { Trigger<> *turn_on_trigger_; Trigger<> *turn_off_trigger_; Trigger<> *prev_trigger_{nullptr}; - bool restore_state_{false}; }; } // namespace template_ diff --git a/tests/test1.yaml b/tests/test1.yaml index d0c9801933..bf099e2844 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -2475,7 +2475,6 @@ switch: level: !lambda "return 0.5;" turn_off_action: - switch.turn_on: living_room_lights_off - restore_state: false on_turn_on: - switch.template.publish: id: livingroom_lights @@ -2511,7 +2510,6 @@ switch: } optimistic: true assumed_state: false - restore_state: true on_turn_off: - switch.template.publish: id: my_switch From ac81fae855c5c8be5b32d8a781123986a0283c5e Mon Sep 17 00:00:00 2001 From: Clyde Stubbs <2366188+clydebarrow@users.noreply.github.com> Date: Mon, 17 Jul 2023 07:17:31 +1000 Subject: [PATCH 08/84] Add timeout filter (#5104) --- esphome/components/sensor/__init__.py | 10 ++++++++++ esphome/components/sensor/filter.cpp | 11 +++++++++++ esphome/components/sensor/filter.h | 12 ++++++++++++ tests/test3.1.yaml | 1 + 4 files changed, 34 insertions(+) diff --git a/esphome/components/sensor/__init__.py b/esphome/components/sensor/__init__.py index 06b96171a7..caaffd9701 100644 --- a/esphome/components/sensor/__init__.py +++ b/esphome/components/sensor/__init__.py @@ -217,6 +217,7 @@ OffsetFilter = sensor_ns.class_("OffsetFilter", Filter) MultiplyFilter = sensor_ns.class_("MultiplyFilter", Filter) FilterOutValueFilter = sensor_ns.class_("FilterOutValueFilter", Filter) ThrottleFilter = sensor_ns.class_("ThrottleFilter", Filter) +TimeoutFilter = sensor_ns.class_("TimeoutFilter", Filter, cg.Component) DebounceFilter = sensor_ns.class_("DebounceFilter", Filter, cg.Component) HeartbeatFilter = sensor_ns.class_("HeartbeatFilter", Filter, cg.Component) DeltaFilter = sensor_ns.class_("DeltaFilter", Filter) @@ -536,6 +537,15 @@ async def heartbeat_filter_to_code(config, filter_id): return var +@FILTER_REGISTRY.register( + "timeout", TimeoutFilter, cv.positive_time_period_milliseconds +) +async def timeout_filter_to_code(config, filter_id): + var = cg.new_Pvariable(filter_id, config) + await cg.register_component(var, {}) + return var + + @FILTER_REGISTRY.register( "debounce", DebounceFilter, cv.positive_time_period_milliseconds ) diff --git a/esphome/components/sensor/filter.cpp b/esphome/components/sensor/filter.cpp index 472649ebdc..ccefa556b6 100644 --- a/esphome/components/sensor/filter.cpp +++ b/esphome/components/sensor/filter.cpp @@ -373,6 +373,17 @@ void OrFilter::initialize(Sensor *parent, Filter *next) { this->phi_.initialize(parent, nullptr); } +// TimeoutFilter +optional TimeoutFilter::new_value(float value) { + this->set_timeout("timeout", this->time_period_, [this]() { this->output(NAN); }); + this->output(value); + + return {}; +} + +TimeoutFilter::TimeoutFilter(uint32_t time_period) : time_period_(time_period) {} +float TimeoutFilter::get_setup_priority() const { return setup_priority::HARDWARE; } + // DebounceFilter optional DebounceFilter::new_value(float value) { this->set_timeout("debounce", this->time_period_, [this, value]() { this->output(value); }); diff --git a/esphome/components/sensor/filter.h b/esphome/components/sensor/filter.h index 05934a26e8..296990f34f 100644 --- a/esphome/components/sensor/filter.h +++ b/esphome/components/sensor/filter.h @@ -313,6 +313,18 @@ class ThrottleFilter : public Filter { uint32_t min_time_between_inputs_; }; +class TimeoutFilter : public Filter, public Component { + public: + explicit TimeoutFilter(uint32_t time_period); + + optional new_value(float value) override; + + float get_setup_priority() const override; + + protected: + uint32_t time_period_; +}; + class DebounceFilter : public Filter, public Component { public: explicit DebounceFilter(uint32_t time_period); diff --git a/tests/test3.1.yaml b/tests/test3.1.yaml index 5f1d3ff28f..104f4bbda8 100644 --- a/tests/test3.1.yaml +++ b/tests/test3.1.yaml @@ -86,6 +86,7 @@ sensor: - delta: 100 - throttle: 100ms - debounce: 500s + - timeout: 10min - calibrate_linear: - 0 -> 0 - 100 -> 100 From 899aa31df3394720db98cc49e5f920ae12e33284 Mon Sep 17 00:00:00 2001 From: Dave T <17680170+davet2001@users.noreply.github.com> Date: Sun, 16 Jul 2023 22:19:08 +0100 Subject: [PATCH 09/84] Mark repo as safe directory to git config (#5102) --- script/setup | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/script/setup b/script/setup index e1b5941d0e..ba3b544352 100755 --- a/script/setup +++ b/script/setup @@ -10,6 +10,11 @@ if [ ! -n "$DEVCONTAINER" ] && [ ! -n "$VIRTUAL_ENV" ] && [ ! "$ESPHOME_NO_VENV" source venv/bin/activate fi +# Avoid unsafe git error when running inside devcontainer +if [ -n "$DEVCONTAINER" ];then + git config --global --add safe.directory "$PWD" +fi + pip3 install -r requirements.txt -r requirements_optional.txt -r requirements_test.txt pip3 install setuptools wheel pip3 install --no-use-pep517 -e . From 1617eba764584a88a11f1edb5550b1f70a82bbb2 Mon Sep 17 00:00:00 2001 From: bwynants Date: Mon, 17 Jul 2023 00:42:49 +0200 Subject: [PATCH 10/84] P1 values for capacity tariff in Belgium (#5081) --- esphome/components/dsmr/__init__.py | 2 +- esphome/components/dsmr/sensor.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/esphome/components/dsmr/__init__.py b/esphome/components/dsmr/__init__.py index d3d20ca2a7..9f56dc3465 100644 --- a/esphome/components/dsmr/__init__.py +++ b/esphome/components/dsmr/__init__.py @@ -87,7 +87,7 @@ async def to_code(config): cg.add_build_flag("-DDSMR_WATER_MBUS_ID=" + str(config[CONF_WATER_MBUS_ID])) # DSMR Parser - cg.add_library("glmnet/Dsmr", "0.7") + cg.add_library("glmnet/Dsmr", "0.8") # Crypto cg.add_library("rweather/Crypto", "0.4.0") diff --git a/esphome/components/dsmr/sensor.py b/esphome/components/dsmr/sensor.py index 2e2050ecab..f2398d1908 100644 --- a/esphome/components/dsmr/sensor.py +++ b/esphome/components/dsmr/sensor.py @@ -243,6 +243,30 @@ CONFIG_SCHEMA = cv.Schema( device_class=DEVICE_CLASS_WATER, state_class=STATE_CLASS_TOTAL_INCREASING, ), + cv.Optional( + "active_energy_import_current_average_demand" + ): sensor.sensor_schema( + unit_of_measurement=UNIT_KILOWATT, + accuracy_decimals=3, + device_class=DEVICE_CLASS_POWER, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional( + "active_energy_import_maximum_demand_running_month" + ): sensor.sensor_schema( + unit_of_measurement=UNIT_KILOWATT, + accuracy_decimals=3, + device_class=DEVICE_CLASS_POWER, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional( + "active_energy_import_maximum_demand_last_13_months" + ): sensor.sensor_schema( + unit_of_measurement=UNIT_KILOWATT, + accuracy_decimals=3, + device_class=DEVICE_CLASS_POWER, + state_class=STATE_CLASS_MEASUREMENT, + ), } ).extend(cv.COMPONENT_SCHEMA) From 6738295475c5ea570a6b28efd03e7ccaf958cbeb Mon Sep 17 00:00:00 2001 From: "Kevin P. Fleming" Date: Sun, 16 Jul 2023 15:43:57 -0700 Subject: [PATCH 11/84] airthings_wave: Silence compiler warnings (#5098) --- .../components/airthings_wave_base/airthings_wave_base.cpp | 2 +- .../components/airthings_wave_plus/airthings_wave_plus.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/esphome/components/airthings_wave_base/airthings_wave_base.cpp b/esphome/components/airthings_wave_base/airthings_wave_base.cpp index eff466f413..16789ff454 100644 --- a/esphome/components/airthings_wave_base/airthings_wave_base.cpp +++ b/esphome/components/airthings_wave_base/airthings_wave_base.cpp @@ -76,7 +76,7 @@ void AirthingsWaveBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt } } -bool AirthingsWaveBase::is_valid_voc_value_(uint16_t voc) { return 0 <= voc && voc <= 16383; } +bool AirthingsWaveBase::is_valid_voc_value_(uint16_t voc) { return voc <= 16383; } void AirthingsWaveBase::update() { if (this->node_state != espbt::ClientState::ESTABLISHED) { diff --git a/esphome/components/airthings_wave_plus/airthings_wave_plus.cpp b/esphome/components/airthings_wave_plus/airthings_wave_plus.cpp index e44d5fbcaa..a32128e992 100644 --- a/esphome/components/airthings_wave_plus/airthings_wave_plus.cpp +++ b/esphome/components/airthings_wave_plus/airthings_wave_plus.cpp @@ -51,9 +51,9 @@ void AirthingsWavePlus::read_sensors(uint8_t *raw_value, uint16_t value_len) { this->response_received_(); } -bool AirthingsWavePlus::is_valid_radon_value_(uint16_t radon) { return 0 <= radon && radon <= 16383; } +bool AirthingsWavePlus::is_valid_radon_value_(uint16_t radon) { return radon <= 16383; } -bool AirthingsWavePlus::is_valid_co2_value_(uint16_t co2) { return 0 <= co2 && co2 <= 16383; } +bool AirthingsWavePlus::is_valid_co2_value_(uint16_t co2) { return co2 <= 16383; } void AirthingsWavePlus::dump_config() { // these really don't belong here, but there doesn't seem to be a From 3cfe1e3083e70f07380a6550b32c2892b550fa0c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 18 Jul 2023 12:47:06 +1200 Subject: [PATCH 12/84] Bump click from 8.1.3 to 8.1.5 (#5099) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 618fc94e0b..d3b5ddee94 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,7 @@ tzdata>=2021.1 # from time pyserial==3.5 platformio==6.1.7 # When updating platformio, also update Dockerfile esptool==4.6.2 -click==8.1.3 +click==8.1.5 esphome-dashboard==20230711.0 aioesphomeapi==15.0.0 zeroconf==0.69.0 From b0e286972d7af9bfaa95e5fe95fbae1191a055e6 Mon Sep 17 00:00:00 2001 From: PlainTechEnthusiast <135363826+PlainTechEnthusiast@users.noreply.github.com> Date: Mon, 17 Jul 2023 20:49:04 -0400 Subject: [PATCH 13/84] Sigma delta fix (#4911) --- .../sigma_delta_output/sigma_delta_output.cpp | 57 +++++++++++++++++++ .../sigma_delta_output/sigma_delta_output.h | 31 ++-------- 2 files changed, 63 insertions(+), 25 deletions(-) create mode 100644 esphome/components/sigma_delta_output/sigma_delta_output.cpp diff --git a/esphome/components/sigma_delta_output/sigma_delta_output.cpp b/esphome/components/sigma_delta_output/sigma_delta_output.cpp new file mode 100644 index 0000000000..d386f8db1a --- /dev/null +++ b/esphome/components/sigma_delta_output/sigma_delta_output.cpp @@ -0,0 +1,57 @@ +#include "sigma_delta_output.h" +#include "esphome/core/log.h" + +namespace esphome { +namespace sigma_delta_output { + +static const char *const TAG = "output.sigma_delta"; + +void SigmaDeltaOutput::setup() { + if (this->pin_) + this->pin_->setup(); +} + +void SigmaDeltaOutput::dump_config() { + ESP_LOGCONFIG(TAG, "Sigma Delta Output:"); + LOG_PIN(" Pin: ", this->pin_); + if (this->state_change_trigger_) { + ESP_LOGCONFIG(TAG, " State change automation configured"); + } + if (this->turn_on_trigger_) { + ESP_LOGCONFIG(TAG, " Turn on automation configured"); + } + if (this->turn_off_trigger_) { + ESP_LOGCONFIG(TAG, " Turn off automation configured"); + } + LOG_UPDATE_INTERVAL(this); + LOG_FLOAT_OUTPUT(this); +} + +void SigmaDeltaOutput::update() { + this->accum_ += this->state_; + const bool next_value = this->accum_ > 0; + + if (next_value) { + this->accum_ -= 1.; + } + + if (next_value != this->value_) { + this->value_ = next_value; + if (this->pin_) { + this->pin_->digital_write(next_value); + } + + if (this->state_change_trigger_) { + this->state_change_trigger_->trigger(next_value); + } + + if (next_value && this->turn_on_trigger_) { + this->turn_on_trigger_->trigger(); + } else if (!next_value && this->turn_off_trigger_) { + this->turn_off_trigger_->trigger(); + } + } +} + +} // namespace sigma_delta_output +} // namespace esphome diff --git a/esphome/components/sigma_delta_output/sigma_delta_output.h b/esphome/components/sigma_delta_output/sigma_delta_output.h index 5a5acd2dfb..8fd1e1f761 100644 --- a/esphome/components/sigma_delta_output/sigma_delta_output.h +++ b/esphome/components/sigma_delta_output/sigma_delta_output.h @@ -1,9 +1,12 @@ #pragma once +#include "esphome/core/automation.h" #include "esphome/core/component.h" +#include "esphome/core/hal.h" #include "esphome/components/output/float_output.h" namespace esphome { namespace sigma_delta_output { + class SigmaDeltaOutput : public PollingComponent, public output::FloatOutput { public: Trigger<> *get_turn_on_trigger() { @@ -25,31 +28,9 @@ class SigmaDeltaOutput : public PollingComponent, public output::FloatOutput { void set_pin(GPIOPin *pin) { this->pin_ = pin; }; void write_state(float state) override { this->state_ = state; } - void update() override { - this->accum_ += this->state_; - const bool next_value = this->accum_ > 0; - - if (next_value) { - this->accum_ -= 1.; - } - - if (next_value != this->value_) { - this->value_ = next_value; - if (this->pin_) { - this->pin_->digital_write(next_value); - } - - if (this->state_change_trigger_) { - this->state_change_trigger_->trigger(next_value); - } - - if (next_value && this->turn_on_trigger_) { - this->turn_on_trigger_->trigger(); - } else if (!next_value && this->turn_off_trigger_) { - this->turn_off_trigger_->trigger(); - } - } - } + void setup() override; + void dump_config() override; + void update() override; protected: GPIOPin *pin_{nullptr}; From 837c749cd7e6797fb175dfda0ce4b3ecf2066597 Mon Sep 17 00:00:00 2001 From: voed Date: Tue, 18 Jul 2023 03:50:32 +0300 Subject: [PATCH 14/84] [LD2410] Remove baud_rate check (#5112) --- esphome/components/ld2410/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/esphome/components/ld2410/__init__.py b/esphome/components/ld2410/__init__.py index be39cc2979..47c4cdb0bd 100644 --- a/esphome/components/ld2410/__init__.py +++ b/esphome/components/ld2410/__init__.py @@ -112,7 +112,6 @@ CONFIG_SCHEMA = cv.All( FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema( "ld2410", - baud_rate=256000, require_tx=True, require_rx=True, parity="NONE", From 417d45939f3392d98c9f8cc15a201d71c4c397fd Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 19 Jul 2023 11:38:47 +1200 Subject: [PATCH 15/84] Fix silence detection flag on voice assistant (#5120) --- esphome/components/api/api.proto | 1 + esphome/components/api/api_connection.cpp | 3 ++- esphome/components/api/api_connection.h | 2 +- esphome/components/api/api_pb2.cpp | 9 +++++++++ esphome/components/api/api_pb2.h | 1 + esphome/components/api/api_server.cpp | 6 +++--- esphome/components/api/api_server.h | 2 +- esphome/components/voice_assistant/voice_assistant.cpp | 2 +- esphome/components/voice_assistant/voice_assistant.h | 6 +----- 9 files changed, 20 insertions(+), 12 deletions(-) diff --git a/esphome/components/api/api.proto b/esphome/components/api/api.proto index 0d68d9fe55..86685aa5e6 100644 --- a/esphome/components/api/api.proto +++ b/esphome/components/api/api.proto @@ -1420,6 +1420,7 @@ message VoiceAssistantRequest { bool start = 1; string conversation_id = 2; + bool use_vad = 3; } message VoiceAssistantResponse { diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 858ff0e525..a46efd80e5 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -907,12 +907,13 @@ BluetoothConnectionsFreeResponse APIConnection::subscribe_bluetooth_connections_ #endif #ifdef USE_VOICE_ASSISTANT -bool APIConnection::request_voice_assistant(bool start, const std::string &conversation_id) { +bool APIConnection::request_voice_assistant(bool start, const std::string &conversation_id, bool use_vad) { if (!this->voice_assistant_subscription_) return false; VoiceAssistantRequest msg; msg.start = start; msg.conversation_id = conversation_id; + msg.use_vad = use_vad; return this->send_voice_assistant_request(msg); } void APIConnection::on_voice_assistant_response(const VoiceAssistantResponse &msg) { diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index c146adff02..acc4578661 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -124,7 +124,7 @@ class APIConnection : public APIServerConnection { void subscribe_voice_assistant(const SubscribeVoiceAssistantRequest &msg) override { this->voice_assistant_subscription_ = msg.subscribe; } - bool request_voice_assistant(bool start, const std::string &conversation_id); + bool request_voice_assistant(bool start, const std::string &conversation_id, bool use_vad); void on_voice_assistant_response(const VoiceAssistantResponse &msg) override; void on_voice_assistant_event_response(const VoiceAssistantEventResponse &msg) override; #endif diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index 8c7f6d0c4a..3a2d980e57 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -6348,6 +6348,10 @@ bool VoiceAssistantRequest::decode_varint(uint32_t field_id, ProtoVarInt value) this->start = value.as_bool(); return true; } + case 3: { + this->use_vad = value.as_bool(); + return true; + } default: return false; } @@ -6365,6 +6369,7 @@ bool VoiceAssistantRequest::decode_length(uint32_t field_id, ProtoLengthDelimite void VoiceAssistantRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->start); buffer.encode_string(2, this->conversation_id); + buffer.encode_bool(3, this->use_vad); } #ifdef HAS_PROTO_MESSAGE_DUMP void VoiceAssistantRequest::dump_to(std::string &out) const { @@ -6377,6 +6382,10 @@ void VoiceAssistantRequest::dump_to(std::string &out) const { out.append(" conversation_id: "); out.append("'").append(this->conversation_id).append("'"); out.append("\n"); + + out.append(" use_vad: "); + out.append(YESNO(this->use_vad)); + out.append("\n"); out.append("}"); } #endif diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index 769f7aaff5..627165953d 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -1655,6 +1655,7 @@ class VoiceAssistantRequest : public ProtoMessage { public: bool start{false}; std::string conversation_id{}; + bool use_vad{false}; void encode(ProtoWriteBuffer buffer) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; diff --git a/esphome/components/api/api_server.cpp b/esphome/components/api/api_server.cpp index 87b5f9e63f..f70d45ecd0 100644 --- a/esphome/components/api/api_server.cpp +++ b/esphome/components/api/api_server.cpp @@ -323,16 +323,16 @@ void APIServer::on_shutdown() { } #ifdef USE_VOICE_ASSISTANT -bool APIServer::start_voice_assistant(const std::string &conversation_id) { +bool APIServer::start_voice_assistant(const std::string &conversation_id, bool use_vad) { for (auto &c : this->clients_) { - if (c->request_voice_assistant(true, conversation_id)) + if (c->request_voice_assistant(true, conversation_id, use_vad)) return true; } return false; } void APIServer::stop_voice_assistant() { for (auto &c : this->clients_) { - if (c->request_voice_assistant(false, "")) + if (c->request_voice_assistant(false, "", false)) return; } } diff --git a/esphome/components/api/api_server.h b/esphome/components/api/api_server.h index be124f42ff..9b40a5ef02 100644 --- a/esphome/components/api/api_server.h +++ b/esphome/components/api/api_server.h @@ -81,7 +81,7 @@ class APIServer : public Component, public Controller { #endif #ifdef USE_VOICE_ASSISTANT - bool start_voice_assistant(const std::string &conversation_id); + bool start_voice_assistant(const std::string &conversation_id, bool use_vad); void stop_voice_assistant(); #endif diff --git a/esphome/components/voice_assistant/voice_assistant.cpp b/esphome/components/voice_assistant/voice_assistant.cpp index 44d640ff39..217ddb6354 100644 --- a/esphome/components/voice_assistant/voice_assistant.cpp +++ b/esphome/components/voice_assistant/voice_assistant.cpp @@ -130,7 +130,7 @@ void VoiceAssistant::start(struct sockaddr_storage *addr, uint16_t port) { void VoiceAssistant::request_start(bool continuous) { ESP_LOGD(TAG, "Requesting start..."); - if (!api::global_api_server->start_voice_assistant(this->conversation_id_)) { + if (!api::global_api_server->start_voice_assistant(this->conversation_id_, this->silence_detection_)) { ESP_LOGW(TAG, "Could not request start."); this->error_trigger_->trigger("not-connected", "Could not request start."); this->continuous_ = false; diff --git a/esphome/components/voice_assistant/voice_assistant.h b/esphome/components/voice_assistant/voice_assistant.h index b103584509..e67baaee65 100644 --- a/esphome/components/voice_assistant/voice_assistant.h +++ b/esphome/components/voice_assistant/voice_assistant.h @@ -25,10 +25,9 @@ namespace voice_assistant { // Version 1: Initial version // Version 2: Adds raw speaker support -// Version 3: Adds continuous support +// Version 3: Unused/skip static const uint32_t INITIAL_VERSION = 1; static const uint32_t SPEAKER_SUPPORT = 2; -static const uint32_t SILENCE_DETECTION_SUPPORT = 3; class VoiceAssistant : public Component { public: @@ -48,9 +47,6 @@ class VoiceAssistant : public Component { uint32_t get_version() const { #ifdef USE_SPEAKER if (this->speaker_ != nullptr) { - if (this->silence_detection_) { - return SILENCE_DETECTION_SUPPORT; - } return SPEAKER_SUPPORT; } #endif From b82c7ad6081b8d0a29b6aa89acf97cd14c571181 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 19 Jul 2023 14:30:12 +1200 Subject: [PATCH 16/84] Bump pyyaml from 6.0 to 6.0.1 (#5117) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index d3b5ddee94..781ed03a49 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ voluptuous==0.13.1 -PyYAML==6.0 +PyYAML==6.0.1 paho-mqtt==1.6.1 colorama==0.4.6 tornado==6.3.2 From 973e78355fa9e8cdfed3a38f6ac6b5bda0e4c3c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Wed, 19 Jul 2023 22:39:35 +0200 Subject: [PATCH 17/84] Dashboard: use Popen() on Windows (#5110) --- esphome/dashboard/dashboard.py | 60 +++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/esphome/dashboard/dashboard.py b/esphome/dashboard/dashboard.py index dd800f534c..a3a44de9ed 100644 --- a/esphome/dashboard/dashboard.py +++ b/esphome/dashboard/dashboard.py @@ -25,6 +25,7 @@ import tornado.ioloop import tornado.iostream import tornado.netutil import tornado.process +import tornado.queues import tornado.web import tornado.websocket import yaml @@ -202,7 +203,11 @@ class EsphomeCommandWebSocket(tornado.websocket.WebSocketHandler): def __init__(self, application, request, **kwargs): super().__init__(application, request, **kwargs) self._proc = None + self._queue = None self._is_closed = False + # Windows doesn't support non-blocking pipes, + # use Popen() with a reading thread instead + self._use_popen = os.name == "nt" @authenticated def on_message(self, message): @@ -224,13 +229,28 @@ class EsphomeCommandWebSocket(tornado.websocket.WebSocketHandler): return command = self.build_command(json_message) _LOGGER.info("Running command '%s'", " ".join(shlex_quote(x) for x in command)) - self._proc = tornado.process.Subprocess( - command, - stdout=tornado.process.Subprocess.STREAM, - stderr=subprocess.STDOUT, - stdin=tornado.process.Subprocess.STREAM, - ) - self._proc.set_exit_callback(self._proc_on_exit) + + if self._use_popen: + self._queue = tornado.queues.Queue() + # pylint: disable=consider-using-with + self._proc = subprocess.Popen( + command, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + ) + stdout_thread = threading.Thread(target=self._stdout_thread) + stdout_thread.daemon = True + stdout_thread.start() + else: + self._proc = tornado.process.Subprocess( + command, + stdout=tornado.process.Subprocess.STREAM, + stderr=subprocess.STDOUT, + stdin=tornado.process.Subprocess.STREAM, + ) + self._proc.set_exit_callback(self._proc_on_exit) + tornado.ioloop.IOLoop.current().spawn_callback(self._redirect_stdout) @property @@ -252,7 +272,13 @@ class EsphomeCommandWebSocket(tornado.websocket.WebSocketHandler): while True: try: - data = yield self._proc.stdout.read_until_regex(reg) + if self._use_popen: + data = yield self._queue.get() + if data is None: + self._proc_on_exit(self._proc.poll()) + break + else: + data = yield self._proc.stdout.read_until_regex(reg) except tornado.iostream.StreamClosedError: break data = codecs.decode(data, "utf8", "replace") @@ -260,6 +286,19 @@ class EsphomeCommandWebSocket(tornado.websocket.WebSocketHandler): _LOGGER.debug("> stdout: %s", data) self.write_message({"event": "line", "data": data}) + def _stdout_thread(self): + if not self._use_popen: + return + while True: + data = self._proc.stdout.readline() + if data: + data = data.replace(b"\r", b"") + self._queue.put_nowait(data) + if self._proc.poll() is not None: + break + self._proc.wait(1.0) + self._queue.put_nowait(None) + def _proc_on_exit(self, returncode): if not self._is_closed: # Check if the proc was not forcibly closed @@ -270,7 +309,10 @@ class EsphomeCommandWebSocket(tornado.websocket.WebSocketHandler): # Check if proc exists (if 'start' has been run) if self.is_process_active: _LOGGER.debug("Terminating process") - self._proc.proc.terminate() + if self._use_popen: + self._proc.terminate() + else: + self._proc.proc.terminate() # Shutdown proc on WS close self._is_closed = True From de626c0f5fe0887a3a08402140c3da65ee2a1518 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Thu, 20 Jul 2023 12:37:29 +1200 Subject: [PATCH 18/84] ignore components folder in root (#5130) --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 71b66b2499..d180b58259 100644 --- a/.gitignore +++ b/.gitignore @@ -129,4 +129,6 @@ tests/.esphome/ sdkconfig.* !sdkconfig.defaults -.tests/ \ No newline at end of file +.tests/ + +/components From d2381556401723e0cd87b6292dde34a0a4f46f29 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Thu, 20 Jul 2023 12:37:42 +1200 Subject: [PATCH 19/84] Add size getter to CallbackManager (#5129) --- esphome/core/helpers.h | 1 + 1 file changed, 1 insertion(+) diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 05a7eaa4cc..63b6949fe9 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -475,6 +475,7 @@ template class CallbackManager { for (auto &cb : this->callbacks_) cb(args...); } + size_t size() const { return this->callbacks_.size(); } /// Call all callbacks in this manager. void operator()(Ts... args) { call(args...); } From 5eb12ac5fe1d3dcfa5f06da79a18b40d2b534565 Mon Sep 17 00:00:00 2001 From: Jimmy Hedman Date: Thu, 20 Jul 2023 05:52:41 +0200 Subject: [PATCH 20/84] Make docker use pip installed pillow (#5074) --- docker/Dockerfile | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 2d9a8a9ae4..e1f3c46a3e 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -22,16 +22,22 @@ RUN \ python3=3.9.2-3 \ python3-pip=20.3.4-4+deb11u1 \ python3-setuptools=52.0.0-4 \ - python3-pil=8.1.2+dfsg-0.3+deb11u1 \ python3-cryptography=3.3.2-1 \ python3-venv=3.9.2-3 \ iputils-ping=3:20210202-1 \ git=1:2.30.2-1+deb11u2 \ curl=7.74.0-1.3+deb11u7 \ openssh-client=1:8.4p1-5+deb11u1 \ - libcairo2=1.16.0-5 \ - python3-cffi=1.14.5-1 \ - && rm -rf \ + python3-cffi=1.14.5-1; \ + if [ "$TARGETARCH$TARGETVARIANT" = "armv7" ]; then \ + apt-get install -y --no-install-recommends \ + build-essential=12.9 \ + python3-dev=3.9.2-3 \ + zlib1g-dev=1:1.2.11.dfsg-2+deb11u2 \ + libjpeg-dev=1:2.0.6-4 \ + libcairo2=1.16.0-5; \ + fi; \ + rm -rf \ /tmp/* \ /var/{cache,log}/* \ /var/lib/apt/lists/* From 76c0d0912f88c6ea1ec9cb09a781d7177360ad3c Mon Sep 17 00:00:00 2001 From: Jimmy Hedman Date: Thu, 20 Jul 2023 05:54:25 +0200 Subject: [PATCH 21/84] Change datatype in e131 addressable light (#5127) --- esphome/components/e131/e131_addressable_light_effect.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/e131/e131_addressable_light_effect.cpp b/esphome/components/e131/e131_addressable_light_effect.cpp index 42eb0fc56b..6b6a726ef3 100644 --- a/esphome/components/e131/e131_addressable_light_effect.cpp +++ b/esphome/components/e131/e131_addressable_light_effect.cpp @@ -51,7 +51,7 @@ bool E131AddressableLightEffect::process_(int universe, const E131Packet &packet if (universe < first_universe_ || universe > get_last_universe()) return false; - int output_offset = (universe - first_universe_) * get_lights_per_universe(); + int32_t output_offset = (universe - first_universe_) * get_lights_per_universe(); // limit amount of lights per universe and received int output_end = std::min(it->size(), std::min(output_offset + get_lights_per_universe(), output_offset + packet.count - 1)); From 89c5298bb936bd2436987384c40b38c2d845b206 Mon Sep 17 00:00:00 2001 From: Graham Brown Date: Thu, 20 Jul 2023 22:47:37 +0200 Subject: [PATCH 22/84] Streamer mode (#5119) --- esphome/__main__.py | 10 ++++++++-- esphome/dashboard/dashboard.py | 13 ++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/esphome/__main__.py b/esphome/__main__.py index ecf0092b05..ca5fc1c008 100644 --- a/esphome/__main__.py +++ b/esphome/__main__.py @@ -365,10 +365,16 @@ def command_wizard(args): def command_config(args, config): - _LOGGER.info("Configuration is valid!") if not CORE.verbose: config = strip_default_ids(config) - safe_print(yaml_util.dump(config, args.show_secrets)) + output = yaml_util.dump(config, args.show_secrets) + # add the console decoration so the front-end can hide the secrets + if not args.show_secrets: + output = re.sub( + r"(password|key|psk|ssid)\:\s(.*)", r"\1: \\033[5m\2\\033[6m", output + ) + safe_print(output) + _LOGGER.info("Configuration is valid!") return 0 diff --git a/esphome/dashboard/dashboard.py b/esphome/dashboard/dashboard.py index a3a44de9ed..b33cb2df5e 100644 --- a/esphome/dashboard/dashboard.py +++ b/esphome/dashboard/dashboard.py @@ -93,6 +93,10 @@ class DashboardSettings: def using_auth(self): return self.using_password or self.using_ha_addon_auth + @property + def streamer_mode(self): + return get_bool_env("ESPHOME_STREAMER_MODE") + def check_password(self, username, password): if not self.using_auth: return True @@ -131,7 +135,7 @@ def template_args(): "docs_link": docs_link, "get_static_file_url": get_static_file_url, "relative_url": settings.relative_url, - "streamer_mode": get_bool_env("ESPHOME_STREAMER_MODE"), + "streamer_mode": settings.streamer_mode, "config_dir": settings.config_dir, } @@ -396,7 +400,10 @@ class EsphomeCompileHandler(EsphomeCommandWebSocket): class EsphomeValidateHandler(EsphomeCommandWebSocket): def build_command(self, json_message): config_file = settings.rel_path(json_message["configuration"]) - return ["esphome", "--dashboard", "config", config_file] + command = ["esphome", "--dashboard", "config", config_file] + if not settings.streamer_mode: + command.append("--show-secrets") + return command class EsphomeCleanMqttHandler(EsphomeCommandWebSocket): @@ -1147,7 +1154,7 @@ class JsonConfigRequestHandler(BaseHandler): self.send_error(404) return - args = ["esphome", "config", settings.rel_path(configuration), "--show-secrets"] + args = ["esphome", "config", filename, "--show-secrets"] rc, stdout, _ = run_system_command(*args) From 1c237aef772976a16a25d1e9c65d196d09f7b7f2 Mon Sep 17 00:00:00 2001 From: Jimmy Hedman Date: Fri, 21 Jul 2023 05:35:44 +0200 Subject: [PATCH 23/84] Version bump for ESP32 IDF and Arduino (#5035) --- esphome/components/esp32/__init__.py | 10 +++++----- platformio.ini | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/esphome/components/esp32/__init__.py b/esphome/components/esp32/__init__.py index 903031c77a..f8e78794b2 100644 --- a/esphome/components/esp32/__init__.py +++ b/esphome/components/esp32/__init__.py @@ -163,23 +163,23 @@ RECOMMENDED_ARDUINO_FRAMEWORK_VERSION = cv.Version(2, 0, 5) # The platformio/espressif32 version to use for arduino frameworks # - https://github.com/platformio/platform-espressif32/releases # - https://api.registry.platformio.org/v3/packages/platformio/platform/espressif32 -ARDUINO_PLATFORM_VERSION = cv.Version(5, 3, 0) +ARDUINO_PLATFORM_VERSION = cv.Version(5, 4, 0) # The default/recommended esp-idf framework version # - https://github.com/espressif/esp-idf/releases # - https://api.registry.platformio.org/v3/packages/platformio/tool/framework-espidf -RECOMMENDED_ESP_IDF_FRAMEWORK_VERSION = cv.Version(4, 4, 4) +RECOMMENDED_ESP_IDF_FRAMEWORK_VERSION = cv.Version(4, 4, 5) # The platformio/espressif32 version to use for esp-idf frameworks # - https://github.com/platformio/platform-espressif32/releases # - https://api.registry.platformio.org/v3/packages/platformio/platform/espressif32 -ESP_IDF_PLATFORM_VERSION = cv.Version(5, 3, 0) +ESP_IDF_PLATFORM_VERSION = cv.Version(5, 4, 0) def _arduino_check_versions(value): value = value.copy() lookups = { "dev": (cv.Version(2, 1, 0), "https://github.com/espressif/arduino-esp32.git"), - "latest": (cv.Version(2, 0, 7), None), + "latest": (cv.Version(2, 0, 9), None), "recommended": (RECOMMENDED_ARDUINO_FRAMEWORK_VERSION, None), } @@ -214,7 +214,7 @@ def _esp_idf_check_versions(value): value = value.copy() lookups = { "dev": (cv.Version(5, 1, 0), "https://github.com/espressif/esp-idf.git"), - "latest": (cv.Version(5, 0, 1), None), + "latest": (cv.Version(5, 1, 0), None), "recommended": (RECOMMENDED_ESP_IDF_FRAMEWORK_VERSION, None), } diff --git a/platformio.ini b/platformio.ini index b970ef7a69..8141c803f1 100644 --- a/platformio.ini +++ b/platformio.ini @@ -104,7 +104,7 @@ extra_scripts = post:esphome/components/esp8266/post_build.py.script ; This are common settings for the ESP32 (all variants) using Arduino. [common:esp32-arduino] extends = common:arduino -platform = platformio/espressif32@5.3.0 +platform = platformio/espressif32@5.4.0 platform_packages = platformio/framework-arduinoespressif32@~3.20005.0 @@ -133,9 +133,9 @@ extra_scripts = post:esphome/components/esp32/post_build.py.script ; This are common settings for the ESP32 (all variants) using IDF. [common:esp32-idf] extends = common:idf -platform = platformio/espressif32@5.3.0 +platform = platformio/espressif32@5.4.0 platform_packages = - platformio/framework-espidf@~3.40404.0 + platformio/framework-espidf@~3.40405.0 framework = espidf lib_deps = From c91b775b734c4b23ff83c52733273f89472eeff2 Mon Sep 17 00:00:00 2001 From: esphomebot Date: Fri, 21 Jul 2023 22:17:48 +1200 Subject: [PATCH 24/84] Synchronise Device Classes from Home Assistant (#5136) --- esphome/components/number/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/esphome/components/number/__init__.py b/esphome/components/number/__init__.py index 73fbfd6e90..f45b8c024a 100644 --- a/esphome/components/number/__init__.py +++ b/esphome/components/number/__init__.py @@ -28,6 +28,7 @@ from esphome.const import ( DEVICE_CLASS_DATA_RATE, DEVICE_CLASS_DATA_SIZE, DEVICE_CLASS_DISTANCE, + DEVICE_CLASS_DURATION, DEVICE_CLASS_EMPTY, DEVICE_CLASS_ENERGY, DEVICE_CLASS_ENERGY_STORAGE, @@ -81,6 +82,7 @@ DEVICE_CLASSES = [ DEVICE_CLASS_DATA_RATE, DEVICE_CLASS_DATA_SIZE, DEVICE_CLASS_DISTANCE, + DEVICE_CLASS_DURATION, DEVICE_CLASS_EMPTY, DEVICE_CLASS_ENERGY, DEVICE_CLASS_ENERGY_STORAGE, From efd0dd4c3db8d98c47d251d93c8986b8ddf7beb5 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Sat, 22 Jul 2023 20:24:40 +1200 Subject: [PATCH 25/84] Update known boards to 5.4.0 (#5134) --- esphome/components/esp32/boards.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/esphome/components/esp32/boards.py b/esphome/components/esp32/boards.py index 30297654bc..61cb8cdc3f 100644 --- a/esphome/components/esp32/boards.py +++ b/esphome/components/esp32/boards.py @@ -1201,6 +1201,10 @@ BOARDS = { "name": "BPI-Bit", "variant": VARIANT_ESP32, }, + "bpi_leaf_s3": { + "name": "BPI-Leaf-S3", + "variant": VARIANT_ESP32S3, + }, "briki_abc_esp32": { "name": "Briki ABC (MBC-WB) - ESP32", "variant": VARIANT_ESP32, @@ -1217,6 +1221,10 @@ BOARDS = { "name": "Connaxio's Espoir", "variant": VARIANT_ESP32, }, + "cytron_maker_feather_aiot_s3": { + "name": "Cytron Maker Feather AIoT S3", + "variant": VARIANT_ESP32S3, + }, "d-duino-32": { "name": "D-duino-32", "variant": VARIANT_ESP32, @@ -1225,6 +1233,10 @@ BOARDS = { "name": "Deneyap Kart 1A", "variant": VARIANT_ESP32, }, + "deneyapkart1Av2": { + "name": "Deneyap Kart 1A v2", + "variant": VARIANT_ESP32S3, + }, "deneyapkartg": { "name": "Deneyap Kart G", "variant": VARIANT_ESP32C3, @@ -1237,6 +1249,10 @@ BOARDS = { "name": "Deneyap Mini", "variant": VARIANT_ESP32S2, }, + "deneyapminiv2": { + "name": "Deneyap Mini v2", + "variant": VARIANT_ESP32S2, + }, "denky32": { "name": "Denky32 (WROOM32)", "variant": VARIANT_ESP32, @@ -1265,6 +1281,10 @@ BOARDS = { "name": "Espressif ESP32-C3-DevKitM-1", "variant": VARIANT_ESP32C3, }, + "esp32-c3-m1i-kit": { + "name": "Ai-Thinker ESP-C3-M1-I-Kit", + "variant": VARIANT_ESP32C3, + }, "esp32cam": { "name": "AI Thinker ESP32-CAM", "variant": VARIANT_ESP32, @@ -1329,6 +1349,10 @@ BOARDS = { "name": "Espressif ESP32-S3-DevKitC-1-N8 (8 MB QD, No PSRAM)", "variant": VARIANT_ESP32S3, }, + "esp32-s3-korvo-2": { + "name": "Espressif ESP32-S3-Korvo-2", + "variant": VARIANT_ESP32S3, + }, "esp32thing": { "name": "SparkFun ESP32 Thing", "variant": VARIANT_ESP32, @@ -1637,6 +1661,10 @@ BOARDS = { "name": "Noduino Quantum", "variant": VARIANT_ESP32, }, + "redpill_esp32s3": { + "name": "Munich Labs RedPill ESP32-S3", + "variant": VARIANT_ESP32S3, + }, "seeed_xiao_esp32c3": { "name": "Seeed Studio XIAO ESP32C3", "variant": VARIANT_ESP32C3, From 80154b280e341cc6a28e4f25ca11b36e87433833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Sat, 22 Jul 2023 10:25:01 +0200 Subject: [PATCH 26/84] Init colorama in ESPHome main (#5111) --- esphome/log.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/esphome/log.py b/esphome/log.py index e7ba0fdd82..b5d72e774c 100644 --- a/esphome/log.py +++ b/esphome/log.py @@ -71,6 +71,8 @@ def setup_log( ) -> None: import colorama + colorama.init() + if debug: log_level = logging.DEBUG CORE.verbose = True @@ -82,7 +84,6 @@ def setup_log( logging.getLogger("urllib3").setLevel(logging.WARNING) - colorama.init() logging.getLogger().handlers[0].setFormatter( ESPHomeLogFormatter(include_timestamp=include_timestamp) ) From 827b2def1e68a765a1a72ee4b64a4716d6b5e066 Mon Sep 17 00:00:00 2001 From: Sergey Dudanov Date: Sun, 23 Jul 2023 00:15:37 +0400 Subject: [PATCH 27/84] Coolix IR protocol improvements (#5105) * coolix protocol * tests * 24-bit range * some DRY in coolix * added short condition * one more change * final prettify * v2023.8 --- esphome/components/coolix/coolix.cpp | 2 +- esphome/components/remote_base/__init__.py | 53 +++++++++---- .../remote_base/coolix_protocol.cpp | 76 +++++++++++++------ .../components/remote_base/coolix_protocol.h | 17 ++++- tests/test1.yaml | 27 ++++++- 5 files changed, 130 insertions(+), 45 deletions(-) diff --git a/esphome/components/coolix/coolix.cpp b/esphome/components/coolix/coolix.cpp index 738fd8d00d..6233014a96 100644 --- a/esphome/components/coolix/coolix.cpp +++ b/esphome/components/coolix/coolix.cpp @@ -114,7 +114,7 @@ bool CoolixClimate::on_coolix(climate::Climate *parent, remote_base::RemoteRecei if (!decoded.has_value()) return false; // Decoded remote state y 3 bytes long code. - uint32_t remote_state = *decoded; + uint32_t remote_state = (*decoded).second; ESP_LOGV(TAG, "Decoded 0x%06X", remote_state); if ((remote_state & 0xFF0000) != 0xB20000) return false; diff --git a/esphome/components/remote_base/__init__.py b/esphome/components/remote_base/__init__.py index 2ef33f3711..0666b96d1e 100644 --- a/esphome/components/remote_base/__init__.py +++ b/esphome/components/remote_base/__init__.py @@ -17,6 +17,7 @@ from esphome.const import ( CONF_PROTOCOL, CONF_GROUP, CONF_DEVICE, + CONF_SECOND, CONF_STATE, CONF_CHANNEL, CONF_FAMILY, @@ -39,6 +40,7 @@ AUTO_LOAD = ["binary_sensor"] CONF_RECEIVER_ID = "receiver_id" CONF_TRANSMITTER_ID = "transmitter_id" +CONF_FIRST = "first" ns = remote_base_ns = cg.esphome_ns.namespace("remote_base") RemoteProtocol = ns.class_("RemoteProtocol") @@ -349,19 +351,48 @@ async def canalsatld_action(var, config, args): CoolixAction, CoolixDumper, ) = declare_protocol("Coolix") -COOLIX_SCHEMA = cv.Schema({cv.Required(CONF_DATA): cv.hex_uint32_t}) -@register_binary_sensor("coolix", CoolixBinarySensor, COOLIX_SCHEMA) +COOLIX_BASE_SCHEMA = cv.Schema( + { + cv.Required(CONF_FIRST): cv.hex_int_range(0, 16777215), + cv.Optional(CONF_SECOND, default=0): cv.hex_int_range(0, 16777215), + cv.Optional(CONF_DATA): cv.invalid( + "'data' option has been removed in ESPHome 2023.8. " + "Use the 'first' and 'second' options instead." + ), + } +) + +COOLIX_SENSOR_SCHEMA = cv.Any(cv.hex_int_range(0, 16777215), COOLIX_BASE_SCHEMA) + + +@register_binary_sensor("coolix", CoolixBinarySensor, COOLIX_SENSOR_SCHEMA) def coolix_binary_sensor(var, config): - cg.add( - var.set_data( - cg.StructInitializer( - CoolixData, - ("data", config[CONF_DATA]), + if isinstance(config, dict): + cg.add( + var.set_data( + cg.StructInitializer( + CoolixData, + ("first", config[CONF_FIRST]), + ("second", config[CONF_SECOND]), + ) ) ) - ) + else: + cg.add( + var.set_data( + cg.StructInitializer(CoolixData, ("first", 0), ("second", config)) + ) + ) + + +@register_action("coolix", CoolixAction, COOLIX_BASE_SCHEMA) +async def coolix_action(var, config, args): + template_ = await cg.templatable(config[CONF_FIRST], args, cg.uint32) + cg.add(var.set_first(template_)) + template_ = await cg.templatable(config[CONF_SECOND], args, cg.uint32) + cg.add(var.set_second(template_)) @register_trigger("coolix", CoolixTrigger, CoolixData) @@ -374,12 +405,6 @@ def coolix_dumper(var, config): pass -@register_action("coolix", CoolixAction, COOLIX_SCHEMA) -async def coolix_action(var, config, args): - template_ = await cg.templatable(config[CONF_DATA], args, cg.uint32) - cg.add(var.set_data(template_)) - - # Dish DishData, DishBinarySensor, DishTrigger, DishAction, DishDumper = declare_protocol( "Dish" diff --git a/esphome/components/remote_base/coolix_protocol.cpp b/esphome/components/remote_base/coolix_protocol.cpp index 252b6f0e91..3c9dadcd1c 100644 --- a/esphome/components/remote_base/coolix_protocol.cpp +++ b/esphome/components/remote_base/coolix_protocol.cpp @@ -15,11 +15,21 @@ static const int32_t BIT_ZERO_SPACE_US = 1 * TICK_US; static const int32_t FOOTER_MARK_US = 1 * TICK_US; static const int32_t FOOTER_SPACE_US = 10 * TICK_US; -static void encode_data(RemoteTransmitData *dst, const CoolixData &src) { - // Break data into bytes, starting at the Most Significant - // Byte. Each byte then being sent normal, then followed inverted. +bool CoolixData::operator==(const CoolixData &other) const { + if (this->first == 0) + return this->second == other.first || this->second == other.second; + if (other.first == 0) + return other.second == this->first || other.second == this->second; + return this->first == other.first && this->second == other.second; +} + +static void encode_frame(RemoteTransmitData *dst, const uint32_t &src) { + // Append header + dst->item(HEADER_MARK_US, HEADER_SPACE_US); + // Break data into bytes, starting at the Most Significant + // Byte. Each byte then being sent normal, then followed inverted. for (unsigned shift = 16;; shift -= 8) { - // Grab a bytes worth of data. + // Grab a bytes worth of data const uint8_t byte = src >> shift; // Normal for (uint8_t mask = 1 << 7; mask; mask >>= 1) @@ -27,27 +37,33 @@ static void encode_data(RemoteTransmitData *dst, const CoolixData &src) { // Inverted for (uint8_t mask = 1 << 7; mask; mask >>= 1) dst->item(BIT_MARK_US, (byte & mask) ? BIT_ZERO_SPACE_US : BIT_ONE_SPACE_US); - // Data end - if (shift == 0) + // End of frame + if (shift == 0) { + // Append footer + dst->mark(FOOTER_MARK_US); break; + } } } void CoolixProtocol::encode(RemoteTransmitData *dst, const CoolixData &data) { dst->set_carrier_frequency(38000); - dst->reserve(2 + 2 * 48 + 2 + 2 + 2 * 48 + 1); - dst->item(HEADER_MARK_US, HEADER_SPACE_US); - encode_data(dst, data); - dst->item(FOOTER_MARK_US, FOOTER_SPACE_US); - dst->item(HEADER_MARK_US, HEADER_SPACE_US); - encode_data(dst, data); - dst->mark(FOOTER_MARK_US); + dst->reserve(100 + 100 * data.has_second()); + encode_frame(dst, data.first); + if (data.has_second()) { + dst->space(FOOTER_SPACE_US); + encode_frame(dst, data.second); + } } -static bool decode_data(RemoteReceiveData &src, CoolixData &dst) { +static bool decode_frame(RemoteReceiveData &src, uint32_t &dst) { + // Checking for header + if (!src.expect_item(HEADER_MARK_US, HEADER_SPACE_US)) + return false; + // Reading data uint32_t data = 0; for (unsigned n = 3;; data <<= 8) { - // Read byte + // Reading byte for (uint32_t mask = 1 << 7; mask; mask >>= 1) { if (!src.expect_mark(BIT_MARK_US)) return false; @@ -57,13 +73,16 @@ static bool decode_data(RemoteReceiveData &src, CoolixData &dst) { return false; } } - // Check for inverse byte + // Checking for inverted byte for (uint32_t mask = 1 << 7; mask; mask >>= 1) { if (!src.expect_item(BIT_MARK_US, (data & mask) ? BIT_ZERO_SPACE_US : BIT_ONE_SPACE_US)) return false; } - // Checking the end of reading + // End of frame if (--n == 0) { + // Checking for footer + if (!src.expect_mark(FOOTER_MARK_US)) + return false; dst = data; return true; } @@ -71,15 +90,24 @@ static bool decode_data(RemoteReceiveData &src, CoolixData &dst) { } optional CoolixProtocol::decode(RemoteReceiveData data) { - CoolixData first, second; - if (data.expect_item(HEADER_MARK_US, HEADER_SPACE_US) && decode_data(data, first) && - data.expect_item(FOOTER_MARK_US, FOOTER_SPACE_US) && data.expect_item(HEADER_MARK_US, HEADER_SPACE_US) && - decode_data(data, second) && data.expect_mark(FOOTER_MARK_US) && first == second) - return first; - return {}; + CoolixData result; + const auto size = data.size(); + if ((size != 200 && size != 100) || !decode_frame(data, result.first)) + return {}; + if (size == 100 || !data.expect_space(FOOTER_SPACE_US) || !decode_frame(data, result.second)) + result.second = 0; + return result; } -void CoolixProtocol::dump(const CoolixData &data) { ESP_LOGD(TAG, "Received Coolix: 0x%06X", data); } +void CoolixProtocol::dump(const CoolixData &data) { + if (data.is_strict()) { + ESP_LOGD(TAG, "Received Coolix: 0x%06X", data.first); + } else if (data.has_second()) { + ESP_LOGD(TAG, "Received unstrict Coolix: [0x%06X, 0x%06X]", data.first, data.second); + } else { + ESP_LOGD(TAG, "Received unstrict Coolix: [0x%06X]", data.first); + } +} } // namespace remote_base } // namespace esphome diff --git a/esphome/components/remote_base/coolix_protocol.h b/esphome/components/remote_base/coolix_protocol.h index 9ce3eabb0e..50ac839200 100644 --- a/esphome/components/remote_base/coolix_protocol.h +++ b/esphome/components/remote_base/coolix_protocol.h @@ -7,7 +7,16 @@ namespace esphome { namespace remote_base { -using CoolixData = uint32_t; +struct CoolixData { + CoolixData() {} + CoolixData(uint32_t a) : first(a), second(a) {} + CoolixData(uint32_t a, uint32_t b) : first(a), second(b) {} + bool operator==(const CoolixData &other) const; + bool is_strict() const { return this->first == this->second; } + bool has_second() const { return this->second != 0; } + uint32_t first; + uint32_t second; +}; class CoolixProtocol : public RemoteProtocol { public: @@ -19,10 +28,10 @@ class CoolixProtocol : public RemoteProtocol { DECLARE_REMOTE_PROTOCOL(Coolix) template class CoolixAction : public RemoteTransmitterActionBase { - TEMPLATABLE_VALUE(CoolixData, data) + TEMPLATABLE_VALUE(uint32_t, first) + TEMPLATABLE_VALUE(uint32_t, second) void encode(RemoteTransmitData *dst, Ts... x) override { - CoolixData data = this->data_.value(x...); - CoolixProtocol().encode(dst, data); + CoolixProtocol().encode(dst, {this->first_.value(x...), this->second_.value(x...)}); } }; diff --git a/tests/test1.yaml b/tests/test1.yaml index bf099e2844..67e675ef3a 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -1594,6 +1594,18 @@ binary_sensor: -2267, 1709, ] + - platform: remote_receiver + name: Coolix Test 1 + coolix: 0xB21F98 + - platform: remote_receiver + name: Coolix Test 2 + coolix: + first: 0xB2E003 + - platform: remote_receiver + name: Coolix Test 3 + coolix: + first: 0xB2E003 + second: 0xB21F98 - platform: as3935 name: Storm Alert - platform: analog_threshold @@ -2265,8 +2277,16 @@ switch: - platform: template name: MIDEA_RAW turn_on_action: - remote_transmitter.transmit_midea: - code: [0xA2, 0x08, 0xFF, 0xFF, 0xFF] + - remote_transmitter.transmit_coolix: + first: 0xB21F98 + - remote_transmitter.transmit_coolix: + first: 0xB21F98 + second: 0xB21F98 + - remote_transmitter.transmit_coolix: + first: !lambda "return 0xB21F98;" + second: !lambda "return 0xB21F98;" + - remote_transmitter.transmit_midea: + code: [0xA2, 0x08, 0xFF, 0xFF, 0xFF] - platform: gpio name: "MCP23S08 Pin #0" pin: @@ -2846,6 +2866,9 @@ tm1651: remote_receiver: pin: GPIO32 dump: all + on_coolix: + then: + delay: !lambda "return x.first + x.second;" status_led: pin: GPIO2 From b0966532bfdeedb65dbbd66ecc0eee8d08606e5b Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Sun, 23 Jul 2023 20:22:46 +1200 Subject: [PATCH 28/84] Allow esp32 idf components to specify submodules and specific components (#5128) --- esphome/components/esp32/__init__.py | 53 ++++++++++++++++++++++------ esphome/components/esp32/const.py | 1 + esphome/components/mdns/__init__.py | 8 ++--- esphome/git.py | 18 ++++++++++ 4 files changed, 66 insertions(+), 14 deletions(-) diff --git a/esphome/components/esp32/__init__.py b/esphome/components/esp32/__init__.py index f8e78794b2..7daaaf7433 100644 --- a/esphome/components/esp32/__init__.py +++ b/esphome/components/esp32/__init__.py @@ -1,5 +1,5 @@ from dataclasses import dataclass -from typing import Union +from typing import Union, Optional from pathlib import Path import logging import os @@ -42,6 +42,7 @@ from .const import ( # noqa KEY_REFRESH, KEY_REPO, KEY_SDKCONFIG_OPTIONS, + KEY_SUBMODULES, KEY_VARIANT, VARIANT_ESP32C3, VARIANT_FRIENDLY, @@ -120,17 +121,28 @@ def add_idf_sdkconfig_option(name: str, value: SdkconfigValueType): def add_idf_component( - name: str, repo: str, ref: str = None, path: str = None, refresh: TimePeriod = None + *, + name: str, + repo: str, + ref: str = None, + path: str = None, + refresh: TimePeriod = None, + components: Optional[list[str]] = None, + submodules: Optional[list[str]] = None, ): """Add an esp-idf component to the project.""" if not CORE.using_esp_idf: raise ValueError("Not an esp-idf project") + if components is None: + components = [] if name not in CORE.data[KEY_ESP32][KEY_COMPONENTS]: CORE.data[KEY_ESP32][KEY_COMPONENTS][name] = { KEY_REPO: repo, KEY_REF: ref, KEY_PATH: path, KEY_REFRESH: refresh, + KEY_COMPONENTS: components, + KEY_SUBMODULES: submodules, } @@ -536,20 +548,41 @@ def copy_files(): ref=component[KEY_REF], refresh=component[KEY_REFRESH], domain="idf_components", + submodules=component[KEY_SUBMODULES], ) mkdir_p(CORE.relative_build_path("components")) component_dir = repo_dir if component[KEY_PATH] is not None: component_dir = component_dir / component[KEY_PATH] - shutil.copytree( - component_dir, - CORE.relative_build_path(f"components/{name}"), - dirs_exist_ok=True, - ignore=shutil.ignore_patterns(".git", ".github"), - symlinks=True, - ignore_dangling_symlinks=True, - ) + if component[KEY_COMPONENTS] == ["*"]: + shutil.copytree( + component_dir, + CORE.relative_build_path("components"), + dirs_exist_ok=True, + ignore=shutil.ignore_patterns(".git*"), + symlinks=True, + ignore_dangling_symlinks=True, + ) + elif len(component[KEY_COMPONENTS]) > 0: + for comp in component[KEY_COMPONENTS]: + shutil.copytree( + component_dir / comp, + CORE.relative_build_path(f"components/{comp}"), + dirs_exist_ok=True, + ignore=shutil.ignore_patterns(".git*"), + symlinks=True, + ignore_dangling_symlinks=True, + ) + else: + shutil.copytree( + component_dir, + CORE.relative_build_path(f"components/{name}"), + dirs_exist_ok=True, + ignore=shutil.ignore_patterns(".git*"), + symlinks=True, + ignore_dangling_symlinks=True, + ) dir = os.path.dirname(__file__) post_build_file = os.path.join(dir, "post_build.py.script") diff --git a/esphome/components/esp32/const.py b/esphome/components/esp32/const.py index d13df01d3a..698310dacb 100644 --- a/esphome/components/esp32/const.py +++ b/esphome/components/esp32/const.py @@ -9,6 +9,7 @@ KEY_REPO = "repo" KEY_REF = "ref" KEY_REFRESH = "refresh" KEY_PATH = "path" +KEY_SUBMODULES = "submodules" VARIANT_ESP32 = "ESP32" VARIANT_ESP32S2 = "ESP32S2" diff --git a/esphome/components/mdns/__init__.py b/esphome/components/mdns/__init__.py index d9b36c7b09..e7d700d149 100644 --- a/esphome/components/mdns/__init__.py +++ b/esphome/components/mdns/__init__.py @@ -86,10 +86,10 @@ async def to_code(config): 5, 0, 0 ): add_idf_component( - "mdns", - "https://github.com/espressif/esp-protocols.git", - "mdns-v1.0.9", - "components/mdns", + name="mdns", + repo="https://github.com/espressif/esp-protocols.git", + ref="mdns-v1.0.9", + path="components/mdns", ) if config[CONF_DISABLED]: diff --git a/esphome/git.py b/esphome/git.py index a607325b73..dcc3e4d0c8 100644 --- a/esphome/git.py +++ b/esphome/git.py @@ -15,6 +15,7 @@ _LOGGER = logging.getLogger(__name__) def run_git_command(cmd, cwd=None) -> str: + _LOGGER.debug("Running git command: %s", " ".join(cmd)) try: ret = subprocess.run(cmd, cwd=cwd, capture_output=True, check=False) except FileNotFoundError as err: @@ -48,6 +49,7 @@ def clone_or_update( domain: str, username: str = None, password: str = None, + submodules: Optional[list[str]] = None, ) -> tuple[Path, Optional[Callable[[], None]]]: key = f"{url}@{ref}" @@ -74,6 +76,14 @@ def clone_or_update( run_git_command(["git", "fetch", "--", "origin", ref], str(repo_dir)) run_git_command(["git", "reset", "--hard", "FETCH_HEAD"], str(repo_dir)) + if submodules is not None: + _LOGGER.info( + "Initialising submodules (%s) for %s", ", ".join(submodules), key + ) + run_git_command( + ["git", "submodule", "update", "--init"] + submodules, str(repo_dir) + ) + else: # Check refresh needed file_timestamp = Path(repo_dir / ".git" / "FETCH_HEAD") @@ -97,6 +107,14 @@ def clone_or_update( # Hard reset to FETCH_HEAD (short-lived git ref corresponding to most recent fetch) run_git_command(["git", "reset", "--hard", "FETCH_HEAD"], str(repo_dir)) + if submodules is not None: + _LOGGER.info( + "Updating submodules (%s) for %s", ", ".join(submodules), key + ) + run_git_command( + ["git", "submodule", "update", "--init"] + submodules, str(repo_dir) + ) + def revert(): _LOGGER.info("Reverting changes to %s -> %s", key, old_sha) run_git_command(["git", "reset", "--hard", old_sha], str(repo_dir)) From 959d1944fddf0d8396fc217b4e3b11c39d2c479e Mon Sep 17 00:00:00 2001 From: esphomebot Date: Mon, 24 Jul 2023 19:17:18 +1200 Subject: [PATCH 29/84] Synchronise Device Classes from Home Assistant (#5147) --- esphome/components/number/__init__.py | 2 ++ esphome/components/sensor/__init__.py | 2 ++ esphome/const.py | 1 + 3 files changed, 5 insertions(+) diff --git a/esphome/components/number/__init__.py b/esphome/components/number/__init__.py index f45b8c024a..e6ad545d70 100644 --- a/esphome/components/number/__init__.py +++ b/esphome/components/number/__init__.py @@ -43,6 +43,7 @@ from esphome.const import ( DEVICE_CLASS_NITROGEN_MONOXIDE, DEVICE_CLASS_NITROUS_OXIDE, DEVICE_CLASS_OZONE, + DEVICE_CLASS_PH, DEVICE_CLASS_PM1, DEVICE_CLASS_PM10, DEVICE_CLASS_PM25, @@ -97,6 +98,7 @@ DEVICE_CLASSES = [ DEVICE_CLASS_NITROGEN_MONOXIDE, DEVICE_CLASS_NITROUS_OXIDE, DEVICE_CLASS_OZONE, + DEVICE_CLASS_PH, DEVICE_CLASS_PM1, DEVICE_CLASS_PM10, DEVICE_CLASS_PM25, diff --git a/esphome/components/sensor/__init__.py b/esphome/components/sensor/__init__.py index caaffd9701..2aebf7bb17 100644 --- a/esphome/components/sensor/__init__.py +++ b/esphome/components/sensor/__init__.py @@ -57,6 +57,7 @@ from esphome.const import ( DEVICE_CLASS_NITROGEN_MONOXIDE, DEVICE_CLASS_NITROUS_OXIDE, DEVICE_CLASS_OZONE, + DEVICE_CLASS_PH, DEVICE_CLASS_PM1, DEVICE_CLASS_PM10, DEVICE_CLASS_PM25, @@ -114,6 +115,7 @@ DEVICE_CLASSES = [ DEVICE_CLASS_NITROGEN_MONOXIDE, DEVICE_CLASS_NITROUS_OXIDE, DEVICE_CLASS_OZONE, + DEVICE_CLASS_PH, DEVICE_CLASS_PM1, DEVICE_CLASS_PM10, DEVICE_CLASS_PM25, diff --git a/esphome/const.py b/esphome/const.py index 1c4ccdf3a4..4977726361 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -988,6 +988,7 @@ DEVICE_CLASS_OCCUPANCY = "occupancy" DEVICE_CLASS_OPENING = "opening" DEVICE_CLASS_OUTLET = "outlet" DEVICE_CLASS_OZONE = "ozone" +DEVICE_CLASS_PH = "ph" DEVICE_CLASS_PLUG = "plug" DEVICE_CLASS_PM1 = "pm1" DEVICE_CLASS_PM10 = "pm10" From 3eff7e76aa42d440c2f89526bfdf47773dfbe0bb Mon Sep 17 00:00:00 2001 From: Jimmy Hedman Date: Thu, 27 Jul 2023 07:18:02 +0200 Subject: [PATCH 30/84] Prepare some components for idf >= 5 (#5061) --- .../components/airthings_ble/airthings_listener.cpp | 3 ++- esphome/components/bedjet/bedjet_hub.cpp | 7 ++++--- esphome/components/esp32_ble/ble_uuid.cpp | 3 ++- .../esp32_ble_tracker/esp32_ble_tracker.cpp | 3 ++- esphome/components/ina226/ina226.cpp | 3 ++- esphome/components/max31865/max31865.cpp | 6 ++++-- esphome/components/sen5x/sen5x.cpp | 13 +++++++------ esphome/components/uart/uart.cpp | 5 +++-- esphome/components/uart/uart_component_esp_idf.cpp | 7 ++++--- .../waveshare_epaper/waveshare_epaper.cpp | 3 ++- 10 files changed, 32 insertions(+), 21 deletions(-) diff --git a/esphome/components/airthings_ble/airthings_listener.cpp b/esphome/components/airthings_ble/airthings_listener.cpp index 951961cb1b..a36d614df5 100644 --- a/esphome/components/airthings_ble/airthings_listener.cpp +++ b/esphome/components/airthings_ble/airthings_listener.cpp @@ -1,5 +1,6 @@ #include "airthings_listener.h" #include "esphome/core/log.h" +#include #ifdef USE_ESP32 @@ -19,7 +20,7 @@ bool AirthingsListener::parse_device(const esp32_ble_tracker::ESPBTDevice &devic sn |= ((uint32_t) it.data[2] << 16); sn |= ((uint32_t) it.data[3] << 24); - ESP_LOGD(TAG, "Found AirThings device Serial:%u (MAC: %s)", sn, device.address_str().c_str()); + ESP_LOGD(TAG, "Found AirThings device Serial:%" PRIu32 " (MAC: %s)", sn, device.address_str().c_str()); return true; } } diff --git a/esphome/components/bedjet/bedjet_hub.cpp b/esphome/components/bedjet/bedjet_hub.cpp index c355953d94..7933a35a97 100644 --- a/esphome/components/bedjet/bedjet_hub.cpp +++ b/esphome/components/bedjet/bedjet_hub.cpp @@ -3,6 +3,7 @@ #include "bedjet_hub.h" #include "bedjet_child.h" #include "bedjet_const.h" +#include namespace esphome { namespace bedjet { @@ -373,7 +374,7 @@ void BedJetHub::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t ga if (this->last_notify_ == 0 || delta > MIN_NOTIFY_THROTTLE || this->force_refresh_) { // Set reentrant flag to prevent processing multiple packets. this->processing_ = true; - ESP_LOGVV(TAG, "[%s] Decoding packet: last=%d, delta=%d, force=%s", this->get_name().c_str(), + ESP_LOGVV(TAG, "[%s] Decoding packet: last=%" PRId32 ", delta=%" PRId32 ", force=%s", this->get_name().c_str(), this->last_notify_, delta, this->force_refresh_ ? "y" : "n"); bool needs_extra = this->codec_->decode_notify(param->notify.value, param->notify.value_len); @@ -523,11 +524,11 @@ void BedJetHub::dispatch_status_() { ESP_LOGI(TAG, "[%s] Still waiting for first GATT notify event.", this->get_name().c_str()); } else if (diff > NOTIFY_WARN_THRESHOLD) { - ESP_LOGW(TAG, "[%s] Last GATT notify was %d seconds ago.", this->get_name().c_str(), diff / 1000); + ESP_LOGW(TAG, "[%s] Last GATT notify was %" PRId32 " seconds ago.", this->get_name().c_str(), diff / 1000); } if (this->timeout_ > 0 && diff > this->timeout_ && this->parent()->enabled) { - ESP_LOGW(TAG, "[%s] Timed out after %d sec. Retrying...", this->get_name().c_str(), this->timeout_); + ESP_LOGW(TAG, "[%s] Timed out after %" PRId32 " sec. Retrying...", this->get_name().c_str(), this->timeout_); // set_enabled(false) will only close the connection if state != IDLE. this->parent()->set_state(espbt::ClientState::CONNECTING); this->parent()->set_enabled(false); diff --git a/esphome/components/esp32_ble/ble_uuid.cpp b/esphome/components/esp32_ble/ble_uuid.cpp index a50d3dbd42..57c2f9df94 100644 --- a/esphome/components/esp32_ble/ble_uuid.cpp +++ b/esphome/components/esp32_ble/ble_uuid.cpp @@ -4,6 +4,7 @@ #include #include +#include #include "esphome/core/log.h" namespace esphome { @@ -166,7 +167,7 @@ std::string ESPBTUUID::to_string() const { case ESP_UUID_LEN_16: return str_snprintf("0x%02X%02X", 6, this->uuid_.uuid.uuid16 >> 8, this->uuid_.uuid.uuid16 & 0xff); case ESP_UUID_LEN_32: - return str_snprintf("0x%02X%02X%02X%02X", 10, this->uuid_.uuid.uuid32 >> 24, + return str_snprintf("0x%02" PRIX32 "%02" PRIX32 "%02" PRIX32 "%02" PRIX32, 10, (this->uuid_.uuid.uuid32 >> 24), (this->uuid_.uuid.uuid32 >> 16 & 0xff), (this->uuid_.uuid.uuid32 >> 8 & 0xff), this->uuid_.uuid.uuid32 & 0xff); default: diff --git a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp index 1569ea0dd5..f67f29477d 100644 --- a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp +++ b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #ifdef USE_OTA #include "esphome/components/ota/ota_component.h" @@ -614,7 +615,7 @@ uint64_t ESPBTDevice::address_uint64() const { return esp32_ble::ble_addr_to_uin void ESP32BLETracker::dump_config() { ESP_LOGCONFIG(TAG, "BLE Tracker:"); - ESP_LOGCONFIG(TAG, " Scan Duration: %u s", this->scan_duration_); + ESP_LOGCONFIG(TAG, " Scan Duration: %" PRIu32 " s", this->scan_duration_); ESP_LOGCONFIG(TAG, " Scan Interval: %.1f ms", this->scan_interval_ * 0.625f); ESP_LOGCONFIG(TAG, " Scan Window: %.1f ms", this->scan_window_ * 0.625f); ESP_LOGCONFIG(TAG, " Scan Type: %s", this->scan_active_ ? "ACTIVE" : "PASSIVE"); diff --git a/esphome/components/ina226/ina226.cpp b/esphome/components/ina226/ina226.cpp index 2e30a5ac01..1fb859da66 100644 --- a/esphome/components/ina226/ina226.cpp +++ b/esphome/components/ina226/ina226.cpp @@ -1,6 +1,7 @@ #include "ina226.h" #include "esphome/core/log.h" #include "esphome/core/hal.h" +#include namespace esphome { namespace ina226 { @@ -68,7 +69,7 @@ void INA226Component::setup() { auto calibration = uint32_t(0.00512 / (lsb * this->shunt_resistance_ohm_ / 1000000.0f)); - ESP_LOGV(TAG, " Using LSB=%u calibration=%u", lsb, calibration); + ESP_LOGV(TAG, " Using LSB=%" PRIu32 " calibration=%" PRIu32, lsb, calibration); if (!this->write_byte_16(INA226_REGISTER_CALIBRATION, calibration)) { this->mark_failed(); diff --git a/esphome/components/max31865/max31865.cpp b/esphome/components/max31865/max31865.cpp index 152d7b340b..b48aa2fdd3 100644 --- a/esphome/components/max31865/max31865.cpp +++ b/esphome/components/max31865/max31865.cpp @@ -2,6 +2,7 @@ #include "esphome/core/log.h" #include +#include namespace esphome { namespace max31865 { @@ -45,14 +46,15 @@ void MAX31865Sensor::update() { config = this->read_register_(CONFIGURATION_REG); fault_detect_time = micros() - start_time; if ((fault_detect_time >= 6000) && (config & 0b00001100)) { - ESP_LOGE(TAG, "Fault detection incomplete (0x%02X) after %uμs (datasheet spec is 600μs max)! Aborting read.", + ESP_LOGE(TAG, + "Fault detection incomplete (0x%02X) after %" PRIu32 "μs (datasheet spec is 600μs max)! Aborting read.", config, fault_detect_time); this->publish_state(NAN); this->status_set_error(); return; } } while (config & 0b00001100); - ESP_LOGV(TAG, "Fault detection completed in %uμs.", fault_detect_time); + ESP_LOGV(TAG, "Fault detection completed in %" PRIu32 "μs.", fault_detect_time); // Start 1-shot conversion this->write_config_(0b11100000, 0b10100000); diff --git a/esphome/components/sen5x/sen5x.cpp b/esphome/components/sen5x/sen5x.cpp index ddce568c97..8b4dcda9ef 100644 --- a/esphome/components/sen5x/sen5x.cpp +++ b/esphome/components/sen5x/sen5x.cpp @@ -1,6 +1,7 @@ #include "sen5x.h" #include "esphome/core/hal.h" #include "esphome/core/log.h" +#include namespace esphome { namespace sen5x { @@ -140,15 +141,15 @@ void SEN5XComponent::setup() { this->pref_ = global_preferences->make_preference(hash, true); if (this->pref_.load(&this->voc_baselines_storage_)) { - ESP_LOGI(TAG, "Loaded VOC baseline state0: 0x%04X, state1: 0x%04X", this->voc_baselines_storage_.state0, - voc_baselines_storage_.state1); + ESP_LOGI(TAG, "Loaded VOC baseline state0: 0x%04" PRIX32 ", state1: 0x%04" PRIX32, + this->voc_baselines_storage_.state0, voc_baselines_storage_.state1); } // Initialize storage timestamp this->seconds_since_last_store_ = 0; if (this->voc_baselines_storage_.state0 > 0 && this->voc_baselines_storage_.state1 > 0) { - ESP_LOGI(TAG, "Setting VOC baseline from save state0: 0x%04X, state1: 0x%04X", + ESP_LOGI(TAG, "Setting VOC baseline from save state0: 0x%04" PRIX32 ", state1: 0x%04" PRIX32, this->voc_baselines_storage_.state0, voc_baselines_storage_.state1); uint16_t states[4]; @@ -252,7 +253,7 @@ void SEN5XComponent::dump_config() { ESP_LOGCONFIG(TAG, " Firmware version: %d", this->firmware_version_); ESP_LOGCONFIG(TAG, " Serial number %02d.%02d.%02d", serial_number_[0], serial_number_[1], serial_number_[2]); if (this->auto_cleaning_interval_.has_value()) { - ESP_LOGCONFIG(TAG, " Auto cleaning interval %d seconds", auto_cleaning_interval_.value()); + ESP_LOGCONFIG(TAG, " Auto cleaning interval %" PRId32 " seconds", auto_cleaning_interval_.value()); } if (this->acceleration_mode_.has_value()) { switch (this->acceleration_mode_.value()) { @@ -302,8 +303,8 @@ void SEN5XComponent::update() { this->voc_baselines_storage_.state1 = state1; if (this->pref_.save(&this->voc_baselines_storage_)) { - ESP_LOGI(TAG, "Stored VOC baseline state0: 0x%04X ,state1: 0x%04X", this->voc_baselines_storage_.state0, - voc_baselines_storage_.state1); + ESP_LOGI(TAG, "Stored VOC baseline state0: 0x%04" PRIX32 " ,state1: 0x%04" PRIX32, + this->voc_baselines_storage_.state0, voc_baselines_storage_.state1); } else { ESP_LOGW(TAG, "Could not store VOC baselines"); } diff --git a/esphome/components/uart/uart.cpp b/esphome/components/uart/uart.cpp index 22a22e2772..9834462ff9 100644 --- a/esphome/components/uart/uart.cpp +++ b/esphome/components/uart/uart.cpp @@ -3,6 +3,7 @@ #include "esphome/core/helpers.h" #include "esphome/core/application.h" #include "esphome/core/defines.h" +#include namespace esphome { namespace uart { @@ -12,8 +13,8 @@ static const char *const TAG = "uart"; void UARTDevice::check_uart_settings(uint32_t baud_rate, uint8_t stop_bits, UARTParityOptions parity, uint8_t data_bits) { if (this->parent_->get_baud_rate() != baud_rate) { - ESP_LOGE(TAG, " Invalid baud_rate: Integration requested baud_rate %u but you have %u!", baud_rate, - this->parent_->get_baud_rate()); + ESP_LOGE(TAG, " Invalid baud_rate: Integration requested baud_rate %" PRIu32 " but you have %" PRIu32 "!", + baud_rate, this->parent_->get_baud_rate()); } if (this->parent_->get_stop_bits() != stop_bits) { ESP_LOGE(TAG, " Invalid stop bits: Integration requested stop_bits %u but you have %u!", stop_bits, diff --git a/esphome/components/uart/uart_component_esp_idf.cpp b/esphome/components/uart/uart_component_esp_idf.cpp index 1560409772..ae772fa8f8 100644 --- a/esphome/components/uart/uart_component_esp_idf.cpp +++ b/esphome/components/uart/uart_component_esp_idf.cpp @@ -5,6 +5,7 @@ #include "esphome/core/defines.h" #include "esphome/core/helpers.h" #include "esphome/core/log.h" +#include #ifdef USE_LOGGER #include "esphome/components/logger/logger.h" @@ -125,7 +126,7 @@ void IDFUARTComponent::dump_config() { if (this->rx_pin_ != nullptr) { ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_); } - ESP_LOGCONFIG(TAG, " Baud Rate: %u baud", this->baud_rate_); + ESP_LOGCONFIG(TAG, " Baud Rate: %" PRIu32 " baud", this->baud_rate_); ESP_LOGCONFIG(TAG, " Data Bits: %u", this->data_bits_); ESP_LOGCONFIG(TAG, " Parity: %s", LOG_STR_ARG(parity_to_str(this->parity_))); ESP_LOGCONFIG(TAG, " Stop bits: %u", this->stop_bits_); @@ -150,7 +151,7 @@ bool IDFUARTComponent::peek_byte(uint8_t *data) { if (this->has_peek_) { *data = this->peek_byte_; } else { - int len = uart_read_bytes(this->uart_num_, data, 1, 20 / portTICK_RATE_MS); + int len = uart_read_bytes(this->uart_num_, data, 1, 20 / portTICK_PERIOD_MS); if (len == 0) { *data = 0; } else { @@ -174,7 +175,7 @@ bool IDFUARTComponent::read_array(uint8_t *data, size_t len) { this->has_peek_ = false; } if (length_to_read > 0) - uart_read_bytes(this->uart_num_, data, length_to_read, 20 / portTICK_RATE_MS); + uart_read_bytes(this->uart_num_, data, length_to_read, 20 / portTICK_PERIOD_MS); xSemaphoreGive(this->lock_); #ifdef USE_UART_DEBUGGER for (size_t i = 0; i < len; i++) { diff --git a/esphome/components/waveshare_epaper/waveshare_epaper.cpp b/esphome/components/waveshare_epaper/waveshare_epaper.cpp index d64a5500dd..a3b1ec7d7b 100644 --- a/esphome/components/waveshare_epaper/waveshare_epaper.cpp +++ b/esphome/components/waveshare_epaper/waveshare_epaper.cpp @@ -2,6 +2,7 @@ #include "esphome/core/log.h" #include "esphome/core/application.h" #include "esphome/core/helpers.h" +#include namespace esphome { namespace waveshare_epaper { @@ -250,7 +251,7 @@ void WaveshareEPaperTypeA::dump_config() { ESP_LOGCONFIG(TAG, " Model: 2.9inV2"); break; } - ESP_LOGCONFIG(TAG, " Full Update Every: %u", this->full_update_every_); + ESP_LOGCONFIG(TAG, " Full Update Every: %" PRIu32, this->full_update_every_); LOG_PIN(" Reset Pin: ", this->reset_pin_); LOG_PIN(" DC Pin: ", this->dc_pin_); LOG_PIN(" Busy Pin: ", this->busy_pin_); From cd72a2ed7eff504089adcf5fc8d37c9ddfdaf86c Mon Sep 17 00:00:00 2001 From: Sergey Dudanov Date: Sun, 30 Jul 2023 23:44:56 +0400 Subject: [PATCH 31/84] Bump clang-tidy from 11 to 14 (#5160) --- .clang-tidy | 15 +++++++++++++-- .github/workflows/ci.yml | 2 +- esphome/components/logger/logger.cpp | 2 +- .../components/matrix_keypad/matrix_keypad.cpp | 6 ++++-- esphome/components/remote_base/midea_protocol.h | 2 -- esphome/components/ssd1322_spi/ssd1322_spi.cpp | 3 +-- esphome/components/ssd1325_spi/ssd1325_spi.cpp | 3 +-- esphome/components/ssd1327_spi/ssd1327_spi.cpp | 3 +-- esphome/components/ssd1331_spi/ssd1331_spi.cpp | 3 +-- esphome/components/ssd1351_spi/ssd1351_spi.cpp | 3 +-- esphome/components/tlc5947/tlc5947.cpp | 3 +-- .../vbus/binary_sensor/vbus_binary_sensor.cpp | 3 ++- esphome/components/vbus/sensor/vbus_sensor.cpp | 3 ++- .../components/voice_assistant/voice_assistant.h | 1 - .../wifi/wifi_component_esp32_arduino.cpp | 2 +- .../components/wifi/wifi_component_esp_idf.cpp | 2 +- script/clang-tidy | 12 ++++++------ 17 files changed, 37 insertions(+), 31 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index c9b77b5720..946f2950d8 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -5,9 +5,12 @@ Checks: >- -altera-*, -android-*, -boost-*, + -bugprone-easily-swappable-parameters, + -bugprone-implicit-widening-of-multiplication-result, -bugprone-narrowing-conversions, -bugprone-signed-char-misuse, -cert-dcl50-cpp, + -cert-err33-c, -cert-err58-cpp, -cert-oop57-cpp, -cert-str34-c, @@ -15,6 +18,7 @@ Checks: >- -clang-analyzer-osx.*, -clang-diagnostic-delete-abstract-non-virtual-dtor, -clang-diagnostic-delete-non-abstract-non-virtual-dtor, + -clang-diagnostic-ignored-optimization-argument, -clang-diagnostic-shadow-field, -clang-diagnostic-unused-const-variable, -clang-diagnostic-unused-parameter, @@ -25,6 +29,7 @@ Checks: >- -cppcoreguidelines-macro-usage, -cppcoreguidelines-narrowing-conversions, -cppcoreguidelines-non-private-member-variables-in-classes, + -cppcoreguidelines-prefer-member-initializer, -cppcoreguidelines-pro-bounds-array-to-pointer-decay, -cppcoreguidelines-pro-bounds-constant-array-index, -cppcoreguidelines-pro-bounds-pointer-arithmetic, @@ -36,6 +41,7 @@ Checks: >- -cppcoreguidelines-pro-type-union-access, -cppcoreguidelines-pro-type-vararg, -cppcoreguidelines-special-member-functions, + -cppcoreguidelines-virtual-class-destructor, -fuchsia-multiple-inheritance, -fuchsia-overloaded-operator, -fuchsia-statically-constructed-objects, @@ -68,6 +74,7 @@ Checks: >- -modernize-use-nodiscard, -mpi-*, -objc-*, + -readability-container-data-pointer, -readability-convert-member-functions-to-static, -readability-else-after-return, -readability-function-cognitive-complexity, @@ -82,8 +89,6 @@ WarningsAsErrors: '*' AnalyzeTemporaryDtors: false FormatStyle: google CheckOptions: - - key: google-readability-braces-around-statements.ShortStatementLines - value: '1' - key: google-readability-function-size.StatementThreshold value: '800' - key: google-runtime-int.TypeSuffix @@ -158,3 +163,9 @@ CheckOptions: value: '' - key: readability-qualified-auto.AddConstToQualified value: 0 + - key: readability-identifier-length.MinimumVariableNameLength + value: 0 + - key: readability-identifier-length.MinimumParameterNameLength + value: 0 + - key: readability-identifier-length.MinimumLoopCounterNameLength + value: 0 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7775a996fc..ba46936952 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -305,7 +305,7 @@ jobs: key: platformio-${{ matrix.pio_cache_key }}-${{ hashFiles('platformio.ini') }} - name: Install clang-tidy - run: sudo apt-get install clang-tidy-11 + run: sudo apt-get install clang-tidy-14 - name: Register problem matchers run: | diff --git a/esphome/components/logger/logger.cpp b/esphome/components/logger/logger.cpp index 8fd39265fd..ca4cc64007 100644 --- a/esphome/components/logger/logger.cpp +++ b/esphome/components/logger/logger.cpp @@ -125,7 +125,7 @@ void HOT Logger::log_message_(int level, const char *tag, int offset) { #elif defined(USE_ESP32_VARIANT_ESP32S3) uart_ == UART_SELECTION_USB_CDC || uart_ == UART_SELECTION_USB_SERIAL_JTAG #else - /* DISABLES CODE */ (false) + /* DISABLES CODE */ (false) // NOLINT #endif ) { puts(msg); diff --git a/esphome/components/matrix_keypad/matrix_keypad.cpp b/esphome/components/matrix_keypad/matrix_keypad.cpp index f4e7bf4d23..4f8962a782 100644 --- a/esphome/components/matrix_keypad/matrix_keypad.cpp +++ b/esphome/components/matrix_keypad/matrix_keypad.cpp @@ -89,11 +89,13 @@ void MatrixKeypad::loop() { void MatrixKeypad::dump_config() { ESP_LOGCONFIG(TAG, "Matrix Keypad:"); ESP_LOGCONFIG(TAG, " Rows:"); - for (auto &pin : this->rows_) + for (auto &pin : this->rows_) { LOG_PIN(" Pin: ", pin); + } ESP_LOGCONFIG(TAG, " Cols:"); - for (auto &pin : this->columns_) + for (auto &pin : this->columns_) { LOG_PIN(" Pin: ", pin); + } } void MatrixKeypad::register_listener(MatrixKeypadListener *listener) { this->listeners_.push_back(listener); } diff --git a/esphome/components/remote_base/midea_protocol.h b/esphome/components/remote_base/midea_protocol.h index a7f5636b06..d81a50241b 100644 --- a/esphome/components/remote_base/midea_protocol.h +++ b/esphome/components/remote_base/midea_protocol.h @@ -22,8 +22,6 @@ class MideaData { MideaData(const std::vector &data) { std::copy_n(data.begin(), std::min(data.size(), this->data_.size()), this->data_.begin()); } - // Default copy constructor - MideaData(const MideaData &) = default; uint8_t *data() { return this->data_.data(); } const uint8_t *data() const { return this->data_.data(); } diff --git a/esphome/components/ssd1322_spi/ssd1322_spi.cpp b/esphome/components/ssd1322_spi/ssd1322_spi.cpp index 50c46c4d02..a841c5606e 100644 --- a/esphome/components/ssd1322_spi/ssd1322_spi.cpp +++ b/esphome/components/ssd1322_spi/ssd1322_spi.cpp @@ -21,8 +21,7 @@ void SPISSD1322::setup() { void SPISSD1322::dump_config() { LOG_DISPLAY("", "SPI SSD1322", this); ESP_LOGCONFIG(TAG, " Model: %s", this->model_str_()); - if (this->cs_) - LOG_PIN(" CS Pin: ", this->cs_); + LOG_PIN(" CS Pin: ", this->cs_); LOG_PIN(" DC Pin: ", this->dc_pin_); LOG_PIN(" Reset Pin: ", this->reset_pin_); ESP_LOGCONFIG(TAG, " Initial Brightness: %.2f", this->brightness_); diff --git a/esphome/components/ssd1325_spi/ssd1325_spi.cpp b/esphome/components/ssd1325_spi/ssd1325_spi.cpp index 98f48b8538..8a95bfeae3 100644 --- a/esphome/components/ssd1325_spi/ssd1325_spi.cpp +++ b/esphome/components/ssd1325_spi/ssd1325_spi.cpp @@ -21,8 +21,7 @@ void SPISSD1325::setup() { void SPISSD1325::dump_config() { LOG_DISPLAY("", "SPI SSD1325", this); ESP_LOGCONFIG(TAG, " Model: %s", this->model_str_()); - if (this->cs_) - LOG_PIN(" CS Pin: ", this->cs_); + LOG_PIN(" CS Pin: ", this->cs_); LOG_PIN(" DC Pin: ", this->dc_pin_); LOG_PIN(" Reset Pin: ", this->reset_pin_); ESP_LOGCONFIG(TAG, " Initial Brightness: %.2f", this->brightness_); diff --git a/esphome/components/ssd1327_spi/ssd1327_spi.cpp b/esphome/components/ssd1327_spi/ssd1327_spi.cpp index 1dd2b73e66..c6ae377119 100644 --- a/esphome/components/ssd1327_spi/ssd1327_spi.cpp +++ b/esphome/components/ssd1327_spi/ssd1327_spi.cpp @@ -21,8 +21,7 @@ void SPISSD1327::setup() { void SPISSD1327::dump_config() { LOG_DISPLAY("", "SPI SSD1327", this); ESP_LOGCONFIG(TAG, " Model: %s", this->model_str_()); - if (this->cs_) - LOG_PIN(" CS Pin: ", this->cs_); + LOG_PIN(" CS Pin: ", this->cs_); LOG_PIN(" DC Pin: ", this->dc_pin_); LOG_PIN(" Reset Pin: ", this->reset_pin_); ESP_LOGCONFIG(TAG, " Initial Brightness: %.2f", this->brightness_); diff --git a/esphome/components/ssd1331_spi/ssd1331_spi.cpp b/esphome/components/ssd1331_spi/ssd1331_spi.cpp index ff42c74b9f..88116f6c00 100644 --- a/esphome/components/ssd1331_spi/ssd1331_spi.cpp +++ b/esphome/components/ssd1331_spi/ssd1331_spi.cpp @@ -20,8 +20,7 @@ void SPISSD1331::setup() { } void SPISSD1331::dump_config() { LOG_DISPLAY("", "SPI SSD1331", this); - if (this->cs_) - LOG_PIN(" CS Pin: ", this->cs_); + LOG_PIN(" CS Pin: ", this->cs_); LOG_PIN(" DC Pin: ", this->dc_pin_); LOG_PIN(" Reset Pin: ", this->reset_pin_); ESP_LOGCONFIG(TAG, " Initial Brightness: %.2f", this->brightness_); diff --git a/esphome/components/ssd1351_spi/ssd1351_spi.cpp b/esphome/components/ssd1351_spi/ssd1351_spi.cpp index 9599c6e644..b71b8f4f88 100644 --- a/esphome/components/ssd1351_spi/ssd1351_spi.cpp +++ b/esphome/components/ssd1351_spi/ssd1351_spi.cpp @@ -21,8 +21,7 @@ void SPISSD1351::setup() { void SPISSD1351::dump_config() { LOG_DISPLAY("", "SPI SSD1351", this); ESP_LOGCONFIG(TAG, " Model: %s", this->model_str_()); - if (this->cs_) - LOG_PIN(" CS Pin: ", this->cs_); + LOG_PIN(" CS Pin: ", this->cs_); LOG_PIN(" DC Pin: ", this->dc_pin_); LOG_PIN(" Reset Pin: ", this->reset_pin_); ESP_LOGCONFIG(TAG, " Initial Brightness: %.2f", this->brightness_); diff --git a/esphome/components/tlc5947/tlc5947.cpp b/esphome/components/tlc5947/tlc5947.cpp index a7e08c8341..8f3f60f087 100644 --- a/esphome/components/tlc5947/tlc5947.cpp +++ b/esphome/components/tlc5947/tlc5947.cpp @@ -27,8 +27,7 @@ void TLC5947::dump_config() { LOG_PIN(" Data Pin: ", this->data_pin_); LOG_PIN(" Clock Pin: ", this->clock_pin_); LOG_PIN(" LAT Pin: ", this->lat_pin_); - if (this->outenable_pin_ != nullptr) - LOG_PIN(" OE Pin: ", this->outenable_pin_); + LOG_PIN(" OE Pin: ", this->outenable_pin_); ESP_LOGCONFIG(TAG, " Number of chips: %u", this->num_chips_); } diff --git a/esphome/components/vbus/binary_sensor/vbus_binary_sensor.cpp b/esphome/components/vbus/binary_sensor/vbus_binary_sensor.cpp index 087d049a57..4ccd149935 100644 --- a/esphome/components/vbus/binary_sensor/vbus_binary_sensor.cpp +++ b/esphome/components/vbus/binary_sensor/vbus_binary_sensor.cpp @@ -147,8 +147,9 @@ void VBusCustomBSensor::dump_config() { ESP_LOGCONFIG(TAG, " Command: 0x%04x", this->command_); } ESP_LOGCONFIG(TAG, " Binary Sensors:"); - for (VBusCustomSubBSensor *bsensor : this->bsensors_) + for (VBusCustomSubBSensor *bsensor : this->bsensors_) { LOG_BINARY_SENSOR(" ", "-", bsensor); + } } void VBusCustomBSensor::handle_message(std::vector &message) { diff --git a/esphome/components/vbus/sensor/vbus_sensor.cpp b/esphome/components/vbus/sensor/vbus_sensor.cpp index 5b4f57f73d..e81c0486d4 100644 --- a/esphome/components/vbus/sensor/vbus_sensor.cpp +++ b/esphome/components/vbus/sensor/vbus_sensor.cpp @@ -232,8 +232,9 @@ void VBusCustomSensor::dump_config() { ESP_LOGCONFIG(TAG, " Command: 0x%04x", this->command_); } ESP_LOGCONFIG(TAG, " Sensors:"); - for (VBusCustomSubSensor *sensor : this->sensors_) + for (VBusCustomSubSensor *sensor : this->sensors_) { LOG_SENSOR(" ", "-", sensor); + } } void VBusCustomSensor::handle_message(std::vector &message) { diff --git a/esphome/components/voice_assistant/voice_assistant.h b/esphome/components/voice_assistant/voice_assistant.h index e67baaee65..75c17965bc 100644 --- a/esphome/components/voice_assistant/voice_assistant.h +++ b/esphome/components/voice_assistant/voice_assistant.h @@ -6,7 +6,6 @@ #include "esphome/core/automation.h" #include "esphome/core/component.h" -#include "esphome/core/defines.h" #include "esphome/core/helpers.h" #include "esphome/components/api/api_pb2.h" diff --git a/esphome/components/wifi/wifi_component_esp32_arduino.cpp b/esphome/components/wifi/wifi_component_esp32_arduino.cpp index f35f5dfc43..3628eca78d 100644 --- a/esphome/components/wifi/wifi_component_esp32_arduino.cpp +++ b/esphome/components/wifi/wifi_component_esp32_arduino.cpp @@ -220,7 +220,7 @@ bool WiFiComponent::wifi_sta_connect_(const WiFiAP &ap) { esp_err_t err; esp_wifi_get_config(WIFI_IF_STA, ¤t_conf); - if (memcmp(¤t_conf, &conf, sizeof(wifi_config_t)) != 0) { + if (memcmp(¤t_conf, &conf, sizeof(wifi_config_t)) != 0) { // NOLINT err = esp_wifi_disconnect(); if (err != ESP_OK) { ESP_LOGV(TAG, "esp_wifi_disconnect failed! %d", err); diff --git a/esphome/components/wifi/wifi_component_esp_idf.cpp b/esphome/components/wifi/wifi_component_esp_idf.cpp index 744fc755fe..a2bbc8ae09 100644 --- a/esphome/components/wifi/wifi_component_esp_idf.cpp +++ b/esphome/components/wifi/wifi_component_esp_idf.cpp @@ -333,7 +333,7 @@ bool WiFiComponent::wifi_sta_connect_(const WiFiAP &ap) { // can continue } - if (memcmp(¤t_conf, &conf, sizeof(wifi_config_t)) != 0) { + if (memcmp(¤t_conf, &conf, sizeof(wifi_config_t)) != 0) { // NOLINT err = esp_wifi_disconnect(); if (err != ESP_OK) { ESP_LOGV(TAG, "esp_wifi_disconnect failed: %s", esp_err_to_name(err)); diff --git a/script/clang-tidy b/script/clang-tidy index 5d2cba6eb5..d8dd033d29 100755 --- a/script/clang-tidy +++ b/script/clang-tidy @@ -113,7 +113,7 @@ def clang_options(idedata): def run_tidy(args, options, tmpdir, queue, lock, failed_files): while True: path = queue.get() - invocation = ["clang-tidy-11"] + invocation = ["clang-tidy-14"] if tmpdir is not None: invocation.append("--export-fixes") @@ -194,14 +194,14 @@ def main(): args = parser.parse_args() try: - get_output("clang-tidy-11", "-version") + get_output("clang-tidy-14", "-version") except: print( """ - Oops. It looks like clang-tidy-11 is not installed. + Oops. It looks like clang-tidy-14 is not installed. - Please check you can run "clang-tidy-11 -version" in your terminal and install - clang-tidy (v11) if necessary. + Please check you can run "clang-tidy-14 -version" in your terminal and install + clang-tidy (v14) if necessary. Note you can also upload your code as a pull request on GitHub and see the CI check output to apply clang-tidy. @@ -272,7 +272,7 @@ def main(): if args.fix and failed_files: print("Applying fixes ...") try: - subprocess.call(["clang-apply-replacements-11", tmpdir]) + subprocess.call(["clang-apply-replacements-14", tmpdir]) except: print("Error applying fixes.\n", file=sys.stderr) raise From a120a455bfa47da44643a881ae2ac7a4bd224cb5 Mon Sep 17 00:00:00 2001 From: Sergey Dudanov Date: Sun, 30 Jul 2023 23:52:01 +0400 Subject: [PATCH 32/84] climate triggers Climate and ClimateCall references (#5028) --- esphome/components/climate/__init__.py | 16 ++++++++++++---- esphome/components/climate/automation.h | 8 ++++---- esphome/components/climate/climate.cpp | 8 ++++---- esphome/components/climate/climate.h | 8 ++++---- esphome/components/mqtt/mqtt_climate.cpp | 2 +- esphome/core/controller.cpp | 2 +- tests/test1.yaml | 9 +++++++-- 7 files changed, 33 insertions(+), 20 deletions(-) diff --git a/esphome/components/climate/__init__.py b/esphome/components/climate/__init__.py index bf167fe837..85242eb344 100644 --- a/esphome/components/climate/__init__.py +++ b/esphome/components/climate/__init__.py @@ -127,8 +127,12 @@ def single_visual_temperature(value): # Actions ControlAction = climate_ns.class_("ControlAction", automation.Action) -StateTrigger = climate_ns.class_("StateTrigger", automation.Trigger.template()) -ControlTrigger = climate_ns.class_("ControlTrigger", automation.Trigger.template()) +StateTrigger = climate_ns.class_( + "StateTrigger", automation.Trigger.template(Climate.operator("ref")) +) +ControlTrigger = climate_ns.class_( + "ControlTrigger", automation.Trigger.template(ClimateCall.operator("ref")) +) VISUAL_TEMPERATURE_STEP_SCHEMA = cv.Any( single_visual_temperature, @@ -322,11 +326,15 @@ async def setup_climate_core_(var, config): for conf in config.get(CONF_ON_STATE, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) - await automation.build_automation(trigger, [], conf) + await automation.build_automation( + trigger, [(Climate.operator("ref"), "x")], conf + ) for conf in config.get(CONF_ON_CONTROL, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) - await automation.build_automation(trigger, [], conf) + await automation.build_automation( + trigger, [(ClimateCall.operator("ref"), "x")], conf + ) async def register_climate(var, config): diff --git a/esphome/components/climate/automation.h b/esphome/components/climate/automation.h index 9b06563eb4..382871e1e7 100644 --- a/esphome/components/climate/automation.h +++ b/esphome/components/climate/automation.h @@ -42,17 +42,17 @@ template class ControlAction : public Action { Climate *climate_; }; -class ControlTrigger : public Trigger<> { +class ControlTrigger : public Trigger { public: ControlTrigger(Climate *climate) { - climate->add_on_control_callback([this]() { this->trigger(); }); + climate->add_on_control_callback([this](ClimateCall &x) { this->trigger(x); }); } }; -class StateTrigger : public Trigger<> { +class StateTrigger : public Trigger { public: StateTrigger(Climate *climate) { - climate->add_on_state_callback([this]() { this->trigger(); }); + climate->add_on_state_callback([this](Climate &x) { this->trigger(x); }); } }; diff --git a/esphome/components/climate/climate.cpp b/esphome/components/climate/climate.cpp index a032596eb3..1680601279 100644 --- a/esphome/components/climate/climate.cpp +++ b/esphome/components/climate/climate.cpp @@ -7,6 +7,7 @@ namespace climate { static const char *const TAG = "climate"; void ClimateCall::perform() { + this->parent_->control_callback_.call(*this); ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str()); this->validate_(); if (this->mode_.has_value()) { @@ -44,7 +45,6 @@ void ClimateCall::perform() { if (this->target_temperature_high_.has_value()) { ESP_LOGD(TAG, " Target Temperature High: %.2f", *this->target_temperature_high_); } - this->parent_->control_callback_.call(); this->parent_->control(*this); } void ClimateCall::validate_() { @@ -300,11 +300,11 @@ ClimateCall &ClimateCall::set_swing_mode(optional swing_mode) return *this; } -void Climate::add_on_state_callback(std::function &&callback) { +void Climate::add_on_state_callback(std::function &&callback) { this->state_callback_.add(std::move(callback)); } -void Climate::add_on_control_callback(std::function &&callback) { +void Climate::add_on_control_callback(std::function &&callback) { this->control_callback_.add(std::move(callback)); } @@ -408,7 +408,7 @@ void Climate::publish_state() { } // Send state to frontend - this->state_callback_.call(); + this->state_callback_.call(*this); // Save state this->save_state_(); } diff --git a/esphome/components/climate/climate.h b/esphome/components/climate/climate.h index 656e1c4852..f90db3f52a 100644 --- a/esphome/components/climate/climate.h +++ b/esphome/components/climate/climate.h @@ -198,7 +198,7 @@ class Climate : public EntityBase { * * @param callback The callback to call. */ - void add_on_state_callback(std::function &&callback); + void add_on_state_callback(std::function &&callback); /** * Add a callback for the climate device configuration; each time the configuration parameters of a climate device @@ -206,7 +206,7 @@ class Climate : public EntityBase { * * @param callback The callback to call. */ - void add_on_control_callback(std::function &&callback); + void add_on_control_callback(std::function &&callback); /** Make a climate device control call, this is used to control the climate device, see the ClimateCall description * for more info. @@ -273,8 +273,8 @@ class Climate : public EntityBase { void dump_traits_(const char *tag); - CallbackManager state_callback_{}; - CallbackManager control_callback_{}; + CallbackManager state_callback_{}; + CallbackManager control_callback_{}; ESPPreferenceObject rtc_; optional visual_min_temperature_override_{}; optional visual_max_temperature_override_{}; diff --git a/esphome/components/mqtt/mqtt_climate.cpp b/esphome/components/mqtt/mqtt_climate.cpp index d63885fa04..44c490c308 100644 --- a/esphome/components/mqtt/mqtt_climate.cpp +++ b/esphome/components/mqtt/mqtt_climate.cpp @@ -216,7 +216,7 @@ void MQTTClimateComponent::setup() { }); } - this->device_->add_on_state_callback([this]() { this->publish_state_(); }); + this->device_->add_on_state_callback([this](Climate & /*unused*/) { this->publish_state_(); }); } MQTTClimateComponent::MQTTClimateComponent(Climate *device) : device_(device) {} bool MQTTClimateComponent::send_initial_state() { return this->publish_state_(); } diff --git a/esphome/core/controller.cpp b/esphome/core/controller.cpp index 2ce471ead0..18d427b40c 100644 --- a/esphome/core/controller.cpp +++ b/esphome/core/controller.cpp @@ -50,7 +50,7 @@ void Controller::setup_controller(bool include_internal) { #ifdef USE_CLIMATE for (auto *obj : App.get_climates()) { if (include_internal || !obj->is_internal()) - obj->add_on_state_callback([this, obj]() { this->on_climate_update(obj); }); + obj->add_on_state_callback([this, obj](climate::Climate & /*unused*/) { this->on_climate_update(obj); }); } #endif #ifdef USE_NUMBER diff --git a/tests/test1.yaml b/tests/test1.yaml index 67e675ef3a..90c62c6b73 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -2200,9 +2200,14 @@ climate: use_fahrenheit: true - platform: midea on_control: - logger.log: Control message received! + - logger.log: Control message received! + - lambda: |- + x.set_mode(CLIMATE_MODE_FAN_ONLY); on_state: - logger.log: State changed! + - logger.log: State changed! + - lambda: |- + if (x.mode == CLIMATE_MODE_FAN_ONLY) + id(binary_sensor1).publish_state(true); id: midea_unit uart_id: uart_0 name: Midea Climate From 794a4bd9a1944796c110e7f5d4b8c90cf48e8f15 Mon Sep 17 00:00:00 2001 From: Sergey Dudanov Date: Mon, 31 Jul 2023 00:07:33 +0400 Subject: [PATCH 33/84] remote_base changes (#5124) --- .../remote_base/pronto_protocol.cpp | 9 +- .../components/remote_base/pronto_protocol.h | 2 +- esphome/components/remote_base/raw_protocol.h | 8 +- .../components/remote_base/remote_base.cpp | 96 ++++++++- esphome/components/remote_base/remote_base.h | 194 ++++-------------- .../remote_transmitter/remote_transmitter.h | 2 +- 6 files changed, 147 insertions(+), 164 deletions(-) diff --git a/esphome/components/remote_base/pronto_protocol.cpp b/esphome/components/remote_base/pronto_protocol.cpp index 4951b12bb1..81ac176666 100644 --- a/esphome/components/remote_base/pronto_protocol.cpp +++ b/esphome/components/remote_base/pronto_protocol.cpp @@ -187,11 +187,10 @@ std::string ProntoProtocol::dump_duration_(uint32_t duration, uint16_t timebase, return dump_number_((duration + timebase / 2) / timebase, end); } -std::string ProntoProtocol::compensate_and_dump_sequence_(std::vector *data, uint16_t timebase) { +std::string ProntoProtocol::compensate_and_dump_sequence_(const RawTimings &data, uint16_t timebase) { std::string out; - for (std::vector::size_type i = 0; i < data->size() - 1; i++) { - int32_t t_length = data->at(i); + for (int32_t t_length : data) { uint32_t t_duration; if (t_length > 0) { // Mark @@ -212,12 +211,12 @@ optional ProntoProtocol::decode(RemoteReceiveData src) { ProntoData out; uint16_t frequency = 38000U; - std::vector *data = src.get_raw_data(); + auto &data = src.get_raw_data(); std::string prontodata; prontodata += dump_number_(frequency > 0 ? LEARNED_TOKEN : LEARNED_NON_MODULATED_TOKEN); prontodata += dump_number_(to_frequency_code_(frequency)); - prontodata += dump_number_((data->size() + 1) / 2); + prontodata += dump_number_((data.size() + 1) / 2); prontodata += dump_number_(0); uint16_t timebase = to_timebase_(frequency); prontodata += compensate_and_dump_sequence_(data, timebase); diff --git a/esphome/components/remote_base/pronto_protocol.h b/esphome/components/remote_base/pronto_protocol.h index 8c491257d3..8b2163af12 100644 --- a/esphome/components/remote_base/pronto_protocol.h +++ b/esphome/components/remote_base/pronto_protocol.h @@ -27,7 +27,7 @@ class ProntoProtocol : public RemoteProtocol { std::string dump_digit_(uint8_t x); std::string dump_number_(uint16_t number, bool end = false); std::string dump_duration_(uint32_t duration, uint16_t timebase, bool end = false); - std::string compensate_and_dump_sequence_(std::vector *data, uint16_t timebase); + std::string compensate_and_dump_sequence_(const RawTimings &data, uint16_t timebase); public: void encode(RemoteTransmitData *dst, const ProntoData &data) override; diff --git a/esphome/components/remote_base/raw_protocol.h b/esphome/components/remote_base/raw_protocol.h index dc22282d1c..494903daa8 100644 --- a/esphome/components/remote_base/raw_protocol.h +++ b/esphome/components/remote_base/raw_protocol.h @@ -31,17 +31,17 @@ class RawBinarySensor : public RemoteReceiverBinarySensorBase { size_t len_; }; -class RawTrigger : public Trigger>, public Component, public RemoteReceiverListener { +class RawTrigger : public Trigger, public Component, public RemoteReceiverListener { protected: bool on_receive(RemoteReceiveData src) override { - this->trigger(*src.get_raw_data()); + this->trigger(src.get_raw_data()); return false; } }; template class RawAction : public RemoteTransmitterActionBase { public: - void set_code_template(std::function(Ts...)> func) { this->code_func_ = func; } + void set_code_template(std::function func) { this->code_func_ = func; } void set_code_static(const int32_t *code, size_t len) { this->code_static_ = code; this->code_static_len_ = len; @@ -65,7 +65,7 @@ template class RawAction : public RemoteTransmitterActionBase(Ts...)> code_func_{}; + std::function code_func_{nullptr}; const int32_t *code_static_{nullptr}; int32_t code_static_len_{0}; }; diff --git a/esphome/components/remote_base/remote_base.cpp b/esphome/components/remote_base/remote_base.cpp index 97ee027b84..7fe5e47ee7 100644 --- a/esphome/components/remote_base/remote_base.cpp +++ b/esphome/components/remote_base/remote_base.cpp @@ -24,11 +24,105 @@ void RemoteRMTChannel::config_rmt(rmt_config_t &rmt) { } #endif +/* RemoteReceiveData */ + +bool RemoteReceiveData::peek_mark(uint32_t length, uint32_t offset) const { + if (!this->is_valid(offset)) + return false; + const int32_t value = this->peek(offset); + const int32_t lo = this->lower_bound_(length); + const int32_t hi = this->upper_bound_(length); + return value >= 0 && lo <= value && value <= hi; +} + +bool RemoteReceiveData::peek_space(uint32_t length, uint32_t offset) const { + if (!this->is_valid(offset)) + return false; + const int32_t value = this->peek(offset); + const int32_t lo = this->lower_bound_(length); + const int32_t hi = this->upper_bound_(length); + return value <= 0 && lo <= -value && -value <= hi; +} + +bool RemoteReceiveData::peek_space_at_least(uint32_t length, uint32_t offset) const { + if (!this->is_valid(offset)) + return false; + const int32_t value = this->peek(offset); + const int32_t lo = this->lower_bound_(length); + return value <= 0 && lo <= -value; +} + +bool RemoteReceiveData::expect_mark(uint32_t length) { + if (!this->peek_mark(length)) + return false; + this->advance(); + return true; +} + +bool RemoteReceiveData::expect_space(uint32_t length) { + if (!this->peek_space(length)) + return false; + this->advance(); + return true; +} + +bool RemoteReceiveData::expect_item(uint32_t mark, uint32_t space) { + if (!this->peek_item(mark, space)) + return false; + this->advance(2); + return true; +} + +bool RemoteReceiveData::expect_pulse_with_gap(uint32_t mark, uint32_t space) { + if (!this->peek_space_at_least(space, 1) || !this->peek_mark(mark)) + return false; + this->advance(2); + return true; +} + +/* RemoteReceiverBinarySensorBase */ + +bool RemoteReceiverBinarySensorBase::on_receive(RemoteReceiveData src) { + if (!this->matches(src)) + return false; + this->publish_state(true); + yield(); + this->publish_state(false); + return true; +} + +/* RemoteReceiverBase */ + +void RemoteReceiverBase::register_dumper(RemoteReceiverDumperBase *dumper) { + if (dumper->is_secondary()) { + this->secondary_dumpers_.push_back(dumper); + } else { + this->dumpers_.push_back(dumper); + } +} + +void RemoteReceiverBase::call_listeners_() { + for (auto *listener : this->listeners_) + listener->on_receive(RemoteReceiveData(this->temp_, this->tolerance_)); +} + +void RemoteReceiverBase::call_dumpers_() { + bool success = false; + for (auto *dumper : this->dumpers_) { + if (dumper->dump(RemoteReceiveData(this->temp_, this->tolerance_))) + success = true; + } + if (!success) { + for (auto *dumper : this->secondary_dumpers_) + dumper->dump(RemoteReceiveData(this->temp_, this->tolerance_)); + } +} + void RemoteReceiverBinarySensorBase::dump_config() { LOG_BINARY_SENSOR("", "Remote Receiver Binary Sensor", this); } void RemoteTransmitterBase::send_(uint32_t send_times, uint32_t send_wait) { #ifdef ESPHOME_LOG_HAS_VERY_VERBOSE - const std::vector &vec = this->temp_.get_data(); + const auto &vec = this->temp_.get_data(); char buffer[256]; uint32_t buffer_offset = 0; buffer_offset += sprintf(buffer, "Sending times=%u wait=%ums: ", send_times, send_wait); diff --git a/esphome/components/remote_base/remote_base.h b/esphome/components/remote_base/remote_base.h index fdb6d45e5f..a456007655 100644 --- a/esphome/components/remote_base/remote_base.h +++ b/esphome/components/remote_base/remote_base.h @@ -15,146 +15,65 @@ namespace esphome { namespace remote_base { +using RawTimings = std::vector; + class RemoteTransmitData { public: void mark(uint32_t length) { this->data_.push_back(length); } - void space(uint32_t length) { this->data_.push_back(-length); } - void item(uint32_t mark, uint32_t space) { this->mark(mark); this->space(space); } - void reserve(uint32_t len) { this->data_.reserve(len); } - void set_carrier_frequency(uint32_t carrier_frequency) { this->carrier_frequency_ = carrier_frequency; } - uint32_t get_carrier_frequency() const { return this->carrier_frequency_; } - - const std::vector &get_data() const { return this->data_; } - - void set_data(const std::vector &data) { - this->data_.clear(); - this->data_.reserve(data.size()); - for (auto dat : data) - this->data_.push_back(dat); - } - + const RawTimings &get_data() const { return this->data_; } + void set_data(const RawTimings &data) { this->data_ = data; } void reset() { this->data_.clear(); this->carrier_frequency_ = 0; } - std::vector::iterator begin() { return this->data_.begin(); } - - std::vector::iterator end() { return this->data_.end(); } - protected: - std::vector data_{}; + RawTimings data_{}; uint32_t carrier_frequency_{0}; }; class RemoteReceiveData { public: - RemoteReceiveData(std::vector *data, uint8_t tolerance) : data_(data), tolerance_(tolerance) {} + explicit RemoteReceiveData(const RawTimings &data, uint8_t tolerance) + : data_(data), index_(0), tolerance_(tolerance) {} - bool peek_mark(uint32_t length, uint32_t offset = 0) { - if (int32_t(this->index_ + offset) >= this->size()) - return false; - int32_t value = this->peek(offset); - const int32_t lo = this->lower_bound_(length); - const int32_t hi = this->upper_bound_(length); - return value >= 0 && lo <= value && value <= hi; + const RawTimings &get_raw_data() const { return this->data_; } + uint32_t get_index() const { return index_; } + int32_t operator[](uint32_t index) const { return this->data_[index]; } + int32_t size() const { return this->data_.size(); } + bool is_valid(uint32_t offset) const { return this->index_ + offset < this->data_.size(); } + int32_t peek(uint32_t offset = 0) const { return this->data_[this->index_ + offset]; } + bool peek_mark(uint32_t length, uint32_t offset = 0) const; + bool peek_space(uint32_t length, uint32_t offset = 0) const; + bool peek_space_at_least(uint32_t length, uint32_t offset = 0) const; + bool peek_item(uint32_t mark, uint32_t space, uint32_t offset = 0) const { + return this->peek_space(space, offset + 1) && this->peek_mark(mark, offset); } - bool peek_space(uint32_t length, uint32_t offset = 0) { - if (int32_t(this->index_ + offset) >= this->size()) - return false; - int32_t value = this->peek(offset); - const int32_t lo = this->lower_bound_(length); - const int32_t hi = this->upper_bound_(length); - return value <= 0 && lo <= -value && -value <= hi; - } - - bool peek_space_at_least(uint32_t length, uint32_t offset = 0) { - if (int32_t(this->index_ + offset) >= this->size()) - return false; - int32_t value = this->pos(this->index_ + offset); - const int32_t lo = this->lower_bound_(length); - return value <= 0 && lo <= -value; - } - - bool peek_item(uint32_t mark, uint32_t space, uint32_t offset = 0) { - return this->peek_mark(mark, offset) && this->peek_space(space, offset + 1); - } - - int32_t peek(uint32_t offset = 0) { return (*this)[this->index_ + offset]; } - + bool expect_mark(uint32_t length); + bool expect_space(uint32_t length); + bool expect_item(uint32_t mark, uint32_t space); + bool expect_pulse_with_gap(uint32_t mark, uint32_t space); void advance(uint32_t amount = 1) { this->index_ += amount; } - - bool expect_mark(uint32_t length) { - if (this->peek_mark(length)) { - this->advance(); - return true; - } - return false; - } - - bool expect_space(uint32_t length) { - if (this->peek_space(length)) { - this->advance(); - return true; - } - return false; - } - - bool expect_item(uint32_t mark, uint32_t space) { - if (this->peek_item(mark, space)) { - this->advance(2); - return true; - } - return false; - } - - bool expect_pulse_with_gap(uint32_t mark, uint32_t space) { - if (this->peek_mark(mark, 0) && this->peek_space_at_least(space, 1)) { - this->advance(2); - return true; - } - return false; - } - - uint32_t get_index() { return index_; } - void reset() { this->index_ = 0; } - int32_t pos(uint32_t index) const { return (*this->data_)[index]; } - - int32_t operator[](uint32_t index) const { return this->pos(index); } - - int32_t size() const { return this->data_->size(); } - - std::vector *get_raw_data() { return this->data_; } - protected: - int32_t lower_bound_(uint32_t length) { return int32_t(100 - this->tolerance_) * length / 100U; } - int32_t upper_bound_(uint32_t length) { return int32_t(100 + this->tolerance_) * length / 100U; } + int32_t lower_bound_(uint32_t length) const { return int32_t(100 - this->tolerance_) * length / 100U; } + int32_t upper_bound_(uint32_t length) const { return int32_t(100 + this->tolerance_) * length / 100U; } - uint32_t index_{0}; - std::vector *data_; + const RawTimings &data_; + uint32_t index_; uint8_t tolerance_; }; -template class RemoteProtocol { - public: - virtual void encode(RemoteTransmitData *dst, const T &data) = 0; - - virtual optional decode(RemoteReceiveData src) = 0; - - virtual void dump(const T &data) = 0; -}; - class RemoteComponentBase { public: explicit RemoteComponentBase(InternalGPIOPin *pin) : pin_(pin){}; @@ -196,7 +115,6 @@ class RemoteTransmitterBase : public RemoteComponentBase { RemoteTransmitData *get_data() { return &this->parent_->temp_; } void set_send_times(uint32_t send_times) { send_times_ = send_times; } void set_send_wait(uint32_t send_wait) { send_wait_ = send_wait; } - void perform() { this->parent_->send_(this->send_times_, this->send_wait_); } protected: @@ -234,51 +152,22 @@ class RemoteReceiverBase : public RemoteComponentBase { public: RemoteReceiverBase(InternalGPIOPin *pin) : RemoteComponentBase(pin) {} void register_listener(RemoteReceiverListener *listener) { this->listeners_.push_back(listener); } - void register_dumper(RemoteReceiverDumperBase *dumper) { - if (dumper->is_secondary()) { - this->secondary_dumpers_.push_back(dumper); - } else { - this->dumpers_.push_back(dumper); - } - } + void register_dumper(RemoteReceiverDumperBase *dumper); void set_tolerance(uint8_t tolerance) { tolerance_ = tolerance; } protected: - bool call_listeners_() { - bool success = false; - for (auto *listener : this->listeners_) { - auto data = RemoteReceiveData(&this->temp_, this->tolerance_); - if (listener->on_receive(data)) - success = true; - } - return success; - } - void call_dumpers_() { - bool success = false; - for (auto *dumper : this->dumpers_) { - auto data = RemoteReceiveData(&this->temp_, this->tolerance_); - if (dumper->dump(data)) - success = true; - } - if (!success) { - for (auto *dumper : this->secondary_dumpers_) { - auto data = RemoteReceiveData(&this->temp_, this->tolerance_); - dumper->dump(data); - } - } - } + void call_listeners_(); + void call_dumpers_(); void call_listeners_dumpers_() { - if (this->call_listeners_()) - return; - // If a listener handled, then do not dump + this->call_listeners_(); this->call_dumpers_(); } std::vector listeners_; std::vector dumpers_; std::vector secondary_dumpers_; - std::vector temp_; - uint8_t tolerance_{25}; + RawTimings temp_; + uint8_t tolerance_; }; class RemoteReceiverBinarySensorBase : public binary_sensor::BinarySensorInitiallyOff, @@ -288,15 +177,16 @@ class RemoteReceiverBinarySensorBase : public binary_sensor::BinarySensorInitial explicit RemoteReceiverBinarySensorBase() {} void dump_config() override; virtual bool matches(RemoteReceiveData src) = 0; - bool on_receive(RemoteReceiveData src) override { - if (this->matches(src)) { - this->publish_state(true); - yield(); - this->publish_state(false); - return true; - } - return false; - } + bool on_receive(RemoteReceiveData src) override; +}; + +/* TEMPLATES */ + +template class RemoteProtocol { + public: + virtual void encode(RemoteTransmitData *dst, const T &data) = 0; + virtual optional decode(RemoteReceiveData src) = 0; + virtual void dump(const T &data) = 0; }; template class RemoteReceiverBinarySensor : public RemoteReceiverBinarySensorBase { diff --git a/esphome/components/remote_transmitter/remote_transmitter.h b/esphome/components/remote_transmitter/remote_transmitter.h index 560d83802e..a20df0cc62 100644 --- a/esphome/components/remote_transmitter/remote_transmitter.h +++ b/esphome/components/remote_transmitter/remote_transmitter.h @@ -48,7 +48,7 @@ class RemoteTransmitterComponent : public remote_base::RemoteTransmitterBase, esp_err_t error_code_{ESP_OK}; bool inverted_{false}; #endif - uint8_t carrier_duty_percent_{50}; + uint8_t carrier_duty_percent_; }; } // namespace remote_transmitter From cd46a69f2cf4ab8eb6560758a6d71d1a95ec3d45 Mon Sep 17 00:00:00 2001 From: Mat931 <49403702+Mat931@users.noreply.github.com> Date: Sun, 30 Jul 2023 21:09:09 +0000 Subject: [PATCH 34/84] Add 'map_linear' and 'clamp' sensor filters (#5040) --- esphome/components/sensor/__init__.py | 106 ++++++++++++++++++++++---- esphome/components/sensor/filter.cpp | 20 ++++- esphome/components/sensor/filter.h | 16 +++- tests/test1.yaml | 10 ++- tests/test3.1.yaml | 8 +- 5 files changed, 137 insertions(+), 23 deletions(-) diff --git a/esphome/components/sensor/__init__.py b/esphome/components/sensor/__init__.py index 2aebf7bb17..bbcc730943 100644 --- a/esphome/components/sensor/__init__.py +++ b/esphome/components/sensor/__init__.py @@ -31,6 +31,9 @@ from esphome.const import ( CONF_MQTT_ID, CONF_FORCE_UPDATE, CONF_VALUE, + CONF_MIN_VALUE, + CONF_MAX_VALUE, + CONF_METHOD, DEVICE_CLASS_APPARENT_POWER, DEVICE_CLASS_AQI, DEVICE_CLASS_ATMOSPHERIC_PRESSURE, @@ -227,6 +230,7 @@ OrFilter = sensor_ns.class_("OrFilter", Filter) CalibrateLinearFilter = sensor_ns.class_("CalibrateLinearFilter", Filter) CalibratePolynomialFilter = sensor_ns.class_("CalibratePolynomialFilter", Filter) SensorInRangeCondition = sensor_ns.class_("SensorInRangeCondition", Filter) +ClampFilter = sensor_ns.class_("ClampFilter", Filter) validate_unit_of_measurement = cv.string_strict validate_accuracy_decimals = cv.int_ @@ -557,30 +561,60 @@ async def debounce_filter_to_code(config, filter_id): return var -def validate_not_all_from_same(config): - if all(conf[CONF_FROM] == config[0][CONF_FROM] for conf in config): - raise cv.Invalid( - "The 'from' values of the calibrate_linear filter cannot all point " - "to the same value! Please add more values to the filter." - ) +CONF_DATAPOINTS = "datapoints" + + +def validate_calibrate_linear(config): + datapoints = config[CONF_DATAPOINTS] + if config[CONF_METHOD] == "exact": + for i in range(len(datapoints) - 1): + if datapoints[i][CONF_FROM] > datapoints[i + 1][CONF_FROM]: + raise cv.Invalid( + "The 'from' values of the calibrate_linear filter must be sorted in ascending order." + ) + for i in range(len(datapoints) - 1): + if datapoints[i][CONF_FROM] == datapoints[i + 1][CONF_FROM]: + raise cv.Invalid( + "The 'from' values of the calibrate_linear filter must not contain duplicates." + ) + elif config[CONF_METHOD] == "least_squares": + if all(conf[CONF_FROM] == datapoints[0][CONF_FROM] for conf in datapoints): + raise cv.Invalid( + "The 'from' values of the calibrate_linear filter cannot all point " + "to the same value! Please add more values to the filter." + ) return config @FILTER_REGISTRY.register( "calibrate_linear", CalibrateLinearFilter, - cv.All( - cv.ensure_list(validate_datapoint), cv.Length(min=2), validate_not_all_from_same + cv.maybe_simple_value( + { + cv.Required(CONF_DATAPOINTS): cv.All( + cv.ensure_list(validate_datapoint), cv.Length(min=2) + ), + cv.Optional(CONF_METHOD, default="least_squares"): cv.one_of( + "least_squares", "exact", lower=True + ), + }, + validate_calibrate_linear, + key=CONF_DATAPOINTS, ), ) async def calibrate_linear_filter_to_code(config, filter_id): - x = [conf[CONF_FROM] for conf in config] - y = [conf[CONF_TO] for conf in config] - k, b = fit_linear(x, y) - return cg.new_Pvariable(filter_id, k, b) + x = [conf[CONF_FROM] for conf in config[CONF_DATAPOINTS]] + y = [conf[CONF_TO] for conf in config[CONF_DATAPOINTS]] + + linear_functions = [] + if config[CONF_METHOD] == "least_squares": + k, b = fit_linear(x, y) + linear_functions = [[k, b, float("NaN")]] + elif config[CONF_METHOD] == "exact": + linear_functions = map_linear(x, y) + return cg.new_Pvariable(filter_id, linear_functions) -CONF_DATAPOINTS = "datapoints" CONF_DEGREE = "degree" @@ -619,6 +653,36 @@ async def calibrate_polynomial_filter_to_code(config, filter_id): return cg.new_Pvariable(filter_id, res) +def validate_clamp(config): + if not math.isfinite(config[CONF_MIN_VALUE]) and not math.isfinite( + config[CONF_MAX_VALUE] + ): + raise cv.Invalid("Either 'min_value' or 'max_value' must be set to a number.") + if config[CONF_MIN_VALUE] > config[CONF_MAX_VALUE]: + raise cv.Invalid("The 'min_value' must not be larger than the 'max_value'.") + return config + + +CLAMP_SCHEMA = cv.All( + cv.Schema( + { + cv.Optional(CONF_MIN_VALUE, default="NaN"): cv.float_, + cv.Optional(CONF_MAX_VALUE, default="NaN"): cv.float_, + } + ), + validate_clamp, +) + + +@FILTER_REGISTRY.register("clamp", ClampFilter, CLAMP_SCHEMA) +async def clamp_filter_to_code(config, filter_id): + return cg.new_Pvariable( + filter_id, + config[CONF_MIN_VALUE], + config[CONF_MAX_VALUE], + ) + + async def build_filters(config): return await cg.build_registry_list(FILTER_REGISTRY, config) @@ -730,6 +794,22 @@ def fit_linear(x, y): return k, b +def map_linear(x, y): + assert len(x) == len(y) + f = [] + for i in range(len(x) - 1): + slope = (y[i + 1] - y[i]) / (x[i + 1] - x[i]) + bias = y[i] - (slope * x[i]) + next_x = x[i + 1] + if i == len(x) - 2: + next_x = float("NaN") + if f and f[-1][0] == slope and f[-1][1] == bias: + f[-1][2] = next_x + else: + f.append([slope, bias, next_x]) + return f + + def _mat_copy(m): return [list(row) for row in m] diff --git a/esphome/components/sensor/filter.cpp b/esphome/components/sensor/filter.cpp index ccefa556b6..cd5ab5f9cd 100644 --- a/esphome/components/sensor/filter.cpp +++ b/esphome/components/sensor/filter.cpp @@ -416,8 +416,13 @@ void HeartbeatFilter::setup() { } float HeartbeatFilter::get_setup_priority() const { return setup_priority::HARDWARE; } -optional CalibrateLinearFilter::new_value(float value) { return value * this->slope_ + this->bias_; } -CalibrateLinearFilter::CalibrateLinearFilter(float slope, float bias) : slope_(slope), bias_(bias) {} +optional CalibrateLinearFilter::new_value(float value) { + for (std::array f : this->linear_functions_) { + if (!std::isfinite(f[2]) || value < f[2]) + return (value * f[0]) + f[1]; + } + return NAN; +} optional CalibratePolynomialFilter::new_value(float value) { float res = 0.0f; @@ -429,5 +434,16 @@ optional CalibratePolynomialFilter::new_value(float value) { return res; } +ClampFilter::ClampFilter(float min, float max) : min_(min), max_(max) {} +optional ClampFilter::new_value(float value) { + if (std::isfinite(value)) { + if (std::isfinite(this->min_) && value < this->min_) + return this->min_; + if (std::isfinite(this->max_) && value > this->max_) + return this->max_; + } + return value; +} + } // namespace sensor } // namespace esphome diff --git a/esphome/components/sensor/filter.h b/esphome/components/sensor/filter.h index 296990f34f..0141b73267 100644 --- a/esphome/components/sensor/filter.h +++ b/esphome/components/sensor/filter.h @@ -390,12 +390,12 @@ class OrFilter : public Filter { class CalibrateLinearFilter : public Filter { public: - CalibrateLinearFilter(float slope, float bias); + CalibrateLinearFilter(std::vector> linear_functions) + : linear_functions_(std::move(linear_functions)) {} optional new_value(float value) override; protected: - float slope_; - float bias_; + std::vector> linear_functions_; }; class CalibratePolynomialFilter : public Filter { @@ -407,5 +407,15 @@ class CalibratePolynomialFilter : public Filter { std::vector coefficients_; }; +class ClampFilter : public Filter { + public: + ClampFilter(float min, float max); + optional new_value(float value) override; + + protected: + float min_{NAN}; + float max_{NAN}; +}; + } // namespace sensor } // namespace esphome diff --git a/tests/test1.yaml b/tests/test1.yaml index 90c62c6b73..a00b886ac1 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -379,9 +379,13 @@ sensor: - offset: 2.0 - multiply: 1.2 - calibrate_linear: - - 0.0 -> 0.0 - - 40.0 -> 45.0 - - 100.0 -> 102.5 + datapoints: + - 0.0 -> 0.0 + - 40.0 -> 45.0 + - 100.0 -> 102.5 + - clamp: + min_value: -100 + max_value: 100 - filter_out: 42.0 - filter_out: nan - median: diff --git a/tests/test3.1.yaml b/tests/test3.1.yaml index 104f4bbda8..42c1e1e1ab 100644 --- a/tests/test3.1.yaml +++ b/tests/test3.1.yaml @@ -88,8 +88,12 @@ sensor: - debounce: 500s - timeout: 10min - calibrate_linear: - - 0 -> 0 - - 100 -> 100 + method: exact + datapoints: + - -1 -> 3 + - 0.0 -> 1.0 + - 1.0 -> 2.0 + - 2.0 -> 3.0 - calibrate_polynomial: degree: 3 datapoints: From 08a41d9bd6102b9632a90ea84e3957dcb8e05b6f Mon Sep 17 00:00:00 2001 From: mullerdavid Date: Sun, 30 Jul 2023 23:10:46 +0200 Subject: [PATCH 35/84] Adding Inkplate 6 v2 model variant (#5165) --- esphome/components/inkplate6/display.py | 1 + esphome/components/inkplate6/inkplate.cpp | 66 ++++++++++++++++------- esphome/components/inkplate6/inkplate.h | 48 +++++++++++++---- 3 files changed, 86 insertions(+), 29 deletions(-) diff --git a/esphome/components/inkplate6/display.py b/esphome/components/inkplate6/display.py index 7534731175..f05169ea2e 100644 --- a/esphome/components/inkplate6/display.py +++ b/esphome/components/inkplate6/display.py @@ -48,6 +48,7 @@ MODELS = { "inkplate_6": InkplateModel.INKPLATE_6, "inkplate_10": InkplateModel.INKPLATE_10, "inkplate_6_plus": InkplateModel.INKPLATE_6_PLUS, + "inkplate_6_v2": InkplateModel.INKPLATE_6_V2, } CONFIG_SCHEMA = cv.All( diff --git a/esphome/components/inkplate6/inkplate.cpp b/esphome/components/inkplate6/inkplate.cpp index e6fb9b773c..92a226de87 100644 --- a/esphome/components/inkplate6/inkplate.cpp +++ b/esphome/components/inkplate6/inkplate.cpp @@ -69,9 +69,9 @@ void Inkplate6::initialize_() { if (this->buffer_ != nullptr) allocator.deallocate(this->buffer_, buffer_size); if (this->glut_ != nullptr) - allocator32.deallocate(this->glut_, 256 * (this->model_ == INKPLATE_6_PLUS ? 9 : 8)); + allocator32.deallocate(this->glut_, 256 * 9); if (this->glut2_ != nullptr) - allocator32.deallocate(this->glut2_, 256 * (this->model_ == INKPLATE_6_PLUS ? 9 : 8)); + allocator32.deallocate(this->glut2_, 256 * 9); this->buffer_ = allocator.allocate(buffer_size); if (this->buffer_ == nullptr) { @@ -80,7 +80,7 @@ void Inkplate6::initialize_() { return; } if (this->greyscale_) { - uint8_t glut_size = (this->model_ == INKPLATE_6_PLUS ? 9 : 8); + uint8_t glut_size = 9; this->glut_ = allocator32.allocate(256 * glut_size); if (this->glut_ == nullptr) { @@ -95,12 +95,14 @@ void Inkplate6::initialize_() { return; } + const auto *const waveform3_bit = waveform3BitAll[this->model_]; + for (int i = 0; i < glut_size; i++) { for (uint32_t j = 0; j < 256; j++) { - uint8_t z = (waveform3Bit[j & 0x07][i] << 2) | (waveform3Bit[(j >> 4) & 0x07][i]); + uint8_t z = (waveform3_bit[j & 0x07][i] << 2) | (waveform3_bit[(j >> 4) & 0x07][i]); this->glut_[i * 256 + j] = ((z & 0b00000011) << 4) | (((z & 0b00001100) >> 2) << 18) | (((z & 0b00010000) >> 4) << 23) | (((z & 0b11100000) >> 5) << 25); - z = ((waveform3Bit[j & 0x07][i] << 2) | (waveform3Bit[(j >> 4) & 0x07][i])) << 4; + z = ((waveform3_bit[j & 0x07][i] << 2) | (waveform3_bit[(j >> 4) & 0x07][i])) << 4; this->glut2_[i * 256 + j] = ((z & 0b00000011) << 4) | (((z & 0b00001100) >> 2) << 18) | (((z & 0b00010000) >> 4) << 23) | (((z & 0b11100000) >> 5) << 25); } @@ -339,13 +341,16 @@ void Inkplate6::display1b_() { clean_fast_(1, 21); clean_fast_(2, 1); clean_fast_(0, 12); + clean_fast_(2, 1); } uint32_t clock = (1 << this->cl_pin_->get_pin()); uint32_t data_mask = this->get_data_pin_mask_(); ESP_LOGV(TAG, "Display1b start loops (%ums)", millis() - start_time); - for (int k = 0; k < 4; k++) { + int rep = (this->model_ == INKPLATE_6_V2) ? 5 : 4; + + for (int k = 0; k < rep; k++) { buffer_ptr = &this->buffer_[this->get_buffer_length_() - 1]; vscan_start_(); for (int i = 0, im = this->get_height_internal(); i < im; i++) { @@ -365,8 +370,11 @@ void Inkplate6::display1b_() { GPIO.out_w1ts = this->pin_lut_[data] | clock; GPIO.out_w1tc = data_mask | clock; } - GPIO.out_w1ts = clock; - GPIO.out_w1tc = data_mask | clock; + // New Inkplate6 panel doesn't need last clock + if (this->model_ != INKPLATE_6_V2) { + GPIO.out_w1ts = clock; + GPIO.out_w1tc = data_mask | clock; + } vscan_end_(); } delayMicroseconds(230); @@ -392,8 +400,11 @@ void Inkplate6::display1b_() { GPIO.out_w1ts = this->pin_lut_[data] | clock; GPIO.out_w1tc = data_mask | clock; } - GPIO.out_w1ts = clock; - GPIO.out_w1tc = data_mask | clock; + // New Inkplate6 panel doesn't need last clock + if (this->model_ != INKPLATE_6_V2) { + GPIO.out_w1ts = clock; + GPIO.out_w1tc = data_mask | clock; + } vscan_end_(); } delayMicroseconds(230); @@ -415,8 +426,11 @@ void Inkplate6::display1b_() { GPIO.out_w1ts = send | clock; GPIO.out_w1tc = data_mask | clock; } - GPIO.out_w1ts = send | clock; - GPIO.out_w1tc = data_mask | clock; + // New Inkplate6 panel doesn't need last clock + if (this->model_ != INKPLATE_6_V2) { + GPIO.out_w1ts = clock; + GPIO.out_w1tc = data_mask | clock; + } vscan_end_(); } delayMicroseconds(230); @@ -450,13 +464,14 @@ void Inkplate6::display3b_() { clean_fast_(1, 21); clean_fast_(2, 1); clean_fast_(0, 12); + clean_fast_(2, 1); } uint32_t clock = (1 << this->cl_pin_->get_pin()); uint32_t data_mask = this->get_data_pin_mask_(); uint32_t pos; uint32_t data; - uint8_t glut_size = this->model_ == INKPLATE_6_PLUS ? 9 : 8; + uint8_t glut_size = 9; for (int k = 0; k < glut_size; k++) { pos = this->get_buffer_length_(); vscan_start_(); @@ -479,8 +494,11 @@ void Inkplate6::display3b_() { GPIO.out_w1ts = data | clock; GPIO.out_w1tc = data_mask | clock; } - GPIO.out_w1ts = clock; - GPIO.out_w1tc = data_mask | clock; + // New Inkplate6 panel doesn't need last clock + if (this->model_ != INKPLATE_6_V2) { + GPIO.out_w1ts = clock; + GPIO.out_w1tc = data_mask | clock; + } vscan_end_(); } delayMicroseconds(230); @@ -517,10 +535,12 @@ bool Inkplate6::partial_update_() { } ESP_LOGV(TAG, "Partial update buffer built after (%ums)", millis() - start_time); + int rep = (this->model_ == INKPLATE_6_V2) ? 6 : 5; + eink_on_(); uint32_t clock = (1 << this->cl_pin_->get_pin()); uint32_t data_mask = this->get_data_pin_mask_(); - for (int k = 0; k < 5; k++) { + for (int k = 0; k < rep; k++) { vscan_start_(); const uint8_t *data_ptr = &this->partial_buffer_2_[(this->get_buffer_length_() * 2) - 1]; for (int i = 0; i < this->get_height_internal(); i++) { @@ -531,8 +551,11 @@ bool Inkplate6::partial_update_() { GPIO.out_w1ts = this->pin_lut_[data] | clock; GPIO.out_w1tc = data_mask | clock; } - GPIO.out_w1ts = clock; - GPIO.out_w1tc = data_mask | clock; + // New Inkplate6 panel doesn't need last clock + if (this->model_ != INKPLATE_6_V2) { + GPIO.out_w1ts = clock; + GPIO.out_w1tc = data_mask | clock; + } vscan_end_(); } delayMicroseconds(230); @@ -634,8 +657,11 @@ void Inkplate6::clean_fast_(uint8_t c, uint8_t rep) { GPIO.out_w1ts = clock; GPIO.out_w1tc = clock; } - GPIO.out_w1ts = send | clock; - GPIO.out_w1tc = clock; + // New Inkplate6 panel doesn't need last clock + if (this->model_ != INKPLATE_6_V2) { + GPIO.out_w1ts = send | clock; + GPIO.out_w1tc = clock; + } vscan_end_(); } delayMicroseconds(230); diff --git a/esphome/components/inkplate6/inkplate.h b/esphome/components/inkplate6/inkplate.h index e650b57631..565bd74710 100644 --- a/esphome/components/inkplate6/inkplate.h +++ b/esphome/components/inkplate6/inkplate.h @@ -14,6 +14,7 @@ enum InkplateModel : uint8_t { INKPLATE_6 = 0, INKPLATE_10 = 1, INKPLATE_6_PLUS = 2, + INKPLATE_6_V2 = 3, }; class Inkplate6 : public PollingComponent, public display::DisplayBuffer, public i2c::I2CDevice { @@ -28,13 +29,42 @@ class Inkplate6 : public PollingComponent, public display::DisplayBuffer, public const uint8_t pixelMaskLUT[8] = {0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80}; const uint8_t pixelMaskGLUT[2] = {0x0F, 0xF0}; - const uint8_t waveform3Bit[8][8] = {{0, 1, 1, 0, 0, 1, 1, 0}, {0, 1, 2, 1, 1, 2, 1, 0}, {1, 1, 1, 2, 2, 1, 0, 0}, - {0, 0, 0, 1, 1, 1, 2, 0}, {2, 1, 1, 1, 2, 1, 2, 0}, {2, 2, 1, 1, 2, 1, 2, 0}, - {1, 1, 1, 2, 1, 2, 2, 0}, {0, 0, 0, 0, 0, 0, 2, 0}}; - const uint8_t waveform3Bit6Plus[8][9] = {{0, 0, 0, 0, 0, 2, 1, 1, 0}, {0, 0, 2, 1, 1, 1, 2, 1, 0}, - {0, 2, 2, 2, 1, 1, 2, 1, 0}, {0, 0, 2, 2, 2, 1, 2, 1, 0}, - {0, 0, 0, 0, 2, 2, 2, 1, 0}, {0, 0, 2, 1, 2, 1, 1, 2, 0}, - {0, 0, 2, 2, 2, 1, 1, 2, 0}, {0, 0, 0, 0, 2, 2, 2, 2, 0}}; + const uint8_t waveform3BitAll[4][8][9] = {// INKPLATE_6 + {{0, 1, 1, 0, 0, 1, 1, 0, 0}, + {0, 1, 2, 1, 1, 2, 1, 0, 0}, + {1, 1, 1, 2, 2, 1, 0, 0, 0}, + {0, 0, 0, 1, 1, 1, 2, 0, 0}, + {2, 1, 1, 1, 2, 1, 2, 0, 0}, + {2, 2, 1, 1, 2, 1, 2, 0, 0}, + {1, 1, 1, 2, 1, 2, 2, 0, 0}, + {0, 0, 0, 0, 0, 0, 2, 0, 0}}, + // INKPLATE_10 + {{0, 0, 0, 0, 0, 0, 0, 1, 0}, + {0, 0, 0, 2, 2, 2, 1, 1, 0}, + {0, 0, 2, 1, 1, 2, 2, 1, 0}, + {0, 1, 2, 2, 1, 2, 2, 1, 0}, + {0, 0, 2, 1, 2, 2, 2, 1, 0}, + {0, 2, 2, 2, 2, 2, 2, 1, 0}, + {0, 0, 0, 0, 0, 2, 1, 2, 0}, + {0, 0, 0, 2, 2, 2, 2, 2, 0}}, + // INKPLATE_6_PLUS + {{0, 0, 0, 0, 0, 2, 1, 1, 0}, + {0, 0, 2, 1, 1, 1, 2, 1, 0}, + {0, 2, 2, 2, 1, 1, 2, 1, 0}, + {0, 0, 2, 2, 2, 1, 2, 1, 0}, + {0, 0, 0, 0, 2, 2, 2, 1, 0}, + {0, 0, 2, 1, 2, 1, 1, 2, 0}, + {0, 0, 2, 2, 2, 1, 1, 2, 0}, + {0, 0, 0, 0, 2, 2, 2, 2, 0}}, + // INKPLATE_6_V2 + {{1, 0, 1, 0, 1, 1, 1, 0, 0}, + {0, 0, 0, 1, 1, 1, 1, 0, 0}, + {1, 1, 1, 1, 0, 2, 1, 0, 0}, + {1, 1, 1, 2, 2, 1, 1, 0, 0}, + {1, 1, 1, 1, 2, 2, 1, 0, 0}, + {0, 1, 1, 1, 2, 2, 1, 0, 0}, + {0, 0, 0, 0, 1, 1, 2, 0, 0}, + {0, 0, 0, 0, 0, 1, 2, 0, 0}}}; void set_greyscale(bool greyscale) { this->greyscale_ = greyscale; @@ -111,7 +141,7 @@ class Inkplate6 : public PollingComponent, public display::DisplayBuffer, public void pins_as_outputs_(); int get_width_internal() override { - if (this->model_ == INKPLATE_6) { + if (this->model_ == INKPLATE_6 || this->model_ == INKPLATE_6_V2) { return 800; } else if (this->model_ == INKPLATE_10) { return 1200; @@ -122,7 +152,7 @@ class Inkplate6 : public PollingComponent, public display::DisplayBuffer, public } int get_height_internal() override { - if (this->model_ == INKPLATE_6) { + if (this->model_ == INKPLATE_6 || this->model_ == INKPLATE_6_V2) { return 600; } else if (this->model_ == INKPLATE_10) { return 825; From 56630bb7177bb67570291c8731ca8c87c22236ef Mon Sep 17 00:00:00 2001 From: Keith Burzinski Date: Sun, 30 Jul 2023 16:19:06 -0500 Subject: [PATCH 36/84] Swap ADC back to use 'int' because C3 (#5151) --- esphome/components/adc/adc_sensor.cpp | 10 +++++----- esphome/components/adc/adc_sensor.h | 2 +- tests/test7.yaml | 8 ++++++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/esphome/components/adc/adc_sensor.cpp b/esphome/components/adc/adc_sensor.cpp index bb6a7a8c85..665ecfd6b5 100644 --- a/esphome/components/adc/adc_sensor.cpp +++ b/esphome/components/adc/adc_sensor.cpp @@ -32,8 +32,8 @@ static const int32_t SOC_ADC_RTC_MAX_BITWIDTH = 12; #endif #endif -static const int32_t ADC_MAX = (1 << SOC_ADC_RTC_MAX_BITWIDTH) - 1; // 4095 (12 bit) or 8191 (13 bit) -static const int32_t ADC_HALF = (1 << SOC_ADC_RTC_MAX_BITWIDTH) >> 1; // 2048 (12 bit) or 4096 (13 bit) +static const int ADC_MAX = (1 << SOC_ADC_RTC_MAX_BITWIDTH) - 1; // 4095 (12 bit) or 8191 (13 bit) +static const int ADC_HALF = (1 << SOC_ADC_RTC_MAX_BITWIDTH) >> 1; // 2048 (12 bit) or 4096 (13 bit) #endif #ifdef USE_RP2040 @@ -59,7 +59,7 @@ extern "C" } // load characteristics for each attenuation - for (int32_t i = 0; i < (int32_t) ADC_ATTEN_MAX; i++) { + for (int32_t i = 0; i <= ADC_ATTEN_DB_11; i++) { auto adc_unit = channel1_ != ADC1_CHANNEL_MAX ? ADC_UNIT_1 : ADC_UNIT_2; auto cal_value = esp_adc_cal_characterize(adc_unit, (adc_atten_t) i, ADC_WIDTH_MAX_SOC_BITS, 1100, // default vref @@ -157,7 +157,7 @@ float ADCSensor::sample() { #ifdef USE_ESP32 float ADCSensor::sample() { if (!autorange_) { - int32_t raw = -1; + int raw = -1; if (channel1_ != ADC1_CHANNEL_MAX) { raw = adc1_get_raw(channel1_); } else if (channel2_ != ADC2_CHANNEL_MAX) { @@ -174,7 +174,7 @@ float ADCSensor::sample() { return mv / 1000.0f; } - int32_t raw11 = ADC_MAX, raw6 = ADC_MAX, raw2 = ADC_MAX, raw0 = ADC_MAX; + int raw11 = ADC_MAX, raw6 = ADC_MAX, raw2 = ADC_MAX, raw0 = ADC_MAX; if (channel1_ != ADC1_CHANNEL_MAX) { adc1_config_channel_atten(channel1_, ADC_ATTEN_DB_11); diff --git a/esphome/components/adc/adc_sensor.h b/esphome/components/adc/adc_sensor.h index a905177790..7d9c8959da 100644 --- a/esphome/components/adc/adc_sensor.h +++ b/esphome/components/adc/adc_sensor.h @@ -62,7 +62,7 @@ class ADCSensor : public sensor::Sensor, public PollingComponent, public voltage adc1_channel_t channel1_{ADC1_CHANNEL_MAX}; adc2_channel_t channel2_{ADC2_CHANNEL_MAX}; bool autorange_{false}; - esp_adc_cal_characteristics_t cal_characteristics_[(int32_t) ADC_ATTEN_MAX] = {}; + esp_adc_cal_characteristics_t cal_characteristics_[ADC_ATTEN_MAX] = {}; #endif }; diff --git a/tests/test7.yaml b/tests/test7.yaml index 10e1b035ab..8d48c9a601 100644 --- a/tests/test7.yaml +++ b/tests/test7.yaml @@ -31,3 +31,11 @@ logger: http_request: useragent: esphome/tagreader timeout: 10s + +sensor: + - platform: adc + id: adc_sensor_p4 + name: ADC pin 4 + pin: 4 + attenuation: 11db + update_interval: 1s From fdb20e4a30d45323c94427bc7144f2c115d39727 Mon Sep 17 00:00:00 2001 From: Stijn Tintel Date: Mon, 31 Jul 2023 00:23:30 +0300 Subject: [PATCH 37/84] wifi: handle WIFI_REASON_ROAMING reason in event (#5153) --- esphome/components/wifi/wifi_component_esp_idf.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/esphome/components/wifi/wifi_component_esp_idf.cpp b/esphome/components/wifi/wifi_component_esp_idf.cpp index a2bbc8ae09..e9d74116cf 100644 --- a/esphome/components/wifi/wifi_component_esp_idf.cpp +++ b/esphome/components/wifi/wifi_component_esp_idf.cpp @@ -569,6 +569,8 @@ const char *get_disconnect_reason_str(uint8_t reason) { return "Handshake Failed"; case WIFI_REASON_CONNECTION_FAIL: return "Connection Failed"; + case WIFI_REASON_ROAMING: + return "Station Roaming"; case WIFI_REASON_UNSPECIFIED: default: return "Unspecified"; @@ -631,7 +633,9 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) { if (it.reason == WIFI_REASON_NO_AP_FOUND) { ESP_LOGW(TAG, "Event: Disconnected ssid='%s' reason='Probe Request Unsuccessful'", buf); s_sta_connect_not_found = true; - + } else if (it.reason == WIFI_REASON_ROAMING) { + ESP_LOGI(TAG, "Event: Disconnected ssid='%s' reason='Station Roaming'", buf); + return; } else { ESP_LOGW(TAG, "Event: Disconnected ssid='%s' bssid=" LOG_SECRET("%s") " reason='%s'", buf, format_mac_addr(it.bssid).c_str(), get_disconnect_reason_str(it.reason)); From 9ff0471274404bb5dfa549279dd90224c7e13641 Mon Sep 17 00:00:00 2001 From: Sergey Dudanov Date: Mon, 31 Jul 2023 01:30:11 +0400 Subject: [PATCH 38/84] duty_time: fix build without binary_sensor. Parented in automations. (#5156) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- .../components/duty_time/duty_time_sensor.cpp | 2 + .../components/duty_time/duty_time_sensor.h | 40 ++++++------------- esphome/components/duty_time/sensor.py | 28 ++++++++----- 3 files changed, 32 insertions(+), 38 deletions(-) diff --git a/esphome/components/duty_time/duty_time_sensor.cpp b/esphome/components/duty_time/duty_time_sensor.cpp index 045cbcceac..1101c4d41e 100644 --- a/esphome/components/duty_time/duty_time_sensor.cpp +++ b/esphome/components/duty_time/duty_time_sensor.cpp @@ -6,9 +6,11 @@ namespace duty_time_sensor { static const char *const TAG = "duty_time_sensor"; +#ifdef USE_BINARY_SENSOR void DutyTimeSensor::set_sensor(binary_sensor::BinarySensor *const sensor) { sensor->add_on_state_callback([this](bool state) { this->process_state_(state); }); } +#endif void DutyTimeSensor::start() { if (!this->last_state_) diff --git a/esphome/components/duty_time/duty_time_sensor.h b/esphome/components/duty_time/duty_time_sensor.h index 27fa383847..1ec2f7b94f 100644 --- a/esphome/components/duty_time/duty_time_sensor.h +++ b/esphome/components/duty_time/duty_time_sensor.h @@ -3,8 +3,10 @@ #include "esphome/core/automation.h" #include "esphome/core/component.h" #include "esphome/core/preferences.h" -#include "esphome/components/binary_sensor/binary_sensor.h" #include "esphome/components/sensor/sensor.h" +#ifdef USE_BINARY_SENSOR +#include "esphome/components/binary_sensor/binary_sensor.h" +#endif namespace esphome { namespace duty_time_sensor { @@ -22,8 +24,10 @@ class DutyTimeSensor : public sensor::Sensor, public PollingComponent { bool is_running() const { return this->last_state_; } void reset() { this->set_value_(0); } - void set_lambda(std::function &&func) { this->func_ = func; } +#ifdef USE_BINARY_SENSOR void set_sensor(binary_sensor::BinarySensor *sensor); +#endif + void set_lambda(std::function &&func) { this->func_ = func; } void set_last_duty_time_sensor(sensor::Sensor *sensor) { this->last_duty_time_sensor_ = sensor; } void set_restore(bool restore) { this->restore_ = restore; } @@ -43,44 +47,26 @@ class DutyTimeSensor : public sensor::Sensor, public PollingComponent { bool restore_; }; -template class StartAction : public Action { - public: - explicit StartAction(DutyTimeSensor *parent) : parent_(parent) {} +template class BaseAction : public Action, public Parented {}; +template class StartAction : public BaseAction { void play(Ts... x) override { this->parent_->start(); } - - protected: - DutyTimeSensor *parent_; }; -template class StopAction : public Action { - public: - explicit StopAction(DutyTimeSensor *parent) : parent_(parent) {} - +template class StopAction : public BaseAction { void play(Ts... x) override { this->parent_->stop(); } - - protected: - DutyTimeSensor *parent_; }; -template class ResetAction : public Action { - public: - explicit ResetAction(DutyTimeSensor *parent) : parent_(parent) {} - +template class ResetAction : public BaseAction { void play(Ts... x) override { this->parent_->reset(); } - - protected: - DutyTimeSensor *parent_; }; -template class RunningCondition : public Condition { +template class RunningCondition : public Condition, public Parented { public: - explicit RunningCondition(DutyTimeSensor *parent, bool state) : parent_(parent), state_(state) {} - - bool check(Ts... x) override { return this->parent_->is_running() == this->state_; } + explicit RunningCondition(DutyTimeSensor *parent, bool state) : Parented(parent), state_(state) {} protected: - DutyTimeSensor *parent_; + bool check(Ts... x) override { return this->parent_->is_running() == this->state_; } bool state_; }; diff --git a/esphome/components/duty_time/sensor.py b/esphome/components/duty_time/sensor.py index 5f8582d481..556cd459a5 100644 --- a/esphome/components/duty_time/sensor.py +++ b/esphome/components/duty_time/sensor.py @@ -26,11 +26,14 @@ duty_time_sensor_ns = cg.esphome_ns.namespace("duty_time_sensor") DutyTimeSensor = duty_time_sensor_ns.class_( "DutyTimeSensor", sensor.Sensor, cg.PollingComponent ) -StartAction = duty_time_sensor_ns.class_("StartAction", Action) -StopAction = duty_time_sensor_ns.class_("StopAction", Action) -ResetAction = duty_time_sensor_ns.class_("ResetAction", Action) -SetAction = duty_time_sensor_ns.class_("SetAction", Action) -RunningCondition = duty_time_sensor_ns.class_("RunningCondition", Condition) +BaseAction = duty_time_sensor_ns.class_("BaseAction", Action, cg.Parented) +StartAction = duty_time_sensor_ns.class_("StartAction", BaseAction) +StopAction = duty_time_sensor_ns.class_("StopAction", BaseAction) +ResetAction = duty_time_sensor_ns.class_("ResetAction", BaseAction) +SetAction = duty_time_sensor_ns.class_("SetAction", BaseAction) +RunningCondition = duty_time_sensor_ns.class_( + "RunningCondition", Condition, cg.Parented +) CONFIG_SCHEMA = cv.All( @@ -89,20 +92,23 @@ DUTY_TIME_ID_SCHEMA = maybe_simple_id( @register_action("sensor.duty_time.start", StartAction, DUTY_TIME_ID_SCHEMA) async def sensor_runtime_start_to_code(config, action_id, template_arg, args): - paren = await cg.get_variable(config[CONF_ID]) - return cg.new_Pvariable(action_id, template_arg, paren) + var = cg.new_Pvariable(action_id, template_arg) + await cg.register_parented(var, config[CONF_ID]) + return var @register_action("sensor.duty_time.stop", StopAction, DUTY_TIME_ID_SCHEMA) async def sensor_runtime_stop_to_code(config, action_id, template_arg, args): - paren = await cg.get_variable(config[CONF_ID]) - return cg.new_Pvariable(action_id, template_arg, paren) + var = cg.new_Pvariable(action_id, template_arg) + await cg.register_parented(var, config[CONF_ID]) + return var @register_action("sensor.duty_time.reset", ResetAction, DUTY_TIME_ID_SCHEMA) async def sensor_runtime_reset_to_code(config, action_id, template_arg, args): - paren = await cg.get_variable(config[CONF_ID]) - return cg.new_Pvariable(action_id, template_arg, paren) + var = cg.new_Pvariable(action_id, template_arg) + await cg.register_parented(var, config[CONF_ID]) + return var @register_condition( From ccb3d3d30878a99c46f4d018e89fbb4f0fda99e3 Mon Sep 17 00:00:00 2001 From: cvwillegen Date: Sun, 30 Jul 2023 23:32:09 +0200 Subject: [PATCH 39/84] Slightly lower template switch setup priority (#5163) --- esphome/components/template/switch/template_switch.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/template/switch/template_switch.cpp b/esphome/components/template/switch/template_switch.cpp index b2a221669e..fa236f6364 100644 --- a/esphome/components/template/switch/template_switch.cpp +++ b/esphome/components/template/switch/template_switch.cpp @@ -36,7 +36,7 @@ void TemplateSwitch::write_state(bool state) { void TemplateSwitch::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; } bool TemplateSwitch::assumed_state() { return this->assumed_state_; } void TemplateSwitch::set_state_lambda(std::function()> &&f) { this->f_ = f; } -float TemplateSwitch::get_setup_priority() const { return setup_priority::HARDWARE; } +float TemplateSwitch::get_setup_priority() const { return setup_priority::HARDWARE - 2.0f; } Trigger<> *TemplateSwitch::get_turn_on_trigger() const { return this->turn_on_trigger_; } Trigger<> *TemplateSwitch::get_turn_off_trigger() const { return this->turn_off_trigger_; } void TemplateSwitch::setup() { From 9aa5ee3372f3a4fb024862901954174a2a390c12 Mon Sep 17 00:00:00 2001 From: PlainTechEnthusiast <135363826+PlainTechEnthusiast@users.noreply.github.com> Date: Sun, 30 Jul 2023 17:40:55 -0400 Subject: [PATCH 40/84] update "Can't convert" warning to match others in homeassistant_sensor (#5162) --- .../components/homeassistant/sensor/homeassistant_sensor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/homeassistant/sensor/homeassistant_sensor.cpp b/esphome/components/homeassistant/sensor/homeassistant_sensor.cpp index f5e73c8854..35e660f7c1 100644 --- a/esphome/components/homeassistant/sensor/homeassistant_sensor.cpp +++ b/esphome/components/homeassistant/sensor/homeassistant_sensor.cpp @@ -12,7 +12,7 @@ void HomeassistantSensor::setup() { this->entity_id_, this->attribute_, [this](const std::string &state) { auto val = parse_number(state); if (!val.has_value()) { - ESP_LOGW(TAG, "Can't convert '%s' to number!", state.c_str()); + ESP_LOGW(TAG, "'%s': Can't convert '%s' to number!", this->entity_id_.c_str(), state.c_str()); this->publish_state(NAN); return; } From 98bf4276000e39f22f4b2977fe7ba4d05609a25c Mon Sep 17 00:00:00 2001 From: Mat931 <49403702+Mat931@users.noreply.github.com> Date: Sun, 30 Jul 2023 21:45:56 +0000 Subject: [PATCH 41/84] Add standardized CRC helper functions (#4798) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/components/pipsolar/pipsolar.cpp | 46 ++--------- esphome/components/sml/constants.h | 23 ------ esphome/components/sml/sml.cpp | 24 ++---- esphome/components/sml/sml.h | 3 - esphome/core/helpers.cpp | 100 ++++++++++++++++++++--- esphome/core/helpers.h | 5 +- 6 files changed, 104 insertions(+), 97 deletions(-) diff --git a/esphome/components/pipsolar/pipsolar.cpp b/esphome/components/pipsolar/pipsolar.cpp index c9d1ed00f6..62e4fbd341 100644 --- a/esphome/components/pipsolar/pipsolar.cpp +++ b/esphome/components/pipsolar/pipsolar.cpp @@ -1,5 +1,6 @@ #include "pipsolar.h" #include "esphome/core/log.h" +#include "esphome/core/helpers.h" namespace esphome { namespace pipsolar { @@ -768,7 +769,7 @@ uint8_t Pipsolar::check_incoming_length_(uint8_t length) { uint8_t Pipsolar::check_incoming_crc_() { uint16_t crc16; - crc16 = cal_crc_half_(read_buffer_, read_pos_ - 3); + crc16 = crc16be(read_buffer_, read_pos_ - 3); ESP_LOGD(TAG, "checking crc on incoming message"); if (((uint8_t) ((crc16) >> 8)) == read_buffer_[read_pos_ - 3] && ((uint8_t) ((crc16) &0xff)) == read_buffer_[read_pos_ - 2]) { @@ -797,7 +798,7 @@ uint8_t Pipsolar::send_next_command_() { this->command_start_millis_ = millis(); this->empty_uart_buffer_(); this->read_pos_ = 0; - crc16 = cal_crc_half_(byte_command, length); + crc16 = crc16be(byte_command, length); this->write_str(command); // checksum this->write(((uint8_t) ((crc16) >> 8))); // highbyte @@ -824,8 +825,8 @@ void Pipsolar::send_next_poll_() { this->command_start_millis_ = millis(); this->empty_uart_buffer_(); this->read_pos_ = 0; - crc16 = cal_crc_half_(this->used_polling_commands_[this->last_polling_command_].command, - this->used_polling_commands_[this->last_polling_command_].length); + crc16 = crc16be(this->used_polling_commands_[this->last_polling_command_].command, + this->used_polling_commands_[this->last_polling_command_].length); this->write_array(this->used_polling_commands_[this->last_polling_command_].command, this->used_polling_commands_[this->last_polling_command_].length); // checksum @@ -892,42 +893,5 @@ void Pipsolar::add_polling_command_(const char *command, ENUMPollingCommand poll } } -uint16_t Pipsolar::cal_crc_half_(uint8_t *msg, uint8_t len) { - uint16_t crc; - - uint8_t da; - uint8_t *ptr; - uint8_t b_crc_hign; - uint8_t b_crc_low; - - uint16_t crc_ta[16] = {0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, - 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef}; - - ptr = msg; - crc = 0; - - while (len-- != 0) { - da = ((uint8_t) (crc >> 8)) >> 4; - crc <<= 4; - crc ^= crc_ta[da ^ (*ptr >> 4)]; - da = ((uint8_t) (crc >> 8)) >> 4; - crc <<= 4; - crc ^= crc_ta[da ^ (*ptr & 0x0f)]; - ptr++; - } - - b_crc_low = crc; - b_crc_hign = (uint8_t) (crc >> 8); - - if (b_crc_low == 0x28 || b_crc_low == 0x0d || b_crc_low == 0x0a) - b_crc_low++; - if (b_crc_hign == 0x28 || b_crc_hign == 0x0d || b_crc_hign == 0x0a) - b_crc_hign++; - - crc = ((uint16_t) b_crc_hign) << 8; - crc += b_crc_low; - return (crc); -} - } // namespace pipsolar } // namespace esphome diff --git a/esphome/components/sml/constants.h b/esphome/components/sml/constants.h index 22114fd233..08a124ccad 100644 --- a/esphome/components/sml/constants.h +++ b/esphome/components/sml/constants.h @@ -17,32 +17,9 @@ enum SmlType : uint8_t { enum SmlMessageType : uint16_t { SML_PUBLIC_OPEN_RES = 0x0101, SML_GET_LIST_RES = 0x701 }; -enum Crc16CheckResult : uint8_t { CHECK_CRC16_FAILED, CHECK_CRC16_X25_SUCCESS, CHECK_CRC16_KERMIT_SUCCESS }; - // masks with two-bit mapping 0x1b -> 0b01; 0x01 -> 0b10; 0x1a -> 0b11 const uint16_t START_MASK = 0x55aa; // 0x1b 1b 1b 1b 1b 01 01 01 01 const uint16_t END_MASK = 0x0157; // 0x1b 1b 1b 1b 1a -const uint16_t CRC16_X25_TABLE[256] = { - 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, - 0xe97e, 0xf8f7, 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 0x9cc9, 0x8d40, 0xbfdb, 0xae52, - 0xdaed, 0xcb64, 0xf9ff, 0xe876, 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 0xad4a, 0xbcc3, - 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, - 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, - 0x2732, 0x36bb, 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 0x5285, 0x430c, 0x7197, 0x601e, - 0x14a1, 0x0528, 0x37b3, 0x263a, 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 0x6306, 0x728f, - 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, - 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, - 0x9af9, 0x8b70, 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 0x0840, 0x19c9, 0x2b52, 0x3adb, - 0x4e64, 0x5fed, 0x6d76, 0x7cff, 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 0x18c1, 0x0948, - 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, - 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, - 0xd0bd, 0xc134, 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 0xc60c, 0xd785, 0xe51e, 0xf497, - 0x8028, 0x91a1, 0xa33a, 0xb2b3, 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 0xd68d, 0xc704, - 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, - 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, - 0x0e70, 0x1ff9, 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 0x7bc7, 0x6a4e, 0x58d5, 0x495c, - 0x3de3, 0x2c6a, 0x1ef1, 0x0f78}; - } // namespace sml } // namespace esphome diff --git a/esphome/components/sml/sml.cpp b/esphome/components/sml/sml.cpp index c6fe7d64cd..87dc25c220 100644 --- a/esphome/components/sml/sml.cpp +++ b/esphome/components/sml/sml.cpp @@ -1,5 +1,6 @@ #include "sml.h" #include "esphome/core/log.h" +#include "esphome/core/helpers.h" #include "sml_parser.h" namespace esphome { @@ -99,12 +100,15 @@ bool check_sml_data(const bytes &buffer) { } uint16_t crc_received = (buffer.at(buffer.size() - 2) << 8) | buffer.at(buffer.size() - 1); - if (crc_received == calc_crc16_x25(buffer.begin(), buffer.end() - 2, 0x6e23)) { + uint16_t crc_calculated = crc16(buffer.data(), buffer.size(), 0x6e23, 0x8408, true, true); + crc_calculated = (crc_calculated >> 8) | (crc_calculated << 8); + if (crc_received == crc_calculated) { ESP_LOGV(TAG, "Checksum verification successful with CRC16/X25."); return true; } - if (crc_received == calc_crc16_kermit(buffer.begin(), buffer.end() - 2, 0xed50)) { + crc_calculated = crc16(buffer.data(), buffer.size(), 0xed50, 0x8408); + if (crc_received == crc_calculated) { ESP_LOGV(TAG, "Checksum verification successful with CRC16/KERMIT."); return true; } @@ -113,22 +117,6 @@ bool check_sml_data(const bytes &buffer) { return false; } -uint16_t calc_crc16_p1021(bytes::const_iterator begin, bytes::const_iterator end, uint16_t crcsum) { - for (auto it = begin; it != end; it++) { - crcsum = (crcsum >> 8) ^ CRC16_X25_TABLE[(crcsum & 0xff) ^ *it]; - } - return crcsum; -} - -uint16_t calc_crc16_x25(bytes::const_iterator begin, bytes::const_iterator end, uint16_t crcsum = 0) { - crcsum = calc_crc16_p1021(begin, end, crcsum ^ 0xffff) ^ 0xffff; - return (crcsum >> 8) | ((crcsum & 0xff) << 8); -} - -uint16_t calc_crc16_kermit(bytes::const_iterator begin, bytes::const_iterator end, uint16_t crcsum = 0) { - return calc_crc16_p1021(begin, end, crcsum); -} - uint8_t get_code(uint8_t byte) { switch (byte) { case 0x1b: diff --git a/esphome/components/sml/sml.h b/esphome/components/sml/sml.h index ac7befb043..ebc8b17d7f 100644 --- a/esphome/components/sml/sml.h +++ b/esphome/components/sml/sml.h @@ -38,9 +38,6 @@ class Sml : public Component, public uart::UARTDevice { }; bool check_sml_data(const bytes &buffer); -uint16_t calc_crc16_p1021(bytes::const_iterator begin, bytes::const_iterator end, uint16_t crcsum); -uint16_t calc_crc16_x25(bytes::const_iterator begin, bytes::const_iterator end, uint16_t crcsum); -uint16_t calc_crc16_kermit(bytes::const_iterator begin, bytes::const_iterator end, uint16_t crcsum); uint8_t get_code(uint8_t byte); } // namespace sml diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index 4c6ee84dde..0bc762fd0d 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -34,6 +34,9 @@ #include #include #endif +#ifdef USE_ESP32 +#include "esp32/rom/crc.h" +#endif #ifdef USE_ESP32_IGNORE_EFUSE_MAC_CRC #include "esp_efuse.h" @@ -44,6 +47,23 @@ namespace esphome { static const char *const TAG = "helpers"; +static const uint16_t CRC16_A001_LE_LUT_L[] = {0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241, + 0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440}; +static const uint16_t CRC16_A001_LE_LUT_H[] = {0x0000, 0xcc01, 0xd801, 0x1400, 0xf001, 0x3c00, 0x2800, 0xe401, + 0xa001, 0x6c00, 0x7800, 0xb401, 0x5000, 0x9c01, 0x8801, 0x4400}; + +#ifndef USE_ESP32 +static const uint16_t CRC16_8408_LE_LUT_L[] = {0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, + 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7}; +static const uint16_t CRC16_8408_LE_LUT_H[] = {0x0000, 0x1081, 0x2102, 0x3183, 0x4204, 0x5285, 0x6306, 0x7387, + 0x8408, 0x9489, 0xa50a, 0xb58b, 0xc60c, 0xd68d, 0xe70e, 0xf78f}; + +static const uint16_t CRC16_1021_BE_LUT_L[] = {0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, + 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef}; +static const uint16_t CRC16_1021_BE_LUT_H[] = {0x0000, 0x1231, 0x2462, 0x3653, 0x48c4, 0x5af5, 0x6ca6, 0x7e97, + 0x9188, 0x83b9, 0xb5ea, 0xa7db, 0xd94c, 0xcb7d, 0xfd2e, 0xef1f}; +#endif + // STL backports #if _GLIBCXX_RELEASE < 7 @@ -76,21 +96,79 @@ uint8_t crc8(uint8_t *data, uint8_t len) { } return crc; } -uint16_t crc16(const uint8_t *data, uint8_t len) { - uint16_t crc = 0xFFFF; - while (len--) { - crc ^= *data++; - for (uint8_t i = 0; i < 8; i++) { - if ((crc & 0x01) != 0) { - crc >>= 1; - crc ^= 0xA001; - } else { - crc >>= 1; + +uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t reverse_poly, bool refin, bool refout) { +#ifdef USE_ESP32 + if (reverse_poly == 0x8408) { + crc = crc16_le(refin ? crc : (crc ^ 0xffff), data, len); + return refout ? crc : (crc ^ 0xffff); + } +#endif + if (refin) { + crc ^= 0xffff; + } +#ifndef USE_ESP32 + if (reverse_poly == 0x8408) { + while (len--) { + uint8_t combo = crc ^ (uint8_t) *data++; + crc = (crc >> 8) ^ CRC16_8408_LE_LUT_L[combo & 0x0F] ^ CRC16_8408_LE_LUT_H[combo >> 4]; + } + } else +#endif + if (reverse_poly == 0xa001) { + while (len--) { + uint8_t combo = crc ^ (uint8_t) *data++; + crc = (crc >> 8) ^ CRC16_A001_LE_LUT_L[combo & 0x0F] ^ CRC16_A001_LE_LUT_H[combo >> 4]; + } + } else { + while (len--) { + crc ^= *data++; + for (uint8_t i = 0; i < 8; i++) { + if (crc & 0x0001) { + crc = (crc >> 1) ^ reverse_poly; + } else { + crc >>= 1; + } } } } - return crc; + return refout ? (crc ^ 0xffff) : crc; } + +uint16_t crc16be(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t poly, bool refin, bool refout) { +#ifdef USE_ESP32 + if (poly == 0x1021) { + crc = crc16_be(refin ? crc : (crc ^ 0xffff), data, len); + return refout ? crc : (crc ^ 0xffff); + } +#endif + if (refin) { + crc ^= 0xffff; + } +#ifndef USE_ESP32 + if (poly == 0x1021) { + while (len--) { + uint8_t combo = (crc >> 8) ^ *data++; + crc = (crc << 8) ^ CRC16_1021_BE_LUT_L[combo & 0x0F] ^ CRC16_1021_BE_LUT_H[combo >> 4]; + } + } else { +#endif + while (len--) { + crc ^= (((uint16_t) *data++) << 8); + for (uint8_t i = 0; i < 8; i++) { + if (crc & 0x8000) { + crc = (crc << 1) ^ poly; + } else { + crc <<= 1; + } + } + } +#ifndef USE_ESP32 + } +#endif + return refout ? (crc ^ 0xffff) : crc; +} + uint32_t fnv1_hash(const std::string &str) { uint32_t hash = 2166136261UL; for (char c : str) { diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 63b6949fe9..115073de80 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -155,7 +155,10 @@ template T remap(U value, U min, U max, T min_out, T max uint8_t crc8(uint8_t *data, uint8_t len); /// Calculate a CRC-16 checksum of \p data with size \p len. -uint16_t crc16(const uint8_t *data, uint8_t len); +uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc = 0xffff, uint16_t reverse_poly = 0xa001, + bool refin = false, bool refout = false); +uint16_t crc16be(const uint8_t *data, uint16_t len, uint16_t crc = 0, uint16_t poly = 0x1021, bool refin = false, + bool refout = false); /// Calculate a FNV-1 hash of \p str. uint32_t fnv1_hash(const std::string &str); From c418eecf839b527f61f05a4b2b1e8128df8933e0 Mon Sep 17 00:00:00 2001 From: Jimmy Hedman Date: Mon, 31 Jul 2023 00:20:55 +0200 Subject: [PATCH 42/84] Enable IPv6 for ESP32 Arduino, wifi and ethernet (#4865) --- .../ethernet/ethernet_component.cpp | 46 +++++++++++++++++++ .../components/ethernet/ethernet_component.h | 7 +++ esphome/components/network/__init__.py | 18 +++++--- .../wifi/wifi_component_esp32_arduino.cpp | 10 ++++ 4 files changed, 75 insertions(+), 6 deletions(-) diff --git a/esphome/components/ethernet/ethernet_component.cpp b/esphome/components/ethernet/ethernet_component.cpp index 3b5804abdd..d9004a913b 100644 --- a/esphome/components/ethernet/ethernet_component.cpp +++ b/esphome/components/ethernet/ethernet_component.cpp @@ -118,6 +118,10 @@ void EthernetComponent::setup() { ESPHL_ERROR_CHECK(err, "ETH event handler register error"); err = esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &EthernetComponent::got_ip_event_handler, nullptr); ESPHL_ERROR_CHECK(err, "GOT IP event handler register error"); +#if LWIP_IPV6 + err = esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &EthernetComponent::got_ip6_event_handler, nullptr); + ESPHL_ERROR_CHECK(err, "GOT IP6 event handler register error"); +#endif /* LWIP_IPV6 */ /* start Ethernet driver state machine */ err = esp_eth_start(this->eth_handle_); @@ -160,6 +164,20 @@ void EthernetComponent::loop() { this->state_ = EthernetComponentState::CONNECTING; this->start_connect_(); } +#if LWIP_IPV6 + else if (this->got_ipv6_) { + esp_ip6_addr_t ip6_addr; + if (esp_netif_get_ip6_global(this->eth_netif_, &ip6_addr) == 0 && + esp_netif_ip6_get_addr_type(&ip6_addr) == ESP_IP6_ADDR_IS_GLOBAL) { + ESP_LOGCONFIG(TAG, "IPv6 Addr (Global): " IPV6STR, IPV62STR(ip6_addr)); + } else { + esp_netif_get_ip6_linklocal(this->eth_netif_, &ip6_addr); + ESP_LOGCONFIG(TAG, " IPv6: " IPV6STR, IPV62STR(ip6_addr)); + } + + this->got_ipv6_ = false; + } +#endif /* LWIP_IPV6 */ break; } } @@ -254,6 +272,15 @@ void EthernetComponent::got_ip_event_handler(void *arg, esp_event_base_t event_b ESP_LOGV(TAG, "[Ethernet event] ETH Got IP (num=%" PRId32 ")", event_id); } +#if LWIP_IPV6 +void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, + void *event_data) { + ESP_LOGV(TAG, "[Ethernet event] ETH Got IP6 (num=%d)", event_id); + global_eth_component->got_ipv6_ = true; + global_eth_component->ipv6_count_ += 1; +} +#endif /* LWIP_IPV6 */ + void EthernetComponent::start_connect_() { this->connect_begin_ = millis(); this->status_set_warning(); @@ -316,6 +343,12 @@ void EthernetComponent::start_connect_() { if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) { ESPHL_ERROR_CHECK(err, "DHCPC start error"); } +#if LWIP_IPV6 + err = esp_netif_create_ip6_linklocal(this->eth_netif_); + if (err != ESP_OK) { + ESPHL_ERROR_CHECK(err, "IPv6 local failed"); + } +#endif /* LWIP_IPV6 */ } this->connect_begin_ = millis(); @@ -343,6 +376,19 @@ void EthernetComponent::dump_connect_params_() { ESP_LOGCONFIG(TAG, " DNS2: %s", network::IPAddress(dns_ip2->addr).str().c_str()); #endif +#if LWIP_IPV6 + if (this->ipv6_count_ > 0) { + esp_ip6_addr_t ip6_addr; + esp_netif_get_ip6_linklocal(this->eth_netif_, &ip6_addr); + ESP_LOGCONFIG(TAG, " IPv6: " IPV6STR, IPV62STR(ip6_addr)); + + if (esp_netif_get_ip6_global(this->eth_netif_, &ip6_addr) == 0 && + esp_netif_ip6_get_addr_type(&ip6_addr) == ESP_IP6_ADDR_IS_GLOBAL) { + ESP_LOGCONFIG(TAG, "IPv6 Addr (Global): " IPV6STR, IPV62STR(ip6_addr)); + } + } +#endif /* LWIP_IPV6 */ + esp_err_t err; uint8_t mac[6]; diff --git a/esphome/components/ethernet/ethernet_component.h b/esphome/components/ethernet/ethernet_component.h index f6b67f3f82..1bd4786b44 100644 --- a/esphome/components/ethernet/ethernet_component.h +++ b/esphome/components/ethernet/ethernet_component.h @@ -65,6 +65,9 @@ class EthernetComponent : public Component { protected: static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); +#if LWIP_IPV6 + static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); +#endif /* LWIP_IPV6 */ void start_connect_(); void dump_connect_params_(); @@ -83,6 +86,10 @@ class EthernetComponent : public Component { bool started_{false}; bool connected_{false}; +#if LWIP_IPV6 + bool got_ipv6_{false}; + uint8_t ipv6_count_{0}; +#endif /* LWIP_IPV6 */ EthernetComponentState state_{EthernetComponentState::STOPPED}; uint32_t connect_begin_; esp_netif_t *eth_netif_{nullptr}; diff --git a/esphome/components/network/__init__.py b/esphome/components/network/__init__.py index 96cfc51ff5..cd29734f42 100644 --- a/esphome/components/network/__init__.py +++ b/esphome/components/network/__init__.py @@ -1,3 +1,4 @@ +from esphome.core import CORE import esphome.codegen as cg import esphome.config_validation as cv from esphome.components.esp32 import add_idf_sdkconfig_option @@ -14,8 +15,8 @@ IPAddress = network_ns.class_("IPAddress") CONFIG_SCHEMA = cv.Schema( { - cv.SplitDefault(CONF_ENABLE_IPV6, esp32_idf=False): cv.All( - cv.only_with_esp_idf, cv.boolean + cv.SplitDefault(CONF_ENABLE_IPV6, esp32=False): cv.All( + cv.only_on_esp32, cv.boolean ), } ) @@ -23,7 +24,12 @@ CONFIG_SCHEMA = cv.Schema( async def to_code(config): if CONF_ENABLE_IPV6 in config: - add_idf_sdkconfig_option("CONFIG_LWIP_IPV6", config[CONF_ENABLE_IPV6]) - add_idf_sdkconfig_option( - "CONFIG_LWIP_IPV6_AUTOCONFIG", config[CONF_ENABLE_IPV6] - ) + if CORE.using_esp_idf: + add_idf_sdkconfig_option("CONFIG_LWIP_IPV6", config[CONF_ENABLE_IPV6]) + add_idf_sdkconfig_option( + "CONFIG_LWIP_IPV6_AUTOCONFIG", config[CONF_ENABLE_IPV6] + ) + else: + if config[CONF_ENABLE_IPV6]: + cg.add_build_flag("-DCONFIG_LWIP_IPV6") + cg.add_build_flag("-DCONFIG_LWIP_IPV6_AUTOCONFIG") diff --git a/esphome/components/wifi/wifi_component_esp32_arduino.cpp b/esphome/components/wifi/wifi_component_esp32_arduino.cpp index 3628eca78d..995e5e587e 100644 --- a/esphome/components/wifi/wifi_component_esp32_arduino.cpp +++ b/esphome/components/wifi/wifi_component_esp32_arduino.cpp @@ -485,6 +485,9 @@ void WiFiComponent::wifi_event_callback_(esphome_wifi_event_id_t event, esphome_ buf[it.ssid_len] = '\0'; ESP_LOGV(TAG, "Event: Connected ssid='%s' bssid=" LOG_SECRET("%s") " channel=%u, authmode=%s", buf, format_mac_addr(it.bssid).c_str(), it.channel, get_auth_mode_str(it.authmode)); +#if LWIP_IPV6 + WiFi.enableIpV6(); +#endif /* LWIP_IPV6 */ break; } @@ -547,6 +550,13 @@ void WiFiComponent::wifi_event_callback_(esphome_wifi_event_id_t event, esphome_ s_sta_connecting = false; break; } +#if LWIP_IPV6 + case ESPHOME_EVENT_ID_WIFI_STA_GOT_IP6: { + auto it = info.got_ip6.ip6_info; + ESP_LOGV(TAG, "Got IPv6 address=" IPV6STR, IPV62STR(it.ip)); + break; + } +#endif /* LWIP_IPV6 */ case ESPHOME_EVENT_ID_WIFI_STA_LOST_IP: { ESP_LOGV(TAG, "Event: Lost IP"); break; From bf732f2a2b093e94551d25bb427e1b369c7bdd38 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 30 Jul 2023 15:23:52 -0700 Subject: [PATCH 43/84] Increase maximum number of BLE notifications (#5155) --- esphome/components/esp32_ble_tracker/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/esphome/components/esp32_ble_tracker/__init__.py b/esphome/components/esp32_ble_tracker/__init__.py index 30589f1a3f..8ba77c7db7 100644 --- a/esphome/components/esp32_ble_tracker/__init__.py +++ b/esphome/components/esp32_ble_tracker/__init__.py @@ -263,6 +263,7 @@ async def to_code(config): # Match arduino CONFIG_BTU_TASK_STACK_SIZE # https://github.com/espressif/arduino-esp32/blob/fd72cf46ad6fc1a6de99c1d83ba8eba17d80a4ee/tools/sdk/esp32/sdkconfig#L1866 add_idf_sdkconfig_option("CONFIG_BTU_TASK_STACK_SIZE", 8192) + add_idf_sdkconfig_option("CONFIG_BT_ACL_CONNECTIONS", 9) cg.add_define("USE_OTA_STATE_CALLBACK") # To be notified when an OTA update starts cg.add_define("USE_ESP32_BLE_CLIENT") From 9a661999044a4f90be4699f2d0b57d9ff8e492e3 Mon Sep 17 00:00:00 2001 From: Joris S <100357138+Jorre05@users.noreply.github.com> Date: Mon, 31 Jul 2023 00:30:21 +0200 Subject: [PATCH 44/84] invert min_rssi check (#5150) --- esphome/components/ble_presence/binary_sensor.py | 2 +- esphome/components/ble_presence/ble_presence_device.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/esphome/components/ble_presence/binary_sensor.py b/esphome/components/ble_presence/binary_sensor.py index d54b7678e1..75366ce864 100644 --- a/esphome/components/ble_presence/binary_sensor.py +++ b/esphome/components/ble_presence/binary_sensor.py @@ -39,7 +39,7 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_IBEACON_MINOR): cv.uint16_t, cv.Optional(CONF_IBEACON_UUID): cv.uuid, cv.Optional(CONF_MIN_RSSI): cv.All( - cv.decibel, cv.int_range(min=-90, max=-30) + cv.decibel, cv.int_range(min=-100, max=-30) ), } ) diff --git a/esphome/components/ble_presence/ble_presence_device.h b/esphome/components/ble_presence/ble_presence_device.h index 953ea460a8..1be9adeb30 100644 --- a/esphome/components/ble_presence/ble_presence_device.h +++ b/esphome/components/ble_presence/ble_presence_device.h @@ -51,7 +51,7 @@ class BLEPresenceDevice : public binary_sensor::BinarySensorInitiallyOff, this->found_ = false; } bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override { - if (this->check_minimum_rssi_ && this->minimum_rssi_ <= device.get_rssi()) { + if (this->check_minimum_rssi_ && this->minimum_rssi_ > device.get_rssi()) { return false; } switch (this->match_by_) { From f0f09d371440400919a7cfa44ebb097842bc9e99 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 31 Jul 2023 10:32:55 +1200 Subject: [PATCH 45/84] Bump zeroconf from 0.69.0 to 0.71.4 (#5148) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 781ed03a49..d5ca30a387 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,7 +11,7 @@ esptool==4.6.2 click==8.1.5 esphome-dashboard==20230711.0 aioesphomeapi==15.0.0 -zeroconf==0.69.0 +zeroconf==0.71.4 # esp-idf requires this, but doesn't bundle it by default # https://github.com/espressif/esp-idf/blob/220590d599e134d7a5e7f1e683cc4550349ffbf8/requirements.txt#L24 From 7dd56fb0fa0e981e1af7143bc4fc369fec2fecf9 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 30 Jul 2023 23:28:26 +0000 Subject: [PATCH 46/84] Bump black from 23.3.0 to 23.7.0 (#5126) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- requirements_test.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9d2eb2908f..c7f55ed804 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,7 +3,7 @@ # See https://pre-commit.com/hooks.html for more hooks repos: - repo: https://github.com/psf/black - rev: 23.3.0 + rev: 23.7.0 hooks: - id: black args: diff --git a/requirements_test.txt b/requirements_test.txt index 75f29ac8dd..e343347436 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -1,6 +1,6 @@ pylint==2.17.4 flake8==6.0.0 # also change in .pre-commit-config.yaml when updating -black==23.3.0 # also change in .pre-commit-config.yaml when updating +black==23.7.0 # also change in .pre-commit-config.yaml when updating pyupgrade==3.7.0 # also change in .pre-commit-config.yaml when updating pre-commit From 869981cfe48dfea81c540d0187712e1466a2180f Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 1 Aug 2023 10:16:46 +1200 Subject: [PATCH 47/84] Bump pylint from 2.17.4 to 2.17.5 (#5172) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- requirements_test.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements_test.txt b/requirements_test.txt index e343347436..ac1446daf4 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -1,4 +1,4 @@ -pylint==2.17.4 +pylint==2.17.5 flake8==6.0.0 # also change in .pre-commit-config.yaml when updating black==23.7.0 # also change in .pre-commit-config.yaml when updating pyupgrade==3.7.0 # also change in .pre-commit-config.yaml when updating From 17be6b106b7dd3baa5d1fad865318b4a940545db Mon Sep 17 00:00:00 2001 From: Maxime Michel Date: Tue, 1 Aug 2023 02:03:34 +0200 Subject: [PATCH 48/84] Fix graininess & streaks for 7.50inV2alt Waveshare e-paper (#5168) --- .../components/waveshare_epaper/waveshare_epaper.cpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/esphome/components/waveshare_epaper/waveshare_epaper.cpp b/esphome/components/waveshare_epaper/waveshare_epaper.cpp index a3b1ec7d7b..5dd573f1b8 100644 --- a/esphome/components/waveshare_epaper/waveshare_epaper.cpp +++ b/esphome/components/waveshare_epaper/waveshare_epaper.cpp @@ -1493,11 +1493,10 @@ void WaveshareEPaper7P5InV2alt::initialize() { this->command(0x01); // 1-0=11: internal power - this->data(0x17); - + this->data(0x07); this->data(0x17); // VGH&VGL this->data(0x3F); // VSH - this->data(0x3F); // VSL + this->data(0x26); // VSL this->data(0x11); // VSHR // VCOM DC Setting @@ -1511,10 +1510,6 @@ void WaveshareEPaper7P5InV2alt::initialize() { this->data(0x2F); this->data(0x17); - // OSC Setting - this->command(0x30); - this->data(0x06); // 2-0=100: N=4 ; 5-3=111: M=7 ; 3C=50Hz 3A=100HZ - // POWER ON this->command(0x04); @@ -1536,7 +1531,7 @@ void WaveshareEPaper7P5InV2alt::initialize() { // COMMAND VCOM AND DATA INTERVAL SETTING this->command(0x50); this->data(0x10); - this->data(0x07); + this->data(0x00); // COMMAND TCON SETTING this->command(0x60); this->data(0x22); From cb8ca433d9a57769502a1e6adc66b0a0f0b1077e Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 2 Aug 2023 07:57:51 +1200 Subject: [PATCH 49/84] Bump pyupgrade from 3.7.0 to 3.9.0 (#5083) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- requirements_test.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c7f55ed804..60053a14a1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -27,7 +27,7 @@ repos: - --branch=release - --branch=beta - repo: https://github.com/asottile/pyupgrade - rev: v3.7.0 + rev: v3.9.0 hooks: - id: pyupgrade args: [--py39-plus] diff --git a/requirements_test.txt b/requirements_test.txt index ac1446daf4..fd1f45abc8 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -1,7 +1,7 @@ pylint==2.17.5 flake8==6.0.0 # also change in .pre-commit-config.yaml when updating black==23.7.0 # also change in .pre-commit-config.yaml when updating -pyupgrade==3.7.0 # also change in .pre-commit-config.yaml when updating +pyupgrade==3.9.0 # also change in .pre-commit-config.yaml when updating pre-commit # Unit tests From 8c66de239191fe4e5eed9a5defe49e31a094442e Mon Sep 17 00:00:00 2001 From: Pavlo Dudnytskyi Date: Wed, 2 Aug 2023 01:06:23 +0200 Subject: [PATCH 50/84] Vertical and horizontal airflow actions fix for Haier climate (#5164) Co-authored-by: Pavlo Dudnytskyi --- esphome/components/haier/climate.py | 9 +++++++-- esphome/components/haier/hon_climate.cpp | 12 ++---------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/esphome/components/haier/climate.py b/esphome/components/haier/climate.py index 12b76084ba..c518282bfa 100644 --- a/esphome/components/haier/climate.py +++ b/esphome/components/haier/climate.py @@ -54,18 +54,23 @@ HonClimate = haier_ns.class_("HonClimate", HaierClimateBase) Smartair2Climate = haier_ns.class_("Smartair2Climate", HaierClimateBase) -AirflowVerticalDirection = haier_ns.enum("AirflowVerticalDirection") +AirflowVerticalDirection = haier_ns.enum("AirflowVerticalDirection", True) AIRFLOW_VERTICAL_DIRECTION_OPTIONS = { + "HEALTH_UP": AirflowVerticalDirection.HEALTH_UP, + "MAX_UP": AirflowVerticalDirection.MAX_UP, "UP": AirflowVerticalDirection.UP, "CENTER": AirflowVerticalDirection.CENTER, "DOWN": AirflowVerticalDirection.DOWN, + "HEALTH_DOWN": AirflowVerticalDirection.HEALTH_DOWN, } -AirflowHorizontalDirection = haier_ns.enum("AirflowHorizontalDirection") +AirflowHorizontalDirection = haier_ns.enum("AirflowHorizontalDirection", True) AIRFLOW_HORIZONTAL_DIRECTION_OPTIONS = { + "MAX_LEFT": AirflowHorizontalDirection.MAX_LEFT, "LEFT": AirflowHorizontalDirection.LEFT, "CENTER": AirflowHorizontalDirection.CENTER, "RIGHT": AirflowHorizontalDirection.RIGHT, + "MAX_RIGHT": AirflowHorizontalDirection.MAX_RIGHT, } SUPPORTED_SWING_MODES_OPTIONS = { diff --git a/esphome/components/haier/hon_climate.cpp b/esphome/components/haier/hon_climate.cpp index 3016cda397..3950b34724 100644 --- a/esphome/components/haier/hon_climate.cpp +++ b/esphome/components/haier/hon_climate.cpp @@ -81,22 +81,14 @@ void HonClimate::set_outdoor_temperature_sensor(esphome::sensor::Sensor *sensor) AirflowVerticalDirection HonClimate::get_vertical_airflow() const { return this->vertical_direction_; }; void HonClimate::set_vertical_airflow(AirflowVerticalDirection direction) { - if (direction > AirflowVerticalDirection::DOWN) { - this->vertical_direction_ = AirflowVerticalDirection::CENTER; - } else { - this->vertical_direction_ = direction; - } + this->vertical_direction_ = direction; this->set_force_send_control_(true); } AirflowHorizontalDirection HonClimate::get_horizontal_airflow() const { return this->horizontal_direction_; } void HonClimate::set_horizontal_airflow(AirflowHorizontalDirection direction) { - if (direction > AirflowHorizontalDirection::RIGHT) { - this->horizontal_direction_ = AirflowHorizontalDirection::CENTER; - } else { - this->horizontal_direction_ = direction; - } + this->horizontal_direction_ = direction; this->set_force_send_control_(true); } From e02aaedc42bb0d82afaab877b841a73ef1e02483 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 2 Aug 2023 16:21:30 +1200 Subject: [PATCH 51/84] Microphone add is_stopped (#5183) --- esphome/components/microphone/microphone.h | 1 + 1 file changed, 1 insertion(+) diff --git a/esphome/components/microphone/microphone.h b/esphome/components/microphone/microphone.h index 5b16a67c00..ac3db3ec0f 100644 --- a/esphome/components/microphone/microphone.h +++ b/esphome/components/microphone/microphone.h @@ -22,6 +22,7 @@ class Microphone { } bool is_running() const { return this->state_ == STATE_RUNNING; } + bool is_stopped() const { return this->state_ == STATE_STOPPED; } protected: State state_{STATE_STOPPED}; From 581cb642ff19534f898b0b78344051f93900a8b5 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 2 Aug 2023 16:24:02 +1200 Subject: [PATCH 52/84] Add get_board function to esp32 module (#5184) --- esphome/components/esp32/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/esphome/components/esp32/__init__.py b/esphome/components/esp32/__init__.py index 7daaaf7433..aa9f8cd66e 100644 --- a/esphome/components/esp32/__init__.py +++ b/esphome/components/esp32/__init__.py @@ -81,6 +81,10 @@ def get_esp32_variant(core_obj=None): return (core_obj or CORE).data[KEY_ESP32][KEY_VARIANT] +def get_board(core_obj=None): + return (core_obj or CORE).data[KEY_ESP32][KEY_BOARD] + + def only_on_variant(*, supported=None, unsupported=None): """Config validator for features only available on some ESP32 variants.""" if supported is not None and not isinstance(supported, list): From ce8091c14e409a4327fa30921c9039f009ad984b Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 2 Aug 2023 16:24:52 +1200 Subject: [PATCH 53/84] Speaker return bytes written and do not wait for queue (#5182) --- .../i2s_audio/speaker/i2s_audio_speaker.cpp | 12 ++++++------ .../components/i2s_audio/speaker/i2s_audio_speaker.h | 2 +- esphome/components/speaker/speaker.h | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp b/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp index 5ae597dc7c..43bc005136 100644 --- a/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp +++ b/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp @@ -185,7 +185,7 @@ void I2SAudioSpeaker::loop() { } } -bool I2SAudioSpeaker::play(const uint8_t *data, size_t length) { +size_t I2SAudioSpeaker::play(const uint8_t *data, size_t length) { if (this->state_ != speaker::STATE_RUNNING && this->state_ != speaker::STATE_STARTING) { this->start(); } @@ -197,13 +197,13 @@ bool I2SAudioSpeaker::play(const uint8_t *data, size_t length) { size_t to_send_length = std::min(remaining, BUFFER_SIZE); event.len = to_send_length; memcpy(event.data, data + index, to_send_length); - if (xQueueSend(this->buffer_queue_, &event, 100 / portTICK_PERIOD_MS) == pdTRUE) { - remaining -= to_send_length; - index += to_send_length; + if (xQueueSend(this->buffer_queue_, &event, 0) != pdTRUE) { + return index; } - App.feed_wdt(); + remaining -= to_send_length; + index += to_send_length; } - return true; + return index; } } // namespace i2s_audio diff --git a/esphome/components/i2s_audio/speaker/i2s_audio_speaker.h b/esphome/components/i2s_audio/speaker/i2s_audio_speaker.h index 4f1d2172d7..f2e83142b3 100644 --- a/esphome/components/i2s_audio/speaker/i2s_audio_speaker.h +++ b/esphome/components/i2s_audio/speaker/i2s_audio_speaker.h @@ -54,7 +54,7 @@ class I2SAudioSpeaker : public Component, public speaker::Speaker, public I2SAud void start(); void stop() override; - bool play(const uint8_t *data, size_t length) override; + size_t play(const uint8_t *data, size_t length) override; protected: void start_(); diff --git a/esphome/components/speaker/speaker.h b/esphome/components/speaker/speaker.h index 5dfabfa40e..53f97da5ac 100644 --- a/esphome/components/speaker/speaker.h +++ b/esphome/components/speaker/speaker.h @@ -12,8 +12,8 @@ enum State : uint8_t { class Speaker { public: - virtual bool play(const uint8_t *data, size_t length) = 0; - virtual bool play(const std::vector &data) { return this->play(data.data(), data.size()); } + virtual size_t play(const uint8_t *data, size_t length) = 0; + virtual size_t play(const std::vector &data) { return this->play(data.data(), data.size()); } virtual void stop() = 0; From f81c556b63fe8949ffbe5cd8e3896ec5e8040fe3 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 2 Aug 2023 16:25:26 +1200 Subject: [PATCH 54/84] Update components "if x in config" (#5181) --- esphome/components/a4988/stepper.py | 4 +- esphome/components/adc/sensor.py | 10 ++--- .../components/addressable_light/display.py | 8 ++-- esphome/components/ade7953/sensor.py | 4 +- esphome/components/aht10/sensor.py | 8 ++-- .../alarm_control_panel/__init__.py | 12 ++--- esphome/components/alpha3/sensor.py | 24 +++++----- esphome/components/am2320/sensor.py | 8 ++-- esphome/components/am43/sensor/__init__.py | 8 ++-- esphome/components/animation/__init__.py | 12 ++--- esphome/components/api/__init__.py | 5 +-- esphome/components/as3935/sensor.py | 14 +++--- esphome/components/as7341/sensor.py | 4 +- .../components/atc_mithermometer/sensor.py | 20 ++++----- esphome/components/atm90e26/sensor.py | 32 +++++++------- esphome/components/atm90e32/sensor.py | 38 ++++++++-------- esphome/components/b_parasite/sensor.py | 4 +- esphome/components/bang_bang/climate.py | 11 +++-- esphome/components/bedjet/__init__.py | 8 ++-- esphome/components/binary/fan/__init__.py | 8 ++-- esphome/components/binary_sensor/__init__.py | 20 ++++----- esphome/components/bl0939/sensor.py | 40 +++++++---------- esphome/components/bl0940/sensor.py | 30 +++++-------- esphome/components/bl0942/sensor.py | 25 +++++------ .../components/ble_client/sensor/__init__.py | 32 ++++---------- .../ble_client/text_sensor/__init__.py | 28 +++--------- .../components/ble_presence/binary_sensor.py | 42 +++++++----------- esphome/components/ble_rssi/sensor.py | 38 +++++++--------- esphome/components/bme280/sensor.py | 21 ++++----- esphome/components/bme680/sensor.py | 26 +++++------ esphome/components/bme680_bsec/sensor.py | 11 ++--- esphome/components/bme680_bsec/text_sensor.py | 5 +-- esphome/components/bmp085/sensor.py | 10 ++--- esphome/components/bmp280/sensor.py | 14 +++--- esphome/components/bmp3xx/sensor.py | 18 ++++---- esphome/components/button/__init__.py | 8 ++-- esphome/components/canbus/__init__.py | 9 ++-- esphome/components/cap1188/__init__.py | 4 +- esphome/components/ccs811/sensor.py | 16 +++---- esphome/components/climate_ir/__init__.py | 8 ++-- esphome/components/cs5460a/sensor.py | 15 +++---- esphome/components/cse7766/sensor.py | 20 ++++----- esphome/components/current_based/cover.py | 44 +++++++++---------- esphome/components/cwww/light.py | 12 ++--- 44 files changed, 325 insertions(+), 413 deletions(-) diff --git a/esphome/components/a4988/stepper.py b/esphome/components/a4988/stepper.py index 7f53856c7b..744e9dc1cc 100644 --- a/esphome/components/a4988/stepper.py +++ b/esphome/components/a4988/stepper.py @@ -28,6 +28,6 @@ async def to_code(config): dir_pin = await cg.gpio_pin_expression(config[CONF_DIR_PIN]) cg.add(var.set_dir_pin(dir_pin)) - if CONF_SLEEP_PIN in config: - sleep_pin = await cg.gpio_pin_expression(config[CONF_SLEEP_PIN]) + if sleep_pin_config := config.get(CONF_SLEEP_PIN): + sleep_pin = await cg.gpio_pin_expression(sleep_pin_config) cg.add(var.set_sleep_pin(sleep_pin)) diff --git a/esphome/components/adc/sensor.py b/esphome/components/adc/sensor.py index a0eda1d659..99d3652698 100644 --- a/esphome/components/adc/sensor.py +++ b/esphome/components/adc/sensor.py @@ -89,14 +89,14 @@ async def to_code(config): pin = await cg.gpio_pin_expression(config[CONF_PIN]) cg.add(var.set_pin(pin)) - if CONF_RAW in config: - cg.add(var.set_output_raw(config[CONF_RAW])) + if raw := config.get(CONF_RAW): + cg.add(var.set_output_raw(raw)) - if CONF_ATTENUATION in config: - if config[CONF_ATTENUATION] == "auto": + if attenuation := config.get(CONF_ATTENUATION): + if attenuation == "auto": cg.add(var.set_autorange(cg.global_ns.true)) else: - cg.add(var.set_attenuation(config[CONF_ATTENUATION])) + cg.add(var.set_attenuation(attenuation)) if CORE.is_esp32: variant = get_esp32_variant() diff --git a/esphome/components/addressable_light/display.py b/esphome/components/addressable_light/display.py index 5fdd84ac2d..2f9b8cf455 100644 --- a/esphome/components/addressable_light/display.py +++ b/esphome/components/addressable_light/display.py @@ -48,16 +48,16 @@ async def to_code(config): await cg.register_component(var, config) await display.register_display(var, config) - if CONF_PIXEL_MAPPER in config: + if pixel_mapper := config.get(CONF_PIXEL_MAPPER): pixel_mapper_template_ = await cg.process_lambda( - config[CONF_PIXEL_MAPPER], + pixel_mapper, [(int, "x"), (int, "y")], return_type=cg.int_, ) cg.add(var.set_pixel_mapper(pixel_mapper_template_)) - if CONF_LAMBDA in config: + if lambda_config := config.get(CONF_LAMBDA): lambda_ = await cg.process_lambda( - config[CONF_LAMBDA], [(display.DisplayRef, "it")], return_type=cg.void + lambda_config, [(display.DisplayRef, "it")], return_type=cg.void ) cg.add(var.set_writer(lambda_)) diff --git a/esphome/components/ade7953/sensor.py b/esphome/components/ade7953/sensor.py index d02f466091..878f2f8e2d 100644 --- a/esphome/components/ade7953/sensor.py +++ b/esphome/components/ade7953/sensor.py @@ -72,8 +72,8 @@ async def to_code(config): await cg.register_component(var, config) await i2c.register_i2c_device(var, config) - if CONF_IRQ_PIN in config: - irq_pin = await cg.gpio_pin_expression(config[CONF_IRQ_PIN]) + if irq_pin_config := config.get(CONF_IRQ_PIN): + irq_pin = await cg.gpio_pin_expression(irq_pin_config) cg.add(var.set_irq_pin(irq_pin)) for key in [ diff --git a/esphome/components/aht10/sensor.py b/esphome/components/aht10/sensor.py index 654d645966..a52773b6d7 100644 --- a/esphome/components/aht10/sensor.py +++ b/esphome/components/aht10/sensor.py @@ -45,10 +45,10 @@ async def to_code(config): await cg.register_component(var, config) await i2c.register_i2c_device(var, config) - if CONF_TEMPERATURE in config: - sens = await sensor.new_sensor(config[CONF_TEMPERATURE]) + if temperature := config.get(CONF_TEMPERATURE): + sens = await sensor.new_sensor(temperature) cg.add(var.set_temperature_sensor(sens)) - if CONF_HUMIDITY in config: - sens = await sensor.new_sensor(config[CONF_HUMIDITY]) + if humidity := config.get(CONF_HUMIDITY): + sens = await sensor.new_sensor(humidity) cg.add(var.set_humidity_sensor(sens)) diff --git a/esphome/components/alarm_control_panel/__init__.py b/esphome/components/alarm_control_panel/__init__.py index 963d5ae719..0839e49875 100644 --- a/esphome/components/alarm_control_panel/__init__.py +++ b/esphome/components/alarm_control_panel/__init__.py @@ -99,8 +99,8 @@ async def register_alarm_control_panel(var, config): async def alarm_action_arm_away_to_code(config, action_id, template_arg, args): paren = await cg.get_variable(config[CONF_ID]) var = cg.new_Pvariable(action_id, template_arg, paren) - if CONF_CODE in config: - templatable_ = await cg.templatable(config[CONF_CODE], args, cg.std_string) + if code_config := config.get(CONF_CODE): + templatable_ = await cg.templatable(code_config, args, cg.std_string) cg.add(var.set_code(templatable_)) return var @@ -111,8 +111,8 @@ async def alarm_action_arm_away_to_code(config, action_id, template_arg, args): async def alarm_action_arm_home_to_code(config, action_id, template_arg, args): paren = await cg.get_variable(config[CONF_ID]) var = cg.new_Pvariable(action_id, template_arg, paren) - if CONF_CODE in config: - templatable_ = await cg.templatable(config[CONF_CODE], args, cg.std_string) + if code_config := config.get(CONF_CODE): + templatable_ = await cg.templatable(code_config, args, cg.std_string) cg.add(var.set_code(templatable_)) return var @@ -123,8 +123,8 @@ async def alarm_action_arm_home_to_code(config, action_id, template_arg, args): async def alarm_action_disarm_to_code(config, action_id, template_arg, args): paren = await cg.get_variable(config[CONF_ID]) var = cg.new_Pvariable(action_id, template_arg, paren) - if CONF_CODE in config: - templatable_ = await cg.templatable(config[CONF_CODE], args, cg.std_string) + if code_config := config.get(CONF_CODE): + templatable_ = await cg.templatable(code_config, args, cg.std_string) cg.add(var.set_code(templatable_)) return var diff --git a/esphome/components/alpha3/sensor.py b/esphome/components/alpha3/sensor.py index ba4ca16a5a..55a5d7c620 100644 --- a/esphome/components/alpha3/sensor.py +++ b/esphome/components/alpha3/sensor.py @@ -60,26 +60,26 @@ async def to_code(config): await cg.register_component(var, config) await ble_client.register_ble_node(var, config) - if CONF_FLOW in config: - sens = await sensor.new_sensor(config[CONF_FLOW]) + if flow_config := config.get(CONF_FLOW): + sens = await sensor.new_sensor(flow_config) cg.add(var.set_flow_sensor(sens)) - if CONF_HEAD in config: - sens = await sensor.new_sensor(config[CONF_HEAD]) + if head_config := config.get(CONF_HEAD): + sens = await sensor.new_sensor(head_config) cg.add(var.set_head_sensor(sens)) - if CONF_POWER in config: - sens = await sensor.new_sensor(config[CONF_POWER]) + if power_config := config.get(CONF_POWER): + sens = await sensor.new_sensor(power_config) cg.add(var.set_power_sensor(sens)) - if CONF_CURRENT in config: - sens = await sensor.new_sensor(config[CONF_CURRENT]) + if current_config := config.get(CONF_CURRENT): + sens = await sensor.new_sensor(current_config) cg.add(var.set_current_sensor(sens)) - if CONF_SPEED in config: - sens = await sensor.new_sensor(config[CONF_SPEED]) + if speed_config := config.get(CONF_SPEED): + sens = await sensor.new_sensor(speed_config) cg.add(var.set_speed_sensor(sens)) - if CONF_VOLTAGE in config: - sens = await sensor.new_sensor(config[CONF_VOLTAGE]) + if voltage_config := config.get(CONF_VOLTAGE): + sens = await sensor.new_sensor(voltage_config) cg.add(var.set_voltage_sensor(sens)) diff --git a/esphome/components/am2320/sensor.py b/esphome/components/am2320/sensor.py index 088978a8f1..ccd37d02c2 100644 --- a/esphome/components/am2320/sensor.py +++ b/esphome/components/am2320/sensor.py @@ -47,10 +47,10 @@ async def to_code(config): await cg.register_component(var, config) await i2c.register_i2c_device(var, config) - if CONF_TEMPERATURE in config: - sens = await sensor.new_sensor(config[CONF_TEMPERATURE]) + if temperature_config := config.get(CONF_TEMPERATURE): + sens = await sensor.new_sensor(temperature_config) cg.add(var.set_temperature_sensor(sens)) - if CONF_HUMIDITY in config: - sens = await sensor.new_sensor(config[CONF_HUMIDITY]) + if humidity_config := config.get(CONF_HUMIDITY): + sens = await sensor.new_sensor(humidity_config) cg.add(var.set_humidity_sensor(sens)) diff --git a/esphome/components/am43/sensor/__init__.py b/esphome/components/am43/sensor/__init__.py index 01588f2299..df068546cd 100644 --- a/esphome/components/am43/sensor/__init__.py +++ b/esphome/components/am43/sensor/__init__.py @@ -44,10 +44,10 @@ async def to_code(config): await cg.register_component(var, config) await ble_client.register_ble_node(var, config) - if CONF_BATTERY_LEVEL in config: - sens = await sensor.new_sensor(config[CONF_BATTERY_LEVEL]) + if battery_level_config := config.get(CONF_BATTERY_LEVEL): + sens = await sensor.new_sensor(battery_level_config) cg.add(var.set_battery(sens)) - if CONF_ILLUMINANCE in config: - sens = await sensor.new_sensor(config[CONF_ILLUMINANCE]) + if illuminance_config := config.get(CONF_ILLUMINANCE): + sens = await sensor.new_sensor(illuminance_config) cg.add(var.set_illuminance(sens)) diff --git a/esphome/components/animation/__init__.py b/esphome/components/animation/__init__.py index 82e724fa00..7fc1e0dcd0 100644 --- a/esphome/components/animation/__init__.py +++ b/esphome/components/animation/__init__.py @@ -115,8 +115,8 @@ async def animation_action_to_code(config, action_id, template_arg, args): paren = await cg.get_variable(config[CONF_ID]) var = cg.new_Pvariable(action_id, template_arg, paren) - if CONF_FRAME in config: - template_ = await cg.templatable(config[CONF_FRAME], args, cg.uint16) + if frame := config.get(CONF_FRAME): + template_ = await cg.templatable(frame, args, cg.uint16) cg.add(var.set_frame(template_)) return var @@ -289,8 +289,8 @@ async def to_code(config): espImage.IMAGE_TYPE[config[CONF_TYPE]], ) cg.add(var.set_transparency(transparent)) - if CONF_LOOP in config: - start = config[CONF_LOOP][CONF_START_FRAME] - end = config[CONF_LOOP].get(CONF_END_FRAME, frames) - count = config[CONF_LOOP].get(CONF_REPEAT, -1) + if loop_config := config.get(CONF_LOOP): + start = loop_config[CONF_START_FRAME] + end = loop_config.get(CONF_END_FRAME, frames) + count = loop_config.get(CONF_REPEAT, -1) cg.add(var.set_loop(start, end, count)) diff --git a/esphome/components/api/__init__.py b/esphome/components/api/__init__.py index 6b2e7fd06b..1076ebc707 100644 --- a/esphome/components/api/__init__.py +++ b/esphome/components/api/__init__.py @@ -116,9 +116,8 @@ async def to_code(config): cg.add(var.register_user_service(trigger)) await automation.build_automation(trigger, func_args, conf) - if CONF_ENCRYPTION in config: - conf = config[CONF_ENCRYPTION] - decoded = base64.b64decode(conf[CONF_KEY]) + if encryption_config := config.get(CONF_ENCRYPTION): + decoded = base64.b64decode(encryption_config[CONF_KEY]) cg.add(var.set_noise_psk(list(decoded))) cg.add_define("USE_API_NOISE") cg.add_library("esphome/noise-c", "0.1.4") diff --git a/esphome/components/as3935/sensor.py b/esphome/components/as3935/sensor.py index 5a3967ed7e..ff78b8a050 100644 --- a/esphome/components/as3935/sensor.py +++ b/esphome/components/as3935/sensor.py @@ -31,12 +31,10 @@ CONFIG_SCHEMA = cv.Schema( async def to_code(config): hub = await cg.get_variable(config[CONF_AS3935_ID]) - if CONF_DISTANCE in config: - conf = config[CONF_DISTANCE] - distance_sensor = await sensor.new_sensor(conf) - cg.add(hub.set_distance_sensor(distance_sensor)) + if distance_config := config.get(CONF_DISTANCE): + sens = await sensor.new_sensor(distance_config) + cg.add(hub.set_distance_sensor(sens)) - if CONF_LIGHTNING_ENERGY in config: - conf = config[CONF_LIGHTNING_ENERGY] - lightning_energy_sensor = await sensor.new_sensor(conf) - cg.add(hub.set_energy_sensor(lightning_energy_sensor)) + if lightning_energy_config := config.get(CONF_LIGHTNING_ENERGY): + sens = await sensor.new_sensor(lightning_energy_config) + cg.add(hub.set_energy_sensor(sens)) diff --git a/esphome/components/as7341/sensor.py b/esphome/components/as7341/sensor.py index 2424087c35..de60444aed 100644 --- a/esphome/components/as7341/sensor.py +++ b/esphome/components/as7341/sensor.py @@ -107,6 +107,6 @@ async def to_code(config): cg.add(var.set_astep(config[CONF_ASTEP])) for conf_id, set_sensor_func in SENSORS.items(): - if conf_id in config: - sens = await sensor.new_sensor(config[conf_id]) + if sens_config := config.get(conf_id): + sens = await sensor.new_sensor(sens_config) cg.add(getattr(var, set_sensor_func)(sens)) diff --git a/esphome/components/atc_mithermometer/sensor.py b/esphome/components/atc_mithermometer/sensor.py index 7baab51944..e86afa500d 100644 --- a/esphome/components/atc_mithermometer/sensor.py +++ b/esphome/components/atc_mithermometer/sensor.py @@ -83,18 +83,18 @@ async def to_code(config): cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex)) - if CONF_TEMPERATURE in config: - sens = await sensor.new_sensor(config[CONF_TEMPERATURE]) + if temperature_config := config.get(CONF_TEMPERATURE): + sens = await sensor.new_sensor(temperature_config) cg.add(var.set_temperature(sens)) - if CONF_HUMIDITY in config: - sens = await sensor.new_sensor(config[CONF_HUMIDITY]) + if humidity_config := config.get(CONF_HUMIDITY): + sens = await sensor.new_sensor(humidity_config) cg.add(var.set_humidity(sens)) - if CONF_BATTERY_LEVEL in config: - sens = await sensor.new_sensor(config[CONF_BATTERY_LEVEL]) + if battery_level_config := config.get(CONF_BATTERY_LEVEL): + sens = await sensor.new_sensor(battery_level_config) cg.add(var.set_battery_level(sens)) - if CONF_BATTERY_VOLTAGE in config: - sens = await sensor.new_sensor(config[CONF_BATTERY_VOLTAGE]) + if battery_voltage_config := config.get(CONF_BATTERY_VOLTAGE): + sens = await sensor.new_sensor(battery_voltage_config) cg.add(var.set_battery_voltage(sens)) - if CONF_SIGNAL_STRENGTH in config: - sens = await sensor.new_sensor(config[CONF_SIGNAL_STRENGTH]) + if signal_strength_config := config.get(CONF_SIGNAL_STRENGTH): + sens = await sensor.new_sensor(signal_strength_config) cg.add(var.set_signal_strength(sens)) diff --git a/esphome/components/atm90e26/sensor.py b/esphome/components/atm90e26/sensor.py index a0d97ab5ae..a702e23cf0 100644 --- a/esphome/components/atm90e26/sensor.py +++ b/esphome/components/atm90e26/sensor.py @@ -124,29 +124,29 @@ async def to_code(config): await cg.register_component(var, config) await spi.register_spi_device(var, config) - if CONF_VOLTAGE in config: - sens = await sensor.new_sensor(config[CONF_VOLTAGE]) + if voltage_config := config.get(CONF_VOLTAGE): + sens = await sensor.new_sensor(voltage_config) cg.add(var.set_voltage_sensor(sens)) - if CONF_CURRENT in config: - sens = await sensor.new_sensor(config[CONF_CURRENT]) + if current_config := config.get(CONF_CURRENT): + sens = await sensor.new_sensor(current_config) cg.add(var.set_current_sensor(sens)) - if CONF_POWER in config: - sens = await sensor.new_sensor(config[CONF_POWER]) + if power_config := config.get(CONF_POWER): + sens = await sensor.new_sensor(power_config) cg.add(var.set_power_sensor(sens)) - if CONF_REACTIVE_POWER in config: - sens = await sensor.new_sensor(config[CONF_REACTIVE_POWER]) + if reactive_power_config := config.get(CONF_REACTIVE_POWER): + sens = await sensor.new_sensor(reactive_power_config) cg.add(var.set_reactive_power_sensor(sens)) - if CONF_POWER_FACTOR in config: - sens = await sensor.new_sensor(config[CONF_POWER_FACTOR]) + if power_factor_config := config.get(CONF_POWER_FACTOR): + sens = await sensor.new_sensor(power_factor_config) cg.add(var.set_power_factor_sensor(sens)) - if CONF_FORWARD_ACTIVE_ENERGY in config: - sens = await sensor.new_sensor(config[CONF_FORWARD_ACTIVE_ENERGY]) + if forward_active_energy_config := config.get(CONF_FORWARD_ACTIVE_ENERGY): + sens = await sensor.new_sensor(forward_active_energy_config) cg.add(var.set_forward_active_energy_sensor(sens)) - if CONF_REVERSE_ACTIVE_ENERGY in config: - sens = await sensor.new_sensor(config[CONF_REVERSE_ACTIVE_ENERGY]) + if reverse_active_energy_config := config.get(CONF_REVERSE_ACTIVE_ENERGY): + sens = await sensor.new_sensor(reverse_active_energy_config) cg.add(var.set_reverse_active_energy_sensor(sens)) - if CONF_FREQUENCY in config: - sens = await sensor.new_sensor(config[CONF_FREQUENCY]) + if frequency_config := config.get(CONF_FREQUENCY): + sens = await sensor.new_sensor(frequency_config) cg.add(var.set_freq_sensor(sens)) cg.add(var.set_line_freq(config[CONF_LINE_FREQUENCY])) cg.add(var.set_meter_constant(config[CONF_METER_CONSTANT])) diff --git a/esphome/components/atm90e32/sensor.py b/esphome/components/atm90e32/sensor.py index 9c876bb62c..6cc0f6ac3e 100644 --- a/esphome/components/atm90e32/sensor.py +++ b/esphome/components/atm90e32/sensor.py @@ -151,33 +151,35 @@ async def to_code(config): conf = config[phase] cg.add(var.set_volt_gain(i, conf[CONF_GAIN_VOLTAGE])) cg.add(var.set_ct_gain(i, conf[CONF_GAIN_CT])) - if CONF_VOLTAGE in conf: - sens = await sensor.new_sensor(conf[CONF_VOLTAGE]) + if voltage_config := conf.get(CONF_VOLTAGE): + sens = await sensor.new_sensor(voltage_config) cg.add(var.set_voltage_sensor(i, sens)) - if CONF_CURRENT in conf: - sens = await sensor.new_sensor(conf[CONF_CURRENT]) + if current_config := conf.get(CONF_CURRENT): + sens = await sensor.new_sensor(current_config) cg.add(var.set_current_sensor(i, sens)) - if CONF_POWER in conf: - sens = await sensor.new_sensor(conf[CONF_POWER]) + if power_config := conf.get(CONF_POWER): + sens = await sensor.new_sensor(power_config) cg.add(var.set_power_sensor(i, sens)) - if CONF_REACTIVE_POWER in conf: - sens = await sensor.new_sensor(conf[CONF_REACTIVE_POWER]) + if reactive_power_config := conf.get(CONF_REACTIVE_POWER): + sens = await sensor.new_sensor(reactive_power_config) cg.add(var.set_reactive_power_sensor(i, sens)) - if CONF_POWER_FACTOR in conf: - sens = await sensor.new_sensor(conf[CONF_POWER_FACTOR]) + if power_factor_config := conf.get(CONF_POWER_FACTOR): + sens = await sensor.new_sensor(power_factor_config) cg.add(var.set_power_factor_sensor(i, sens)) - if CONF_FORWARD_ACTIVE_ENERGY in conf: - sens = await sensor.new_sensor(conf[CONF_FORWARD_ACTIVE_ENERGY]) + if forward_active_energy_config := conf.get(CONF_FORWARD_ACTIVE_ENERGY): + sens = await sensor.new_sensor(forward_active_energy_config) cg.add(var.set_forward_active_energy_sensor(i, sens)) - if CONF_REVERSE_ACTIVE_ENERGY in conf: - sens = await sensor.new_sensor(conf[CONF_REVERSE_ACTIVE_ENERGY]) + if reverse_active_energy_config := conf.get(CONF_REVERSE_ACTIVE_ENERGY): + sens = await sensor.new_sensor(reverse_active_energy_config) cg.add(var.set_reverse_active_energy_sensor(i, sens)) - if CONF_FREQUENCY in config: - sens = await sensor.new_sensor(config[CONF_FREQUENCY]) + + if frequency_config := config.get(CONF_FREQUENCY): + sens = await sensor.new_sensor(frequency_config) cg.add(var.set_freq_sensor(sens)) - if CONF_CHIP_TEMPERATURE in config: - sens = await sensor.new_sensor(config[CONF_CHIP_TEMPERATURE]) + if chip_temperature_config := config.get(CONF_CHIP_TEMPERATURE): + sens = await sensor.new_sensor(chip_temperature_config) cg.add(var.set_chip_temperature_sensor(sens)) + cg.add(var.set_line_freq(config[CONF_LINE_FREQUENCY])) cg.add(var.set_current_phases(config[CONF_CURRENT_PHASES])) cg.add(var.set_pga_gain(config[CONF_GAIN_PGA])) diff --git a/esphome/components/b_parasite/sensor.py b/esphome/components/b_parasite/sensor.py index 1b65bf7f1d..86eef29b14 100644 --- a/esphome/components/b_parasite/sensor.py +++ b/esphome/components/b_parasite/sensor.py @@ -87,6 +87,6 @@ async def to_code(config): (CONF_MOISTURE, var.set_soil_moisture), (CONF_ILLUMINANCE, var.set_illuminance), ]: - if config_key in config: - sens = await sensor.new_sensor(config[config_key]) + if sensor_config := config.get(config_key): + sens = await sensor.new_sensor(sensor_config) cg.add(setter(sens)) diff --git a/esphome/components/bang_bang/climate.py b/esphome/components/bang_bang/climate.py index 5c935987de..ac0c328000 100644 --- a/esphome/components/bang_bang/climate.py +++ b/esphome/components/bang_bang/climate.py @@ -57,19 +57,18 @@ async def to_code(config): var.get_idle_trigger(), [], config[CONF_IDLE_ACTION] ) - if CONF_COOL_ACTION in config: + if cool_action_config := config.get(CONF_COOL_ACTION): await automation.build_automation( - var.get_cool_trigger(), [], config[CONF_COOL_ACTION] + var.get_cool_trigger(), [], cool_action_config ) cg.add(var.set_supports_cool(True)) - if CONF_HEAT_ACTION in config: + if heat_action_config := config.get(CONF_HEAT_ACTION): await automation.build_automation( - var.get_heat_trigger(), [], config[CONF_HEAT_ACTION] + var.get_heat_trigger(), [], heat_action_config ) cg.add(var.set_supports_heat(True)) - if CONF_AWAY_CONFIG in config: - away = config[CONF_AWAY_CONFIG] + if away := config.get(CONF_AWAY_CONFIG): away_config = BangBangClimateTargetTempConfig( away[CONF_DEFAULT_TARGET_TEMPERATURE_LOW], away[CONF_DEFAULT_TARGET_TEMPERATURE_HIGH], diff --git a/esphome/components/bedjet/__init__.py b/esphome/components/bedjet/__init__.py index 1697c549b3..4ff3c34075 100644 --- a/esphome/components/bedjet/__init__.py +++ b/esphome/components/bedjet/__init__.py @@ -45,8 +45,8 @@ async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(var, config) await ble_client.register_ble_node(var, config) - if CONF_TIME_ID in config: - time_ = await cg.get_variable(config[CONF_TIME_ID]) + if time_id := config.get(CONF_TIME_ID): + time_ = await cg.get_variable(time_id) cg.add(var.set_time_id(time_)) - if CONF_RECEIVE_TIMEOUT in config: - cg.add(var.set_status_timeout(config[CONF_RECEIVE_TIMEOUT])) + if receive_timeout := config.get(CONF_RECEIVE_TIMEOUT): + cg.add(var.set_status_timeout(receive_timeout)) diff --git a/esphome/components/binary/fan/__init__.py b/esphome/components/binary/fan/__init__.py index 6edfa885c9..73d6b9339f 100644 --- a/esphome/components/binary/fan/__init__.py +++ b/esphome/components/binary/fan/__init__.py @@ -29,10 +29,10 @@ async def to_code(config): output_ = await cg.get_variable(config[CONF_OUTPUT]) cg.add(var.set_output(output_)) - if CONF_OSCILLATION_OUTPUT in config: - oscillation_output = await cg.get_variable(config[CONF_OSCILLATION_OUTPUT]) + if oscillation_output_id := config.get(CONF_OSCILLATION_OUTPUT): + oscillation_output = await cg.get_variable(oscillation_output_id) cg.add(var.set_oscillating(oscillation_output)) - if CONF_DIRECTION_OUTPUT in config: - direction_output = await cg.get_variable(config[CONF_DIRECTION_OUTPUT]) + if direction_output_id := config.get(CONF_DIRECTION_OUTPUT): + direction_output = await cg.get_variable(direction_output_id) cg.add(var.set_direction(direction_output)) diff --git a/esphome/components/binary_sensor/__init__.py b/esphome/components/binary_sensor/__init__.py index 95e35a45f2..fee6b6b434 100644 --- a/esphome/components/binary_sensor/__init__.py +++ b/esphome/components/binary_sensor/__init__.py @@ -467,14 +467,14 @@ def binary_sensor_schema( async def setup_binary_sensor_core_(var, config): await setup_entity(var, config) - if CONF_DEVICE_CLASS in config: - cg.add(var.set_device_class(config[CONF_DEVICE_CLASS])) - if CONF_PUBLISH_INITIAL_STATE in config: - cg.add(var.set_publish_initial_state(config[CONF_PUBLISH_INITIAL_STATE])) - if CONF_INVERTED in config: - cg.add(var.set_inverted(config[CONF_INVERTED])) - if CONF_FILTERS in config: - filters = await cg.build_registry_list(FILTER_REGISTRY, config[CONF_FILTERS]) + if device_class := config.get(CONF_DEVICE_CLASS): + cg.add(var.set_device_class(device_class)) + if publish_initial_state := config.get(CONF_PUBLISH_INITIAL_STATE): + cg.add(var.set_publish_initial_state(publish_initial_state)) + if inverted := config.get(CONF_INVERTED): + cg.add(var.set_inverted(inverted)) + if filters_config := config.get(CONF_FILTERS): + filters = await cg.build_registry_list(FILTER_REGISTRY, filters_config) cg.add(var.add_filters(filters)) for conf in config.get(CONF_ON_PRESS, []): @@ -518,8 +518,8 @@ async def setup_binary_sensor_core_(var, config): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) await automation.build_automation(trigger, [(bool, "x")], conf) - if CONF_MQTT_ID in config: - mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var) + if mqtt_id := config.get(CONF_MQTT_ID): + mqtt_ = cg.new_Pvariable(mqtt_id, var) await mqtt.register_mqtt_component(mqtt_, config) diff --git a/esphome/components/bl0939/sensor.py b/esphome/components/bl0939/sensor.py index 4c6e3ea4d9..2a85b34567 100644 --- a/esphome/components/bl0939/sensor.py +++ b/esphome/components/bl0939/sensor.py @@ -93,35 +93,27 @@ async def to_code(config): await cg.register_component(var, config) await uart.register_uart_device(var, config) - if CONF_VOLTAGE in config: - conf = config[CONF_VOLTAGE] - sens = await sensor.new_sensor(conf) + if voltage_config := config.get(CONF_VOLTAGE): + sens = await sensor.new_sensor(voltage_config) cg.add(var.set_voltage_sensor(sens)) - if CONF_CURRENT_1 in config: - conf = config[CONF_CURRENT_1] - sens = await sensor.new_sensor(conf) + if current_1_config := config.get(CONF_CURRENT_1): + sens = await sensor.new_sensor(current_1_config) cg.add(var.set_current_sensor_1(sens)) - if CONF_CURRENT_2 in config: - conf = config[CONF_CURRENT_2] - sens = await sensor.new_sensor(conf) + if current_2_config := config.get(CONF_CURRENT_2): + sens = await sensor.new_sensor(current_2_config) cg.add(var.set_current_sensor_2(sens)) - if CONF_ACTIVE_POWER_1 in config: - conf = config[CONF_ACTIVE_POWER_1] - sens = await sensor.new_sensor(conf) + if active_power_1_config := config.get(CONF_ACTIVE_POWER_1): + sens = await sensor.new_sensor(active_power_1_config) cg.add(var.set_power_sensor_1(sens)) - if CONF_ACTIVE_POWER_2 in config: - conf = config[CONF_ACTIVE_POWER_2] - sens = await sensor.new_sensor(conf) + if active_power_2_config := config.get(CONF_ACTIVE_POWER_2): + sens = await sensor.new_sensor(active_power_2_config) cg.add(var.set_power_sensor_2(sens)) - if CONF_ENERGY_1 in config: - conf = config[CONF_ENERGY_1] - sens = await sensor.new_sensor(conf) + if energy_1_config := config.get(CONF_ENERGY_1): + sens = await sensor.new_sensor(energy_1_config) cg.add(var.set_energy_sensor_1(sens)) - if CONF_ENERGY_2 in config: - conf = config[CONF_ENERGY_2] - sens = await sensor.new_sensor(conf) + if energy_2_config := config.get(CONF_ENERGY_2): + sens = await sensor.new_sensor(energy_2_config) cg.add(var.set_energy_sensor_2(sens)) - if CONF_ENERGY_TOTAL in config: - conf = config[CONF_ENERGY_TOTAL] - sens = await sensor.new_sensor(conf) + if energy_total_config := config.get(CONF_ENERGY_TOTAL): + sens = await sensor.new_sensor(energy_total_config) cg.add(var.set_energy_sensor_sum(sens)) diff --git a/esphome/components/bl0940/sensor.py b/esphome/components/bl0940/sensor.py index 9f516a8691..702230e020 100644 --- a/esphome/components/bl0940/sensor.py +++ b/esphome/components/bl0940/sensor.py @@ -79,27 +79,21 @@ async def to_code(config): await cg.register_component(var, config) await uart.register_uart_device(var, config) - if CONF_VOLTAGE in config: - conf = config[CONF_VOLTAGE] - sens = await sensor.new_sensor(conf) + if voltage_config := config.get(CONF_VOLTAGE): + sens = await sensor.new_sensor(voltage_config) cg.add(var.set_voltage_sensor(sens)) - if CONF_CURRENT in config: - conf = config[CONF_CURRENT] - sens = await sensor.new_sensor(conf) + if current_config := config.get(CONF_CURRENT): + sens = await sensor.new_sensor(current_config) cg.add(var.set_current_sensor(sens)) - if CONF_POWER in config: - conf = config[CONF_POWER] - sens = await sensor.new_sensor(conf) + if power_config := config.get(CONF_POWER): + sens = await sensor.new_sensor(power_config) cg.add(var.set_power_sensor(sens)) - if CONF_ENERGY in config: - conf = config[CONF_ENERGY] - sens = await sensor.new_sensor(conf) + if energy_config := config.get(CONF_ENERGY): + sens = await sensor.new_sensor(energy_config) cg.add(var.set_energy_sensor(sens)) - if CONF_INTERNAL_TEMPERATURE in config: - conf = config[CONF_INTERNAL_TEMPERATURE] - sens = await sensor.new_sensor(conf) + if internal_temperature_config := config.get(CONF_INTERNAL_TEMPERATURE): + sens = await sensor.new_sensor(internal_temperature_config) cg.add(var.set_internal_temperature_sensor(sens)) - if CONF_EXTERNAL_TEMPERATURE in config: - conf = config[CONF_EXTERNAL_TEMPERATURE] - sens = await sensor.new_sensor(conf) + if external_temperature_config := config.get(CONF_EXTERNAL_TEMPERATURE): + sens = await sensor.new_sensor(external_temperature_config) cg.add(var.set_external_temperature_sensor(sens)) diff --git a/esphome/components/bl0942/sensor.py b/esphome/components/bl0942/sensor.py index f23375b309..663eea0c4d 100644 --- a/esphome/components/bl0942/sensor.py +++ b/esphome/components/bl0942/sensor.py @@ -71,23 +71,18 @@ async def to_code(config): await cg.register_component(var, config) await uart.register_uart_device(var, config) - if CONF_VOLTAGE in config: - conf = config[CONF_VOLTAGE] - sens = await sensor.new_sensor(conf) + if voltage_config := config.get(CONF_VOLTAGE): + sens = await sensor.new_sensor(voltage_config) cg.add(var.set_voltage_sensor(sens)) - if CONF_CURRENT in config: - conf = config[CONF_CURRENT] - sens = await sensor.new_sensor(conf) + if current_config := config.get(CONF_CURRENT): + sens = await sensor.new_sensor(current_config) cg.add(var.set_current_sensor(sens)) - if CONF_POWER in config: - conf = config[CONF_POWER] - sens = await sensor.new_sensor(conf) + if power_config := config.get(CONF_POWER): + sens = await sensor.new_sensor(power_config) cg.add(var.set_power_sensor(sens)) - if CONF_ENERGY in config: - conf = config[CONF_ENERGY] - sens = await sensor.new_sensor(conf) + if energy_config := config.get(CONF_ENERGY): + sens = await sensor.new_sensor(energy_config) cg.add(var.set_energy_sensor(sens)) - if CONF_FREQUENCY in config: - conf = config[CONF_FREQUENCY] - sens = await sensor.new_sensor(conf) + if frequency_config := config.get(CONF_FREQUENCY): + sens = await sensor.new_sensor(frequency_config) cg.add(var.set_frequency_sensor(sens)) diff --git a/esphome/components/ble_client/sensor/__init__.py b/esphome/components/ble_client/sensor/__init__.py index c9bf2995b1..d0b27c30a9 100644 --- a/esphome/components/ble_client/sensor/__init__.py +++ b/esphome/components/ble_client/sensor/__init__.py @@ -129,32 +129,18 @@ async def characteristic_sensor_to_code(config): ) cg.add(var.set_char_uuid128(uuid128)) - if CONF_DESCRIPTOR_UUID in config: - if len(config[CONF_DESCRIPTOR_UUID]) == len(esp32_ble_tracker.bt_uuid16_format): - cg.add( - var.set_descr_uuid16( - esp32_ble_tracker.as_hex(config[CONF_DESCRIPTOR_UUID]) - ) - ) - elif len(config[CONF_DESCRIPTOR_UUID]) == len( - esp32_ble_tracker.bt_uuid32_format - ): - cg.add( - var.set_descr_uuid32( - esp32_ble_tracker.as_hex(config[CONF_DESCRIPTOR_UUID]) - ) - ) - elif len(config[CONF_DESCRIPTOR_UUID]) == len( - esp32_ble_tracker.bt_uuid128_format - ): - uuid128 = esp32_ble_tracker.as_reversed_hex_array( - config[CONF_DESCRIPTOR_UUID] - ) + if descriptor_uuid := config.get(CONF_DESCRIPTOR_UUID): + if len(descriptor_uuid) == len(esp32_ble_tracker.bt_uuid16_format): + cg.add(var.set_descr_uuid16(esp32_ble_tracker.as_hex(descriptor_uuid))) + elif len(descriptor_uuid) == len(esp32_ble_tracker.bt_uuid32_format): + cg.add(var.set_descr_uuid32(esp32_ble_tracker.as_hex(descriptor_uuid))) + elif len(descriptor_uuid) == len(esp32_ble_tracker.bt_uuid128_format): + uuid128 = esp32_ble_tracker.as_reversed_hex_array(descriptor_uuid) cg.add(var.set_descr_uuid128(uuid128)) - if CONF_LAMBDA in config: + if lambda_config := config.get(CONF_LAMBDA): lambda_ = await cg.process_lambda( - config[CONF_LAMBDA], [(adv_data_t_const_ref, "x")], return_type=cg.float_ + lambda_config, [(adv_data_t_const_ref, "x")], return_type=cg.float_ ) cg.add(var.set_data_to_value(lambda_)) diff --git a/esphome/components/ble_client/text_sensor/__init__.py b/esphome/components/ble_client/text_sensor/__init__.py index 66f00c551b..7a93c4e4ae 100644 --- a/esphome/components/ble_client/text_sensor/__init__.py +++ b/esphome/components/ble_client/text_sensor/__init__.py @@ -88,27 +88,13 @@ async def to_code(config): ) cg.add(var.set_char_uuid128(uuid128)) - if CONF_DESCRIPTOR_UUID in config: - if len(config[CONF_DESCRIPTOR_UUID]) == len(esp32_ble_tracker.bt_uuid16_format): - cg.add( - var.set_descr_uuid16( - esp32_ble_tracker.as_hex(config[CONF_DESCRIPTOR_UUID]) - ) - ) - elif len(config[CONF_DESCRIPTOR_UUID]) == len( - esp32_ble_tracker.bt_uuid32_format - ): - cg.add( - var.set_descr_uuid32( - esp32_ble_tracker.as_hex(config[CONF_DESCRIPTOR_UUID]) - ) - ) - elif len(config[CONF_DESCRIPTOR_UUID]) == len( - esp32_ble_tracker.bt_uuid128_format - ): - uuid128 = esp32_ble_tracker.as_reversed_hex_array( - config[CONF_DESCRIPTOR_UUID] - ) + if descriptor_uuid := config: + if len(descriptor_uuid) == len(esp32_ble_tracker.bt_uuid16_format): + cg.add(var.set_descr_uuid16(esp32_ble_tracker.as_hex(descriptor_uuid))) + elif len(descriptor_uuid) == len(esp32_ble_tracker.bt_uuid32_format): + cg.add(var.set_descr_uuid32(esp32_ble_tracker.as_hex(descriptor_uuid))) + elif len(descriptor_uuid) == len(esp32_ble_tracker.bt_uuid128_format): + uuid128 = esp32_ble_tracker.as_reversed_hex_array(descriptor_uuid) cg.add(var.set_descr_uuid128(uuid128)) await cg.register_component(var, config) diff --git a/esphome/components/ble_presence/binary_sensor.py b/esphome/components/ble_presence/binary_sensor.py index 75366ce864..2320d45287 100644 --- a/esphome/components/ble_presence/binary_sensor.py +++ b/esphome/components/ble_presence/binary_sensor.py @@ -55,35 +55,27 @@ async def to_code(config): await cg.register_component(var, config) await esp32_ble_tracker.register_ble_device(var, config) - if CONF_MIN_RSSI in config: - cg.add(var.set_minimum_rssi(config[CONF_MIN_RSSI])) + if min_rssi := config.get(CONF_MIN_RSSI): + cg.add(var.set_minimum_rssi(min_rssi)) - if CONF_MAC_ADDRESS in config: - cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex)) + if mac_address := config.get(CONF_MAC_ADDRESS): + cg.add(var.set_address(mac_address.as_hex)) - if CONF_SERVICE_UUID in config: - if len(config[CONF_SERVICE_UUID]) == len(esp32_ble_tracker.bt_uuid16_format): - cg.add( - var.set_service_uuid16( - esp32_ble_tracker.as_hex(config[CONF_SERVICE_UUID]) - ) - ) - elif len(config[CONF_SERVICE_UUID]) == len(esp32_ble_tracker.bt_uuid32_format): - cg.add( - var.set_service_uuid32( - esp32_ble_tracker.as_hex(config[CONF_SERVICE_UUID]) - ) - ) - elif len(config[CONF_SERVICE_UUID]) == len(esp32_ble_tracker.bt_uuid128_format): - uuid128 = esp32_ble_tracker.as_reversed_hex_array(config[CONF_SERVICE_UUID]) + if service_uuid := config.get(CONF_SERVICE_UUID): + if len(service_uuid) == len(esp32_ble_tracker.bt_uuid16_format): + cg.add(var.set_service_uuid16(esp32_ble_tracker.as_hex(service_uuid))) + elif len(service_uuid) == len(esp32_ble_tracker.bt_uuid32_format): + cg.add(var.set_service_uuid32(esp32_ble_tracker.as_hex(service_uuid))) + elif len(service_uuid) == len(esp32_ble_tracker.bt_uuid128_format): + uuid128 = esp32_ble_tracker.as_reversed_hex_array(service_uuid) cg.add(var.set_service_uuid128(uuid128)) - if CONF_IBEACON_UUID in config: - ibeacon_uuid = esp32_ble_tracker.as_hex_array(str(config[CONF_IBEACON_UUID])) + if ibeacon_uuid := config.get(CONF_IBEACON_UUID): + ibeacon_uuid = esp32_ble_tracker.as_hex_array(str(ibeacon_uuid)) cg.add(var.set_ibeacon_uuid(ibeacon_uuid)) - if CONF_IBEACON_MAJOR in config: - cg.add(var.set_ibeacon_major(config[CONF_IBEACON_MAJOR])) + if ibeacon_major := config.get(CONF_IBEACON_MAJOR): + cg.add(var.set_ibeacon_major(ibeacon_major)) - if CONF_IBEACON_MINOR in config: - cg.add(var.set_ibeacon_minor(config[CONF_IBEACON_MINOR])) + if ibeacon_minor := config.get(CONF_IBEACON_MINOR): + cg.add(var.set_ibeacon_minor(ibeacon_minor)) diff --git a/esphome/components/ble_rssi/sensor.py b/esphome/components/ble_rssi/sensor.py index 7c7bfc58a7..014843ab60 100644 --- a/esphome/components/ble_rssi/sensor.py +++ b/esphome/components/ble_rssi/sensor.py @@ -57,32 +57,24 @@ async def to_code(config): await cg.register_component(var, config) await esp32_ble_tracker.register_ble_device(var, config) - if CONF_MAC_ADDRESS in config: - cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex)) + if mac_address := config.get(CONF_MAC_ADDRESS): + cg.add(var.set_address(mac_address.as_hex)) - if CONF_SERVICE_UUID in config: - if len(config[CONF_SERVICE_UUID]) == len(esp32_ble_tracker.bt_uuid16_format): - cg.add( - var.set_service_uuid16( - esp32_ble_tracker.as_hex(config[CONF_SERVICE_UUID]) - ) - ) - elif len(config[CONF_SERVICE_UUID]) == len(esp32_ble_tracker.bt_uuid32_format): - cg.add( - var.set_service_uuid32( - esp32_ble_tracker.as_hex(config[CONF_SERVICE_UUID]) - ) - ) - elif len(config[CONF_SERVICE_UUID]) == len(esp32_ble_tracker.bt_uuid128_format): - uuid128 = esp32_ble_tracker.as_reversed_hex_array(config[CONF_SERVICE_UUID]) + if service_uuid := config.get(CONF_SERVICE_UUID): + if len(service_uuid) == len(esp32_ble_tracker.bt_uuid16_format): + cg.add(var.set_service_uuid16(esp32_ble_tracker.as_hex(service_uuid))) + elif len(service_uuid) == len(esp32_ble_tracker.bt_uuid32_format): + cg.add(var.set_service_uuid32(esp32_ble_tracker.as_hex(service_uuid))) + elif len(service_uuid) == len(esp32_ble_tracker.bt_uuid128_format): + uuid128 = esp32_ble_tracker.as_reversed_hex_array(service_uuid) cg.add(var.set_service_uuid128(uuid128)) - if CONF_IBEACON_UUID in config: - ibeacon_uuid = esp32_ble_tracker.as_hex_array(str(config[CONF_IBEACON_UUID])) + if ibeacon_uuid := config.get(CONF_IBEACON_UUID): + ibeacon_uuid = esp32_ble_tracker.as_hex_array(str(ibeacon_uuid)) cg.add(var.set_ibeacon_uuid(ibeacon_uuid)) - if CONF_IBEACON_MAJOR in config: - cg.add(var.set_ibeacon_major(config[CONF_IBEACON_MAJOR])) + if ibeacon_major := config.get(CONF_IBEACON_MAJOR): + cg.add(var.set_ibeacon_major(ibeacon_major)) - if CONF_IBEACON_MINOR in config: - cg.add(var.set_ibeacon_minor(config[CONF_IBEACON_MINOR])) + if ibeacon_minor := config.get(CONF_IBEACON_MINOR): + cg.add(var.set_ibeacon_minor(ibeacon_minor)) diff --git a/esphome/components/bme280/sensor.py b/esphome/components/bme280/sensor.py index dcb842d879..35744a436d 100644 --- a/esphome/components/bme280/sensor.py +++ b/esphome/components/bme280/sensor.py @@ -98,22 +98,19 @@ async def to_code(config): await cg.register_component(var, config) await i2c.register_i2c_device(var, config) - if CONF_TEMPERATURE in config: - conf = config[CONF_TEMPERATURE] - sens = await sensor.new_sensor(conf) + if temperature_config := config.get(CONF_TEMPERATURE): + sens = await sensor.new_sensor(temperature_config) cg.add(var.set_temperature_sensor(sens)) - cg.add(var.set_temperature_oversampling(conf[CONF_OVERSAMPLING])) + cg.add(var.set_temperature_oversampling(temperature_config[CONF_OVERSAMPLING])) - if CONF_PRESSURE in config: - conf = config[CONF_PRESSURE] - sens = await sensor.new_sensor(conf) + if pressure_config := config.get(CONF_PRESSURE): + sens = await sensor.new_sensor(pressure_config) cg.add(var.set_pressure_sensor(sens)) - cg.add(var.set_pressure_oversampling(conf[CONF_OVERSAMPLING])) + cg.add(var.set_pressure_oversampling(pressure_config[CONF_OVERSAMPLING])) - if CONF_HUMIDITY in config: - conf = config[CONF_HUMIDITY] - sens = await sensor.new_sensor(conf) + if humidity_config := config.get(CONF_HUMIDITY): + sens = await sensor.new_sensor(humidity_config) cg.add(var.set_humidity_sensor(sens)) - cg.add(var.set_humidity_oversampling(conf[CONF_OVERSAMPLING])) + cg.add(var.set_humidity_oversampling(humidity_config[CONF_OVERSAMPLING])) cg.add(var.set_iir_filter(config[CONF_IIR_FILTER])) diff --git a/esphome/components/bme680/sensor.py b/esphome/components/bme680/sensor.py index 76472c7562..586b454697 100644 --- a/esphome/components/bme680/sensor.py +++ b/esphome/components/bme680/sensor.py @@ -130,27 +130,23 @@ async def to_code(config): await cg.register_component(var, config) await i2c.register_i2c_device(var, config) - if CONF_TEMPERATURE in config: - conf = config[CONF_TEMPERATURE] - sens = await sensor.new_sensor(conf) + if temperature_config := config.get(CONF_TEMPERATURE): + sens = await sensor.new_sensor(temperature_config) cg.add(var.set_temperature_sensor(sens)) - cg.add(var.set_temperature_oversampling(conf[CONF_OVERSAMPLING])) + cg.add(var.set_temperature_oversampling(temperature_config[CONF_OVERSAMPLING])) - if CONF_PRESSURE in config: - conf = config[CONF_PRESSURE] - sens = await sensor.new_sensor(conf) + if pressure_config := config.get(CONF_PRESSURE): + sens = await sensor.new_sensor(pressure_config) cg.add(var.set_pressure_sensor(sens)) - cg.add(var.set_pressure_oversampling(conf[CONF_OVERSAMPLING])) + cg.add(var.set_pressure_oversampling(pressure_config[CONF_OVERSAMPLING])) - if CONF_HUMIDITY in config: - conf = config[CONF_HUMIDITY] - sens = await sensor.new_sensor(conf) + if humidity_config := config.get(CONF_HUMIDITY): + sens = await sensor.new_sensor(humidity_config) cg.add(var.set_humidity_sensor(sens)) - cg.add(var.set_humidity_oversampling(conf[CONF_OVERSAMPLING])) + cg.add(var.set_humidity_oversampling(humidity_config[CONF_OVERSAMPLING])) - if CONF_GAS_RESISTANCE in config: - conf = config[CONF_GAS_RESISTANCE] - sens = await sensor.new_sensor(conf) + if gas_resistance_config := config.get(CONF_GAS_RESISTANCE): + sens = await sensor.new_sensor(gas_resistance_config) cg.add(var.set_gas_resistance_sensor(sens)) cg.add(var.set_iir_filter(IIR_FILTER_OPTIONS[config[CONF_IIR_FILTER]])) diff --git a/esphome/components/bme680_bsec/sensor.py b/esphome/components/bme680_bsec/sensor.py index 3bd082481e..43b068b926 100644 --- a/esphome/components/bme680_bsec/sensor.py +++ b/esphome/components/bme680_bsec/sensor.py @@ -108,12 +108,13 @@ CONFIG_SCHEMA = cv.Schema( async def setup_conf(config, key, hub): - if key in config: - conf = config[key] - sens = await sensor.new_sensor(conf) + if sensor_config := config.get(key): + sens = await sensor.new_sensor(sensor_config) cg.add(getattr(hub, f"set_{key}_sensor")(sens)) - if CONF_SAMPLE_RATE in conf: - cg.add(getattr(hub, f"set_{key}_sample_rate")(conf[CONF_SAMPLE_RATE])) + if CONF_SAMPLE_RATE in sensor_config: + cg.add( + getattr(hub, f"set_{key}_sample_rate")(sensor_config[CONF_SAMPLE_RATE]) + ) async def to_code(config): diff --git a/esphome/components/bme680_bsec/text_sensor.py b/esphome/components/bme680_bsec/text_sensor.py index 2d93c90818..3494ba0cac 100644 --- a/esphome/components/bme680_bsec/text_sensor.py +++ b/esphome/components/bme680_bsec/text_sensor.py @@ -21,9 +21,8 @@ CONFIG_SCHEMA = cv.Schema( async def setup_conf(config, key, hub): - if key in config: - conf = config[key] - sens = await text_sensor.new_text_sensor(conf) + if sensor_config := config.get(key): + sens = await text_sensor.new_text_sensor(sensor_config) cg.add(getattr(hub, f"set_{key}_text_sensor")(sens)) diff --git a/esphome/components/bmp085/sensor.py b/esphome/components/bmp085/sensor.py index 52f554120a..83f5a0c821 100644 --- a/esphome/components/bmp085/sensor.py +++ b/esphome/components/bmp085/sensor.py @@ -47,12 +47,10 @@ async def to_code(config): await cg.register_component(var, config) await i2c.register_i2c_device(var, config) - if CONF_TEMPERATURE in config: - conf = config[CONF_TEMPERATURE] - sens = await sensor.new_sensor(conf) + if temperature_config := config.get(CONF_TEMPERATURE): + sens = await sensor.new_sensor(temperature_config) cg.add(var.set_temperature(sens)) - if CONF_PRESSURE in config: - conf = config[CONF_PRESSURE] - sens = await sensor.new_sensor(conf) + if pressure_config := config.get(CONF_PRESSURE): + sens = await sensor.new_sensor(pressure_config) cg.add(var.set_pressure(sens)) diff --git a/esphome/components/bmp280/sensor.py b/esphome/components/bmp280/sensor.py index e80511a509..a23bc0766a 100644 --- a/esphome/components/bmp280/sensor.py +++ b/esphome/components/bmp280/sensor.py @@ -83,16 +83,14 @@ async def to_code(config): await cg.register_component(var, config) await i2c.register_i2c_device(var, config) - if CONF_TEMPERATURE in config: - conf = config[CONF_TEMPERATURE] - sens = await sensor.new_sensor(conf) + if temperature_config := config.get(CONF_TEMPERATURE): + sens = await sensor.new_sensor(temperature_config) cg.add(var.set_temperature_sensor(sens)) - cg.add(var.set_temperature_oversampling(conf[CONF_OVERSAMPLING])) + cg.add(var.set_temperature_oversampling(temperature_config[CONF_OVERSAMPLING])) - if CONF_PRESSURE in config: - conf = config[CONF_PRESSURE] - sens = await sensor.new_sensor(conf) + if pressure_config := config.get(CONF_PRESSURE): + sens = await sensor.new_sensor(pressure_config) cg.add(var.set_pressure_sensor(sens)) - cg.add(var.set_pressure_oversampling(conf[CONF_OVERSAMPLING])) + cg.add(var.set_pressure_oversampling(pressure_config[CONF_OVERSAMPLING])) cg.add(var.set_iir_filter(config[CONF_IIR_FILTER])) diff --git a/esphome/components/bmp3xx/sensor.py b/esphome/components/bmp3xx/sensor.py index f0da1c3c24..6f90173c7b 100644 --- a/esphome/components/bmp3xx/sensor.py +++ b/esphome/components/bmp3xx/sensor.py @@ -87,14 +87,16 @@ async def to_code(config): await cg.register_component(var, config) await i2c.register_i2c_device(var, config) cg.add(var.set_iir_filter_config(config[CONF_IIR_FILTER])) - if CONF_TEMPERATURE in config: - conf = config[CONF_TEMPERATURE] - sens = await sensor.new_sensor(conf) + if temperature_config := config.get(CONF_TEMPERATURE): + sens = await sensor.new_sensor(temperature_config) cg.add(var.set_temperature_sensor(sens)) - cg.add(var.set_temperature_oversampling_config(conf[CONF_OVERSAMPLING])) + cg.add( + var.set_temperature_oversampling_config( + temperature_config[CONF_OVERSAMPLING] + ) + ) - if CONF_PRESSURE in config: - conf = config[CONF_PRESSURE] - sens = await sensor.new_sensor(conf) + if pressure_config := config.get(CONF_PRESSURE): + sens = await sensor.new_sensor(pressure_config) cg.add(var.set_pressure_sensor(sens)) - cg.add(var.set_pressure_oversampling_config(conf[CONF_OVERSAMPLING])) + cg.add(var.set_pressure_oversampling_config(pressure_config[CONF_OVERSAMPLING])) diff --git a/esphome/components/button/__init__.py b/esphome/components/button/__init__.py index a999c6d91e..5dcbf7ad01 100644 --- a/esphome/components/button/__init__.py +++ b/esphome/components/button/__init__.py @@ -85,11 +85,11 @@ async def setup_button_core_(var, config): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) await automation.build_automation(trigger, [], conf) - if CONF_DEVICE_CLASS in config: - cg.add(var.set_device_class(config[CONF_DEVICE_CLASS])) + if device_class := config.get(CONF_DEVICE_CLASS): + cg.add(var.set_device_class(device_class)) - if CONF_MQTT_ID in config: - mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var) + if mqtt_id := config.get(CONF_MQTT_ID): + mqtt_ = cg.new_Pvariable(mqtt_id, var) await mqtt.register_mqtt_component(mqtt_, config) diff --git a/esphome/components/canbus/__init__.py b/esphome/components/canbus/__init__.py index 1dbd743c75..efa55b220e 100644 --- a/esphome/components/canbus/__init__.py +++ b/esphome/components/canbus/__init__.py @@ -17,11 +17,10 @@ CONF_ON_FRAME = "on_frame" def validate_id(config): - if CONF_CAN_ID in config: - id_value = config[CONF_CAN_ID] + if can_id := config.get(CONF_CAN_ID): id_ext = config[CONF_USE_EXTENDED_ID] if not id_ext: - if id_value > 0x7FF: + if can_id > 0x7FF: raise cv.Invalid("Standard IDs must be 11 Bit (0x000-0x7ff / 0-2047)") return config @@ -145,8 +144,8 @@ async def canbus_action_to_code(config, action_id, template_arg, args): var = cg.new_Pvariable(action_id, template_arg) await cg.register_parented(var, config[CONF_CANBUS_ID]) - if CONF_CAN_ID in config: - can_id = await cg.templatable(config[CONF_CAN_ID], args, cg.uint32) + if can_id := config.get(CONF_CAN_ID): + can_id = await cg.templatable(can_id, args, cg.uint32) cg.add(var.set_can_id(can_id)) use_extended_id = await cg.templatable( config[CONF_USE_EXTENDED_ID], args, cg.uint32 diff --git a/esphome/components/cap1188/__init__.py b/esphome/components/cap1188/__init__.py index 80794c5146..74be9df186 100644 --- a/esphome/components/cap1188/__init__.py +++ b/esphome/components/cap1188/__init__.py @@ -37,8 +37,8 @@ async def to_code(config): cg.add(var.set_touch_threshold(config[CONF_TOUCH_THRESHOLD])) cg.add(var.set_allow_multiple_touches(config[CONF_ALLOW_MULTIPLE_TOUCHES])) - if CONF_RESET_PIN in config: - pin = await cg.gpio_pin_expression(config[CONF_RESET_PIN]) + if reset_pin_config := config.get(CONF_RESET_PIN): + pin = await cg.gpio_pin_expression(reset_pin_config) cg.add(var.set_reset_pin(pin)) await cg.register_component(var, config) diff --git a/esphome/components/ccs811/sensor.py b/esphome/components/ccs811/sensor.py index af3e6574ab..4133255b15 100644 --- a/esphome/components/ccs811/sensor.py +++ b/esphome/components/ccs811/sensor.py @@ -69,16 +69,16 @@ async def to_code(config): sens = await sensor.new_sensor(config[CONF_TVOC]) cg.add(var.set_tvoc(sens)) - if CONF_VERSION in config: - sens = await text_sensor.new_text_sensor(config[CONF_VERSION]) + if version_config := config.get(CONF_VERSION): + sens = await text_sensor.new_text_sensor(version_config) cg.add(var.set_version(sens)) - if CONF_BASELINE in config: - cg.add(var.set_baseline(config[CONF_BASELINE])) + if baseline := config.get(CONF_BASELINE): + cg.add(var.set_baseline(baseline)) - if CONF_TEMPERATURE in config: - sens = await cg.get_variable(config[CONF_TEMPERATURE]) + if temperature_id := config.get(CONF_TEMPERATURE): + sens = await cg.get_variable(temperature_id) cg.add(var.set_temperature(sens)) - if CONF_HUMIDITY in config: - sens = await cg.get_variable(config[CONF_HUMIDITY]) + if humidity_id := config.get(CONF_HUMIDITY): + sens = await cg.get_variable(humidity_id) cg.add(var.set_humidity(sens)) diff --git a/esphome/components/climate_ir/__init__.py b/esphome/components/climate_ir/__init__.py index 1389ebfc6d..0cf1339971 100644 --- a/esphome/components/climate_ir/__init__.py +++ b/esphome/components/climate_ir/__init__.py @@ -44,11 +44,11 @@ async def register_climate_ir(var, config): cg.add(var.set_supports_cool(config[CONF_SUPPORTS_COOL])) cg.add(var.set_supports_heat(config[CONF_SUPPORTS_HEAT])) - if CONF_SENSOR in config: - sens = await cg.get_variable(config[CONF_SENSOR]) + if sensor_id := config.get(CONF_SENSOR): + sens = await cg.get_variable(sensor_id) cg.add(var.set_sensor(sens)) - if CONF_RECEIVER_ID in config: - receiver = await cg.get_variable(config[CONF_RECEIVER_ID]) + if receiver_id := config.get(CONF_RECEIVER_ID): + receiver = await cg.get_variable(receiver_id) cg.add(receiver.register_listener(var)) transmitter = await cg.get_variable(config[CONF_TRANSMITTER_ID]) diff --git a/esphome/components/cs5460a/sensor.py b/esphome/components/cs5460a/sensor.py index d6e3c2ba48..c27fc5fc3c 100644 --- a/esphome/components/cs5460a/sensor.py +++ b/esphome/components/cs5460a/sensor.py @@ -113,17 +113,14 @@ async def to_code(config): cg.add(var.set_hpf_enable(config[CONF_CURRENT_HPF], config[CONF_VOLTAGE_HPF])) cg.add(var.set_pulse_energy_wh(config[CONF_PULSE_ENERGY])) - if CONF_VOLTAGE in config: - conf = config[CONF_VOLTAGE] - sens = await sensor.new_sensor(conf) + if voltage_config := config.get(CONF_VOLTAGE): + sens = await sensor.new_sensor(voltage_config) cg.add(var.set_voltage_sensor(sens)) - if CONF_CURRENT in config: - conf = config[CONF_CURRENT] - sens = await sensor.new_sensor(conf) + if current_config := config.get(CONF_CURRENT): + sens = await sensor.new_sensor(current_config) cg.add(var.set_current_sensor(sens)) - if CONF_POWER in config: - conf = config[CONF_POWER] - sens = await sensor.new_sensor(conf) + if power_config := config.get(CONF_POWER): + sens = await sensor.new_sensor(power_config) cg.add(var.set_power_sensor(sens)) diff --git a/esphome/components/cse7766/sensor.py b/esphome/components/cse7766/sensor.py index 2f48aff0aa..d98b351287 100644 --- a/esphome/components/cse7766/sensor.py +++ b/esphome/components/cse7766/sensor.py @@ -69,19 +69,15 @@ async def to_code(config): await cg.register_component(var, config) await uart.register_uart_device(var, config) - if CONF_VOLTAGE in config: - conf = config[CONF_VOLTAGE] - sens = await sensor.new_sensor(conf) + if voltage_config := config.get(CONF_VOLTAGE): + sens = await sensor.new_sensor(voltage_config) cg.add(var.set_voltage_sensor(sens)) - if CONF_CURRENT in config: - conf = config[CONF_CURRENT] - sens = await sensor.new_sensor(conf) + if current_config := config.get(CONF_CURRENT): + sens = await sensor.new_sensor(current_config) cg.add(var.set_current_sensor(sens)) - if CONF_POWER in config: - conf = config[CONF_POWER] - sens = await sensor.new_sensor(conf) + if power_config := config.get(CONF_POWER): + sens = await sensor.new_sensor(power_config) cg.add(var.set_power_sensor(sens)) - if CONF_ENERGY in config: - conf = config[CONF_ENERGY] - sens = await sensor.new_sensor(conf) + if energy_config := config.get(CONF_ENERGY): + sens = await sensor.new_sensor(energy_config) cg.add(var.set_energy_sensor(sens)) diff --git a/esphome/components/current_based/cover.py b/esphome/components/current_based/cover.py index eb77a90aff..41732305a1 100644 --- a/esphome/components/current_based/cover.py +++ b/esphome/components/current_based/cover.py @@ -66,59 +66,57 @@ CONFIG_SCHEMA = cover.COVER_SCHEMA.extend( ).extend(cv.COMPONENT_SCHEMA) -def to_code(config): +async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) - yield cg.register_component(var, config) - yield cover.register_cover(var, config) + await cg.register_component(var, config) + await cover.register_cover(var, config) - yield automation.build_automation( + await automation.build_automation( var.get_stop_trigger(), [], config[CONF_STOP_ACTION] ) # OPEN - bin = yield cg.get_variable(config[CONF_OPEN_SENSOR]) + bin = await cg.get_variable(config[CONF_OPEN_SENSOR]) cg.add(var.set_open_sensor(bin)) cg.add( var.set_open_moving_current_threshold( config[CONF_OPEN_MOVING_CURRENT_THRESHOLD] ) ) - if CONF_OPEN_OBSTACLE_CURRENT_THRESHOLD in config: - cg.add( - var.set_open_obstacle_current_threshold( - config[CONF_OPEN_OBSTACLE_CURRENT_THRESHOLD] - ) - ) + if open_obsticle_current_threshold := config.get( + CONF_OPEN_OBSTACLE_CURRENT_THRESHOLD + ): + cg.add(var.set_open_obstacle_current_threshold(open_obsticle_current_threshold)) cg.add(var.set_open_duration(config[CONF_OPEN_DURATION])) - yield automation.build_automation( + await automation.build_automation( var.get_open_trigger(), [], config[CONF_OPEN_ACTION] ) # CLOSE - bin = yield cg.get_variable(config[CONF_CLOSE_SENSOR]) + bin = await cg.get_variable(config[CONF_CLOSE_SENSOR]) cg.add(var.set_close_sensor(bin)) cg.add( var.set_close_moving_current_threshold( config[CONF_CLOSE_MOVING_CURRENT_THRESHOLD] ) ) - if CONF_CLOSE_OBSTACLE_CURRENT_THRESHOLD in config: + if close_obsticle_current_threshold := config.get( + CONF_CLOSE_OBSTACLE_CURRENT_THRESHOLD + ): cg.add( - var.set_close_obstacle_current_threshold( - config[CONF_CLOSE_OBSTACLE_CURRENT_THRESHOLD] - ) + var.set_close_obstacle_current_threshold(close_obsticle_current_threshold) ) cg.add(var.set_close_duration(config[CONF_CLOSE_DURATION])) - yield automation.build_automation( + await automation.build_automation( var.get_close_trigger(), [], config[CONF_CLOSE_ACTION] ) cg.add(var.set_obstacle_rollback(config[CONF_OBSTACLE_ROLLBACK])) - if CONF_MAX_DURATION in config: - cg.add(var.set_max_duration(config[CONF_MAX_DURATION])) + if max_duration := config.get(CONF_MAX_DURATION): + cg.add(var.set_max_duration(max_duration)) cg.add(var.set_malfunction_detection(config[CONF_MALFUNCTION_DETECTION])) - if CONF_MALFUNCTION_ACTION in config: - yield automation.build_automation( - var.get_malfunction_trigger(), [], config[CONF_MALFUNCTION_ACTION] + if malfunction_action := config.get(CONF_MALFUNCTION_ACTION): + await automation.build_automation( + var.get_malfunction_trigger(), [], malfunction_action ) cg.add(var.set_start_sensing_delay(config[CONF_START_SENSING_DELAY])) diff --git a/esphome/components/cwww/light.py b/esphome/components/cwww/light.py index fc204b2f3b..c88a6b0054 100644 --- a/esphome/components/cwww/light.py +++ b/esphome/components/cwww/light.py @@ -37,16 +37,12 @@ async def to_code(config): cwhite = await cg.get_variable(config[CONF_COLD_WHITE]) cg.add(var.set_cold_white(cwhite)) - if CONF_COLD_WHITE_COLOR_TEMPERATURE in config: - cg.add( - var.set_cold_white_temperature(config[CONF_COLD_WHITE_COLOR_TEMPERATURE]) - ) + if cold_white_color_temperature := config.get(CONF_COLD_WHITE_COLOR_TEMPERATURE): + cg.add(var.set_cold_white_temperature(cold_white_color_temperature)) wwhite = await cg.get_variable(config[CONF_WARM_WHITE]) cg.add(var.set_warm_white(wwhite)) - if CONF_WARM_WHITE_COLOR_TEMPERATURE in config: - cg.add( - var.set_warm_white_temperature(config[CONF_WARM_WHITE_COLOR_TEMPERATURE]) - ) + if warm_white_color_temperature := config.get(CONF_WARM_WHITE_COLOR_TEMPERATURE): + cg.add(var.set_warm_white_temperature(warm_white_color_temperature)) cg.add(var.set_constant_brightness(config[CONF_CONSTANT_BRIGHTNESS])) From dfffa67c0fa6d693df28100c31df05043412af0f Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 2 Aug 2023 16:35:16 +1200 Subject: [PATCH 55/84] Bump click from 8.1.5 to 8.1.6 (#5179) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index d5ca30a387..3160fe42de 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,7 @@ tzdata>=2021.1 # from time pyserial==3.5 platformio==6.1.7 # When updating platformio, also update Dockerfile esptool==4.6.2 -click==8.1.5 +click==8.1.6 esphome-dashboard==20230711.0 aioesphomeapi==15.0.0 zeroconf==0.71.4 From 0ae3fcb0b741648648ec784fc89ab5104d702ae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Soko=C5=82owski?= Date: Mon, 7 Aug 2023 01:41:44 +0200 Subject: [PATCH 56/84] PWM Output on RP2040 for high frequencies (#5204) --- esphome/components/rp2040_pwm/rp2040_pwm.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/esphome/components/rp2040_pwm/rp2040_pwm.cpp b/esphome/components/rp2040_pwm/rp2040_pwm.cpp index 3c5591885e..170af59905 100644 --- a/esphome/components/rp2040_pwm/rp2040_pwm.cpp +++ b/esphome/components/rp2040_pwm/rp2040_pwm.cpp @@ -27,8 +27,12 @@ void RP2040PWM::setup_pwm_() { uint32_t clock = clock_get_hz(clk_sys); float divider = ceil(clock / (4096 * this->frequency_)) / 16.0f; + if (divider < 1.0f) { + divider = 1.0f; + } uint16_t wrap = clock / divider / this->frequency_ - 1; this->wrap_ = wrap; + ESP_LOGD(TAG, "divider=%.5f, wrap=%d, clock=%d", divider, wrap, clock); pwm_config_set_clkdiv(&config, divider); pwm_config_set_wrap(&config, wrap); From 00f9af70a92da04894e0d349a01d26dc9887c62c Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Mon, 7 Aug 2023 11:48:23 +1200 Subject: [PATCH 57/84] Fix some configs after #5181 (#5209) --- esphome/components/adc/sensor.py | 3 +-- esphome/components/animation/__init__.py | 2 +- esphome/components/bedjet/__init__.py | 2 +- esphome/components/binary_sensor/__init__.py | 2 +- .../components/ble_presence/binary_sensor.py | 4 ++-- esphome/components/ble_rssi/sensor.py | 4 ++-- esphome/components/canbus/__init__.py | 10 +++++----- esphome/components/ccs811/sensor.py | 2 +- esphome/components/current_based/cover.py | 20 ++++++++++++------- 9 files changed, 27 insertions(+), 22 deletions(-) diff --git a/esphome/components/adc/sensor.py b/esphome/components/adc/sensor.py index 99d3652698..c1ae22214d 100644 --- a/esphome/components/adc/sensor.py +++ b/esphome/components/adc/sensor.py @@ -89,8 +89,7 @@ async def to_code(config): pin = await cg.gpio_pin_expression(config[CONF_PIN]) cg.add(var.set_pin(pin)) - if raw := config.get(CONF_RAW): - cg.add(var.set_output_raw(raw)) + cg.add(var.set_output_raw(config[CONF_RAW])) if attenuation := config.get(CONF_ATTENUATION): if attenuation == "auto": diff --git a/esphome/components/animation/__init__.py b/esphome/components/animation/__init__.py index 7fc1e0dcd0..52e14f0a43 100644 --- a/esphome/components/animation/__init__.py +++ b/esphome/components/animation/__init__.py @@ -115,7 +115,7 @@ async def animation_action_to_code(config, action_id, template_arg, args): paren = await cg.get_variable(config[CONF_ID]) var = cg.new_Pvariable(action_id, template_arg, paren) - if frame := config.get(CONF_FRAME): + if (frame := config.get(CONF_FRAME)) is not None: template_ = await cg.templatable(frame, args, cg.uint16) cg.add(var.set_frame(template_)) return var diff --git a/esphome/components/bedjet/__init__.py b/esphome/components/bedjet/__init__.py index 4ff3c34075..395a5f25e4 100644 --- a/esphome/components/bedjet/__init__.py +++ b/esphome/components/bedjet/__init__.py @@ -48,5 +48,5 @@ async def to_code(config): if time_id := config.get(CONF_TIME_ID): time_ = await cg.get_variable(time_id) cg.add(var.set_time_id(time_)) - if receive_timeout := config.get(CONF_RECEIVE_TIMEOUT): + if (receive_timeout := config.get(CONF_RECEIVE_TIMEOUT)) is not None: cg.add(var.set_status_timeout(receive_timeout)) diff --git a/esphome/components/binary_sensor/__init__.py b/esphome/components/binary_sensor/__init__.py index fee6b6b434..babe46e082 100644 --- a/esphome/components/binary_sensor/__init__.py +++ b/esphome/components/binary_sensor/__init__.py @@ -467,7 +467,7 @@ def binary_sensor_schema( async def setup_binary_sensor_core_(var, config): await setup_entity(var, config) - if device_class := config.get(CONF_DEVICE_CLASS): + if (device_class := config.get(CONF_DEVICE_CLASS)) is not None: cg.add(var.set_device_class(device_class)) if publish_initial_state := config.get(CONF_PUBLISH_INITIAL_STATE): cg.add(var.set_publish_initial_state(publish_initial_state)) diff --git a/esphome/components/ble_presence/binary_sensor.py b/esphome/components/ble_presence/binary_sensor.py index 2320d45287..81878391bb 100644 --- a/esphome/components/ble_presence/binary_sensor.py +++ b/esphome/components/ble_presence/binary_sensor.py @@ -74,8 +74,8 @@ async def to_code(config): ibeacon_uuid = esp32_ble_tracker.as_hex_array(str(ibeacon_uuid)) cg.add(var.set_ibeacon_uuid(ibeacon_uuid)) - if ibeacon_major := config.get(CONF_IBEACON_MAJOR): + if (ibeacon_major := config.get(CONF_IBEACON_MAJOR)) is not None: cg.add(var.set_ibeacon_major(ibeacon_major)) - if ibeacon_minor := config.get(CONF_IBEACON_MINOR): + if (ibeacon_minor := config.get(CONF_IBEACON_MINOR)) is not None: cg.add(var.set_ibeacon_minor(ibeacon_minor)) diff --git a/esphome/components/ble_rssi/sensor.py b/esphome/components/ble_rssi/sensor.py index 014843ab60..4246d311ab 100644 --- a/esphome/components/ble_rssi/sensor.py +++ b/esphome/components/ble_rssi/sensor.py @@ -73,8 +73,8 @@ async def to_code(config): ibeacon_uuid = esp32_ble_tracker.as_hex_array(str(ibeacon_uuid)) cg.add(var.set_ibeacon_uuid(ibeacon_uuid)) - if ibeacon_major := config.get(CONF_IBEACON_MAJOR): + if (ibeacon_major := config.get(CONF_IBEACON_MAJOR)) is not None: cg.add(var.set_ibeacon_major(ibeacon_major)) - if ibeacon_minor := config.get(CONF_IBEACON_MINOR): + if (ibeacon_minor := config.get(CONF_IBEACON_MINOR)) is not None: cg.add(var.set_ibeacon_minor(ibeacon_minor)) diff --git a/esphome/components/canbus/__init__.py b/esphome/components/canbus/__init__.py index efa55b220e..c5a9924644 100644 --- a/esphome/components/canbus/__init__.py +++ b/esphome/components/canbus/__init__.py @@ -17,11 +17,11 @@ CONF_ON_FRAME = "on_frame" def validate_id(config): - if can_id := config.get(CONF_CAN_ID): - id_ext = config[CONF_USE_EXTENDED_ID] - if not id_ext: - if can_id > 0x7FF: - raise cv.Invalid("Standard IDs must be 11 Bit (0x000-0x7ff / 0-2047)") + can_id = config[CONF_CAN_ID] + id_ext = config[CONF_USE_EXTENDED_ID] + if not id_ext: + if can_id > 0x7FF: + raise cv.Invalid("Standard IDs must be 11 Bit (0x000-0x7ff / 0-2047)") return config diff --git a/esphome/components/ccs811/sensor.py b/esphome/components/ccs811/sensor.py index 4133255b15..66d9274fe8 100644 --- a/esphome/components/ccs811/sensor.py +++ b/esphome/components/ccs811/sensor.py @@ -73,7 +73,7 @@ async def to_code(config): sens = await text_sensor.new_text_sensor(version_config) cg.add(var.set_version(sens)) - if baseline := config.get(CONF_BASELINE): + if (baseline := config.get(CONF_BASELINE)) is not None: cg.add(var.set_baseline(baseline)) if temperature_id := config.get(CONF_TEMPERATURE): diff --git a/esphome/components/current_based/cover.py b/esphome/components/current_based/cover.py index 41732305a1..1fa4eaa03f 100644 --- a/esphome/components/current_based/cover.py +++ b/esphome/components/current_based/cover.py @@ -83,10 +83,13 @@ async def to_code(config): config[CONF_OPEN_MOVING_CURRENT_THRESHOLD] ) ) - if open_obsticle_current_threshold := config.get( - CONF_OPEN_OBSTACLE_CURRENT_THRESHOLD - ): + if ( + open_obsticle_current_threshold := config.get( + CONF_OPEN_OBSTACLE_CURRENT_THRESHOLD + ) + ) is not None: cg.add(var.set_open_obstacle_current_threshold(open_obsticle_current_threshold)) + cg.add(var.set_open_duration(config[CONF_OPEN_DURATION])) await automation.build_automation( var.get_open_trigger(), [], config[CONF_OPEN_ACTION] @@ -100,19 +103,22 @@ async def to_code(config): config[CONF_CLOSE_MOVING_CURRENT_THRESHOLD] ) ) - if close_obsticle_current_threshold := config.get( - CONF_CLOSE_OBSTACLE_CURRENT_THRESHOLD - ): + if ( + close_obsticle_current_threshold := config.get( + CONF_CLOSE_OBSTACLE_CURRENT_THRESHOLD + ) + ) is not None: cg.add( var.set_close_obstacle_current_threshold(close_obsticle_current_threshold) ) + cg.add(var.set_close_duration(config[CONF_CLOSE_DURATION])) await automation.build_automation( var.get_close_trigger(), [], config[CONF_CLOSE_ACTION] ) cg.add(var.set_obstacle_rollback(config[CONF_OBSTACLE_ROLLBACK])) - if max_duration := config.get(CONF_MAX_DURATION): + if (max_duration := config.get(CONF_MAX_DURATION)) is not None: cg.add(var.set_max_duration(max_duration)) cg.add(var.set_malfunction_detection(config[CONF_MALFUNCTION_DETECTION])) if malfunction_action := config.get(CONF_MALFUNCTION_ACTION): From 62fed4c1eb1079f29e72e1a80b749a8dafe43f8e Mon Sep 17 00:00:00 2001 From: arno1801 <120399978+arno1801@users.noreply.github.com> Date: Sun, 6 Aug 2023 19:59:17 -0400 Subject: [PATCH 58/84] Improved compensation sgp30 (#5208) --- esphome/components/sgp30/sgp30.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/esphome/components/sgp30/sgp30.cpp b/esphome/components/sgp30/sgp30.cpp index a6572620c4..25a3c1ab8f 100644 --- a/esphome/components/sgp30/sgp30.cpp +++ b/esphome/components/sgp30/sgp30.cpp @@ -181,9 +181,18 @@ void SGP30Component::send_env_data_() { ESP_LOGD(TAG, "External compensation data received: Temperature %0.2f°C", temperature); } - float absolute_humidity = - 216.7f * (((humidity / 100) * 6.112f * std::exp((17.62f * temperature) / (243.12f + temperature))) / - (273.15f + temperature)); + float absolute_humidity; + if (temperature < 0) { + absolute_humidity = + 216.67f * + ((humidity * 0.061121f * std::exp((23.036f - temperature / 333.7f) * (temperature / (279.82f + temperature)))) / + (273.15f + temperature)); + } else { + absolute_humidity = + 216.67f * + ((humidity * 0.061121f * std::exp((18.678f - temperature / 234.5f) * (temperature / (257.14f + temperature)))) / + (273.15f + temperature)); + } uint8_t humidity_full = uint8_t(std::floor(absolute_humidity)); uint8_t humidity_dec = uint8_t(std::floor((absolute_humidity - std::floor(absolute_humidity)) * 256)); ESP_LOGD(TAG, "Calculated Absolute humidity: %0.3f g/m³ (0x%04X)", absolute_humidity, From 1495fada9049cefe6d9fac3a9df6b10e19b630a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Cirne?= Date: Mon, 7 Aug 2023 01:22:18 +0100 Subject: [PATCH 59/84] Add support for a01nyub (#4863) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- CODEOWNERS | 1 + esphome/components/a01nyub/__init__.py | 1 + esphome/components/a01nyub/a01nyub.cpp | 57 ++++++++++++++++++++++++++ esphome/components/a01nyub/a01nyub.h | 27 ++++++++++++ esphome/components/a01nyub/sensor.py | 41 ++++++++++++++++++ tests/test4.yaml | 18 ++++++-- 6 files changed, 142 insertions(+), 3 deletions(-) create mode 100644 esphome/components/a01nyub/__init__.py create mode 100644 esphome/components/a01nyub/a01nyub.cpp create mode 100644 esphome/components/a01nyub/a01nyub.h create mode 100644 esphome/components/a01nyub/sensor.py diff --git a/CODEOWNERS b/CODEOWNERS index 122dc71b48..17d1462405 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -11,6 +11,7 @@ esphome/*.py @esphome/core esphome/core/* @esphome/core # Integrations +esphome/components/a01nyub/* @MrSuicideParrot esphome/components/absolute_humidity/* @DAVe3283 esphome/components/ac_dimmer/* @glmnet esphome/components/adc/* @esphome/core diff --git a/esphome/components/a01nyub/__init__.py b/esphome/components/a01nyub/__init__.py new file mode 100644 index 0000000000..4c84847fb6 --- /dev/null +++ b/esphome/components/a01nyub/__init__.py @@ -0,0 +1 @@ +CODEOWNERS = ["@MrSuicideParrot"] diff --git a/esphome/components/a01nyub/a01nyub.cpp b/esphome/components/a01nyub/a01nyub.cpp new file mode 100644 index 0000000000..75cb276f84 --- /dev/null +++ b/esphome/components/a01nyub/a01nyub.cpp @@ -0,0 +1,57 @@ +// Datasheet https://wiki.dfrobot.com/A01NYUB%20Waterproof%20Ultrasonic%20Sensor%20SKU:%20SEN0313 + +#include "a01nyub.h" +#include "esphome/core/helpers.h" +#include "esphome/core/log.h" + +namespace esphome { +namespace a01nyub { + +static const char *const TAG = "a01nyub.sensor"; +static const uint8_t MAX_DATA_LENGTH_BYTES = 4; + +void A01nyubComponent::loop() { + uint8_t data; + while (this->available() > 0) { + if (this->read_byte(&data)) { + buffer_.push_back(data); + this->check_buffer_(); + } + } +} + +void A01nyubComponent::check_buffer_() { + if (this->buffer_.size() >= MAX_DATA_LENGTH_BYTES) { + size_t i; + for (i = 0; i < this->buffer_.size(); i++) { + // Look for the first packet + if (this->buffer_[i] == 0xFF) { + if (i + 1 + 3 < this->buffer_.size()) { // Packet is not complete + return; // Wait for completion + } + + uint8_t checksum = (this->buffer_[i] + this->buffer_[i + 1] + this->buffer_[i + 2]) & 0xFF; + if (this->buffer_[i + 3] == checksum) { + float distance = (this->buffer_[i + 1] << 8) + this->buffer_[i + 2]; + if (distance > 280) { + float meters = distance / 1000.0; + ESP_LOGV(TAG, "Distance from sensor: %f mm, %f m", distance, meters); + this->publish_state(meters); + } else { + ESP_LOGW(TAG, "Invalid data read from sensor: %s", format_hex_pretty(this->buffer_).c_str()); + } + } + break; + } + } + this->buffer_.clear(); + } +} + +void A01nyubComponent::dump_config() { + ESP_LOGCONFIG(TAG, "A01nyub Sensor:"); + LOG_SENSOR(" ", "Distance", this); +} + +} // namespace a01nyub +} // namespace esphome diff --git a/esphome/components/a01nyub/a01nyub.h b/esphome/components/a01nyub/a01nyub.h new file mode 100644 index 0000000000..6b22e9bcad --- /dev/null +++ b/esphome/components/a01nyub/a01nyub.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +#include "esphome/core/component.h" +#include "esphome/components/sensor/sensor.h" +#include "esphome/components/uart/uart.h" + +namespace esphome { +namespace a01nyub { + +class A01nyubComponent : public sensor::Sensor, public Component, public uart::UARTDevice { + public: + // Nothing really public. + + // ========== INTERNAL METHODS ========== + void loop() override; + void dump_config() override; + + protected: + void check_buffer_(); + + std::vector buffer_; +}; + +} // namespace a01nyub +} // namespace esphome diff --git a/esphome/components/a01nyub/sensor.py b/esphome/components/a01nyub/sensor.py new file mode 100644 index 0000000000..b57daa0357 --- /dev/null +++ b/esphome/components/a01nyub/sensor.py @@ -0,0 +1,41 @@ +import esphome.codegen as cg +from esphome.components import sensor, uart +from esphome.const import ( + STATE_CLASS_MEASUREMENT, + UNIT_METER, + ICON_ARROW_EXPAND_VERTICAL, + DEVICE_CLASS_DISTANCE, +) + +CODEOWNERS = ["@MrSuicideParrot"] +DEPENDENCIES = ["uart"] + +a01nyub_ns = cg.esphome_ns.namespace("a01nyub") +A01nyubComponent = a01nyub_ns.class_( + "A01nyubComponent", sensor.Sensor, cg.Component, uart.UARTDevice +) + +CONFIG_SCHEMA = sensor.sensor_schema( + A01nyubComponent, + unit_of_measurement=UNIT_METER, + icon=ICON_ARROW_EXPAND_VERTICAL, + accuracy_decimals=3, + state_class=STATE_CLASS_MEASUREMENT, + device_class=DEVICE_CLASS_DISTANCE, +).extend(uart.UART_DEVICE_SCHEMA) + +FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema( + "a01nyub", + baud_rate=9600, + require_tx=False, + require_rx=True, + data_bits=8, + parity=None, + stop_bits=1, +) + + +async def to_code(config): + var = await sensor.new_sensor(config) + await cg.register_component(var, config) + await uart.register_uart_device(var, config) diff --git a/tests/test4.yaml b/tests/test4.yaml index 2a8cb02413..54caebf1fe 100644 --- a/tests/test4.yaml +++ b/tests/test4.yaml @@ -34,9 +34,14 @@ spi: miso_pin: GPIO23 uart: - tx_pin: GPIO22 - rx_pin: GPIO23 - baud_rate: 115200 + - id: uart115200 + tx_pin: GPIO22 + rx_pin: GPIO23 + baud_rate: 115200 + - id: uart9600 + tx_pin: GPIO22 + rx_pin: GPIO23 + baud_rate: 9600 ota: safe_mode: true @@ -58,6 +63,7 @@ time: tuya: time_id: sntp_time + uart_id: uart115200 status_pin: number: 14 inverted: true @@ -73,6 +79,7 @@ select: pipsolar: id: inverter0 + uart_id: uart115200 sx1509: - id: sx1509_hub @@ -229,6 +236,7 @@ sensor: name: inverter0_pv_charging_power - platform: hrxl_maxsonar_wr name: Rainwater Tank Level + uart_id: uart115200 filters: - sliding_window_moving_average: window_size: 12 @@ -257,6 +265,10 @@ sensor: name: Ufire Temperature ph: name: Ufire pH + - platform: a01nyub + id: a01nyub_sensor + name: "a01nyub Distance" + uart_id: uart9600 # # platform sensor.apds9960 requires component apds9960 From 3a07121784c3c69c5966d0ae23ac9fd677703b75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Kozubal?= Date: Mon, 7 Aug 2023 03:46:31 +0200 Subject: [PATCH 60/84] Change device name in MQTT discovery messages to friendly names (#5205) --- esphome/components/mqtt/mqtt_component.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/esphome/components/mqtt/mqtt_component.cpp b/esphome/components/mqtt/mqtt_component.cpp index b663d7751d..1c7d9f86dd 100644 --- a/esphome/components/mqtt/mqtt_component.cpp +++ b/esphome/components/mqtt/mqtt_component.cpp @@ -132,9 +132,14 @@ bool MQTTComponent::send_discovery_() { if (discovery_info.object_id_generator == MQTT_DEVICE_NAME_OBJECT_ID_GENERATOR) root[MQTT_OBJECT_ID] = node_name + "_" + this->get_default_object_id_(); + std::string node_friendly_name = App.get_friendly_name(); + if (node_friendly_name.empty()) { + node_friendly_name = node_name; + } + JsonObject device_info = root.createNestedObject(MQTT_DEVICE); device_info[MQTT_DEVICE_IDENTIFIERS] = get_mac_address(); - device_info[MQTT_DEVICE_NAME] = node_name; + device_info[MQTT_DEVICE_NAME] = node_friendly_name; device_info[MQTT_DEVICE_SW_VERSION] = "esphome v" ESPHOME_VERSION " " + App.get_compilation_time(); device_info[MQTT_DEVICE_MODEL] = ESPHOME_BOARD; device_info[MQTT_DEVICE_MANUFACTURER] = "espressif"; From fd08f1e23d5efb4f4ff3e23d9125fbd82732cf61 Mon Sep 17 00:00:00 2001 From: Keith Burzinski Date: Sun, 6 Aug 2023 20:54:25 -0500 Subject: [PATCH 61/84] Add ESP32-S2/S3 capacitive touch support (#5116) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/components/esp32_touch/__init__.py | 235 +++++++++++++++++- .../components/esp32_touch/binary_sensor.py | 75 +----- .../components/esp32_touch/esp32_touch.cpp | 206 +++++++++++++-- esphome/components/esp32_touch/esp32_touch.h | 99 +++++--- 4 files changed, 484 insertions(+), 131 deletions(-) diff --git a/esphome/components/esp32_touch/__init__.py b/esphome/components/esp32_touch/__init__.py index 3c9bef9665..0180d18104 100644 --- a/esphome/components/esp32_touch/__init__.py +++ b/esphome/components/esp32_touch/__init__.py @@ -12,25 +12,112 @@ from esphome.const import ( ) from esphome.core import TimePeriod from esphome.components import esp32 +from esphome.components.esp32 import get_esp32_variant, gpio +from esphome.components.esp32.const import ( + VARIANT_ESP32, + VARIANT_ESP32S2, + VARIANT_ESP32S3, +) AUTO_LOAD = ["binary_sensor"] DEPENDENCIES = ["esp32"] +CONF_DEBOUNCE_COUNT = "debounce_count" +CONF_DENOISE_GRADE = "denoise_grade" +CONF_DENOISE_CAP_LEVEL = "denoise_cap_level" +CONF_FILTER_MODE = "filter_mode" +CONF_NOISE_THRESHOLD = "noise_threshold" +CONF_JITTER_STEP = "jitter_step" +CONF_SMOOTH_MODE = "smooth_mode" +CONF_WATERPROOF_GUARD_RING = "waterproof_guard_ring" +CONF_WATERPROOF_SHIELD_DRIVER = "waterproof_shield_driver" + esp32_touch_ns = cg.esphome_ns.namespace("esp32_touch") ESP32TouchComponent = esp32_touch_ns.class_("ESP32TouchComponent", cg.Component) +TOUCH_PADS = { + VARIANT_ESP32: { + 4: cg.global_ns.TOUCH_PAD_NUM0, + 0: cg.global_ns.TOUCH_PAD_NUM1, + 2: cg.global_ns.TOUCH_PAD_NUM2, + 15: cg.global_ns.TOUCH_PAD_NUM3, + 13: cg.global_ns.TOUCH_PAD_NUM4, + 12: cg.global_ns.TOUCH_PAD_NUM5, + 14: cg.global_ns.TOUCH_PAD_NUM6, + 27: cg.global_ns.TOUCH_PAD_NUM7, + 33: cg.global_ns.TOUCH_PAD_NUM8, + 32: cg.global_ns.TOUCH_PAD_NUM9, + }, + VARIANT_ESP32S2: { + 1: cg.global_ns.TOUCH_PAD_NUM1, + 2: cg.global_ns.TOUCH_PAD_NUM2, + 3: cg.global_ns.TOUCH_PAD_NUM3, + 4: cg.global_ns.TOUCH_PAD_NUM4, + 5: cg.global_ns.TOUCH_PAD_NUM5, + 6: cg.global_ns.TOUCH_PAD_NUM6, + 7: cg.global_ns.TOUCH_PAD_NUM7, + 8: cg.global_ns.TOUCH_PAD_NUM8, + 9: cg.global_ns.TOUCH_PAD_NUM9, + 10: cg.global_ns.TOUCH_PAD_NUM10, + 11: cg.global_ns.TOUCH_PAD_NUM11, + 12: cg.global_ns.TOUCH_PAD_NUM12, + 13: cg.global_ns.TOUCH_PAD_NUM13, + 14: cg.global_ns.TOUCH_PAD_NUM14, + }, + VARIANT_ESP32S3: { + 1: cg.global_ns.TOUCH_PAD_NUM1, + 2: cg.global_ns.TOUCH_PAD_NUM2, + 3: cg.global_ns.TOUCH_PAD_NUM3, + 4: cg.global_ns.TOUCH_PAD_NUM4, + 5: cg.global_ns.TOUCH_PAD_NUM5, + 6: cg.global_ns.TOUCH_PAD_NUM6, + 7: cg.global_ns.TOUCH_PAD_NUM7, + 8: cg.global_ns.TOUCH_PAD_NUM8, + 9: cg.global_ns.TOUCH_PAD_NUM9, + 10: cg.global_ns.TOUCH_PAD_NUM10, + 11: cg.global_ns.TOUCH_PAD_NUM11, + 12: cg.global_ns.TOUCH_PAD_NUM12, + 13: cg.global_ns.TOUCH_PAD_NUM13, + 14: cg.global_ns.TOUCH_PAD_NUM14, + }, +} -def validate_voltage(values): - def validator(value): - if isinstance(value, float) and value.is_integer(): - value = int(value) - value = cv.string(value) - if not value.endswith("V"): - value += "V" - return cv.one_of(*values)(value) - return validator +TOUCH_PAD_DENOISE_GRADE = { + "BIT12": cg.global_ns.TOUCH_PAD_DENOISE_BIT12, + "BIT10": cg.global_ns.TOUCH_PAD_DENOISE_BIT10, + "BIT8": cg.global_ns.TOUCH_PAD_DENOISE_BIT8, + "BIT4": cg.global_ns.TOUCH_PAD_DENOISE_BIT4, +} +TOUCH_PAD_DENOISE_CAP_LEVEL = { + "L0": cg.global_ns.TOUCH_PAD_DENOISE_CAP_L0, + "L1": cg.global_ns.TOUCH_PAD_DENOISE_CAP_L1, + "L2": cg.global_ns.TOUCH_PAD_DENOISE_CAP_L2, + "L3": cg.global_ns.TOUCH_PAD_DENOISE_CAP_L3, + "L4": cg.global_ns.TOUCH_PAD_DENOISE_CAP_L4, + "L5": cg.global_ns.TOUCH_PAD_DENOISE_CAP_L5, + "L6": cg.global_ns.TOUCH_PAD_DENOISE_CAP_L6, + "L7": cg.global_ns.TOUCH_PAD_DENOISE_CAP_L7, +} + +TOUCH_PAD_FILTER_MODE = { + "IIR_4": cg.global_ns.TOUCH_PAD_FILTER_IIR_4, + "IIR_8": cg.global_ns.TOUCH_PAD_FILTER_IIR_8, + "IIR_16": cg.global_ns.TOUCH_PAD_FILTER_IIR_16, + "IIR_32": cg.global_ns.TOUCH_PAD_FILTER_IIR_32, + "IIR_64": cg.global_ns.TOUCH_PAD_FILTER_IIR_64, + "IIR_128": cg.global_ns.TOUCH_PAD_FILTER_IIR_128, + "IIR_256": cg.global_ns.TOUCH_PAD_FILTER_IIR_256, + "JITTER": cg.global_ns.TOUCH_PAD_FILTER_JITTER, +} + +TOUCH_PAD_SMOOTH_MODE = { + "OFF": cg.global_ns.TOUCH_PAD_SMOOTH_OFF, + "IIR_2": cg.global_ns.TOUCH_PAD_SMOOTH_IIR_2, + "IIR_4": cg.global_ns.TOUCH_PAD_SMOOTH_IIR_4, + "IIR_8": cg.global_ns.TOUCH_PAD_SMOOTH_IIR_8, +} LOW_VOLTAGE_REFERENCE = { "0.5V": cg.global_ns.TOUCH_LVOLT_0V5, @@ -50,15 +137,74 @@ VOLTAGE_ATTENUATION = { "0.5V": cg.global_ns.TOUCH_HVOLT_ATTEN_0V5, "0V": cg.global_ns.TOUCH_HVOLT_ATTEN_0V, } +TOUCH_PAD_WATERPROOF_SHIELD_DRIVER = { + "L0": cg.global_ns.TOUCH_PAD_SHIELD_DRV_L0, + "L1": cg.global_ns.TOUCH_PAD_SHIELD_DRV_L1, + "L2": cg.global_ns.TOUCH_PAD_SHIELD_DRV_L2, + "L3": cg.global_ns.TOUCH_PAD_SHIELD_DRV_L3, + "L4": cg.global_ns.TOUCH_PAD_SHIELD_DRV_L4, + "L5": cg.global_ns.TOUCH_PAD_SHIELD_DRV_L5, + "L6": cg.global_ns.TOUCH_PAD_SHIELD_DRV_L6, + "L7": cg.global_ns.TOUCH_PAD_SHIELD_DRV_L7, +} + + +def validate_touch_pad(value): + value = gpio.validate_gpio_pin(value) + variant = get_esp32_variant() + if variant not in TOUCH_PADS: + raise cv.Invalid(f"ESP32 variant {variant} does not support touch pads.") + + pads = TOUCH_PADS[variant] + if value not in pads: + raise cv.Invalid(f"Pin {value} does not support touch pads.") + return cv.enum(pads)(value) + + +def validate_variant_vars(config): + if get_esp32_variant() == VARIANT_ESP32: + variant_vars = { + CONF_DEBOUNCE_COUNT, + CONF_DENOISE_GRADE, + CONF_DENOISE_CAP_LEVEL, + CONF_FILTER_MODE, + CONF_NOISE_THRESHOLD, + CONF_JITTER_STEP, + CONF_SMOOTH_MODE, + CONF_WATERPROOF_GUARD_RING, + CONF_WATERPROOF_SHIELD_DRIVER, + } + for vvar in variant_vars: + if vvar in config: + raise cv.Invalid(f"{vvar} is not valid on {VARIANT_ESP32}") + elif ( + get_esp32_variant() == VARIANT_ESP32S2 or get_esp32_variant() == VARIANT_ESP32S3 + ) and CONF_IIR_FILTER in config: + raise cv.Invalid( + f"{CONF_IIR_FILTER} is not valid on {VARIANT_ESP32S2} or {VARIANT_ESP32S3}" + ) + + return config + + +def validate_voltage(values): + def validator(value): + if isinstance(value, float) and value.is_integer(): + value = int(value) + value = cv.string(value) + if not value.endswith("V"): + value += "V" + return cv.one_of(*values)(value) + + return validator + CONFIG_SCHEMA = cv.All( cv.Schema( { cv.GenerateID(): cv.declare_id(ESP32TouchComponent), cv.Optional(CONF_SETUP_MODE, default=False): cv.boolean, - cv.Optional( - CONF_IIR_FILTER, default="0ms" - ): cv.positive_time_period_milliseconds, + # common options cv.Optional(CONF_SLEEP_DURATION, default="27306us"): cv.All( cv.positive_time_period, cv.Range(max=TimePeriod(microseconds=436906)) ), @@ -74,13 +220,47 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_VOLTAGE_ATTENUATION, default="0V"): validate_voltage( VOLTAGE_ATTENUATION ), + # ESP32 only + cv.Optional(CONF_IIR_FILTER): cv.positive_time_period_milliseconds, + # ESP32-S2/S3 only + cv.Optional(CONF_DEBOUNCE_COUNT): cv.int_range(min=0, max=7), + cv.Optional(CONF_FILTER_MODE): cv.enum( + TOUCH_PAD_FILTER_MODE, upper=True, space="_" + ), + cv.Optional(CONF_NOISE_THRESHOLD): cv.int_range(min=0, max=3), + cv.Optional(CONF_JITTER_STEP): cv.int_range(min=0, max=15), + cv.Optional(CONF_SMOOTH_MODE): cv.enum( + TOUCH_PAD_SMOOTH_MODE, upper=True, space="_" + ), + cv.Optional(CONF_DENOISE_GRADE): cv.enum( + TOUCH_PAD_DENOISE_GRADE, upper=True, space="_" + ), + cv.Optional(CONF_DENOISE_CAP_LEVEL): cv.enum( + TOUCH_PAD_DENOISE_CAP_LEVEL, upper=True, space="_" + ), + cv.Optional(CONF_WATERPROOF_GUARD_RING): validate_touch_pad, + cv.Optional(CONF_WATERPROOF_SHIELD_DRIVER): cv.enum( + TOUCH_PAD_WATERPROOF_SHIELD_DRIVER, upper=True, space="_" + ), } ).extend(cv.COMPONENT_SCHEMA), + cv.has_none_or_all_keys(CONF_DENOISE_GRADE, CONF_DENOISE_CAP_LEVEL), + cv.has_none_or_all_keys( + CONF_DEBOUNCE_COUNT, + CONF_FILTER_MODE, + CONF_NOISE_THRESHOLD, + CONF_JITTER_STEP, + CONF_SMOOTH_MODE, + ), + cv.has_none_or_all_keys(CONF_WATERPROOF_GUARD_RING, CONF_WATERPROOF_SHIELD_DRIVER), esp32.only_on_variant( supported=[ esp32.const.VARIANT_ESP32, + esp32.const.VARIANT_ESP32S2, + esp32.const.VARIANT_ESP32S3, ] ), + validate_variant_vars, ) @@ -89,7 +269,6 @@ async def to_code(config): await cg.register_component(touch, config) cg.add(touch.set_setup_mode(config[CONF_SETUP_MODE])) - cg.add(touch.set_iir_filter(config[CONF_IIR_FILTER])) sleep_duration = int(round(config[CONF_SLEEP_DURATION].total_microseconds * 0.15)) cg.add(touch.set_sleep_duration(sleep_duration)) @@ -114,3 +293,33 @@ async def to_code(config): VOLTAGE_ATTENUATION[config[CONF_VOLTAGE_ATTENUATION]] ) ) + + if get_esp32_variant() == VARIANT_ESP32: + if CONF_IIR_FILTER in config: + cg.add(touch.set_iir_filter(config[CONF_IIR_FILTER])) + + if get_esp32_variant() == VARIANT_ESP32S2 or get_esp32_variant() == VARIANT_ESP32S3: + if CONF_FILTER_MODE in config: + cg.add(touch.set_filter_mode(config[CONF_FILTER_MODE])) + if CONF_DEBOUNCE_COUNT in config: + cg.add(touch.set_debounce_count(config[CONF_DEBOUNCE_COUNT])) + if CONF_NOISE_THRESHOLD in config: + cg.add(touch.set_noise_threshold(config[CONF_NOISE_THRESHOLD])) + if CONF_JITTER_STEP in config: + cg.add(touch.set_jitter_step(config[CONF_JITTER_STEP])) + if CONF_SMOOTH_MODE in config: + cg.add(touch.set_smooth_level(config[CONF_SMOOTH_MODE])) + if CONF_DENOISE_GRADE in config: + cg.add(touch.set_denoise_grade(config[CONF_DENOISE_GRADE])) + if CONF_DENOISE_CAP_LEVEL in config: + cg.add(touch.set_denoise_cap(config[CONF_DENOISE_CAP_LEVEL])) + if CONF_WATERPROOF_GUARD_RING in config: + cg.add( + touch.set_waterproof_guard_ring_pad(config[CONF_WATERPROOF_GUARD_RING]) + ) + if CONF_WATERPROOF_SHIELD_DRIVER in config: + cg.add( + touch.set_waterproof_shield_driver( + config[CONF_WATERPROOF_SHIELD_DRIVER] + ) + ) diff --git a/esphome/components/esp32_touch/binary_sensor.py b/esphome/components/esp32_touch/binary_sensor.py index 2cdf1343c3..e9322b3080 100644 --- a/esphome/components/esp32_touch/binary_sensor.py +++ b/esphome/components/esp32_touch/binary_sensor.py @@ -1,87 +1,18 @@ import esphome.codegen as cg import esphome.config_validation as cv -from esphome.core import CORE from esphome.components import binary_sensor from esphome.const import ( CONF_PIN, CONF_THRESHOLD, CONF_ID, ) -from esphome.components.esp32 import gpio -from esphome.components.esp32.const import ( - KEY_ESP32, - KEY_VARIANT, - VARIANT_ESP32, - VARIANT_ESP32S2, - VARIANT_ESP32S3, -) -from . import esp32_touch_ns, ESP32TouchComponent +from . import esp32_touch_ns, ESP32TouchComponent, validate_touch_pad DEPENDENCIES = ["esp32_touch", "esp32"] CONF_ESP32_TOUCH_ID = "esp32_touch_id" CONF_WAKEUP_THRESHOLD = "wakeup_threshold" -TOUCH_PADS = { - VARIANT_ESP32: { - 4: cg.global_ns.TOUCH_PAD_NUM0, - 0: cg.global_ns.TOUCH_PAD_NUM1, - 2: cg.global_ns.TOUCH_PAD_NUM2, - 15: cg.global_ns.TOUCH_PAD_NUM3, - 13: cg.global_ns.TOUCH_PAD_NUM4, - 12: cg.global_ns.TOUCH_PAD_NUM5, - 14: cg.global_ns.TOUCH_PAD_NUM6, - 27: cg.global_ns.TOUCH_PAD_NUM7, - 33: cg.global_ns.TOUCH_PAD_NUM8, - 32: cg.global_ns.TOUCH_PAD_NUM9, - }, - VARIANT_ESP32S2: { - 1: cg.global_ns.TOUCH_PAD_NUM1, - 2: cg.global_ns.TOUCH_PAD_NUM2, - 3: cg.global_ns.TOUCH_PAD_NUM3, - 4: cg.global_ns.TOUCH_PAD_NUM4, - 5: cg.global_ns.TOUCH_PAD_NUM5, - 6: cg.global_ns.TOUCH_PAD_NUM6, - 7: cg.global_ns.TOUCH_PAD_NUM7, - 8: cg.global_ns.TOUCH_PAD_NUM8, - 9: cg.global_ns.TOUCH_PAD_NUM9, - 10: cg.global_ns.TOUCH_PAD_NUM10, - 11: cg.global_ns.TOUCH_PAD_NUM11, - 12: cg.global_ns.TOUCH_PAD_NUM12, - 13: cg.global_ns.TOUCH_PAD_NUM13, - 14: cg.global_ns.TOUCH_PAD_NUM14, - }, - VARIANT_ESP32S3: { - 1: cg.global_ns.TOUCH_PAD_NUM1, - 2: cg.global_ns.TOUCH_PAD_NUM2, - 3: cg.global_ns.TOUCH_PAD_NUM3, - 4: cg.global_ns.TOUCH_PAD_NUM4, - 5: cg.global_ns.TOUCH_PAD_NUM5, - 6: cg.global_ns.TOUCH_PAD_NUM6, - 7: cg.global_ns.TOUCH_PAD_NUM7, - 8: cg.global_ns.TOUCH_PAD_NUM8, - 9: cg.global_ns.TOUCH_PAD_NUM9, - 10: cg.global_ns.TOUCH_PAD_NUM10, - 11: cg.global_ns.TOUCH_PAD_NUM11, - 12: cg.global_ns.TOUCH_PAD_NUM12, - 13: cg.global_ns.TOUCH_PAD_NUM13, - 14: cg.global_ns.TOUCH_PAD_NUM14, - }, -} - - -def validate_touch_pad(value): - value = gpio.validate_gpio_pin(value) - variant = CORE.data[KEY_ESP32][KEY_VARIANT] - if variant not in TOUCH_PADS: - raise cv.Invalid(f"ESP32 variant {variant} does not support touch pads.") - - pads = TOUCH_PADS[variant] - if value not in pads: - raise cv.Invalid(f"Pin {value} does not support touch pads.") - return cv.enum(pads)(value) - - ESP32TouchBinarySensor = esp32_touch_ns.class_( "ESP32TouchBinarySensor", binary_sensor.BinarySensor ) @@ -90,8 +21,8 @@ CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(ESP32TouchBinarySensor).exten { cv.GenerateID(CONF_ESP32_TOUCH_ID): cv.use_id(ESP32TouchComponent), cv.Required(CONF_PIN): validate_touch_pad, - cv.Required(CONF_THRESHOLD): cv.uint16_t, - cv.Optional(CONF_WAKEUP_THRESHOLD, default=0): cv.uint16_t, + cv.Required(CONF_THRESHOLD): cv.uint32_t, + cv.Optional(CONF_WAKEUP_THRESHOLD, default=0): cv.uint32_t, } ) diff --git a/esphome/components/esp32_touch/esp32_touch.cpp b/esphome/components/esp32_touch/esp32_touch.cpp index 0e3d3d9fd5..e43c3b844c 100644 --- a/esphome/components/esp32_touch/esp32_touch.cpp +++ b/esphome/components/esp32_touch/esp32_touch.cpp @@ -5,6 +5,8 @@ #include "esphome/core/log.h" #include "esphome/core/hal.h" +#include + namespace esphome { namespace esp32_touch { @@ -13,18 +15,58 @@ static const char *const TAG = "esp32_touch"; void ESP32TouchComponent::setup() { ESP_LOGCONFIG(TAG, "Setting up ESP32 Touch Hub..."); touch_pad_init(); +// set up and enable/start filtering based on ESP32 variant +#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) + if (this->filter_configured_()) { + touch_filter_config_t filter_info = { + .mode = this->filter_mode_, + .debounce_cnt = this->debounce_count_, + .noise_thr = this->noise_threshold_, + .jitter_step = this->jitter_step_, + .smh_lvl = this->smooth_level_, + }; + touch_pad_filter_set_config(&filter_info); + touch_pad_filter_enable(); + } + if (this->denoise_configured_()) { + touch_pad_denoise_t denoise = { + .grade = this->grade_, + .cap_level = this->cap_level_, + }; + touch_pad_denoise_set_config(&denoise); + touch_pad_denoise_enable(); + } + + if (this->waterproof_configured_()) { + touch_pad_waterproof_t waterproof = { + .guard_ring_pad = this->waterproof_guard_ring_pad_, + .shield_driver = this->waterproof_shield_driver_, + }; + touch_pad_waterproof_set_config(&waterproof); + touch_pad_waterproof_enable(); + } +#else if (this->iir_filter_enabled_()) { touch_pad_filter_start(this->iir_filter_); } +#endif touch_pad_set_meas_time(this->sleep_cycle_, this->meas_cycle_); touch_pad_set_voltage(this->high_voltage_reference_, this->low_voltage_reference_, this->voltage_attenuation_); for (auto *child : this->children_) { +#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) + touch_pad_config(child->get_touch_pad()); +#else // Disable interrupt threshold touch_pad_config(child->get_touch_pad(), 0); +#endif } +#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) + touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER); + touch_pad_fsm_start(); +#endif } void ESP32TouchComponent::dump_config() { @@ -92,38 +134,168 @@ void ESP32TouchComponent::dump_config() { } ESP_LOGCONFIG(TAG, " Voltage Attenuation: %s", atten_s); +#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) + if (this->filter_configured_()) { + const char *filter_mode_s; + switch (this->filter_mode_) { + case TOUCH_PAD_FILTER_IIR_4: + filter_mode_s = "IIR_4"; + break; + case TOUCH_PAD_FILTER_IIR_8: + filter_mode_s = "IIR_8"; + break; + case TOUCH_PAD_FILTER_IIR_16: + filter_mode_s = "IIR_16"; + break; + case TOUCH_PAD_FILTER_IIR_32: + filter_mode_s = "IIR_32"; + break; + case TOUCH_PAD_FILTER_IIR_64: + filter_mode_s = "IIR_64"; + break; + case TOUCH_PAD_FILTER_IIR_128: + filter_mode_s = "IIR_128"; + break; + case TOUCH_PAD_FILTER_IIR_256: + filter_mode_s = "IIR_256"; + break; + case TOUCH_PAD_FILTER_JITTER: + filter_mode_s = "JITTER"; + break; + default: + filter_mode_s = "UNKNOWN"; + break; + } + ESP_LOGCONFIG(TAG, " Filter mode: %s", filter_mode_s); + ESP_LOGCONFIG(TAG, " Debounce count: %" PRIu32, this->debounce_count_); + ESP_LOGCONFIG(TAG, " Noise threshold coefficient: %" PRIu32, this->noise_threshold_); + ESP_LOGCONFIG(TAG, " Jitter filter step size: %" PRIu32, this->jitter_step_); + const char *smooth_level_s; + switch (this->smooth_level_) { + case TOUCH_PAD_SMOOTH_OFF: + smooth_level_s = "OFF"; + break; + case TOUCH_PAD_SMOOTH_IIR_2: + smooth_level_s = "IIR_2"; + break; + case TOUCH_PAD_SMOOTH_IIR_4: + smooth_level_s = "IIR_4"; + break; + case TOUCH_PAD_SMOOTH_IIR_8: + smooth_level_s = "IIR_8"; + break; + default: + smooth_level_s = "UNKNOWN"; + break; + } + ESP_LOGCONFIG(TAG, " Smooth level: %s", smooth_level_s); + } + + if (this->denoise_configured_()) { + const char *grade_s; + switch (this->grade_) { + case TOUCH_PAD_DENOISE_BIT12: + grade_s = "BIT12"; + break; + case TOUCH_PAD_DENOISE_BIT10: + grade_s = "BIT10"; + break; + case TOUCH_PAD_DENOISE_BIT8: + grade_s = "BIT8"; + break; + case TOUCH_PAD_DENOISE_BIT4: + grade_s = "BIT4"; + break; + default: + grade_s = "UNKNOWN"; + break; + } + ESP_LOGCONFIG(TAG, " Denoise grade: %s", grade_s); + + const char *cap_level_s; + switch (this->cap_level_) { + case TOUCH_PAD_DENOISE_CAP_L0: + cap_level_s = "L0"; + break; + case TOUCH_PAD_DENOISE_CAP_L1: + cap_level_s = "L1"; + break; + case TOUCH_PAD_DENOISE_CAP_L2: + cap_level_s = "L2"; + break; + case TOUCH_PAD_DENOISE_CAP_L3: + cap_level_s = "L3"; + break; + case TOUCH_PAD_DENOISE_CAP_L4: + cap_level_s = "L4"; + break; + case TOUCH_PAD_DENOISE_CAP_L5: + cap_level_s = "L5"; + break; + case TOUCH_PAD_DENOISE_CAP_L6: + cap_level_s = "L6"; + break; + case TOUCH_PAD_DENOISE_CAP_L7: + cap_level_s = "L7"; + break; + default: + cap_level_s = "UNKNOWN"; + break; + } + ESP_LOGCONFIG(TAG, " Denoise capacitance level: %s", cap_level_s); + } +#else if (this->iir_filter_enabled_()) { - ESP_LOGCONFIG(TAG, " IIR Filter: %ums", this->iir_filter_); + ESP_LOGCONFIG(TAG, " IIR Filter: %" PRIu32 "ms", this->iir_filter_); } else { ESP_LOGCONFIG(TAG, " IIR Filter DISABLED"); } +#endif + if (this->setup_mode_) { - ESP_LOGCONFIG(TAG, " Setup Mode ENABLED!"); + ESP_LOGCONFIG(TAG, " Setup Mode ENABLED"); } for (auto *child : this->children_) { LOG_BINARY_SENSOR(" ", "Touch Pad", child); - ESP_LOGCONFIG(TAG, " Pad: T%d", child->get_touch_pad()); - ESP_LOGCONFIG(TAG, " Threshold: %u", child->get_threshold()); + ESP_LOGCONFIG(TAG, " Pad: T%" PRIu32, (uint32_t) child->get_touch_pad()); + ESP_LOGCONFIG(TAG, " Threshold: %" PRIu32, child->get_threshold()); } } +uint32_t ESP32TouchComponent::component_touch_pad_read(touch_pad_t tp) { +#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) + uint32_t value = 0; + if (this->filter_configured_()) { + touch_pad_filter_read_smooth(tp, &value); + } else { + touch_pad_read_raw_data(tp, &value); + } +#else + uint16_t value = 0; + if (this->iir_filter_enabled_()) { + touch_pad_read_filtered(tp, &value); + } else { + touch_pad_read(tp, &value); + } +#endif + return value; +} + void ESP32TouchComponent::loop() { const uint32_t now = millis(); bool should_print = this->setup_mode_ && now - this->setup_mode_last_log_print_ > 250; for (auto *child : this->children_) { - uint16_t value; - if (this->iir_filter_enabled_()) { - touch_pad_read_filtered(child->get_touch_pad(), &value); - } else { - touch_pad_read(child->get_touch_pad(), &value); - } - - child->value_ = value; - child->publish_state(value < child->get_threshold()); + child->value_ = this->component_touch_pad_read(child->get_touch_pad()); +#if !(defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)) + child->publish_state(child->value_ < child->get_threshold()); +#else + child->publish_state(child->value_ > child->get_threshold()); +#endif if (should_print) { - ESP_LOGD(TAG, "Touch Pad '%s' (T%u): %u", child->get_name().c_str(), child->get_touch_pad(), value); + ESP_LOGD(TAG, "Touch Pad '%s' (T%" PRIu32 "): %" PRIu32, child->get_name().c_str(), + (uint32_t) child->get_touch_pad(), child->value_); } App.feed_wdt(); @@ -138,10 +310,12 @@ void ESP32TouchComponent::loop() { void ESP32TouchComponent::on_shutdown() { bool is_wakeup_source = false; +#if !(defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)) if (this->iir_filter_enabled_()) { touch_pad_filter_stop(); touch_pad_filter_delete(); } +#endif for (auto *child : this->children_) { if (child->get_wakeup_threshold() != 0) { @@ -151,8 +325,10 @@ void ESP32TouchComponent::on_shutdown() { touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER); } +#if !(defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)) // No filter available when using as wake-up source. touch_pad_config(child->get_touch_pad(), child->get_wakeup_threshold()); +#endif } } @@ -161,7 +337,7 @@ void ESP32TouchComponent::on_shutdown() { } } -ESP32TouchBinarySensor::ESP32TouchBinarySensor(touch_pad_t touch_pad, uint16_t threshold, uint16_t wakeup_threshold) +ESP32TouchBinarySensor::ESP32TouchBinarySensor(touch_pad_t touch_pad, uint32_t threshold, uint32_t wakeup_threshold) : touch_pad_(touch_pad), threshold_(threshold), wakeup_threshold_(wakeup_threshold) {} } // namespace esp32_touch diff --git a/esphome/components/esp32_touch/esp32_touch.h b/esphome/components/esp32_touch/esp32_touch.h index c954a14654..0ba7ed6255 100644 --- a/esphome/components/esp32_touch/esp32_touch.h +++ b/esphome/components/esp32_touch/esp32_touch.h @@ -21,25 +21,37 @@ class ESP32TouchBinarySensor; class ESP32TouchComponent : public Component { public: - void register_touch_pad(ESP32TouchBinarySensor *pad) { children_.push_back(pad); } - - void set_setup_mode(bool setup_mode) { setup_mode_ = setup_mode; } - - void set_iir_filter(uint32_t iir_filter) { iir_filter_ = iir_filter; } - - void set_sleep_duration(uint16_t sleep_duration) { sleep_cycle_ = sleep_duration; } - - void set_measurement_duration(uint16_t meas_cycle) { meas_cycle_ = meas_cycle; } + void register_touch_pad(ESP32TouchBinarySensor *pad) { this->children_.push_back(pad); } + void set_setup_mode(bool setup_mode) { this->setup_mode_ = setup_mode; } + void set_sleep_duration(uint16_t sleep_duration) { this->sleep_cycle_ = sleep_duration; } + void set_measurement_duration(uint16_t meas_cycle) { this->meas_cycle_ = meas_cycle; } void set_low_voltage_reference(touch_low_volt_t low_voltage_reference) { - low_voltage_reference_ = low_voltage_reference; + this->low_voltage_reference_ = low_voltage_reference; } - void set_high_voltage_reference(touch_high_volt_t high_voltage_reference) { - high_voltage_reference_ = high_voltage_reference; + this->high_voltage_reference_ = high_voltage_reference; } + void set_voltage_attenuation(touch_volt_atten_t voltage_attenuation) { + this->voltage_attenuation_ = voltage_attenuation; + } +#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) + void set_filter_mode(touch_filter_mode_t filter_mode) { this->filter_mode_ = filter_mode; } + void set_debounce_count(uint32_t debounce_count) { this->debounce_count_ = debounce_count; } + void set_noise_threshold(uint32_t noise_threshold) { this->noise_threshold_ = noise_threshold; } + void set_jitter_step(uint32_t jitter_step) { this->jitter_step_ = jitter_step; } + void set_smooth_level(touch_smooth_mode_t smooth_level) { this->smooth_level_ = smooth_level; } + void set_denoise_grade(touch_pad_denoise_grade_t denoise_grade) { this->grade_ = denoise_grade; } + void set_denoise_cap(touch_pad_denoise_cap_t cap_level) { this->cap_level_ = cap_level; } + void set_waterproof_guard_ring_pad(touch_pad_t pad) { this->waterproof_guard_ring_pad_ = pad; } + void set_waterproof_shield_driver(touch_pad_shield_driver_t drive_capability) { + this->waterproof_shield_driver_ = drive_capability; + } +#else + void set_iir_filter(uint32_t iir_filter) { this->iir_filter_ = iir_filter; } +#endif - void set_voltage_attenuation(touch_volt_atten_t voltage_attenuation) { voltage_attenuation_ = voltage_attenuation; } + uint32_t component_touch_pad_read(touch_pad_t tp); void setup() override; void dump_config() override; @@ -49,38 +61,63 @@ class ESP32TouchComponent : public Component { void on_shutdown() override; protected: - /// Is the IIR filter enabled? - bool iir_filter_enabled_() const { return iir_filter_ > 0; } +#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) + bool filter_configured_() const { + return (this->filter_mode_ != TOUCH_PAD_FILTER_MAX) && (this->smooth_level_ != TOUCH_PAD_SMOOTH_MAX); + } + bool denoise_configured_() const { + return (this->grade_ != TOUCH_PAD_DENOISE_MAX) && (this->cap_level_ != TOUCH_PAD_DENOISE_CAP_MAX); + } + bool waterproof_configured_() const { + return (this->waterproof_guard_ring_pad_ != TOUCH_PAD_MAX) && + (this->waterproof_shield_driver_ != TOUCH_PAD_SHIELD_DRV_MAX); + } +#else + bool iir_filter_enabled_() const { return this->iir_filter_ > 0; } +#endif - uint16_t sleep_cycle_{}; - uint16_t meas_cycle_{65535}; - touch_low_volt_t low_voltage_reference_{}; - touch_high_volt_t high_voltage_reference_{}; - touch_volt_atten_t voltage_attenuation_{}; std::vector children_; bool setup_mode_{false}; - uint32_t setup_mode_last_log_print_{}; + uint32_t setup_mode_last_log_print_{0}; + // common parameters + uint16_t sleep_cycle_{4095}; + uint16_t meas_cycle_{65535}; + touch_low_volt_t low_voltage_reference_{TOUCH_LVOLT_0V5}; + touch_high_volt_t high_voltage_reference_{TOUCH_HVOLT_2V7}; + touch_volt_atten_t voltage_attenuation_{TOUCH_HVOLT_ATTEN_0V}; +#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) + touch_filter_mode_t filter_mode_{TOUCH_PAD_FILTER_MAX}; + uint32_t debounce_count_{0}; + uint32_t noise_threshold_{0}; + uint32_t jitter_step_{0}; + touch_smooth_mode_t smooth_level_{TOUCH_PAD_SMOOTH_MAX}; + touch_pad_denoise_grade_t grade_{TOUCH_PAD_DENOISE_MAX}; + touch_pad_denoise_cap_t cap_level_{TOUCH_PAD_DENOISE_CAP_MAX}; + touch_pad_t waterproof_guard_ring_pad_{TOUCH_PAD_MAX}; + touch_pad_shield_driver_t waterproof_shield_driver_{TOUCH_PAD_SHIELD_DRV_MAX}; +#else uint32_t iir_filter_{0}; +#endif }; /// Simple helper class to expose a touch pad value as a binary sensor. class ESP32TouchBinarySensor : public binary_sensor::BinarySensor { public: - ESP32TouchBinarySensor(touch_pad_t touch_pad, uint16_t threshold, uint16_t wakeup_threshold); + ESP32TouchBinarySensor(touch_pad_t touch_pad, uint32_t threshold, uint32_t wakeup_threshold); - touch_pad_t get_touch_pad() const { return touch_pad_; } - uint16_t get_threshold() const { return threshold_; } - void set_threshold(uint16_t threshold) { threshold_ = threshold; } - uint16_t get_value() const { return value_; } - uint16_t get_wakeup_threshold() const { return wakeup_threshold_; } + touch_pad_t get_touch_pad() const { return this->touch_pad_; } + uint32_t get_threshold() const { return this->threshold_; } + void set_threshold(uint32_t threshold) { this->threshold_ = threshold; } + uint32_t get_value() const { return this->value_; } + uint32_t get_wakeup_threshold() const { return this->wakeup_threshold_; } protected: friend ESP32TouchComponent; - touch_pad_t touch_pad_; - uint16_t threshold_; - uint16_t value_; - const uint16_t wakeup_threshold_; + touch_pad_t touch_pad_{TOUCH_PAD_MAX}; + uint32_t threshold_{0}; + uint32_t value_{0}; + const uint32_t wakeup_threshold_{0}; }; } // namespace esp32_touch From c541fa1763c7082d10c3c205dca4c16db2586daf Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 7 Aug 2023 13:55:38 +1200 Subject: [PATCH 62/84] Bump zeroconf from 0.71.4 to 0.74.0 (#5199) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 3160fe42de..2bb1e81fbc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,7 +11,7 @@ esptool==4.6.2 click==8.1.6 esphome-dashboard==20230711.0 aioesphomeapi==15.0.0 -zeroconf==0.71.4 +zeroconf==0.74.0 # esp-idf requires this, but doesn't bundle it by default # https://github.com/espressif/esp-idf/blob/220590d599e134d7a5e7f1e683cc4550349ffbf8/requirements.txt#L24 From 40697fea9623fd6e37cf76adba1310d589acd46d Mon Sep 17 00:00:00 2001 From: Lucas Prim Date: Mon, 7 Aug 2023 16:31:09 -0300 Subject: [PATCH 63/84] Implemented Waveshare 7.5in B V3 (#5210) --- .../components/waveshare_epaper/display.py | 4 + .../waveshare_epaper/waveshare_epaper.cpp | 151 ++++++++++++++++++ .../waveshare_epaper/waveshare_epaper.h | 34 ++++ 3 files changed, 189 insertions(+) diff --git a/esphome/components/waveshare_epaper/display.py b/esphome/components/waveshare_epaper/display.py index 6113fe943c..eb0faadc02 100644 --- a/esphome/components/waveshare_epaper/display.py +++ b/esphome/components/waveshare_epaper/display.py @@ -51,6 +51,9 @@ WaveshareEPaper7P5InBC = waveshare_epaper_ns.class_( WaveshareEPaper7P5InBV2 = waveshare_epaper_ns.class_( "WaveshareEPaper7P5InBV2", WaveshareEPaper ) +WaveshareEPaper7P5InBV3 = waveshare_epaper_ns.class_( + "WaveshareEPaper7P5InBV3", WaveshareEPaper +) WaveshareEPaper7P5InV2 = waveshare_epaper_ns.class_( "WaveshareEPaper7P5InV2", WaveshareEPaper ) @@ -87,6 +90,7 @@ MODELS = { "5.83inv2": ("b", WaveshareEPaper5P8InV2), "7.50in": ("b", WaveshareEPaper7P5In), "7.50in-bv2": ("b", WaveshareEPaper7P5InBV2), + "7.50in-bv3": ("b", WaveshareEPaper7P5InBV3), "7.50in-bc": ("b", WaveshareEPaper7P5InBC), "7.50inv2": ("b", WaveshareEPaper7P5InV2), "7.50inv2alt": ("b", WaveshareEPaper7P5InV2alt), diff --git a/esphome/components/waveshare_epaper/waveshare_epaper.cpp b/esphome/components/waveshare_epaper/waveshare_epaper.cpp index 5dd573f1b8..73c2680add 100644 --- a/esphome/components/waveshare_epaper/waveshare_epaper.cpp +++ b/esphome/components/waveshare_epaper/waveshare_epaper.cpp @@ -1328,6 +1328,157 @@ void WaveshareEPaper7P5InBV2::dump_config() { LOG_PIN(" Busy Pin: ", this->busy_pin_); LOG_UPDATE_INTERVAL(this); } + +bool WaveshareEPaper7P5InBV3::wait_until_idle_() { + if (this->busy_pin_ == nullptr) { + return true; + } + + const uint32_t start = millis(); + while (this->busy_pin_->digital_read()) { + this->command(0x71); + if (millis() - start > this->idle_timeout_()) { + ESP_LOGI(TAG, "Timeout while displaying image!"); + return false; + } + delay(10); + } + delay(200); // NOLINT + return true; +}; +void WaveshareEPaper7P5InBV3::initialize() { + this->reset_(); + + // COMMAND POWER SETTING + this->command(0x01); + + // 1-0=11: internal power + this->data(0x07); + this->data(0x17); // VGH&VGL + this->data(0x3F); // VSH + this->data(0x26); // VSL + this->data(0x11); // VSHR + + // VCOM DC Setting + this->command(0x82); + this->data(0x24); // VCOM + + // Booster Setting + this->command(0x06); + this->data(0x27); + this->data(0x27); + this->data(0x2F); + this->data(0x17); + + // POWER ON + this->command(0x04); + + delay(100); // NOLINT + this->wait_until_idle_(); + // COMMAND PANEL SETTING + this->command(0x00); + this->data(0x3F); // KW-3f KWR-2F BWROTP 0f BWOTP 1f + + // COMMAND RESOLUTION SETTING + this->command(0x61); + this->data(0x03); // source 800 + this->data(0x20); + this->data(0x01); // gate 480 + this->data(0xE0); + // COMMAND ...? + this->command(0x15); + this->data(0x00); + // COMMAND VCOM AND DATA INTERVAL SETTING + this->command(0x50); + this->data(0x10); + this->data(0x00); + // COMMAND TCON SETTING + this->command(0x60); + this->data(0x22); + // Resolution setting + this->command(0x65); + this->data(0x00); + this->data(0x00); // 800*480 + this->data(0x00); + this->data(0x00); + + this->wait_until_idle_(); + + uint8_t lut_vcom_7_i_n5_v2[] = { + 0x0, 0xF, 0xF, 0x0, 0x0, 0x1, 0x0, 0xF, 0x1, 0xF, 0x1, 0x2, 0x0, 0xF, 0xF, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + }; + + uint8_t lut_ww_7_i_n5_v2[] = { + 0x10, 0xF, 0xF, 0x0, 0x0, 0x1, 0x84, 0xF, 0x1, 0xF, 0x1, 0x2, 0x20, 0xF, 0xF, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + }; + + uint8_t lut_bw_7_i_n5_v2[] = { + 0x10, 0xF, 0xF, 0x0, 0x0, 0x1, 0x84, 0xF, 0x1, 0xF, 0x1, 0x2, 0x20, 0xF, 0xF, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + }; + + uint8_t lut_wb_7_i_n5_v2[] = { + 0x80, 0xF, 0xF, 0x0, 0x0, 0x3, 0x84, 0xF, 0x1, 0xF, 0x1, 0x4, 0x40, 0xF, 0xF, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + }; + + uint8_t lut_bb_7_i_n5_v2[] = { + 0x80, 0xF, 0xF, 0x0, 0x0, 0x1, 0x84, 0xF, 0x1, 0xF, 0x1, 0x2, 0x40, 0xF, 0xF, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + }; + + uint8_t count; + this->command(0x20); // VCOM + for (count = 0; count < 42; count++) + this->data(lut_vcom_7_i_n5_v2[count]); + + this->command(0x21); // LUTBW + for (count = 0; count < 42; count++) + this->data(lut_ww_7_i_n5_v2[count]); + + this->command(0x22); // LUTBW + for (count = 0; count < 42; count++) + this->data(lut_bw_7_i_n5_v2[count]); + + this->command(0x23); // LUTWB + for (count = 0; count < 42; count++) + this->data(lut_wb_7_i_n5_v2[count]); + + this->command(0x24); // LUTBB + for (count = 0; count < 42; count++) + this->data(lut_bb_7_i_n5_v2[count]); + + this->command(0x10); + for (uint32_t i = 0; i < 800 * 480 / 8; i++) { + this->data(0xFF); + } +}; +void HOT WaveshareEPaper7P5InBV3::display() { + uint32_t buf_len = this->get_buffer_length_(); + + this->command(0x13); // Start Transmission + delay(2); + for (uint32_t i = 0; i < buf_len; i++) { + this->data(~(this->buffer_[i])); + } + + this->command(0x12); // Display Refresh + delay(100); // NOLINT + this->wait_until_idle_(); +} +int WaveshareEPaper7P5InBV3::get_width_internal() { return 800; } +int WaveshareEPaper7P5InBV3::get_height_internal() { return 480; } +void WaveshareEPaper7P5InBV3::dump_config() { + LOG_DISPLAY("", "Waveshare E-Paper", this); + ESP_LOGCONFIG(TAG, " Model: 7.5in-bv3"); + LOG_PIN(" Reset Pin: ", this->reset_pin_); + LOG_PIN(" DC Pin: ", this->dc_pin_); + LOG_PIN(" Busy Pin: ", this->busy_pin_); + LOG_UPDATE_INTERVAL(this); +} + void WaveshareEPaper7P5In::initialize() { // COMMAND POWER SETTING this->command(0x01); diff --git a/esphome/components/waveshare_epaper/waveshare_epaper.h b/esphome/components/waveshare_epaper/waveshare_epaper.h index a84a1d4541..315af9ea82 100644 --- a/esphome/components/waveshare_epaper/waveshare_epaper.h +++ b/esphome/components/waveshare_epaper/waveshare_epaper.h @@ -413,6 +413,40 @@ class WaveshareEPaper7P5InBV2 : public WaveshareEPaper { int get_height_internal() override; }; +class WaveshareEPaper7P5InBV3 : public WaveshareEPaper { + public: + bool wait_until_idle_(); + + void initialize() override; + + void display() override; + + void dump_config() override; + + void deep_sleep() override { + this->command(0x02); // Power off + this->wait_until_idle_(); + this->command(0x07); // Deep sleep + this->data(0xA5); + } + + protected: + int get_width_internal() override; + + int get_height_internal() override; + + void reset_() { + if (this->reset_pin_ != nullptr) { + this->reset_pin_->digital_write(true); + delay(200); // NOLINT + this->reset_pin_->digital_write(false); + delay(5); + this->reset_pin_->digital_write(true); + delay(200); // NOLINT + } + }; +}; + class WaveshareEPaper7P5InBC : public WaveshareEPaper { public: void initialize() override; From 93b7ca77ca2863e3a454bf4d0087f60eff39c149 Mon Sep 17 00:00:00 2001 From: Trent Houliston Date: Tue, 8 Aug 2023 06:14:20 +1000 Subject: [PATCH 64/84] Refactor `pulse_meter` to better handle higher frequencies (#4231) --- .../pulse_meter/pulse_meter_sensor.cpp | 204 +++++++----------- .../pulse_meter/pulse_meter_sensor.h | 47 ++-- 2 files changed, 103 insertions(+), 148 deletions(-) diff --git a/esphome/components/pulse_meter/pulse_meter_sensor.cpp b/esphome/components/pulse_meter/pulse_meter_sensor.cpp index d0c627313c..7eef18e5e0 100644 --- a/esphome/components/pulse_meter/pulse_meter_sensor.cpp +++ b/esphome/components/pulse_meter/pulse_meter_sensor.cpp @@ -1,4 +1,5 @@ #include "pulse_meter_sensor.h" +#include #include "esphome/core/log.h" namespace esphome { @@ -9,66 +10,58 @@ static const char *const TAG = "pulse_meter"; void PulseMeterSensor::setup() { this->pin_->setup(); this->isr_pin_ = pin_->to_isr(); - this->pin_->attach_interrupt(PulseMeterSensor::gpio_intr, this, gpio::INTERRUPT_ANY_EDGE); - this->last_detected_edge_us_ = 0; - this->last_valid_edge_us_ = 0; - this->pulse_width_us_ = 0; - this->sensor_is_high_ = this->isr_pin_.digital_read(); - this->has_valid_edge_ = false; - this->pending_state_change_ = NONE; + if (this->filter_mode_ == FILTER_EDGE) { + this->pin_->attach_interrupt(PulseMeterSensor::edge_intr, this, gpio::INTERRUPT_RISING_EDGE); + } else if (this->filter_mode_ == FILTER_PULSE) { + this->pin_->attach_interrupt(PulseMeterSensor::pulse_intr, this, gpio::INTERRUPT_ANY_EDGE); + } } -// In PULSE mode we set a flag (pending_state_change_) for every interrupt -// that constitutes a state change. In the loop() method we check if a time -// interval greater than the internal_filter time has passed without any -// interrupts. void PulseMeterSensor::loop() { - // Get a snapshot of the needed volatile sensor values, to make sure they are not - // modified by the ISR while we are in the loop() method. If they are changed - // after we the variable "now" has been set, overflow will occur in the - // subsequent arithmetic - const bool has_valid_edge = this->has_valid_edge_; - const uint32_t last_detected_edge_us = this->last_detected_edge_us_; - const uint32_t last_valid_edge_us = this->last_valid_edge_us_; - // Get the current time after the snapshot of saved times - const uint32_t now = micros(); + // Reset the count in get before we pass it back to the ISR as set + this->get_->count_ = 0; - this->handle_state_change_(now, last_detected_edge_us, last_valid_edge_us, has_valid_edge); + // Swap out set and get to get the latest state from the ISR + // The ISR could interrupt on any of these lines and the results would be consistent + auto *temp = this->set_; + this->set_ = this->get_; + this->get_ = temp; - // If we've exceeded our timeout interval without receiving any pulses, assume 0 pulses/min until - // we get at least two valid pulses. - const uint32_t time_since_valid_edge_us = now - last_detected_edge_us; - if ((has_valid_edge) && (time_since_valid_edge_us > this->timeout_us_)) { - ESP_LOGD(TAG, "No pulse detected for %us, assuming 0 pulses/min", time_since_valid_edge_us / 1000000); - - this->last_valid_edge_us_ = 0; - this->pulse_width_us_ = 0; - this->has_valid_edge_ = false; - this->last_detected_edge_us_ = 0; - } - - // We quantize our pulse widths to 1 ms to avoid unnecessary jitter - const uint32_t pulse_width_ms = this->pulse_width_us_ / 1000; - if (this->pulse_width_dedupe_.next(pulse_width_ms)) { - if (pulse_width_ms == 0) { - // Treat 0 pulse width as 0 pulses/min (normally because we've not detected any pulses for a while) - this->publish_state(0); - } else { - // Calculate pulses/min from the pulse width in ms - this->publish_state((60.0f * 1000.0f) / pulse_width_ms); - } - } - - if (this->total_sensor_ != nullptr) { - const uint32_t total = this->total_pulses_; - if (this->total_dedupe_.next(total)) { + // Check if we detected a pulse this loop + if (this->get_->count_ > 0) { + // Keep a running total of pulses if a total sensor is configured + if (this->total_sensor_ != nullptr) { + this->total_pulses_ += this->get_->count_; + const uint32_t total = this->total_pulses_; this->total_sensor_->publish_state(total); } + + // We need to detect at least two edges to have a valid pulse width + if (!this->initialized_) { + this->initialized_ = true; + } else { + uint32_t delta_us = this->get_->last_detected_edge_us_ - this->last_processed_edge_us_; + float pulse_width_us = delta_us / float(this->get_->count_); + this->publish_state((60.0f * 1000000.0f) / pulse_width_us); + } + + this->last_processed_edge_us_ = this->get_->last_detected_edge_us_; + } + // No detected edges this loop + else { + const uint32_t now = micros(); + const uint32_t time_since_valid_edge_us = now - this->last_processed_edge_us_; + + if (this->initialized_ && time_since_valid_edge_us > this->timeout_us_) { + ESP_LOGD(TAG, "No pulse detected for %us, assuming 0 pulses/min", time_since_valid_edge_us / 1000000); + this->initialized_ = false; + this->publish_state(0.0f); + } } } -void PulseMeterSensor::set_total_pulses(uint32_t pulses) { this->total_pulses_ = pulses; } +float PulseMeterSensor::get_setup_priority() const { return setup_priority::DATA; } void PulseMeterSensor::dump_config() { LOG_SENSOR("", "Pulse Meter", this); @@ -81,96 +74,49 @@ void PulseMeterSensor::dump_config() { ESP_LOGCONFIG(TAG, " Assuming 0 pulses/min after not receiving a pulse for %us", this->timeout_us_ / 1000000); } -void IRAM_ATTR PulseMeterSensor::gpio_intr(PulseMeterSensor *sensor) { +void IRAM_ATTR PulseMeterSensor::edge_intr(PulseMeterSensor *sensor) { + // This is an interrupt handler - we can't call any virtual method from this method + // Get the current time before we do anything else so the measurements are consistent + const uint32_t now = micros(); + + if ((now - sensor->last_edge_candidate_us_) >= sensor->filter_us_) { + sensor->last_edge_candidate_us_ = now; + sensor->set_->last_detected_edge_us_ = now; + sensor->set_->count_++; + } +} + +void IRAM_ATTR PulseMeterSensor::pulse_intr(PulseMeterSensor *sensor) { // This is an interrupt handler - we can't call any virtual method from this method // Get the current time before we do anything else so the measurements are consistent const uint32_t now = micros(); const bool pin_val = sensor->isr_pin_.digital_read(); - if (sensor->filter_mode_ == FILTER_EDGE) { - // We only look at rising edges - if (!pin_val) { - return; + // A pulse occurred faster than we can detect + if (sensor->last_pin_val_ == pin_val) { + // If we haven't reached the filter length yet we need to reset our last_intr_ to now + // otherwise we can consider this noise as the "pulse" was certainly less than filter_us_ + if (now - sensor->last_intr_ < sensor->filter_us_) { + sensor->last_intr_ = now; } - // Check to see if we should filter this edge out - if ((now - sensor->last_detected_edge_us_) >= sensor->filter_us_) { - // Don't measure the first valid pulse (we need at least two pulses to measure the width) - if (sensor->has_valid_edge_) { - sensor->pulse_width_us_ = (now - sensor->last_valid_edge_us_); - } - sensor->total_pulses_++; - sensor->last_valid_edge_us_ = now; - sensor->has_valid_edge_ = true; - } - sensor->last_detected_edge_us_ = now; } else { - // Filter Mode is PULSE - const uint32_t delta_t_us = now - sensor->last_detected_edge_us_; - // We need to check if we have missed to handle a state change in the - // loop() function. This can happen when the filter_us value is less than - // the loop() interval, which is ~50-60ms - // The section below is essentially a modified repeat of the - // handle_state_change method. Ideally i would refactor and call the - // method here as well. However functions called in ISRs need to meet - // strict criteria and I don't think the methos would meet them. - if (sensor->pending_state_change_ != NONE && (delta_t_us > sensor->filter_us_)) { - // We have missed to handle a state change in the loop function. - sensor->sensor_is_high_ = sensor->pending_state_change_ == TO_HIGH; - if (sensor->sensor_is_high_) { - // We need to handle a pulse that would have been missed by the loop function - sensor->total_pulses_++; - if (sensor->has_valid_edge_) { - sensor->pulse_width_us_ = sensor->last_detected_edge_us_ - sensor->last_valid_edge_us_; - sensor->has_valid_edge_ = true; - sensor->last_valid_edge_us_ = sensor->last_detected_edge_us_; - } + // Check if the last interrupt was long enough in the past + if (now - sensor->last_intr_ > sensor->filter_us_) { + // High pulse of filter length now falling (therefore last_intr_ was the rising edge) + if (!sensor->in_pulse_ && sensor->last_pin_val_) { + sensor->last_edge_candidate_us_ = sensor->last_intr_; + sensor->in_pulse_ = true; } - } // End of checking for and handling of change in state - - // Ignore false edges that may be caused by bouncing and exit the ISR ASAP - if (pin_val == sensor->sensor_is_high_) { - sensor->pending_state_change_ = NONE; - return; - } - sensor->pending_state_change_ = pin_val ? TO_HIGH : TO_LOW; - sensor->last_detected_edge_us_ = now; - } -} - -void PulseMeterSensor::handle_state_change_(uint32_t now, uint32_t last_detected_edge_us, uint32_t last_valid_edge_us, - bool has_valid_edge) { - if (this->pending_state_change_ == NONE) { - return; - } - - const bool pin_val = this->isr_pin_.digital_read(); - if (pin_val == this->sensor_is_high_) { - // Most likely caused by high frequency bouncing. Theoretically we should - // expect interrupts of alternating state. Here we are registering an - // interrupt with no change in state. Another interrupt will likely trigger - // just after this one and have an alternate state. - this->pending_state_change_ = NONE; - return; - } - - if ((now - last_detected_edge_us) > this->filter_us_) { - this->sensor_is_high_ = pin_val; - ESP_LOGVV(TAG, "State is now %s", pin_val ? "high" : "low"); - - // Increment with valid rising edges only - if (pin_val) { - this->total_pulses_++; - ESP_LOGVV(TAG, "Incremented pulses to %u", this->total_pulses_); - - if (has_valid_edge) { - this->pulse_width_us_ = last_detected_edge_us - last_valid_edge_us; - ESP_LOGVV(TAG, "Set pulse width to %u", this->pulse_width_us_); + // Low pulse of filter length now rising (therefore last_intr_ was the falling edge) + else if (sensor->in_pulse_ && !sensor->last_pin_val_) { + sensor->set_->last_detected_edge_us_ = sensor->last_edge_candidate_us_; + sensor->set_->count_++; + sensor->in_pulse_ = false; } - this->has_valid_edge_ = true; - this->last_valid_edge_us_ = last_detected_edge_us; - ESP_LOGVV(TAG, "last_valid_edge_us_ is now %u", this->last_valid_edge_us_); } - this->pending_state_change_ = NONE; + + sensor->last_intr_ = now; + sensor->last_pin_val_ = pin_val; } } diff --git a/esphome/components/pulse_meter/pulse_meter_sensor.h b/esphome/components/pulse_meter/pulse_meter_sensor.h index 47af6e2398..ddd42c2ed5 100644 --- a/esphome/components/pulse_meter/pulse_meter_sensor.h +++ b/esphome/components/pulse_meter/pulse_meter_sensor.h @@ -17,41 +17,50 @@ class PulseMeterSensor : public sensor::Sensor, public Component { void set_pin(InternalGPIOPin *pin) { this->pin_ = pin; } void set_filter_us(uint32_t filter) { this->filter_us_ = filter; } - void set_filter_mode(InternalFilterMode mode) { this->filter_mode_ = mode; } void set_timeout_us(uint32_t timeout) { this->timeout_us_ = timeout; } void set_total_sensor(sensor::Sensor *sensor) { this->total_sensor_ = sensor; } - - void set_total_pulses(uint32_t pulses); + void set_filter_mode(InternalFilterMode mode) { this->filter_mode_ = mode; } + void set_total_pulses(uint32_t pulses) { this->total_pulses_ = pulses; } void setup() override; void loop() override; - float get_setup_priority() const override { return setup_priority::DATA; } + float get_setup_priority() const override; void dump_config() override; protected: - enum StateChange { TO_LOW = 0, TO_HIGH, NONE }; - - static void gpio_intr(PulseMeterSensor *sensor); - void handle_state_change_(uint32_t now, uint32_t last_detected_edge_us, uint32_t last_valid_edge_us, - bool has_valid_edge); + static void edge_intr(PulseMeterSensor *sensor); + static void pulse_intr(PulseMeterSensor *sensor); InternalGPIOPin *pin_{nullptr}; - ISRInternalGPIOPin isr_pin_; uint32_t filter_us_ = 0; uint32_t timeout_us_ = 1000000UL * 60UL * 5UL; sensor::Sensor *total_sensor_{nullptr}; InternalFilterMode filter_mode_{FILTER_EDGE}; - Deduplicator pulse_width_dedupe_; - Deduplicator total_dedupe_; + // Variables used in the loop + bool initialized_ = false; + uint32_t total_pulses_ = 0; + uint32_t last_processed_edge_us_ = 0; - volatile uint32_t last_detected_edge_us_ = 0; - volatile uint32_t last_valid_edge_us_ = 0; - volatile uint32_t pulse_width_us_ = 0; - volatile uint32_t total_pulses_ = 0; - volatile bool sensor_is_high_ = false; - volatile bool has_valid_edge_ = false; - volatile StateChange pending_state_change_{NONE}; + // This struct (and the two pointers) are used to pass data between the ISR and loop. + // These two pointers are exchanged each loop. + // Therefore you can't use data in the pointer to loop receives to set values in the pointer to loop sends. + // As a result it's easiest if you only use these pointers to send data from the ISR to the loop. + // (except for resetting the values) + struct State { + uint32_t last_detected_edge_us_ = 0; + uint32_t count_ = 0; + }; + State state_[2]; + volatile State *set_ = state_; + volatile State *get_ = state_ + 1; + + // Only use these variables in the ISR + ISRInternalGPIOPin isr_pin_; + uint32_t last_edge_candidate_us_ = 0; + uint32_t last_intr_ = 0; + bool in_pulse_ = false; + bool last_pin_val_ = false; }; } // namespace pulse_meter From 9980b9972fcac080a1eb1987107484644e7a8d1d Mon Sep 17 00:00:00 2001 From: Jimmy Hedman Date: Mon, 7 Aug 2023 23:16:42 +0200 Subject: [PATCH 65/84] Change MQTT client for ESP32 Arduino (#5157) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/components/mqtt/__init__.py | 4 ++-- ..._backend_idf.cpp => mqtt_backend_esp32.cpp} | 17 +++++++++-------- ...mqtt_backend_idf.h => mqtt_backend_esp32.h} | 4 ++-- ...ackend_arduino.h => mqtt_backend_esp8266.h} | 6 +++--- esphome/components/mqtt/mqtt_client.cpp | 6 +++--- esphome/components/mqtt/mqtt_client.h | 18 +++++++++--------- platformio.ini | 2 +- 7 files changed, 29 insertions(+), 28 deletions(-) rename esphome/components/mqtt/{mqtt_backend_idf.cpp => mqtt_backend_esp32.cpp} (93%) rename esphome/components/mqtt/{mqtt_backend_idf.h => mqtt_backend_esp32.h} (98%) rename esphome/components/mqtt/{mqtt_backend_arduino.h => mqtt_backend_esp8266.h} (96%) diff --git a/esphome/components/mqtt/__init__.py b/esphome/components/mqtt/__init__.py index 7207eaddc1..102c070eb6 100644 --- a/esphome/components/mqtt/__init__.py +++ b/esphome/components/mqtt/__init__.py @@ -271,8 +271,8 @@ def exp_mqtt_message(config): async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(var, config) - # Add required libraries for arduino - if CORE.using_arduino: + # Add required libraries for ESP8266 + if CORE.is_esp8266: # https://github.com/OttoWinter/async-mqtt-client/blob/master/library.json cg.add_library("ottowinter/AsyncMqttClient-esphome", "0.8.6") diff --git a/esphome/components/mqtt/mqtt_backend_idf.cpp b/esphome/components/mqtt/mqtt_backend_esp32.cpp similarity index 93% rename from esphome/components/mqtt/mqtt_backend_idf.cpp rename to esphome/components/mqtt/mqtt_backend_esp32.cpp index 7a7aca3fa6..2d4e6802f2 100644 --- a/esphome/components/mqtt/mqtt_backend_idf.cpp +++ b/esphome/components/mqtt/mqtt_backend_esp32.cpp @@ -1,7 +1,7 @@ -#ifdef USE_ESP_IDF +#ifdef USE_ESP32 #include -#include "mqtt_backend_idf.h" +#include "mqtt_backend_esp32.h" #include "esphome/core/log.h" #include "esphome/core/helpers.h" @@ -10,7 +10,7 @@ namespace mqtt { static const char *const TAG = "mqtt.idf"; -bool MQTTBackendIDF::initialize_() { +bool MQTTBackendESP32::initialize_() { #if ESP_IDF_VERSION_MAJOR < 5 mqtt_cfg_.user_context = (void *) this; mqtt_cfg_.buffer_size = MQTT_BUFFER_SIZE; @@ -95,7 +95,7 @@ bool MQTTBackendIDF::initialize_() { } } -void MQTTBackendIDF::loop() { +void MQTTBackendESP32::loop() { // process new events // handle only 1 message per loop iteration if (!mqtt_events_.empty()) { @@ -105,7 +105,7 @@ void MQTTBackendIDF::loop() { } } -void MQTTBackendIDF::mqtt_event_handler_(const Event &event) { +void MQTTBackendESP32::mqtt_event_handler_(const Event &event) { ESP_LOGV(TAG, "Event dispatched from event loop event_id=%d", event.event_id); switch (event.event_id) { case MQTT_EVENT_BEFORE_CONNECT: @@ -166,8 +166,9 @@ void MQTTBackendIDF::mqtt_event_handler_(const Event &event) { } /// static - Dispatch event to instance method -void MQTTBackendIDF::mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { - MQTTBackendIDF *instance = static_cast(handler_args); +void MQTTBackendESP32::mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, + void *event_data) { + MQTTBackendESP32 *instance = static_cast(handler_args); // queue event to decouple processing if (instance) { auto event = *static_cast(event_data); @@ -177,4 +178,4 @@ void MQTTBackendIDF::mqtt_event_handler(void *handler_args, esp_event_base_t bas } // namespace mqtt } // namespace esphome -#endif // USE_ESP_IDF +#endif // USE_ESP32 diff --git a/esphome/components/mqtt/mqtt_backend_idf.h b/esphome/components/mqtt/mqtt_backend_esp32.h similarity index 98% rename from esphome/components/mqtt/mqtt_backend_idf.h rename to esphome/components/mqtt/mqtt_backend_esp32.h index 9c7a5f80e9..a4ee96ca59 100644 --- a/esphome/components/mqtt/mqtt_backend_idf.h +++ b/esphome/components/mqtt/mqtt_backend_esp32.h @@ -1,6 +1,6 @@ #pragma once -#ifdef USE_ESP_IDF +#ifdef USE_ESP32 #include #include @@ -41,7 +41,7 @@ struct Event { error_handle(*event.error_handle) {} }; -class MQTTBackendIDF final : public MQTTBackend { +class MQTTBackendESP32 final : public MQTTBackend { public: static const size_t MQTT_BUFFER_SIZE = 4096; diff --git a/esphome/components/mqtt/mqtt_backend_arduino.h b/esphome/components/mqtt/mqtt_backend_esp8266.h similarity index 96% rename from esphome/components/mqtt/mqtt_backend_arduino.h rename to esphome/components/mqtt/mqtt_backend_esp8266.h index 6399ec88e0..2d91877e9d 100644 --- a/esphome/components/mqtt/mqtt_backend_arduino.h +++ b/esphome/components/mqtt/mqtt_backend_esp8266.h @@ -1,6 +1,6 @@ #pragma once -#ifdef USE_ARDUINO +#ifdef USE_ESP8266 #include "mqtt_backend.h" #include @@ -8,7 +8,7 @@ namespace esphome { namespace mqtt { -class MQTTBackendArduino final : public MQTTBackend { +class MQTTBackendESP8266 final : public MQTTBackend { public: void set_keep_alive(uint16_t keep_alive) final { mqtt_client_.setKeepAlive(keep_alive); } void set_client_id(const char *client_id) final { mqtt_client_.setClientId(client_id); } @@ -71,4 +71,4 @@ class MQTTBackendArduino final : public MQTTBackend { } // namespace mqtt } // namespace esphome -#endif // defined(USE_ARDUINO) +#endif // defined(USE_ESP8266) diff --git a/esphome/components/mqtt/mqtt_client.cpp b/esphome/components/mqtt/mqtt_client.cpp index cb5d306976..d3f759c072 100644 --- a/esphome/components/mqtt/mqtt_client.cpp +++ b/esphome/components/mqtt/mqtt_client.cpp @@ -556,8 +556,8 @@ static bool topic_match(const char *message, const char *subscription) { } void MQTTClientComponent::on_message(const std::string &topic, const std::string &payload) { -#ifdef USE_ARDUINO - // on Arduino, this is called in lwIP/AsyncTCP task; some components do not like running +#ifdef USE_ESP8266 + // on ESP8266, this is called in lwIP/AsyncTCP task; some components do not like running // from a different task. this->defer([this, topic, payload]() { #endif @@ -565,7 +565,7 @@ void MQTTClientComponent::on_message(const std::string &topic, const std::string if (topic_match(topic.c_str(), subscription.topic.c_str())) subscription.callback(topic, payload); } -#ifdef USE_ARDUINO +#ifdef USE_ESP8266 }); #endif } diff --git a/esphome/components/mqtt/mqtt_client.h b/esphome/components/mqtt/mqtt_client.h index 83ed3cc645..00eb3fdd40 100644 --- a/esphome/components/mqtt/mqtt_client.h +++ b/esphome/components/mqtt/mqtt_client.h @@ -9,10 +9,10 @@ #include "esphome/core/log.h" #include "esphome/components/json/json_util.h" #include "esphome/components/network/ip_address.h" -#if defined(USE_ESP_IDF) -#include "mqtt_backend_idf.h" -#elif defined(USE_ARDUINO) -#include "mqtt_backend_arduino.h" +#if defined(USE_ESP32) +#include "mqtt_backend_esp32.h" +#elif defined(USE_ESP8266) +#include "mqtt_backend_esp8266.h" #endif #include "lwip/ip_addr.h" @@ -142,7 +142,7 @@ class MQTTClientComponent : public Component { */ void add_ssl_fingerprint(const std::array &fingerprint); #endif -#ifdef USE_ESP_IDF +#ifdef USE_ESP32 void set_ca_certificate(const char *cert) { this->mqtt_backend_.set_ca_certificate(cert); } void set_skip_cert_cn_check(bool skip_check) { this->mqtt_backend_.set_skip_cert_cn_check(skip_check); } #endif @@ -296,10 +296,10 @@ class MQTTClientComponent : public Component { int log_level_{ESPHOME_LOG_LEVEL}; std::vector subscriptions_; -#if defined(USE_ESP_IDF) - MQTTBackendIDF mqtt_backend_; -#elif defined(USE_ARDUINO) - MQTTBackendArduino mqtt_backend_; +#if defined(USE_ESP32) + MQTTBackendESP32 mqtt_backend_; +#elif defined(USE_ESP8266) + MQTTBackendESP8266 mqtt_backend_; #endif MQTTClientState state_{MQTT_CLIENT_DISCONNECTED}; diff --git a/platformio.ini b/platformio.ini index 8141c803f1..ba149ce99e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -57,7 +57,6 @@ lib_deps = ${common.lib_deps} SPI ; spi (Arduino built-in) Wire ; i2c (Arduino built-int) - ottowinter/AsyncMqttClient-esphome@0.8.6 ; mqtt esphome/ESPAsyncWebServer-esphome@2.1.0 ; web_server_base fastled/FastLED@3.3.2 ; fastled_base mikalhart/TinyGPSPlus@1.0.2 ; gps @@ -89,6 +88,7 @@ lib_deps = ${common:arduino.lib_deps} ESP8266WiFi ; wifi (Arduino built-in) Update ; ota (Arduino built-in) + ottowinter/AsyncMqttClient-esphome@0.8.6 ; mqtt esphome/ESPAsyncTCP-esphome@1.2.3 ; async_tcp ESP8266HTTPClient ; http_request (Arduino built-in) ESP8266mDNS ; mdns (Arduino built-in) From 0b1b25191dea6f7a145170d8db26691b668f1326 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Tue, 8 Aug 2023 09:45:56 +1200 Subject: [PATCH 66/84] Add read interface to microphone (#5131) --- .../microphone/i2s_audio_microphone.cpp | 49 +++++++++---------- .../microphone/i2s_audio_microphone.h | 3 +- esphome/components/microphone/microphone.h | 1 + 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/esphome/components/i2s_audio/microphone/i2s_audio_microphone.cpp b/esphome/components/i2s_audio/microphone/i2s_audio_microphone.cpp index 9c661c3ac2..cf0628d638 100644 --- a/esphome/components/i2s_audio/microphone/i2s_audio_microphone.cpp +++ b/esphome/components/i2s_audio/microphone/i2s_audio_microphone.cpp @@ -16,14 +16,6 @@ static const char *const TAG = "i2s_audio.microphone"; void I2SAudioMicrophone::setup() { ESP_LOGCONFIG(TAG, "Setting up I2S Audio Microphone..."); - ExternalRAMAllocator allocator(ExternalRAMAllocator::ALLOW_FAILURE); - this->buffer_ = allocator.allocate(BUFFER_SIZE); - if (this->buffer_ == nullptr) { - ESP_LOGE(TAG, "Failed to allocate buffer!"); - this->mark_failed(); - return; - } - #if SOC_I2S_SUPPORTS_ADC if (this->adc_) { if (this->parent_->get_port() != I2S_NUM_0) { @@ -110,37 +102,38 @@ void I2SAudioMicrophone::stop_() { this->high_freq_.stop(); } -void I2SAudioMicrophone::read_() { +size_t I2SAudioMicrophone::read(int16_t *buf, size_t len) { size_t bytes_read = 0; - esp_err_t err = - i2s_read(this->parent_->get_port(), this->buffer_, BUFFER_SIZE, &bytes_read, (100 / portTICK_PERIOD_MS)); + esp_err_t err = i2s_read(this->parent_->get_port(), buf, len, &bytes_read, (100 / portTICK_PERIOD_MS)); if (err != ESP_OK) { ESP_LOGW(TAG, "Error reading from I2S microphone: %s", esp_err_to_name(err)); this->status_set_warning(); - return; + return 0; } this->status_clear_warning(); - - std::vector samples; - size_t samples_read = 0; if (this->bits_per_sample_ == I2S_BITS_PER_SAMPLE_16BIT) { - samples_read = bytes_read / sizeof(int16_t); - } else if (this->bits_per_sample_ == I2S_BITS_PER_SAMPLE_32BIT) { - samples_read = bytes_read / sizeof(int32_t); - } else { - ESP_LOGE(TAG, "Unsupported bits per sample: %d", this->bits_per_sample_); - return; - } - samples.resize(samples_read); - if (this->bits_per_sample_ == I2S_BITS_PER_SAMPLE_16BIT) { - memcpy(samples.data(), this->buffer_, bytes_read); + return bytes_read; } else if (this->bits_per_sample_ == I2S_BITS_PER_SAMPLE_32BIT) { + std::vector samples; + size_t samples_read = bytes_read / sizeof(int32_t); + samples.resize(samples_read); for (size_t i = 0; i < samples_read; i++) { - int32_t temp = reinterpret_cast(this->buffer_)[i] >> 14; + int32_t temp = reinterpret_cast(buf)[i] >> 14; samples[i] = clamp(temp, INT16_MIN, INT16_MAX); } + memcpy(buf, samples.data(), samples_read * sizeof(int16_t)); + return samples_read * sizeof(int16_t); + } else { + ESP_LOGE(TAG, "Unsupported bits per sample: %d", this->bits_per_sample_); + return 0; } +} +void I2SAudioMicrophone::read_() { + std::vector samples; + samples.resize(BUFFER_SIZE); + size_t bytes_read = this->read(samples.data(), BUFFER_SIZE / sizeof(int16_t)); + samples.resize(bytes_read / sizeof(int16_t)); this->data_callbacks_.call(samples); } @@ -152,7 +145,9 @@ void I2SAudioMicrophone::loop() { this->start_(); break; case microphone::STATE_RUNNING: - this->read_(); + if (this->data_callbacks_.size() > 0) { + this->read_(); + } break; case microphone::STATE_STOPPING: this->stop_(); diff --git a/esphome/components/i2s_audio/microphone/i2s_audio_microphone.h b/esphome/components/i2s_audio/microphone/i2s_audio_microphone.h index 0cb87d42fd..dc6b70047a 100644 --- a/esphome/components/i2s_audio/microphone/i2s_audio_microphone.h +++ b/esphome/components/i2s_audio/microphone/i2s_audio_microphone.h @@ -21,6 +21,8 @@ class I2SAudioMicrophone : public I2SAudioIn, public microphone::Microphone, pub void set_din_pin(int8_t pin) { this->din_pin_ = pin; } void set_pdm(bool pdm) { this->pdm_ = pdm; } + size_t read(int16_t *buf, size_t len) override; + #if SOC_I2S_SUPPORTS_ADC void set_adc_channel(adc1_channel_t channel) { this->adc_channel_ = channel; @@ -42,7 +44,6 @@ class I2SAudioMicrophone : public I2SAudioIn, public microphone::Microphone, pub bool adc_{false}; #endif bool pdm_{false}; - uint8_t *buffer_; i2s_channel_fmt_t channel_; i2s_bits_per_sample_t bits_per_sample_; diff --git a/esphome/components/microphone/microphone.h b/esphome/components/microphone/microphone.h index ac3db3ec0f..e01a10e15c 100644 --- a/esphome/components/microphone/microphone.h +++ b/esphome/components/microphone/microphone.h @@ -20,6 +20,7 @@ class Microphone { void add_data_callback(std::function &)> &&data_callback) { this->data_callbacks_.add(std::move(data_callback)); } + virtual size_t read(int16_t *buf, size_t len) = 0; bool is_running() const { return this->state_ == STATE_RUNNING; } bool is_stopped() const { return this->state_ == STATE_STOPPED; } From 9876d5276ca3220db812f1ca91f37db336bd569c Mon Sep 17 00:00:00 2001 From: Stijn Tintel Date: Tue, 8 Aug 2023 00:51:22 +0300 Subject: [PATCH 67/84] i2c: fix build on ESP-IDF >= 5.1 (#5200) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/components/i2c/i2c_bus_esp_idf.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/esphome/components/i2c/i2c_bus_esp_idf.cpp b/esphome/components/i2c/i2c_bus_esp_idf.cpp index 24c1860e6f..e2c7e7ddcb 100644 --- a/esphome/components/i2c/i2c_bus_esp_idf.cpp +++ b/esphome/components/i2c/i2c_bus_esp_idf.cpp @@ -15,8 +15,19 @@ static const char *const TAG = "i2c.idf"; void IDFI2CBus::setup() { ESP_LOGCONFIG(TAG, "Setting up I2C bus..."); - static i2c_port_t next_port = 0; - port_ = next_port++; + static i2c_port_t next_port = I2C_NUM_0; + port_ = next_port; +#if I2C_NUM_MAX > 1 + next_port = (next_port == I2C_NUM_0) ? I2C_NUM_1 : I2C_NUM_MAX; +#else + next_port = I2C_NUM_MAX; +#endif + + if (port_ == I2C_NUM_MAX) { + ESP_LOGE(TAG, "Too many I2C buses configured"); + this->mark_failed(); + return; + } recover_(); From e4cf7b86fa33a1b440c106fbae7ab3a096c2fa2b Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Tue, 8 Aug 2023 10:22:10 +1200 Subject: [PATCH 68/84] Add socket define for rp2040 dev (#4968) --- esphome/core/defines.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/esphome/core/defines.h b/esphome/core/defines.h index 638dd39364..8d4d7e3f22 100644 --- a/esphome/core/defines.h +++ b/esphome/core/defines.h @@ -103,6 +103,11 @@ #endif +#ifdef USE_RP2040 +#define USE_ARDUINO_VERSION_CODE VERSION_CODE(3, 3, 0) +#define USE_SOCKET_IMPL_LWIP_TCP +#endif + #ifdef USE_HOST #define USE_SOCKET_IMPL_BSD_SOCKETS #endif From f4ac176d771981a23bcf8c9bebdca6d1d5f17289 Mon Sep 17 00:00:00 2001 From: Stijn Tintel Date: Tue, 8 Aug 2023 01:45:50 +0300 Subject: [PATCH 69/84] core: read ESP32 MAC address from eFuse if IEEE802.15.4 is supported (#5176) --- esphome/core/helpers.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index 0bc762fd0d..c65928556a 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -38,7 +38,7 @@ #include "esp32/rom/crc.h" #endif -#ifdef USE_ESP32_IGNORE_EFUSE_MAC_CRC +#if defined(CONFIG_SOC_IEEE802154_SUPPORTED) || defined(USE_ESP32_IGNORE_EFUSE_MAC_CRC) #include "esp_efuse.h" #include "esp_efuse_table.h" #endif @@ -540,7 +540,9 @@ bool HighFrequencyLoopRequester::is_high_frequency() { return num_requests > 0; void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter) #if defined(USE_ESP32) -#if defined(USE_ESP32_IGNORE_EFUSE_MAC_CRC) +#if defined(CONFIG_SOC_IEEE802154_SUPPORTED) || defined(USE_ESP32_IGNORE_EFUSE_MAC_CRC) + // When CONFIG_SOC_IEEE802154_SUPPORTED is defined, esp_efuse_mac_get_default + // returns the 802.15.4 EUI-64 address. Read directly from eFuse instead. // On some devices, the MAC address that is burnt into EFuse does not // match the CRC that goes along with it. For those devices, this // work-around reads and uses the MAC address as-is from EFuse, From 4e7011c25defe99ecde2e6bc8f67bffe0d037cd2 Mon Sep 17 00:00:00 2001 From: Stijn Tintel Date: Tue, 8 Aug 2023 02:18:06 +0300 Subject: [PATCH 70/84] esp32_ble_beacon: enable CONFIG_BT_BLE_42_FEATURES_SUPPORTED (#5211) --- esphome/components/esp32_ble_beacon/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/esphome/components/esp32_ble_beacon/__init__.py b/esphome/components/esp32_ble_beacon/__init__.py index 311919dcd4..9aac48cbb2 100644 --- a/esphome/components/esp32_ble_beacon/__init__.py +++ b/esphome/components/esp32_ble_beacon/__init__.py @@ -72,3 +72,4 @@ async def to_code(config): if CORE.using_esp_idf: add_idf_sdkconfig_option("CONFIG_BT_ENABLED", True) + add_idf_sdkconfig_option("CONFIG_BT_BLE_42_FEATURES_SUPPORTED", True) From 1d5f088740da0e20b3e59d2fe84caacc2730d406 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 8 Aug 2023 11:21:51 +1200 Subject: [PATCH 71/84] Bump pytest-asyncio from 0.21.0 to 0.21.1 (#5187) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- requirements_test.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements_test.txt b/requirements_test.txt index fd1f45abc8..e67c57323e 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -8,7 +8,7 @@ pre-commit pytest==7.4.0 pytest-cov==4.1.0 pytest-mock==3.11.1 -pytest-asyncio==0.21.0 +pytest-asyncio==0.21.1 asyncmock==0.4.2 hypothesis==5.49.0 From ffd2cb9814dd5e424a0771a076c113a5cee9d74e Mon Sep 17 00:00:00 2001 From: Stijn Tintel Date: Tue, 8 Aug 2023 02:47:57 +0300 Subject: [PATCH 72/84] ledc: check SOC_LEDC_SUPPORT_APB_CLOCK (#5212) --- esphome/components/ledc/ledc_output.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/esphome/components/ledc/ledc_output.cpp b/esphome/components/ledc/ledc_output.cpp index d6241dd5b0..dfb84c1e76 100644 --- a/esphome/components/ledc/ledc_output.cpp +++ b/esphome/components/ledc/ledc_output.cpp @@ -17,7 +17,11 @@ #define CLOCK_FREQUENCY 40e6f #endif #else +#ifdef SOC_LEDC_SUPPORT_APB_CLOCK #define DEFAULT_CLK LEDC_USE_APB_CLK +#else +#define DEFAULT_CLK LEDC_AUTO_CLK +#endif #endif static const uint8_t SETUP_ATTEMPT_COUNT_MAX = 5; From a6b89e4e8a93de0b8edc3a4ef8db62a119a2a662 Mon Sep 17 00:00:00 2001 From: Francesco Ciocchetti <2085772+primeroz@users.noreply.github.com> Date: Tue, 8 Aug 2023 01:57:40 +0200 Subject: [PATCH 73/84] Add arm night to alarm control panel (#5186) --- .../alarm_control_panel/__init__.py | 13 ++++++++++++ .../alarm_control_panel_call.cpp | 5 +++++ .../alarm_control_panel_state.cpp | 2 +- .../alarm_control_panel/automation.h | 20 +++++++++++++++++++ .../template/alarm_control_panel/__init__.py | 15 ++++++++++++++ .../template_alarm_control_panel.cpp | 16 +++++++++++++++ .../template_alarm_control_panel.h | 12 +++++++++++ tests/test3.yaml | 2 ++ 8 files changed, 84 insertions(+), 1 deletion(-) diff --git a/esphome/components/alarm_control_panel/__init__.py b/esphome/components/alarm_control_panel/__init__.py index 0839e49875..52f8ac7894 100644 --- a/esphome/components/alarm_control_panel/__init__.py +++ b/esphome/components/alarm_control_panel/__init__.py @@ -31,6 +31,7 @@ ClearedTrigger = alarm_control_panel_ns.class_( ) ArmAwayAction = alarm_control_panel_ns.class_("ArmAwayAction", automation.Action) ArmHomeAction = alarm_control_panel_ns.class_("ArmHomeAction", automation.Action) +ArmNightAction = alarm_control_panel_ns.class_("ArmNightAction", automation.Action) DisarmAction = alarm_control_panel_ns.class_("DisarmAction", automation.Action) PendingAction = alarm_control_panel_ns.class_("PendingAction", automation.Action) TriggeredAction = alarm_control_panel_ns.class_("TriggeredAction", automation.Action) @@ -117,6 +118,18 @@ async def alarm_action_arm_home_to_code(config, action_id, template_arg, args): return var +@automation.register_action( + "alarm_control_panel.arm_night", ArmNightAction, ALARM_CONTROL_PANEL_ACTION_SCHEMA +) +async def alarm_action_arm_night_to_code(config, action_id, template_arg, args): + paren = await cg.get_variable(config[CONF_ID]) + var = cg.new_Pvariable(action_id, template_arg, paren) + if CONF_CODE in config: + templatable_ = await cg.templatable(config[CONF_CODE], args, cg.std_string) + cg.add(var.set_code(templatable_)) + return var + + @automation.register_action( "alarm_control_panel.disarm", DisarmAction, ALARM_CONTROL_PANEL_ACTION_SCHEMA ) diff --git a/esphome/components/alarm_control_panel/alarm_control_panel_call.cpp b/esphome/components/alarm_control_panel/alarm_control_panel_call.cpp index eb50c4f4b5..b1d2b2a097 100644 --- a/esphome/components/alarm_control_panel/alarm_control_panel_call.cpp +++ b/esphome/components/alarm_control_panel/alarm_control_panel_call.cpp @@ -85,6 +85,11 @@ void AlarmControlPanelCall::validate_() { this->state_.reset(); return; } + if (state == ACP_STATE_ARMED_NIGHT && (this->parent_->get_supported_features() & ACP_FEAT_ARM_NIGHT) == 0) { + ESP_LOGW(TAG, "Cannot arm night when not supported"); + this->state_.reset(); + return; + } } } diff --git a/esphome/components/alarm_control_panel/alarm_control_panel_state.cpp b/esphome/components/alarm_control_panel/alarm_control_panel_state.cpp index 231e7228e1..abe6f51995 100644 --- a/esphome/components/alarm_control_panel/alarm_control_panel_state.cpp +++ b/esphome/components/alarm_control_panel/alarm_control_panel_state.cpp @@ -12,7 +12,7 @@ const LogString *alarm_control_panel_state_to_string(AlarmControlPanelState stat case ACP_STATE_ARMED_AWAY: return LOG_STR("ARMED_AWAY"); case ACP_STATE_ARMED_NIGHT: - return LOG_STR("NIGHT"); + return LOG_STR("ARMED_NIGHT"); case ACP_STATE_ARMED_VACATION: return LOG_STR("ARMED_VACATION"); case ACP_STATE_ARMED_CUSTOM_BYPASS: diff --git a/esphome/components/alarm_control_panel/automation.h b/esphome/components/alarm_control_panel/automation.h index 4368129609..81ac584f71 100644 --- a/esphome/components/alarm_control_panel/automation.h +++ b/esphome/components/alarm_control_panel/automation.h @@ -67,6 +67,26 @@ template class ArmHomeAction : public Action { AlarmControlPanel *alarm_control_panel_; }; +template class ArmNightAction : public Action { + public: + explicit ArmNightAction(AlarmControlPanel *alarm_control_panel) : alarm_control_panel_(alarm_control_panel) {} + + TEMPLATABLE_VALUE(std::string, code) + + void play(Ts... x) override { + auto call = this->alarm_control_panel_->make_call(); + auto code = this->code_.optional_value(x...); + if (code.has_value()) { + call.set_code(code.value()); + } + call.arm_night(); + call.perform(); + } + + protected: + AlarmControlPanel *alarm_control_panel_; +}; + template class DisarmAction : public Action { public: explicit DisarmAction(AlarmControlPanel *alarm_control_panel) : alarm_control_panel_(alarm_control_panel) {} diff --git a/esphome/components/template/alarm_control_panel/__init__.py b/esphome/components/template/alarm_control_panel/__init__.py index 5156a0832a..27b7e92b4f 100644 --- a/esphome/components/template/alarm_control_panel/__init__.py +++ b/esphome/components/template/alarm_control_panel/__init__.py @@ -16,18 +16,22 @@ CODEOWNERS = ["@grahambrown11"] CONF_CODES = "codes" CONF_BYPASS_ARMED_HOME = "bypass_armed_home" +CONF_BYPASS_ARMED_NIGHT = "bypass_armed_night" CONF_REQUIRES_CODE_TO_ARM = "requires_code_to_arm" CONF_ARMING_HOME_TIME = "arming_home_time" +CONF_ARMING_NIGHT_TIME = "arming_night_time" CONF_ARMING_AWAY_TIME = "arming_away_time" CONF_PENDING_TIME = "pending_time" CONF_TRIGGER_TIME = "trigger_time" FLAG_NORMAL = "normal" FLAG_BYPASS_ARMED_HOME = "bypass_armed_home" +FLAG_BYPASS_ARMED_NIGHT = "bypass_armed_night" BinarySensorFlags = { FLAG_NORMAL: 1 << 0, FLAG_BYPASS_ARMED_HOME: 1 << 1, + FLAG_BYPASS_ARMED_NIGHT: 1 << 2, } TemplateAlarmControlPanel = template_ns.class_( @@ -55,6 +59,7 @@ TEMPLATE_ALARM_CONTROL_PANEL_BINARY_SENSOR_SCHEMA = cv.maybe_simple_value( { cv.Required(CONF_INPUT): cv.use_id(binary_sensor.BinarySensor), cv.Optional(CONF_BYPASS_ARMED_HOME, default=False): cv.boolean, + cv.Optional(CONF_BYPASS_ARMED_NIGHT, default=False): cv.boolean, }, key=CONF_INPUT, ) @@ -66,6 +71,7 @@ TEMPLATE_ALARM_CONTROL_PANEL_SCHEMA = ( cv.Optional(CONF_CODES): cv.ensure_list(cv.string_strict), cv.Optional(CONF_REQUIRES_CODE_TO_ARM): cv.boolean, cv.Optional(CONF_ARMING_HOME_TIME): cv.positive_time_period_milliseconds, + cv.Optional(CONF_ARMING_NIGHT_TIME): cv.positive_time_period_milliseconds, cv.Optional( CONF_ARMING_AWAY_TIME, default="0s" ): cv.positive_time_period_milliseconds, @@ -110,14 +116,23 @@ async def to_code(config): cg.add(var.set_arming_home_time(config[CONF_ARMING_HOME_TIME])) supports_arm_home = True + supports_arm_night = False + if CONF_ARMING_NIGHT_TIME in config: + cg.add(var.set_arming_night_time(config[CONF_ARMING_NIGHT_TIME])) + supports_arm_night = True + for sensor in config.get(CONF_BINARY_SENSORS, []): bs = await cg.get_variable(sensor[CONF_INPUT]) flags = BinarySensorFlags[FLAG_NORMAL] if sensor[CONF_BYPASS_ARMED_HOME]: flags |= BinarySensorFlags[FLAG_BYPASS_ARMED_HOME] supports_arm_home = True + if sensor[CONF_BYPASS_ARMED_NIGHT]: + flags |= BinarySensorFlags[FLAG_BYPASS_ARMED_NIGHT] + supports_arm_night = True cg.add(var.add_sensor(bs, flags)) cg.add(var.set_supports_arm_home(supports_arm_home)) + cg.add(var.set_supports_arm_night(supports_arm_night)) cg.add(var.set_restore_mode(config[CONF_RESTORE_MODE])) diff --git a/esphome/components/template/alarm_control_panel/template_alarm_control_panel.cpp b/esphome/components/template/alarm_control_panel/template_alarm_control_panel.cpp index 1c54998e42..da56976b56 100644 --- a/esphome/components/template/alarm_control_panel/template_alarm_control_panel.cpp +++ b/esphome/components/template/alarm_control_panel/template_alarm_control_panel.cpp @@ -29,6 +29,8 @@ void TemplateAlarmControlPanel::dump_config() { ESP_LOGCONFIG(TAG, " Arming Away Time: %us", (this->arming_away_time_ / 1000)); if (this->arming_home_time_ != 0) ESP_LOGCONFIG(TAG, " Arming Home Time: %us", (this->arming_home_time_ / 1000)); + if (this->arming_night_time_ != 0) + ESP_LOGCONFIG(TAG, " Arming Night Time: %us", (this->arming_night_time_ / 1000)); ESP_LOGCONFIG(TAG, " Pending Time: %us", (this->pending_time_ / 1000)); ESP_LOGCONFIG(TAG, " Trigger Time: %us", (this->trigger_time_ / 1000)); ESP_LOGCONFIG(TAG, " Supported Features: %u", this->get_supported_features()); @@ -38,6 +40,8 @@ void TemplateAlarmControlPanel::dump_config() { ESP_LOGCONFIG(TAG, " Name: %s", sensor_pair.first->get_name().c_str()); ESP_LOGCONFIG(TAG, " Armed home bypass: %s", TRUEFALSE(sensor_pair.second & BINARY_SENSOR_MODE_BYPASS_ARMED_HOME)); + ESP_LOGCONFIG(TAG, " Armed night bypass: %s", + TRUEFALSE(sensor_pair.second & BINARY_SENSOR_MODE_BYPASS_ARMED_NIGHT)); } #endif } @@ -69,6 +73,9 @@ void TemplateAlarmControlPanel::loop() { if (this->desired_state_ == ACP_STATE_ARMED_HOME) { delay = this->arming_home_time_; } + if (this->desired_state_ == ACP_STATE_ARMED_NIGHT) { + delay = this->arming_night_time_; + } if ((millis() - this->last_update_) > delay) { this->publish_state(this->desired_state_); } @@ -95,6 +102,10 @@ void TemplateAlarmControlPanel::loop() { (sensor_pair.second & BINARY_SENSOR_MODE_BYPASS_ARMED_HOME)) { continue; } + if (this->current_state_ == ACP_STATE_ARMED_NIGHT && + (sensor_pair.second & BINARY_SENSOR_MODE_BYPASS_ARMED_NIGHT)) { + continue; + } trigger = true; break; } @@ -129,6 +140,9 @@ uint32_t TemplateAlarmControlPanel::get_supported_features() const { if (this->supports_arm_home_) { features |= ACP_FEAT_ARM_HOME; } + if (this->supports_arm_night_) { + features |= ACP_FEAT_ARM_NIGHT; + } return features; } @@ -158,6 +172,8 @@ void TemplateAlarmControlPanel::control(const AlarmControlPanelCall &call) { this->arm_(call.get_code(), ACP_STATE_ARMED_AWAY, this->arming_away_time_); } else if (call.get_state() == ACP_STATE_ARMED_HOME) { this->arm_(call.get_code(), ACP_STATE_ARMED_HOME, this->arming_home_time_); + } else if (call.get_state() == ACP_STATE_ARMED_NIGHT) { + this->arm_(call.get_code(), ACP_STATE_ARMED_NIGHT, this->arming_night_time_); } else if (call.get_state() == ACP_STATE_DISARMED) { if (!this->is_code_valid_(call.get_code())) { ESP_LOGW(TAG, "Not disarming code doesn't match"); diff --git a/esphome/components/template/alarm_control_panel/template_alarm_control_panel.h b/esphome/components/template/alarm_control_panel/template_alarm_control_panel.h index 4065356ba8..ebd8696692 100644 --- a/esphome/components/template/alarm_control_panel/template_alarm_control_panel.h +++ b/esphome/components/template/alarm_control_panel/template_alarm_control_panel.h @@ -19,6 +19,7 @@ namespace template_ { enum BinarySensorFlags : uint16_t { BINARY_SENSOR_MODE_NORMAL = 1 << 0, BINARY_SENSOR_MODE_BYPASS_ARMED_HOME = 1 << 1, + BINARY_SENSOR_MODE_BYPASS_ARMED_NIGHT = 1 << 2, }; #endif @@ -71,6 +72,12 @@ class TemplateAlarmControlPanel : public alarm_control_panel::AlarmControlPanel, */ void set_arming_home_time(uint32_t time) { this->arming_home_time_ = time; } + /** set the delay before arming night + * + * @param time The milliseconds + */ + void set_arming_night_time(uint32_t time) { this->arming_night_time_ = time; } + /** set the delay before triggering * * @param time The milliseconds @@ -85,6 +92,8 @@ class TemplateAlarmControlPanel : public alarm_control_panel::AlarmControlPanel, void set_supports_arm_home(bool supports_arm_home) { supports_arm_home_ = supports_arm_home; } + void set_supports_arm_night(bool supports_arm_night) { supports_arm_night_ = supports_arm_night; } + protected: void control(const alarm_control_panel::AlarmControlPanelCall &call) override; #ifdef USE_BINARY_SENSOR @@ -97,6 +106,8 @@ class TemplateAlarmControlPanel : public alarm_control_panel::AlarmControlPanel, uint32_t arming_away_time_; // the arming home delay uint32_t arming_home_time_{0}; + // the arming night delay + uint32_t arming_night_time_{0}; // the trigger delay uint32_t pending_time_; // the time in trigger @@ -106,6 +117,7 @@ class TemplateAlarmControlPanel : public alarm_control_panel::AlarmControlPanel, // requires a code to arm bool requires_code_to_arm_ = false; bool supports_arm_home_ = false; + bool supports_arm_night_ = false; // check if the code is valid bool is_code_valid_(optional code); diff --git a/tests/test3.yaml b/tests/test3.yaml index f7b66a748e..3ab1d561b3 100644 --- a/tests/test3.yaml +++ b/tests/test3.yaml @@ -1194,12 +1194,14 @@ alarm_control_panel: - "1234" requires_code_to_arm: true arming_home_time: 1s + arming_night_time: 1s arming_away_time: 15s pending_time: 15s trigger_time: 30s binary_sensors: - input: bin1 bypass_armed_home: true + bypass_armed_night: true on_state: then: - lambda: !lambda |- From 689bbf2419c9b5c4a4800e13f0d0dd002ff48688 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 7 Aug 2023 23:59:46 +0000 Subject: [PATCH 74/84] Bump pyupgrade from 3.9.0 to 3.10.1 (#5189) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- requirements_test.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 60053a14a1..cf86d354b7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -27,7 +27,7 @@ repos: - --branch=release - --branch=beta - repo: https://github.com/asottile/pyupgrade - rev: v3.9.0 + rev: v3.10.1 hooks: - id: pyupgrade args: [--py39-plus] diff --git a/requirements_test.txt b/requirements_test.txt index e67c57323e..7ab6742b02 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -1,7 +1,7 @@ pylint==2.17.5 flake8==6.0.0 # also change in .pre-commit-config.yaml when updating black==23.7.0 # also change in .pre-commit-config.yaml when updating -pyupgrade==3.9.0 # also change in .pre-commit-config.yaml when updating +pyupgrade==3.10.1 # also change in .pre-commit-config.yaml when updating pre-commit # Unit tests From f3329fdc8c341906fa21a958d6aec979afabebfb Mon Sep 17 00:00:00 2001 From: Rudd-O Date: Tue, 8 Aug 2023 00:32:34 +0000 Subject: [PATCH 75/84] Add KMeterISO component. (#5170) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/components/kmeteriso/__init__.py | 0 esphome/components/kmeteriso/kmeteriso.cpp | 82 ++++++++++++++++++++++ esphome/components/kmeteriso/kmeteriso.h | 34 +++++++++ esphome/components/kmeteriso/sensor.py | 55 +++++++++++++++ tests/test1.yaml | 7 ++ 5 files changed, 178 insertions(+) create mode 100644 esphome/components/kmeteriso/__init__.py create mode 100644 esphome/components/kmeteriso/kmeteriso.cpp create mode 100644 esphome/components/kmeteriso/kmeteriso.h create mode 100644 esphome/components/kmeteriso/sensor.py diff --git a/esphome/components/kmeteriso/__init__.py b/esphome/components/kmeteriso/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/esphome/components/kmeteriso/kmeteriso.cpp b/esphome/components/kmeteriso/kmeteriso.cpp new file mode 100644 index 0000000000..0276ab3f67 --- /dev/null +++ b/esphome/components/kmeteriso/kmeteriso.cpp @@ -0,0 +1,82 @@ +#include "kmeteriso.h" +#include "esphome/core/hal.h" +#include "esphome/core/log.h" + +namespace esphome { +namespace kmeteriso { + +static const char *const TAG = "kmeteriso.sensor"; + +static const uint8_t KMETER_ERROR_STATUS_REG = 0x20; +static const uint8_t KMETER_TEMP_VAL_REG = 0x00; +static const uint8_t KMETER_INTERNAL_TEMP_VAL_REG = 0x10; +static const uint8_t KMETER_FIRMWARE_VERSION_REG = 0xFE; + +void KMeterISOComponent::setup() { + ESP_LOGCONFIG(TAG, "Setting up KMeterISO..."); + this->error_code_ = NONE; + + // Mark as not failed before initializing. Some devices will turn off sensors to save on batteries + // and when they come back on, the COMPONENT_STATE_FAILED bit must be unset on the component. + if ((this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_FAILED) { + this->component_state_ &= ~COMPONENT_STATE_MASK; + this->component_state_ |= COMPONENT_STATE_CONSTRUCTION; + } + + auto err = this->bus_->writev(this->address_, nullptr, 0); + if (err == esphome::i2c::ERROR_OK) { + ESP_LOGCONFIG(TAG, "Could write to the address %d.", this->address_); + } else { + ESP_LOGCONFIG(TAG, "Could not write to the address %d.", this->address_); + this->error_code_ = COMMUNICATION_FAILED; + this->mark_failed(); + return; + } + + uint8_t read_buf[4] = {1}; + if (!this->read_bytes(KMETER_ERROR_STATUS_REG, read_buf, 1)) { + ESP_LOGCONFIG(TAG, "Could not read from the device."); + this->error_code_ = COMMUNICATION_FAILED; + this->mark_failed(); + return; + } + if (read_buf[0] != 0) { + ESP_LOGCONFIG(TAG, "The device is not ready."); + this->error_code_ = STATUS_FAILED; + this->mark_failed(); + return; + } + ESP_LOGCONFIG(TAG, "The device was successfully setup."); +} + +float KMeterISOComponent::get_setup_priority() const { return setup_priority::DATA; } + +void KMeterISOComponent::update() { + uint8_t read_buf[4]; + + if (this->temperature_sensor_ != nullptr) { + if (!this->read_bytes(KMETER_TEMP_VAL_REG, read_buf, 4)) { + ESP_LOGW(TAG, "Error reading temperature."); + } else { + int32_t temp = encode_uint32(read_buf[3], read_buf[2], read_buf[1], read_buf[0]); + float temp_f = temp / 100.0; + ESP_LOGV(TAG, "Got temperature=%.2f °C", temp_f); + this->temperature_sensor_->publish_state(temp_f); + } + } + + if (this->internal_temperature_sensor_ != nullptr) { + if (!this->read_bytes(KMETER_INTERNAL_TEMP_VAL_REG, read_buf, 4)) { + ESP_LOGW(TAG, "Error reading internal temperature."); + return; + } else { + int32_t internal_temp = encode_uint32(read_buf[3], read_buf[2], read_buf[1], read_buf[0]); + float internal_temp_f = internal_temp / 100.0; + ESP_LOGV(TAG, "Got internal temperature=%.2f °C", internal_temp_f); + this->internal_temperature_sensor_->publish_state(internal_temp_f); + } + } +} + +} // namespace kmeteriso +} // namespace esphome diff --git a/esphome/components/kmeteriso/kmeteriso.h b/esphome/components/kmeteriso/kmeteriso.h new file mode 100644 index 0000000000..c8bed662b0 --- /dev/null +++ b/esphome/components/kmeteriso/kmeteriso.h @@ -0,0 +1,34 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/components/sensor/sensor.h" +#include "esphome/components/i2c/i2c.h" +#include "esphome/components/i2c/i2c_bus.h" + +namespace esphome { +namespace kmeteriso { + +/// This class implements support for the KMeterISO thermocouple sensor. +class KMeterISOComponent : public PollingComponent, public i2c::I2CDevice { + public: + void set_temperature_sensor(sensor::Sensor *t) { this->temperature_sensor_ = t; } + void set_internal_temperature_sensor(sensor::Sensor *t) { this->internal_temperature_sensor_ = t; } + + // ========== INTERNAL METHODS ========== + // (In most use cases you won't need these) + void setup() override; + float get_setup_priority() const override; + void update() override; + + protected: + sensor::Sensor *temperature_sensor_{nullptr}; + sensor::Sensor *internal_temperature_sensor_{nullptr}; + enum ErrorCode { + NONE = 0, + COMMUNICATION_FAILED, + STATUS_FAILED, + } error_code_{NONE}; +}; + +} // namespace kmeteriso +} // namespace esphome diff --git a/esphome/components/kmeteriso/sensor.py b/esphome/components/kmeteriso/sensor.py new file mode 100644 index 0000000000..e730e446ae --- /dev/null +++ b/esphome/components/kmeteriso/sensor.py @@ -0,0 +1,55 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import i2c, sensor +from esphome.const import ( + CONF_ID, + CONF_TEMPERATURE, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, + UNIT_CELSIUS, + ENTITY_CATEGORY_DIAGNOSTIC, +) + +CONF_INTERNAL_TEMPERATURE = "internal_temperature" +DEPENDENCIES = ["i2c"] + +kmeteriso_ns = cg.esphome_ns.namespace("kmeteriso") + +KMeterISOComponent = kmeteriso_ns.class_( + "KMeterISOComponent", cg.PollingComponent, i2c.I2CDevice +) + +CONFIG_SCHEMA = ( + cv.Schema( + { + cv.GenerateID(): cv.declare_id(KMeterISOComponent), + cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( + unit_of_measurement=UNIT_CELSIUS, + accuracy_decimals=2, + device_class=DEVICE_CLASS_TEMPERATURE, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional(CONF_INTERNAL_TEMPERATURE): sensor.sensor_schema( + unit_of_measurement=UNIT_CELSIUS, + device_class=DEVICE_CLASS_TEMPERATURE, + state_class=STATE_CLASS_MEASUREMENT, + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, + ), + } + ) + .extend(cv.polling_component_schema("60s")) + .extend(i2c.i2c_device_schema(0x66)) +) + + +async def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + await cg.register_component(var, config) + await i2c.register_i2c_device(var, config) + + if temperature_config := config.get(CONF_TEMPERATURE): + sens = await sensor.new_sensor(temperature_config) + cg.add(var.set_temperature_sensor(sens)) + if internal_temperature_config := config.get(CONF_INTERNAL_TEMPERATURE): + sens = await sensor.new_sensor(internal_temperature_config) + cg.add(var.set_internal_temperature_sensor(sens)) diff --git a/tests/test1.yaml b/tests/test1.yaml index a00b886ac1..2419634570 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -792,6 +792,13 @@ sensor: name: INA3221 Channel 1 Shunt Voltage update_interval: 15s i2c_id: i2c_bus + - platform: kmeteriso + temperature: + name: Outside Temperature + internal_temperature: + name: Internal Ttemperature + update_interval: 15s + i2c_id: i2c_bus - platform: kalman_combinator name: Kalman-filtered temperature process_std_dev: 0.00139 From cd514b140ee6349c51a2f5a61c34a8a443cfa447 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 8 Aug 2023 14:31:49 +1200 Subject: [PATCH 76/84] Bump platformio from 6.1.7 to 6.1.9 (#5066) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- requirements.txt | 2 +- script/clang-tidy | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 2bb1e81fbc..6330a0996e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,7 +6,7 @@ tornado==6.3.2 tzlocal==5.0.1 # from time tzdata>=2021.1 # from time pyserial==3.5 -platformio==6.1.7 # When updating platformio, also update Dockerfile +platformio==6.1.9 # When updating platformio, also update Dockerfile esptool==4.6.2 click==8.1.6 esphome-dashboard==20230711.0 diff --git a/script/clang-tidy b/script/clang-tidy index d8dd033d29..b025221fa8 100755 --- a/script/clang-tidy +++ b/script/clang-tidy @@ -72,7 +72,7 @@ def clang_options(idedata): # copy compiler flags, except those clang doesn't understand. cmd.extend( flag - for flag in idedata["cxx_flags"].split(" ") + for flag in idedata["cxx_flags"] if flag not in ( "-free", From a8fa4b56f93f8da897948f2832793edc3b1adf3a Mon Sep 17 00:00:00 2001 From: kahrendt Date: Tue, 8 Aug 2023 01:05:08 -0400 Subject: [PATCH 77/84] New component: Add support for bmp581 pressure and temperature sensors (#4657) --- CODEOWNERS | 1 + esphome/components/bmp581/__init__.py | 0 esphome/components/bmp581/bmp581.cpp | 596 ++++++++++++++++++++++++++ esphome/components/bmp581/bmp581.h | 222 ++++++++++ esphome/components/bmp581/sensor.py | 163 +++++++ tests/test1.yaml | 8 + 6 files changed, 990 insertions(+) create mode 100644 esphome/components/bmp581/__init__.py create mode 100644 esphome/components/bmp581/bmp581.cpp create mode 100644 esphome/components/bmp581/bmp581.h create mode 100644 esphome/components/bmp581/sensor.py diff --git a/CODEOWNERS b/CODEOWNERS index 17d1462405..cda8a2f0f5 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -49,6 +49,7 @@ esphome/components/ble_client/* @buxtronix esphome/components/bluetooth_proxy/* @jesserockz esphome/components/bme680_bsec/* @trvrnrth esphome/components/bmp3xx/* @martgras +esphome/components/bmp581/* @kahrendt esphome/components/bp1658cj/* @Cossid esphome/components/bp5758d/* @Cossid esphome/components/button/* @esphome/core diff --git a/esphome/components/bmp581/__init__.py b/esphome/components/bmp581/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/esphome/components/bmp581/bmp581.cpp b/esphome/components/bmp581/bmp581.cpp new file mode 100644 index 0000000000..0308da0bcb --- /dev/null +++ b/esphome/components/bmp581/bmp581.cpp @@ -0,0 +1,596 @@ +/* + * Adds support for Bosch's BMP581 high accuracy pressure and temperature sensor + * - Component structure based on ESPHome's BMP3XX component (as of March, 2023) + * - Implementation is easier as the sensor itself automatically compensates pressure for the temperature + * - Temperature and pressure data is converted via simple divison operations in this component + * - IIR filter level can independently be applied to temperature and pressure measurements + * - Bosch's BMP5-Sensor-API was consulted to verify that sensor configuration is done correctly + * - Copyright (c) 2022 Bosch Sensortec Gmbh, SPDX-License-Identifier: BSD-3-Clause + * - This component uses forced power mode only so measurements are synchronized by the host + * - All datasheet page references refer to Bosch Document Number BST-BMP581-DS004-04 (revision number 1.4) + */ + +#include "bmp581.h" +#include "esphome/core/log.h" +#include "esphome/core/hal.h" + +namespace esphome { +namespace bmp581 { + +static const char *const TAG = "bmp581"; + +static const LogString *oversampling_to_str(Oversampling oversampling) { + switch (oversampling) { + case Oversampling::OVERSAMPLING_NONE: + return LOG_STR("None"); + case Oversampling::OVERSAMPLING_X2: + return LOG_STR("2x"); + case Oversampling::OVERSAMPLING_X4: + return LOG_STR("4x"); + case Oversampling::OVERSAMPLING_X8: + return LOG_STR("8x"); + case Oversampling::OVERSAMPLING_X16: + return LOG_STR("16x"); + case Oversampling::OVERSAMPLING_X32: + return LOG_STR("32x"); + case Oversampling::OVERSAMPLING_X64: + return LOG_STR("64x"); + case Oversampling::OVERSAMPLING_X128: + return LOG_STR("128x"); + default: + return LOG_STR(""); + } +} + +static const LogString *iir_filter_to_str(IIRFilter filter) { + switch (filter) { + case IIRFilter::IIR_FILTER_OFF: + return LOG_STR("OFF"); + case IIRFilter::IIR_FILTER_2: + return LOG_STR("2x"); + case IIRFilter::IIR_FILTER_4: + return LOG_STR("4x"); + case IIRFilter::IIR_FILTER_8: + return LOG_STR("8x"); + case IIRFilter::IIR_FILTER_16: + return LOG_STR("16x"); + case IIRFilter::IIR_FILTER_32: + return LOG_STR("32x"); + case IIRFilter::IIR_FILTER_64: + return LOG_STR("64x"); + case IIRFilter::IIR_FILTER_128: + return LOG_STR("128x"); + default: + return LOG_STR(""); + } +} + +void BMP581Component::dump_config() { + ESP_LOGCONFIG(TAG, "BMP581:"); + + switch (this->error_code_) { + case NONE: + break; + case ERROR_COMMUNICATION_FAILED: + ESP_LOGE(TAG, " Communication with BMP581 failed!"); + break; + case ERROR_WRONG_CHIP_ID: + ESP_LOGE(TAG, " BMP581 has wrong chip ID - please verify you are using a BMP 581"); + break; + case ERROR_SENSOR_RESET: + ESP_LOGE(TAG, " BMP581 failed to reset"); + break; + case ERROR_SENSOR_STATUS: + ESP_LOGE(TAG, " BMP581 sensor status failed, there were NVM problems"); + break; + case ERROR_PRIME_IIR_FAILED: + ESP_LOGE(TAG, " BMP581's IIR Filter failed to prime with an initial measurement"); + break; + default: + ESP_LOGE(TAG, " BMP581 error code %d", (int) this->error_code_); + break; + } + + LOG_I2C_DEVICE(this); + LOG_UPDATE_INTERVAL(this); + + ESP_LOGCONFIG(TAG, " Measurement conversion time: %ums", this->conversion_time_); + + if (this->temperature_sensor_) { + LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); + ESP_LOGCONFIG(TAG, " IIR Filter: %s", LOG_STR_ARG(iir_filter_to_str(this->iir_temperature_level_))); + ESP_LOGCONFIG(TAG, " Oversampling: %s", LOG_STR_ARG(oversampling_to_str(this->temperature_oversampling_))); + } + + if (this->pressure_sensor_) { + LOG_SENSOR(" ", "Pressure", this->pressure_sensor_); + ESP_LOGCONFIG(TAG, " IIR Filter: %s", LOG_STR_ARG(iir_filter_to_str(this->iir_pressure_level_))); + ESP_LOGCONFIG(TAG, " Oversampling: %s", LOG_STR_ARG(oversampling_to_str(this->pressure_oversampling_))); + } +} + +void BMP581Component::setup() { + /* + * Setup goes through several stages, which follows the post-power-up procedure (page 18 of datasheet) and then sets + * configured options + * 1) Soft reboot + * 2) Verify ASIC chip ID matches BMP581 + * 3) Verify sensor status (check if NVM is okay) + * 4) Enable data ready interrupt + * 5) Write oversampling settings and set internal configuration values + * 6) Configure and prime IIR Filter(s), if enabled + */ + + this->error_code_ = NONE; + ESP_LOGCONFIG(TAG, "Setting up BMP581..."); + + //////////////////// + // 1) Soft reboot // + //////////////////// + + // Power-On-Reboot bit is asserted if sensor successfully reset + if (!this->reset_()) { + ESP_LOGE(TAG, "BMP581 failed to reset"); + + this->error_code_ = ERROR_SENSOR_RESET; + this->mark_failed(); + + return; + } + + /////////////////////////////////////////// + // 2) Verify ASIC chip ID matches BMP581 // + /////////////////////////////////////////// + + uint8_t chip_id; + + // read chip id from sensor + if (!this->read_byte(BMP581_CHIP_ID, &chip_id)) { + ESP_LOGE(TAG, "Failed to read chip id"); + + this->error_code_ = ERROR_COMMUNICATION_FAILED; + this->mark_failed(); + + return; + } + + // verify id + if (chip_id != BMP581_ASIC_ID) { + ESP_LOGE(TAG, "Unknown chip ID, is this a BMP581?"); + + this->error_code_ = ERROR_WRONG_CHIP_ID; + this->mark_failed(); + + return; + } + + //////////////////////////////////////////////////// + // 3) Verify sensor status (check if NVM is okay) // + //////////////////////////////////////////////////// + + if (!this->read_byte(BMP581_STATUS, &this->status_.reg)) { + ESP_LOGE(TAG, "Failed to read status register"); + + this->error_code_ = ERROR_COMMUNICATION_FAILED; + this->mark_failed(); + + return; + } + + // verify status_nvm_rdy bit (it is asserted if boot was successful) + if (!(this->status_.bit.status_nvm_rdy)) { + ESP_LOGE(TAG, "NVM not ready after boot"); + + this->error_code_ = ERROR_SENSOR_STATUS; + this->mark_failed(); + + return; + } + + // verify status_nvm_err bit (it is asserted if an error is detected) + if (this->status_.bit.status_nvm_err) { + ESP_LOGE(TAG, "NVM error detected on boot"); + + this->error_code_ = ERROR_SENSOR_STATUS; + this->mark_failed(); + + return; + } + + //////////////////////////////////// + // 4) Enable data ready interrupt // + //////////////////////////////////// + + // enable the data ready interrupt source + if (!this->write_interrupt_source_settings_(true)) { + ESP_LOGE(TAG, "Failed to write interrupt source register"); + + this->error_code_ = ERROR_COMMUNICATION_FAILED; + this->mark_failed(); + + return; + } + + ////////////////////////////////////////////////////////////////////////// + // 5) Write oversampling settings and set internal configuration values // + ////////////////////////////////////////////////////////////////////////// + + // configure pressure readings, if sensor is defined + // otherwise, disable pressure oversampling + if (this->pressure_sensor_) { + this->osr_config_.bit.press_en = true; + } else { + this->pressure_oversampling_ = OVERSAMPLING_NONE; + } + + // write oversampling settings + if (!this->write_oversampling_settings_(this->temperature_oversampling_, this->pressure_oversampling_)) { + ESP_LOGE(TAG, "Failed to write oversampling register"); + + this->error_code_ = ERROR_COMMUNICATION_FAILED; + this->mark_failed(); + + return; + } + + // set output data rate to 4 Hz=0x19 (page 65 of datasheet) + // - ?shouldn't? matter as this component only uses FORCED_MODE - datasheet is ambiguous + // - If in NORMAL_MODE or NONSTOP_MODE, then this would still allow deep standby to save power + // - will be written to BMP581 at next requested measurement + this->odr_config_.bit.odr = 0x19; + + /////////////////////////////////////////////////////// + /// 6) Configure and prime IIR Filter(s), if enabled // + /////////////////////////////////////////////////////// + + if ((this->iir_temperature_level_ != IIR_FILTER_OFF) || (this->iir_pressure_level_ != IIR_FILTER_OFF)) { + if (!this->write_iir_settings_(this->iir_temperature_level_, this->iir_pressure_level_)) { + ESP_LOGE(TAG, "Failed to write IIR configuration registers"); + + this->error_code_ = ERROR_COMMUNICATION_FAILED; + this->mark_failed(); + + return; + } + + if (!this->prime_iir_filter_()) { + ESP_LOGE(TAG, "Failed to prime the IIR filter with an intiial measurement"); + + this->error_code_ = ERROR_PRIME_IIR_FAILED; + this->mark_failed(); + + return; + } + } +} + +void BMP581Component::update() { + /* + * Each update goes through several stages + * 0) Verify either a temperature or pressure sensor is defined before proceeding + * 1) Request a measurement + * 2) Wait for measurement to finish (based on oversampling rates) + * 3) Read data registers for temperature and pressure, if applicable + * 4) Publish measurements to sensor(s), if applicable + */ + + //////////////////////////////////////////////////////////////////////////////////// + // 0) Verify either a temperature or pressure sensor is defined before proceeding // + //////////////////////////////////////////////////////////////////////////////////// + + if ((!this->temperature_sensor_) && (!this->pressure_sensor_)) { + return; + } + + ////////////////////////////// + // 1) Request a measurement // + ////////////////////////////// + + ESP_LOGVV(TAG, "Requesting a measurement from sensor"); + + if (!this->start_measurement_()) { + ESP_LOGW(TAG, "Failed to request forced measurement of sensor"); + this->status_set_warning(); + + return; + } + + ////////////////////////////////////////////////////////////////////// + // 2) Wait for measurement to finish (based on oversampling rates) // + ////////////////////////////////////////////////////////////////////// + + ESP_LOGVV(TAG, "Measurement is expected to take %d ms to complete", this->conversion_time_); + + this->set_timeout("measurement", this->conversion_time_, [this]() { + float temperature = 0.0; + float pressure = 0.0; + + //////////////////////////////////////////////////////////////////////// + // 3) Read data registers for temperature and pressure, if applicable // + //////////////////////////////////////////////////////////////////////// + + if (this->pressure_sensor_) { + if (!this->read_temperature_and_pressure_(temperature, pressure)) { + ESP_LOGW(TAG, "Failed to read temperature and pressure measurements, skipping update"); + this->status_set_warning(); + + return; + } + } else { + if (!this->read_temperature_(temperature)) { + ESP_LOGW(TAG, "Failed to read temperature measurement, skipping update"); + this->status_set_warning(); + + return; + } + } + + ///////////////////////////////////////////////////////// + // 4) Publish measurements to sensor(s), if applicable // + ///////////////////////////////////////////////////////// + + if (this->temperature_sensor_) { + this->temperature_sensor_->publish_state(temperature); + } + + if (this->pressure_sensor_) { + this->pressure_sensor_->publish_state(pressure); + } + + this->status_clear_warning(); + }); +} + +bool BMP581Component::check_data_readiness_() { + // - verifies component is not internally in standby mode + // - reads interrupt status register + // - checks if data ready bit is asserted + // - If true, then internally sets component to standby mode if in forced mode + // - returns data readiness state + + if (this->odr_config_.bit.pwr_mode == STANDBY_MODE) { + ESP_LOGD(TAG, "Data is not ready, sensor is in standby mode"); + return false; + } + + uint8_t status; + + if (!this->read_byte(BMP581_INT_STATUS, &status)) { + ESP_LOGE(TAG, "Failed to read interrupt status register"); + return false; + } + + this->int_status_.reg = status; + + if (this->int_status_.bit.drdy_data_reg) { + // If in forced mode, then set internal record of the power mode to STANDBY_MODE + // - sensor automatically returns to standby mode after completing a forced measurement + if (this->odr_config_.bit.pwr_mode == FORCED_MODE) { + this->odr_config_.bit.pwr_mode = STANDBY_MODE; + } + + return true; + } + + return false; +} + +bool BMP581Component::prime_iir_filter_() { + // - temporarily disables oversampling for a fast initial measurement; avoids slowing down ESPHome's startup process + // - enables IIR filter flushing with forced measurements + // - forces a measurement; flushing the IIR filter and priming it with a current value + // - disables IIR filter flushing with forced measurements + // - reverts to internally configured oversampling rates + // - returns success of all register writes/priming + + // store current internal oversampling settings to revert to after priming + Oversampling current_temperature_oversampling = (Oversampling) this->osr_config_.bit.osr_t; + Oversampling current_pressure_oversampling = (Oversampling) this->osr_config_.bit.osr_p; + + // temporarily disables oversampling for temperature and pressure for a fast priming measurement + if (!this->write_oversampling_settings_(OVERSAMPLING_NONE, OVERSAMPLING_NONE)) { + ESP_LOGE(TAG, "Failed to write oversampling register"); + + return false; + } + + // flush the IIR filter with forced measurements (we will only flush once) + this->dsp_config_.bit.iir_flush_forced_en = true; + if (!this->write_byte(BMP581_DSP, this->dsp_config_.reg)) { + ESP_LOGE(TAG, "Failed to write IIR source register"); + + return false; + } + + // forces an intial measurement + // - this measurements flushes the IIR filter reflecting written DSP settings + // - flushing with this initial reading avoids having the internal previous data aquisition being 0, which + // (I)nfinitely affects future values + if (!this->start_measurement_()) { + ESP_LOGE(TAG, "Failed to request a forced measurement"); + + return false; + } + + // wait for priming measurement to complete + // - with oversampling disabled, the conversion time for a single measurement for pressure and temperature is + // ceilf(1.05*(1.0+1.0)) = 3ms + // - see page 12 of datasheet for details + delay(3); + + if (!this->check_data_readiness_()) { + ESP_LOGE(TAG, "IIR priming measurement was not ready"); + + return false; + } + + // disable IIR filter flushings on future forced measurements + this->dsp_config_.bit.iir_flush_forced_en = false; + if (!this->write_byte(BMP581_DSP, this->dsp_config_.reg)) { + ESP_LOGE(TAG, "Failed to write IIR source register"); + + return false; + } + + // revert oversampling rates to original settings + return this->write_oversampling_settings_(current_temperature_oversampling, current_pressure_oversampling); +} + +bool BMP581Component::read_temperature_(float &temperature) { + // - verifies data is ready to be read + // - reads in 3 bytes of temperature data + // - returns whether successful, where the the variable parameter contains + // - the measured temperature (in degrees Celsius) + + if (!this->check_data_readiness_()) { + ESP_LOGW(TAG, "Data from sensor isn't ready, skipping this update"); + this->status_set_warning(); + + return false; + } + + uint8_t data[3]; + if (!this->read_bytes(BMP581_MEASUREMENT_DATA, &data[0], 3)) { + ESP_LOGW(TAG, "Failed to read sensor's measurement data"); + this->status_set_warning(); + + return false; + } + + // temperature MSB is in data[2], LSB is in data[1], XLSB in data[0] + int32_t raw_temp = (int32_t) data[2] << 16 | (int32_t) data[1] << 8 | (int32_t) data[0]; + temperature = (float) (raw_temp / 65536.0); // convert measurement to degrees Celsius (page 22 of datasheet) + + return true; +} + +bool BMP581Component::read_temperature_and_pressure_(float &temperature, float &pressure) { + // - verifies data is ready to be read + // - reads in 6 bytes of temperature data (3 for temeperature, 3 for pressure) + // - returns whether successful, where the variable parameters contain + // - the measured temperature (in degrees Celsius) + // - the measured pressure (in Pa) + + if (!this->check_data_readiness_()) { + ESP_LOGW(TAG, "Data from sensor isn't ready, skipping this update"); + this->status_set_warning(); + + return false; + } + + uint8_t data[6]; + if (!this->read_bytes(BMP581_MEASUREMENT_DATA, &data[0], 6)) { + ESP_LOGW(TAG, "Failed to read sensor's measurement data"); + this->status_set_warning(); + + return false; + } + + // temperature MSB is in data[2], LSB is in data[1], XLSB in data[0] + int32_t raw_temp = (int32_t) data[2] << 16 | (int32_t) data[1] << 8 | (int32_t) data[0]; + temperature = (float) (raw_temp / 65536.0); // convert measurement to degrees Celsius (page 22 of datasheet) + + // pressure MSB is in data[5], LSB is in data[4], XLSB in data[3] + int32_t raw_press = (int32_t) data[5] << 16 | (int32_t) data[4] << 8 | (int32_t) data[3]; + pressure = (float) (raw_press / 64.0); // Divide by 2^6=64 for Pa (page 22 of datasheet) + + return true; +} + +bool BMP581Component::reset_() { + // - writes reset command to the command register + // - waits for sensor to complete reset + // - returns the Power-On-Reboot interrupt status, which is asserted if successful + + // writes reset command to BMP's command register + if (!this->write_byte(BMP581_COMMAND, RESET_COMMAND)) { + ESP_LOGE(TAG, "Failed to write reset command"); + + return false; + } + + // t_{soft_res} = 2ms (page 11 of datasheet); time it takes to enter standby mode + // - round up to 3 ms + delay(3); + + // read interrupt status register + if (!this->read_byte(BMP581_INT_STATUS, &this->int_status_.reg)) { + ESP_LOGE(TAG, "Failed to read interrupt status register"); + + return false; + } + + // Power-On-Reboot bit is asserted if sensor successfully reset + return this->int_status_.bit.por; +} + +bool BMP581Component::start_measurement_() { + // - only pushes the sensor into FORCED_MODE for a reading if already in STANDBY_MODE + // - returns whether a measurement is in progress or has been initiated + + if (this->odr_config_.bit.pwr_mode == STANDBY_MODE) { + return this->write_power_mode_(FORCED_MODE); + } else { + return true; + } +} + +bool BMP581Component::write_iir_settings_(IIRFilter temperature_iir, IIRFilter pressure_iir) { + // - ensures data registers store filtered values + // - sets IIR filter levels on sensor + // - matches other default settings on sensor + // - writes configuration to the two relevant registers + // - returns success or failure of write to the registers + + // If the temperature/pressure IIR filter is configured, then ensure data registers store the filtered measurement + this->dsp_config_.bit.shdw_sel_iir_t = (temperature_iir != IIR_FILTER_OFF); + this->dsp_config_.bit.shdw_sel_iir_p = (pressure_iir != IIR_FILTER_OFF); + + // set temperature and pressure IIR filter level to configured values + this->iir_config_.bit.set_iir_t = temperature_iir; + this->iir_config_.bit.set_iir_p = pressure_iir; + + // enable pressure and temperature compensation (page 61 of datasheet) + // - ?only relevant if IIR filter is applied?; the datasheet is ambiguous + // - matches BMP's default setting + this->dsp_config_.bit.comp_pt_en = 0x3; + + // BMP581_DSP register and BMP581_DSP_IIR registers are successive + // - allows us to write the IIR configuration with one command to both registers + uint8_t register_data[2] = {this->dsp_config_.reg, this->iir_config_.reg}; + return this->write_bytes(BMP581_DSP, register_data, sizeof(register_data)); +} + +bool BMP581Component::write_interrupt_source_settings_(bool data_ready_enable) { + // - updates component's internal setting + // - returns success or failure of write to interrupt source register + + this->int_source_.bit.drdy_data_reg_en = data_ready_enable; + + // write interrupt source register + return this->write_byte(BMP581_INT_SOURCE, this->int_source_.reg); +} + +bool BMP581Component::write_oversampling_settings_(Oversampling temperature_oversampling, + Oversampling pressure_oversampling) { + // - updates component's internal setting + // - returns success or failure of write to Over-Sampling Rate register + + this->osr_config_.bit.osr_t = temperature_oversampling; + this->osr_config_.bit.osr_p = pressure_oversampling; + + return this->write_byte(BMP581_OSR, this->osr_config_.reg); +} + +bool BMP581Component::write_power_mode_(OperationMode mode) { + // - updates the component's internal power mode + // - returns success or failure of write to Output Data Rate register + + this->odr_config_.bit.pwr_mode = mode; + + // write odr register + return this->write_byte(BMP581_ODR, this->odr_config_.reg); +} + +} // namespace bmp581 +} // namespace esphome diff --git a/esphome/components/bmp581/bmp581.h b/esphome/components/bmp581/bmp581.h new file mode 100644 index 0000000000..7327be44ae --- /dev/null +++ b/esphome/components/bmp581/bmp581.h @@ -0,0 +1,222 @@ +// All datasheet page references refer to Bosch Document Number BST-BMP581-DS004-04 (revision number 1.4) + +#pragma once + +#include "esphome/core/component.h" +#include "esphome/components/i2c/i2c.h" +#include "esphome/components/sensor/sensor.h" + +namespace esphome { +namespace bmp581 { + +static const uint8_t BMP581_ASIC_ID = 0x50; // BMP581's ASIC chip ID (page 51 of datasheet) +static const uint8_t RESET_COMMAND = 0xB6; // Soft reset command + +// BMP581 Register Addresses +enum { + BMP581_CHIP_ID = 0x01, // read chip ID + BMP581_INT_SOURCE = 0x15, // write interrupt sources + BMP581_MEASUREMENT_DATA = + 0x1D, // read measurement registers, 0x1D-0x1F are temperature XLSB to MSB and 0x20-0x22 are pressure XLSB to MSB + BMP581_INT_STATUS = 0x27, // read interrupt statuses + BMP581_STATUS = 0x28, // read sensor status + BMP581_DSP = 0x30, // write sensor configuration + BMP581_DSP_IIR = 0x31, // write IIR filter configuration + BMP581_OSR = 0x36, // write oversampling configuration + BMP581_ODR = 0x37, // write data rate and power mode configuration + BMP581_COMMAND = 0x7E // write sensor command +}; + +// BMP581 Power mode operations +enum OperationMode { + STANDBY_MODE = 0x0, // no active readings + NORMAL_MODE = 0x1, // read continuously at ODR configured rate and standby between + FORCED_MODE = 0x2, // read sensor once (only reading mode used by this component) + NONSTOP_MODE = 0x3 // read continuously with no standby +}; + +// Temperature and pressure sensors can be oversampled to reduce noise +enum Oversampling { + OVERSAMPLING_NONE = 0x0, + OVERSAMPLING_X2 = 0x1, + OVERSAMPLING_X4 = 0x2, + OVERSAMPLING_X8 = 0x3, + OVERSAMPLING_X16 = 0x4, + OVERSAMPLING_X32 = 0x5, + OVERSAMPLING_X64 = 0x6, + OVERSAMPLING_X128 = 0x7 +}; + +// Infinite Impulse Response filter reduces noise caused by ambient disturbances +enum IIRFilter { + IIR_FILTER_OFF = 0x0, + IIR_FILTER_2 = 0x1, + IIR_FILTER_4 = 0x2, + IIR_FILTER_8 = 0x3, + IIR_FILTER_16 = 0x4, + IIR_FILTER_32 = 0x5, + IIR_FILTER_64 = 0x6, + IIR_FILTER_128 = 0x7 +}; + +class BMP581Component : public PollingComponent, public i2c::I2CDevice { + public: + float get_setup_priority() const override { return setup_priority::DATA; } + + void dump_config() override; + + void setup() override; + void update() override; + + void set_temperature_sensor(sensor::Sensor *temperature_sensor) { this->temperature_sensor_ = temperature_sensor; } + void set_pressure_sensor(sensor::Sensor *pressure_sensor) { this->pressure_sensor_ = pressure_sensor; } + + void set_temperature_oversampling_config(Oversampling temperature_oversampling) { + this->temperature_oversampling_ = temperature_oversampling; + } + void set_pressure_oversampling_config(Oversampling pressure_oversampling) { + this->pressure_oversampling_ = pressure_oversampling; + } + + void set_temperature_iir_filter_config(IIRFilter iir_temperature_level) { + this->iir_temperature_level_ = iir_temperature_level; + } + void set_pressure_iir_filter_config(IIRFilter iir_pressure_level) { this->iir_pressure_level_ = iir_pressure_level; } + + void set_conversion_time(uint8_t conversion_time) { this->conversion_time_ = conversion_time; } + + protected: + sensor::Sensor *temperature_sensor_{nullptr}; + sensor::Sensor *pressure_sensor_{nullptr}; + + Oversampling temperature_oversampling_; + Oversampling pressure_oversampling_; + + IIRFilter iir_temperature_level_; + IIRFilter iir_pressure_level_; + + // Stores the sensors conversion time needed for a measurement based on oversampling settings and datasheet (page 12) + // Computed in Python during codegen + uint8_t conversion_time_; + + // Checks if the BMP581 has measurement data ready by checking the sensor's interrupts + bool check_data_readiness_(); + + // Flushes the IIR filter and primes an initial reading + bool prime_iir_filter_(); + + // Reads temperature data from sensor and converts data to measurement in degrees Celsius + bool read_temperature_(float &temperature); + // Reads temperature and pressure data from sensor and converts data to measurements in degrees Celsius and Pa + bool read_temperature_and_pressure_(float &temperature, float &pressure); + + // Soft resets the BMP581 + bool reset_(); + + // Initiates a measurement on sensor by switching to FORCED_MODE + bool start_measurement_(); + + // Writes the IIR filter configuration to the DSP and DSP_IIR registers + bool write_iir_settings_(IIRFilter temperature_iir, IIRFilter pressure_iir); + + // Writes whether to enable the data ready interrupt to the interrupt source register + bool write_interrupt_source_settings_(bool data_ready_enable); + + // Writes the oversampling settings to the OSR register + bool write_oversampling_settings_(Oversampling temperature_oversampling, Oversampling pressure_oversampling); + + // Sets the power mode on the BMP581 by writing to the ODR register + bool write_power_mode_(OperationMode mode); + + enum ErrorCode { + NONE = 0, + ERROR_COMMUNICATION_FAILED, + ERROR_WRONG_CHIP_ID, + ERROR_SENSOR_STATUS, + ERROR_SENSOR_RESET, + ERROR_PRIME_IIR_FAILED + } error_code_{NONE}; + + // BMP581's interrupt source register (address 0x15) to configure which interrupts are enabled (page 54 of datasheet) + union { + struct { + uint8_t drdy_data_reg_en : 1; // Data ready interrupt enable + uint8_t fifo_full_en : 1; // FIFO full interrupt enable + uint8_t fifo_ths_en : 1; // FIFO threshold/watermark interrupt enable + uint8_t oor_p_en : 1; // Pressure data out-of-range interrupt enable + } bit; + uint8_t reg; + } int_source_ = {.reg = 0}; + + // BMP581's interrupt status register (address 0x27) to determine ensor's current state (page 58 of datasheet) + union { + struct { + uint8_t drdy_data_reg : 1; // Data ready + uint8_t fifo_full : 1; // FIFO full + uint8_t fifo_ths : 1; // FIFO fhreshold/watermark + uint8_t oor_p : 1; // Pressure data out-of-range + uint8_t por : 1; // Power-On-Reset complete + } bit; + uint8_t reg; + } int_status_ = {.reg = 0}; + + // BMP581's status register (address 0x28) to determine if sensor has setup correctly (page 58 of datasheet) + union { + struct { + uint8_t status_core_rdy : 1; + uint8_t status_nvm_rdy : 1; // asserted if NVM is ready of operations + uint8_t status_nvm_err : 1; // asserted if NVM error + uint8_t status_nvm_cmd_err : 1; // asserted if boot command error + uint8_t status_boot_err_corrected : 1; // asserted if a boot error has been corrected + uint8_t : 2; + uint8_t st_crack_pass : 1; // asserted if crack check has executed without detecting a crack + } bit; + uint8_t reg; + } status_ = {.reg = 0}; + + // BMP581's dsp register (address 0x30) to configure data registers iir selection (page 61 of datasheet) + union { + struct { + uint8_t comp_pt_en : 2; // enable temperature and pressure compensation + uint8_t iir_flush_forced_en : 1; // IIR filter is flushed in forced mode + uint8_t shdw_sel_iir_t : 1; // temperature data register value selected before or after iir + uint8_t fifo_sel_iir_t : 1; // FIFO temperature data register value secected before or after iir + uint8_t shdw_sel_iir_p : 1; // pressure data register value selected before or after iir + uint8_t fifo_sel_iir_p : 1; // FIFO pressure data register value selected before or after iir + uint8_t oor_sel_iir_p : 1; // pressure out-of-range value selected before or after iir + } bit; + uint8_t reg; + } dsp_config_ = {.reg = 0}; + + // BMP581's iir register (address 0x31) to configure iir filtering(page 62 of datasheet) + union { + struct { + uint8_t set_iir_t : 3; // Temperature IIR filter coefficient + uint8_t set_iir_p : 3; // Pressure IIR filter coefficient + } bit; + uint8_t reg; + } iir_config_ = {.reg = 0}; + + // BMP581's OSR register (address 0x36) to configure Over-Sampling Rates (page 64 of datasheet) + union { + struct { + uint8_t osr_t : 3; // Temperature oversampling + uint8_t osr_p : 3; // Pressure oversampling + uint8_t press_en : 1; // Enables pressure measurement + } bit; + uint8_t reg; + } osr_config_ = {.reg = 0}; + + // BMP581's odr register (address 0x37) to configure output data rate and power mode (page 64 of datasheet) + union { + struct { + uint8_t pwr_mode : 2; // power mode of sensor + uint8_t odr : 5; // output data rate + uint8_t deep_dis : 1; // deep standby disabled if asserted + } bit; + uint8_t reg; + } odr_config_ = {.reg = 0}; +}; + +} // namespace bmp581 +} // namespace esphome diff --git a/esphome/components/bmp581/sensor.py b/esphome/components/bmp581/sensor.py new file mode 100644 index 0000000000..1e0350075a --- /dev/null +++ b/esphome/components/bmp581/sensor.py @@ -0,0 +1,163 @@ +import math +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import i2c, sensor +from esphome.const import ( + CONF_ID, + CONF_IIR_FILTER, + CONF_OVERSAMPLING, + CONF_PRESSURE, + CONF_TEMPERATURE, + DEVICE_CLASS_ATMOSPHERIC_PRESSURE, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, + UNIT_CELSIUS, + UNIT_PASCAL, +) + +CODEOWNERS = ["@kahrendt"] +DEPENDENCIES = ["i2c"] + +bmp581_ns = cg.esphome_ns.namespace("bmp581") + +Oversampling = bmp581_ns.enum("Oversampling") +OVERSAMPLING_OPTIONS = { + "NONE": Oversampling.OVERSAMPLING_NONE, + "2X": Oversampling.OVERSAMPLING_X2, + "4X": Oversampling.OVERSAMPLING_X4, + "8X": Oversampling.OVERSAMPLING_X8, + "16X": Oversampling.OVERSAMPLING_X16, + "32X": Oversampling.OVERSAMPLING_X32, + "64X": Oversampling.OVERSAMPLING_X64, + "128X": Oversampling.OVERSAMPLING_X128, +} + +IIRFilter = bmp581_ns.enum("IIRFilter") +IIR_FILTER_OPTIONS = { + "OFF": IIRFilter.IIR_FILTER_OFF, + "2X": IIRFilter.IIR_FILTER_2, + "4X": IIRFilter.IIR_FILTER_4, + "8X": IIRFilter.IIR_FILTER_8, + "16X": IIRFilter.IIR_FILTER_16, + "32X": IIRFilter.IIR_FILTER_32, + "64X": IIRFilter.IIR_FILTER_64, + "128X": IIRFilter.IIR_FILTER_128, +} + +BMP581Component = bmp581_ns.class_( + "BMP581Component", cg.PollingComponent, i2c.I2CDevice +) + + +def compute_measurement_conversion_time(config): + # - adds up sensor conversion time based on temperature and pressure oversampling rates given in datasheet + # - returns a rounded up time in ms + + # Page 12 of datasheet + PRESSURE_OVERSAMPLING_CONVERSION_TIMES = { + "NONE": 1.0, + "2X": 1.7, + "4X": 2.9, + "8X": 5.4, + "16X": 10.4, + "32X": 20.4, + "64X": 40.4, + "128X": 80.4, + } + + # Page 12 of datasheet + TEMPERATURE_OVERSAMPLING_CONVERSION_TIMES = { + "NONE": 1.0, + "2X": 1.1, + "4X": 1.5, + "8X": 2.1, + "16X": 3.3, + "32X": 5.8, + "64X": 10.8, + "128X": 20.8, + } + + pressure_conversion_time = ( + 0.0 # No conversion time necessary without a pressure sensor + ) + if pressure_config := config.get(CONF_PRESSURE): + pressure_conversion_time = PRESSURE_OVERSAMPLING_CONVERSION_TIMES[ + pressure_config.get(CONF_OVERSAMPLING) + ] + + temperature_conversion_time = ( + 1.0 # BMP581 always samples the temperature even if only reading pressure + ) + if temperature_config := config.get(CONF_TEMPERATURE): + temperature_conversion_time = TEMPERATURE_OVERSAMPLING_CONVERSION_TIMES[ + temperature_config.get(CONF_OVERSAMPLING) + ] + + # Datasheet indicates a 5% possible error in each conversion time listed + return math.ceil(1.05 * (pressure_conversion_time + temperature_conversion_time)) + + +CONFIG_SCHEMA = ( + cv.Schema( + { + cv.GenerateID(): cv.declare_id(BMP581Component), + cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( + unit_of_measurement=UNIT_CELSIUS, + accuracy_decimals=1, + device_class=DEVICE_CLASS_TEMPERATURE, + state_class=STATE_CLASS_MEASUREMENT, + ).extend( + { + cv.Optional(CONF_OVERSAMPLING, default="NONE"): cv.enum( + OVERSAMPLING_OPTIONS, upper=True + ), + cv.Optional(CONF_IIR_FILTER, default="OFF"): cv.enum( + IIR_FILTER_OPTIONS, upper=True + ), + } + ), + cv.Optional(CONF_PRESSURE): sensor.sensor_schema( + unit_of_measurement=UNIT_PASCAL, + accuracy_decimals=0, + device_class=DEVICE_CLASS_ATMOSPHERIC_PRESSURE, + state_class=STATE_CLASS_MEASUREMENT, + ).extend( + { + cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( + OVERSAMPLING_OPTIONS, upper=True + ), + cv.Optional(CONF_IIR_FILTER, default="OFF"): cv.enum( + IIR_FILTER_OPTIONS, upper=True + ), + } + ), + } + ) + .extend(cv.polling_component_schema("60s")) + .extend(i2c.i2c_device_schema(0x46)) +) + + +async def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + await cg.register_component(var, config) + await i2c.register_i2c_device(var, config) + if temperature_config := config.get(CONF_TEMPERATURE): + sens = await sensor.new_sensor(temperature_config) + cg.add(var.set_temperature_sensor(sens)) + cg.add( + var.set_temperature_oversampling_config( + temperature_config[CONF_OVERSAMPLING] + ) + ) + cg.add( + var.set_temperature_iir_filter_config(temperature_config[CONF_IIR_FILTER]) + ) + + if pressure_config := config.get(CONF_PRESSURE): + sens = await sensor.new_sensor(pressure_config) + cg.add(var.set_pressure_sensor(sens)) + cg.add(var.set_pressure_oversampling_config(pressure_config[CONF_OVERSAMPLING])) + cg.add(var.set_pressure_iir_filter_config(pressure_config[CONF_IIR_FILTER])) + + cg.add(var.set_conversion_time(compute_measurement_conversion_time(config))) diff --git a/tests/test1.yaml b/tests/test1.yaml index 2419634570..254abe2b01 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -1355,6 +1355,14 @@ sensor: name: "Distance" update_interval: 60s i2c_id: i2c_bus + - platform: bmp581 + i2c_id: i2c_bus + temperature: + name: "BMP581 Temperature" + iir_filter: 2x + pressure: + name: "BMP581 Pressure" + oversampling: 128x esp32_touch: setup_mode: false From 0ed0bdc65515dc685f70474fb781de3cc39acda8 Mon Sep 17 00:00:00 2001 From: Greg Cormier Date: Thu, 10 Aug 2023 01:04:22 -0400 Subject: [PATCH 78/84] New PM sensor Panasonic SN-GCJA5 (#4988) --- CODEOWNERS | 1 + esphome/components/gcja5/__init__.py | 1 + esphome/components/gcja5/gcja5.cpp | 119 +++++++++++++++++++++++++++ esphome/components/gcja5/gcja5.h | 52 ++++++++++++ esphome/components/gcja5/sensor.py | 118 ++++++++++++++++++++++++++ tests/test1.yaml | 22 +++++ 6 files changed, 313 insertions(+) create mode 100644 esphome/components/gcja5/__init__.py create mode 100644 esphome/components/gcja5/gcja5.cpp create mode 100644 esphome/components/gcja5/gcja5.h create mode 100644 esphome/components/gcja5/sensor.py diff --git a/CODEOWNERS b/CODEOWNERS index cda8a2f0f5..408caee4f2 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -102,6 +102,7 @@ esphome/components/fastled_base/* @OttoWinter esphome/components/feedback/* @ianchi esphome/components/fingerprint_grow/* @OnFreund @loongyh esphome/components/fs3000/* @kahrendt +esphome/components/gcja5/* @gcormier esphome/components/globals/* @esphome/core esphome/components/gp8403/* @jesserockz esphome/components/gpio/* @esphome/core diff --git a/esphome/components/gcja5/__init__.py b/esphome/components/gcja5/__init__.py new file mode 100644 index 0000000000..122ffaf6ed --- /dev/null +++ b/esphome/components/gcja5/__init__.py @@ -0,0 +1 @@ +CODEOWNERS = ["@gcormier"] diff --git a/esphome/components/gcja5/gcja5.cpp b/esphome/components/gcja5/gcja5.cpp new file mode 100644 index 0000000000..7f980ca0ad --- /dev/null +++ b/esphome/components/gcja5/gcja5.cpp @@ -0,0 +1,119 @@ +/* From snooping with a logic analyzer, the I2C on this sensor is broken. I was only able + * to receive 1's as a response from the sensor. I was able to get the UART working. + * + * The datasheet says the values should be divided by 1000, but this must only be for the I2C + * implementation. Comparing UART values with another sensor, there is no need to divide by 1000. + */ +#include "gcja5.h" +#include "esphome/core/log.h" +#include + +namespace esphome { +namespace gcja5 { + +static const char *const TAG = "gcja5"; + +void GCJA5Component::setup() { ESP_LOGCONFIG(TAG, "Setting up gcja5..."); } + +void GCJA5Component::loop() { + const uint32_t now = millis(); + if (now - this->last_transmission_ >= 500) { + // last transmission too long ago. Reset RX index. + this->rx_message_.clear(); + } + + if (this->available() == 0) { + return; + } + + // There must now be data waiting + this->last_transmission_ = now; + uint8_t val; + while (this->available() != 0) { + this->read_byte(&val); + this->rx_message_.push_back(val); + + // check if rx_message_ has 32 bytes of data + if (this->rx_message_.size() == 32) { + this->parse_data_(); + + if (this->have_good_data_) { + if (this->pm_1_0_sensor_ != nullptr) + this->pm_1_0_sensor_->publish_state(get_32_bit_uint_(1)); + if (this->pm_2_5_sensor_ != nullptr) + this->pm_2_5_sensor_->publish_state(get_32_bit_uint_(5)); + if (this->pm_10_0_sensor_ != nullptr) + this->pm_10_0_sensor_->publish_state(get_32_bit_uint_(9)); + if (this->pmc_0_3_sensor_ != nullptr) + this->pmc_0_3_sensor_->publish_state(get_16_bit_uint_(13)); + if (this->pmc_0_5_sensor_ != nullptr) + this->pmc_0_5_sensor_->publish_state(get_16_bit_uint_(15)); + if (this->pmc_1_0_sensor_ != nullptr) + this->pmc_1_0_sensor_->publish_state(get_16_bit_uint_(17)); + if (this->pmc_2_5_sensor_ != nullptr) + this->pmc_2_5_sensor_->publish_state(get_16_bit_uint_(21)); + if (this->pmc_5_0_sensor_ != nullptr) + this->pmc_5_0_sensor_->publish_state(get_16_bit_uint_(23)); + if (this->pmc_10_0_sensor_ != nullptr) + this->pmc_10_0_sensor_->publish_state(get_16_bit_uint_(25)); + } else { + this->status_set_warning(); + ESP_LOGV(TAG, "Have 32 bytes but not good data. Skipping."); + } + + this->rx_message_.clear(); + } + } +} + +bool GCJA5Component::calculate_checksum_() { + uint8_t crc = 0; + + for (uint8_t i = 1; i < 30; i++) + crc = crc ^ this->rx_message_[i]; + + ESP_LOGVV(TAG, "Checksum packet was (0x%02X), calculated checksum was (0x%02X)", this->rx_message_[30], crc); + + return (crc == this->rx_message_[30]); +} + +uint32_t GCJA5Component::get_32_bit_uint_(uint8_t start_index) { + return (((uint32_t) this->rx_message_[start_index + 3]) << 24) | + (((uint32_t) this->rx_message_[start_index + 2]) << 16) | + (((uint32_t) this->rx_message_[start_index + 1]) << 8) | ((uint32_t) this->rx_message_[start_index]); +} + +uint16_t GCJA5Component::get_16_bit_uint_(uint8_t start_index) { + return (((uint32_t) this->rx_message_[start_index + 1]) << 8) | ((uint32_t) this->rx_message_[start_index]); +} + +void GCJA5Component::parse_data_() { + ESP_LOGVV(TAG, "GCJA5 Data: "); + for (uint8_t i = 0; i < 32; i++) { + ESP_LOGVV(TAG, " %u: 0b" BYTE_TO_BINARY_PATTERN " (0x%02X)", i + 1, BYTE_TO_BINARY(this->rx_message_[i]), + this->rx_message_[i]); + } + + if (this->rx_message_[0] != 0x02 || this->rx_message_[31] != 0x03 || !this->calculate_checksum_()) { + ESP_LOGVV(TAG, "Discarding bad packet - failed checks."); + return; + } else + ESP_LOGVV(TAG, "Good packet found."); + + this->have_good_data_ = true; + uint8_t status = this->rx_message_[29]; + if (!this->first_status_log_) { + this->first_status_log_ = true; + + ESP_LOGI(TAG, "GCJA5 Status"); + ESP_LOGI(TAG, "Overall Status : %i", (status >> 6) & 0x03); + ESP_LOGI(TAG, "PD Status : %i", (status >> 4) & 0x03); + ESP_LOGI(TAG, "LD Status : %i", (status >> 2) & 0x03); + ESP_LOGI(TAG, "Fan Status : %i", (status >> 0) & 0x03); + } +} + +void GCJA5Component::dump_config() { ; } + +} // namespace gcja5 +} // namespace esphome diff --git a/esphome/components/gcja5/gcja5.h b/esphome/components/gcja5/gcja5.h new file mode 100644 index 0000000000..7593c90323 --- /dev/null +++ b/esphome/components/gcja5/gcja5.h @@ -0,0 +1,52 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/components/sensor/sensor.h" +#include "esphome/components/uart/uart.h" + +namespace esphome { +namespace gcja5 { + +class GCJA5Component : public Component, public uart::UARTDevice { + public: + void setup() override; + void dump_config() override; + void loop() override; + float get_setup_priority() const override { return setup_priority::DATA; } + + void set_pm_1_0_sensor(sensor::Sensor *pm_1_0) { pm_1_0_sensor_ = pm_1_0; } + void set_pm_2_5_sensor(sensor::Sensor *pm_2_5) { pm_2_5_sensor_ = pm_2_5; } + void set_pm_10_0_sensor(sensor::Sensor *pm_10_0) { pm_10_0_sensor_ = pm_10_0; } + + void set_pmc_0_3_sensor(sensor::Sensor *pmc_0_3) { pmc_0_3_sensor_ = pmc_0_3; } + void set_pmc_0_5_sensor(sensor::Sensor *pmc_0_5) { pmc_0_5_sensor_ = pmc_0_5; } + void set_pmc_1_0_sensor(sensor::Sensor *pmc_1_0) { pmc_1_0_sensor_ = pmc_1_0; } + void set_pmc_2_5_sensor(sensor::Sensor *pmc_2_5) { pmc_2_5_sensor_ = pmc_2_5; } + void set_pmc_5_0_sensor(sensor::Sensor *pmc_5_0) { pmc_5_0_sensor_ = pmc_5_0; } + void set_pmc_10_0_sensor(sensor::Sensor *pmc_10_0) { pmc_10_0_sensor_ = pmc_10_0; } + + protected: + void parse_data_(); + bool calculate_checksum_(); + + uint32_t get_32_bit_uint_(uint8_t start_index); + uint16_t get_16_bit_uint_(uint8_t start_index); + uint32_t last_transmission_{0}; + std::vector rx_message_; + + bool have_good_data_{false}; + bool first_status_log_{false}; + sensor::Sensor *pm_1_0_sensor_{nullptr}; + sensor::Sensor *pm_2_5_sensor_{nullptr}; + sensor::Sensor *pm_10_0_sensor_{nullptr}; + + sensor::Sensor *pmc_0_3_sensor_{nullptr}; + sensor::Sensor *pmc_0_5_sensor_{nullptr}; + sensor::Sensor *pmc_1_0_sensor_{nullptr}; + sensor::Sensor *pmc_2_5_sensor_{nullptr}; + sensor::Sensor *pmc_5_0_sensor_{nullptr}; + sensor::Sensor *pmc_10_0_sensor_{nullptr}; +}; + +} // namespace gcja5 +} // namespace esphome diff --git a/esphome/components/gcja5/sensor.py b/esphome/components/gcja5/sensor.py new file mode 100644 index 0000000000..5bcdc572ff --- /dev/null +++ b/esphome/components/gcja5/sensor.py @@ -0,0 +1,118 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import uart, sensor +from esphome.const import ( + CONF_ID, + CONF_PM_1_0, + CONF_PM_2_5, + CONF_PM_10_0, + CONF_PMC_0_5, + CONF_PMC_1_0, + CONF_PMC_2_5, + CONF_PMC_10_0, + UNIT_MICROGRAMS_PER_CUBIC_METER, + ICON_CHEMICAL_WEAPON, + ICON_COUNTER, + DEVICE_CLASS_PM1, + DEVICE_CLASS_PM10, + DEVICE_CLASS_PM25, + STATE_CLASS_MEASUREMENT, +) + +CODEOWNERS = ["@gcormier"] +DEPENDENCIES = ["uart"] + +gcja5_ns = cg.esphome_ns.namespace("gcja5") + +GCJA5Component = gcja5_ns.class_("GCJA5Component", cg.PollingComponent, uart.UARTDevice) + +CONF_PMC_0_3 = "pmc_0_3" +CONF_PMC_5_0 = "pmc_5_0" + +CONFIG_SCHEMA = cv.Schema( + { + cv.GenerateID(): cv.declare_id(GCJA5Component), + cv.Optional(CONF_PM_1_0): sensor.sensor_schema( + unit_of_measurement=UNIT_MICROGRAMS_PER_CUBIC_METER, + icon=ICON_CHEMICAL_WEAPON, + accuracy_decimals=2, + device_class=DEVICE_CLASS_PM1, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional(CONF_PM_2_5): sensor.sensor_schema( + unit_of_measurement=UNIT_MICROGRAMS_PER_CUBIC_METER, + icon=ICON_CHEMICAL_WEAPON, + accuracy_decimals=2, + device_class=DEVICE_CLASS_PM25, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional(CONF_PM_10_0): sensor.sensor_schema( + unit_of_measurement=UNIT_MICROGRAMS_PER_CUBIC_METER, + icon=ICON_CHEMICAL_WEAPON, + accuracy_decimals=2, + device_class=DEVICE_CLASS_PM10, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional(CONF_PMC_0_3): sensor.sensor_schema( + unit_of_measurement=UNIT_MICROGRAMS_PER_CUBIC_METER, + icon=ICON_COUNTER, + accuracy_decimals=0, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional(CONF_PMC_0_5): sensor.sensor_schema( + unit_of_measurement=UNIT_MICROGRAMS_PER_CUBIC_METER, + icon=ICON_COUNTER, + accuracy_decimals=0, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional(CONF_PMC_1_0): sensor.sensor_schema( + unit_of_measurement=UNIT_MICROGRAMS_PER_CUBIC_METER, + icon=ICON_COUNTER, + accuracy_decimals=0, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional(CONF_PMC_2_5): sensor.sensor_schema( + unit_of_measurement=UNIT_MICROGRAMS_PER_CUBIC_METER, + icon=ICON_COUNTER, + accuracy_decimals=0, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional(CONF_PMC_5_0): sensor.sensor_schema( + unit_of_measurement=UNIT_MICROGRAMS_PER_CUBIC_METER, + icon=ICON_COUNTER, + accuracy_decimals=0, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional(CONF_PMC_10_0): sensor.sensor_schema( + unit_of_measurement=UNIT_MICROGRAMS_PER_CUBIC_METER, + icon=ICON_COUNTER, + accuracy_decimals=0, + state_class=STATE_CLASS_MEASUREMENT, + ), + } +).extend(uart.UART_DEVICE_SCHEMA) +FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema( + "gcja5", baud_rate=9600, require_rx=True, parity="EVEN" +) +TYPES = { + CONF_PM_1_0: "set_pm_1_0_sensor", + CONF_PM_2_5: "set_pm_2_5_sensor", + CONF_PM_10_0: "set_pm_10_0_sensor", + CONF_PMC_0_3: "set_pmc_0_3_sensor", + CONF_PMC_0_5: "set_pmc_0_5_sensor", + CONF_PMC_1_0: "set_pmc_1_0_sensor", + CONF_PMC_2_5: "set_pmc_2_5_sensor", + CONF_PMC_5_0: "set_pmc_5_0_sensor", + CONF_PMC_10_0: "set_pmc_10_0_sensor", +} + + +async def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + await cg.register_component(var, config) + await uart.register_uart_device(var, config) + + for key, funcName in TYPES.items(): + if key in config: + sens = await sensor.new_sensor(config[key]) + cg.add(getattr(var, funcName)(sens)) diff --git a/tests/test1.yaml b/tests/test1.yaml index 254abe2b01..5c9b83a915 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -228,6 +228,10 @@ uart: baud_rate: 256000 parity: NONE stop_bits: 1 + - id: gcja5_uart + rx_pin: GPIO10 + parity: EVEN + baud_rate: 9600 ota: safe_mode: true @@ -341,6 +345,24 @@ mcp23s17: deviceaddress: 1 sensor: + - platform: gcja5 + pm_1_0: + name: "Particulate Matter <1.0µm Concentration" + pm_2_5: + name: "Particulate Matter <2.5µm Concentration" + pm_10_0: + name: "Particulate Matter <10.0µm Concentration" + pmc_0_5: + name: "PMC 0.5" + pmc_1_0: + name: "PMC 1.0" + pmc_2_5: + name: "PMC 2.5" + pmc_5_0: + name: "PMC 5.0" + pmc_10_0: + name: "PMC 10.0" + uart_id: gcja5_uart - platform: internal_temperature name: Internal Temperature - platform: ble_client From 5b0b9da0b961c054e15751d3b571d6ef0e7a4125 Mon Sep 17 00:00:00 2001 From: matthias882 <30553262+matthias882@users.noreply.github.com> Date: Thu, 10 Aug 2023 07:05:01 +0200 Subject: [PATCH 79/84] Daly BMS improvements (#3388) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Co-authored-by: Samuel Sieb --- esphome/components/daly_bms/__init__.py | 1 - esphome/components/daly_bms/binary_sensor.py | 5 +- esphome/components/daly_bms/daly_bms.cpp | 241 ++++++++++++------- esphome/components/daly_bms/daly_bms.h | 140 +++++------ esphome/components/daly_bms/sensor.py | 5 +- esphome/components/daly_bms/text_sensor.py | 5 +- 6 files changed, 217 insertions(+), 180 deletions(-) diff --git a/esphome/components/daly_bms/__init__.py b/esphome/components/daly_bms/__init__.py index ce0cf5216a..2cc2c512f3 100644 --- a/esphome/components/daly_bms/__init__.py +++ b/esphome/components/daly_bms/__init__.py @@ -5,7 +5,6 @@ from esphome.const import CONF_ID, CONF_ADDRESS CODEOWNERS = ["@s1lvi0"] DEPENDENCIES = ["uart"] -AUTO_LOAD = ["sensor", "text_sensor", "binary_sensor"] CONF_BMS_DALY_ID = "bms_daly_id" diff --git a/esphome/components/daly_bms/binary_sensor.py b/esphome/components/daly_bms/binary_sensor.py index 7b252b5e89..724f19315b 100644 --- a/esphome/components/daly_bms/binary_sensor.py +++ b/esphome/components/daly_bms/binary_sensor.py @@ -27,9 +27,8 @@ CONFIG_SCHEMA = cv.All( async def setup_conf(config, key, hub): - if key in config: - conf = config[key] - var = await binary_sensor.new_binary_sensor(conf) + if sensor_config := config.get(key): + var = await binary_sensor.new_binary_sensor(sensor_config) cg.add(getattr(hub, f"set_{key}_binary_sensor")(var)) diff --git a/esphome/components/daly_bms/daly_bms.cpp b/esphome/components/daly_bms/daly_bms.cpp index 3b41723327..8f6fc0fb57 100644 --- a/esphome/components/daly_bms/daly_bms.cpp +++ b/esphome/components/daly_bms/daly_bms.cpp @@ -1,6 +1,6 @@ #include "daly_bms.h" -#include "esphome/core/log.h" #include +#include "esphome/core/log.h" namespace esphome { namespace daly_bms { @@ -19,7 +19,7 @@ static const uint8_t DALY_REQUEST_STATUS = 0x94; static const uint8_t DALY_REQUEST_CELL_VOLTAGE = 0x95; static const uint8_t DALY_REQUEST_TEMPERATURE = 0x96; -void DalyBmsComponent::setup() {} +void DalyBmsComponent::setup() { this->next_request_ = 1; } void DalyBmsComponent::dump_config() { ESP_LOGCONFIG(TAG, "Daly BMS:"); @@ -27,20 +27,78 @@ void DalyBmsComponent::dump_config() { } void DalyBmsComponent::update() { - this->request_data_(DALY_REQUEST_BATTERY_LEVEL); - this->request_data_(DALY_REQUEST_MIN_MAX_VOLTAGE); - this->request_data_(DALY_REQUEST_MIN_MAX_TEMPERATURE); - this->request_data_(DALY_REQUEST_MOS); - this->request_data_(DALY_REQUEST_STATUS); - this->request_data_(DALY_REQUEST_CELL_VOLTAGE); - this->request_data_(DALY_REQUEST_TEMPERATURE); + this->trigger_next_ = true; + this->next_request_ = 0; +} - std::vector get_battery_level_data; - int available_data = this->available(); - if (available_data >= DALY_FRAME_SIZE) { - get_battery_level_data.resize(available_data); - this->read_array(get_battery_level_data.data(), available_data); - this->decode_data_(get_battery_level_data); +void DalyBmsComponent::loop() { + const uint32_t now = millis(); + if (this->receiving_ && (now - this->last_transmission_ >= 200)) { + // last transmission too long ago. Reset RX index. + ESP_LOGW(TAG, "Last transmission too long ago. Reset RX index."); + this->data_.clear(); + this->receiving_ = false; + } + if ((now - this->last_transmission_ >= 250) && !this->trigger_next_) { + // last transmittion longer than 0.25s ago -> trigger next request + this->last_transmission_ = now; + this->trigger_next_ = true; + } + if (available()) + this->last_transmission_ = now; + while (available()) { + uint8_t c; + read_byte(&c); + if (!this->receiving_) { + if (c != 0xa5) + continue; + this->receiving_ = true; + } + this->data_.push_back(c); + if (this->data_.size() == 4) + this->data_count_ = c; + if ((this->data_.size() > 4) and (data_.size() == this->data_count_ + 5)) { + this->decode_data_(this->data_); + this->data_.clear(); + this->receiving_ = false; + } + } + + if (this->trigger_next_) { + this->trigger_next_ = false; + switch (this->next_request_) { + case 0: + this->request_data_(DALY_REQUEST_BATTERY_LEVEL); + this->next_request_ = 1; + break; + case 1: + this->request_data_(DALY_REQUEST_MIN_MAX_VOLTAGE); + this->next_request_ = 2; + break; + case 2: + this->request_data_(DALY_REQUEST_MIN_MAX_TEMPERATURE); + this->next_request_ = 3; + break; + case 3: + this->request_data_(DALY_REQUEST_MOS); + this->next_request_ = 4; + break; + case 4: + this->request_data_(DALY_REQUEST_STATUS); + this->next_request_ = 5; + break; + case 5: + this->request_data_(DALY_REQUEST_CELL_VOLTAGE); + this->next_request_ = 6; + break; + case 6: + this->request_data_(DALY_REQUEST_TEMPERATURE); + this->next_request_ = 7; + break; + case 7: + default: + break; + } } } @@ -49,21 +107,23 @@ float DalyBmsComponent::get_setup_priority() const { return setup_priority::DATA void DalyBmsComponent::request_data_(uint8_t data_id) { uint8_t request_message[DALY_FRAME_SIZE]; - request_message[0] = 0xA5; // Start Flag - request_message[1] = addr_; // Communication Module Address - request_message[2] = data_id; // Data ID - request_message[3] = 0x08; // Data Length (Fixed) - request_message[4] = 0x00; // Empty Data - request_message[5] = 0x00; // | - request_message[6] = 0x00; // | - request_message[7] = 0x00; // | - request_message[8] = 0x00; // | - request_message[9] = 0x00; // | - request_message[10] = 0x00; // | - request_message[11] = 0x00; // Empty Data + request_message[0] = 0xA5; // Start Flag + request_message[1] = this->addr_; // Communication Module Address + request_message[2] = data_id; // Data ID + request_message[3] = 0x08; // Data Length (Fixed) + request_message[4] = 0x00; // Empty Data + request_message[5] = 0x00; // | + request_message[6] = 0x00; // | + request_message[7] = 0x00; // | + request_message[8] = 0x00; // | + request_message[9] = 0x00; // | + request_message[10] = 0x00; // | + request_message[11] = 0x00; // Empty Data + request_message[12] = (uint8_t) (request_message[0] + request_message[1] + request_message[2] + request_message[3]); // Checksum (Lower byte of the other bytes sum) + ESP_LOGV(TAG, "Request datapacket Nr %x", data_id); this->write_array(request_message, sizeof(request_message)); this->flush(); } @@ -82,6 +142,7 @@ void DalyBmsComponent::decode_data_(std::vector data) { if (checksum == it[12]) { switch (it[2]) { +#ifdef USE_SENSOR case DALY_REQUEST_BATTERY_LEVEL: if (this->voltage_sensor_) { this->voltage_sensor_->publish_state((float) encode_uint16(it[4], it[5]) / 10); @@ -95,36 +156,37 @@ void DalyBmsComponent::decode_data_(std::vector data) { break; case DALY_REQUEST_MIN_MAX_VOLTAGE: - if (this->max_cell_voltage_) { - this->max_cell_voltage_->publish_state((float) encode_uint16(it[4], it[5]) / 1000); + if (this->max_cell_voltage_sensor_) { + this->max_cell_voltage_sensor_->publish_state((float) encode_uint16(it[4], it[5]) / 1000); } - if (this->max_cell_voltage_number_) { - this->max_cell_voltage_number_->publish_state(it[6]); + if (this->max_cell_voltage_number_sensor_) { + this->max_cell_voltage_number_sensor_->publish_state(it[6]); } - if (this->min_cell_voltage_) { - this->min_cell_voltage_->publish_state((float) encode_uint16(it[7], it[8]) / 1000); + if (this->min_cell_voltage_sensor_) { + this->min_cell_voltage_sensor_->publish_state((float) encode_uint16(it[7], it[8]) / 1000); } - if (this->min_cell_voltage_number_) { - this->min_cell_voltage_number_->publish_state(it[9]); + if (this->min_cell_voltage_number_sensor_) { + this->min_cell_voltage_number_sensor_->publish_state(it[9]); } break; case DALY_REQUEST_MIN_MAX_TEMPERATURE: - if (this->max_temperature_) { - this->max_temperature_->publish_state(it[4] - DALY_TEMPERATURE_OFFSET); + if (this->max_temperature_sensor_) { + this->max_temperature_sensor_->publish_state(it[4] - DALY_TEMPERATURE_OFFSET); } - if (this->max_temperature_probe_number_) { - this->max_temperature_probe_number_->publish_state(it[5]); + if (this->max_temperature_probe_number_sensor_) { + this->max_temperature_probe_number_sensor_->publish_state(it[5]); } - if (this->min_temperature_) { - this->min_temperature_->publish_state(it[6] - DALY_TEMPERATURE_OFFSET); + if (this->min_temperature_sensor_) { + this->min_temperature_sensor_->publish_state(it[6] - DALY_TEMPERATURE_OFFSET); } - if (this->min_temperature_probe_number_) { - this->min_temperature_probe_number_->publish_state(it[7]); + if (this->min_temperature_probe_number_sensor_) { + this->min_temperature_probe_number_sensor_->publish_state(it[7]); } break; - +#endif case DALY_REQUEST_MOS: +#ifdef USE_TEXT_SENSOR if (this->status_text_sensor_ != nullptr) { switch (it[4]) { case 0: @@ -140,20 +202,27 @@ void DalyBmsComponent::decode_data_(std::vector data) { break; } } - if (this->charging_mos_enabled_) { - this->charging_mos_enabled_->publish_state(it[5]); +#endif +#ifdef USE_BINARY_SENSOR + if (this->charging_mos_enabled_binary_sensor_) { + this->charging_mos_enabled_binary_sensor_->publish_state(it[5]); } - if (this->discharging_mos_enabled_) { - this->discharging_mos_enabled_->publish_state(it[6]); + if (this->discharging_mos_enabled_binary_sensor_) { + this->discharging_mos_enabled_binary_sensor_->publish_state(it[6]); } - if (this->remaining_capacity_) { - this->remaining_capacity_->publish_state((float) encode_uint32(it[8], it[9], it[10], it[11]) / 1000); +#endif +#ifdef USE_SENSOR + if (this->remaining_capacity_sensor_) { + this->remaining_capacity_sensor_->publish_state((float) encode_uint32(it[8], it[9], it[10], it[11]) / + 1000); } +#endif break; +#ifdef USE_SENSOR case DALY_REQUEST_STATUS: - if (this->cells_number_) { - this->cells_number_->publish_state(it[4]); + if (this->cells_number_sensor_) { + this->cells_number_sensor_->publish_state(it[4]); } break; @@ -171,71 +240,73 @@ void DalyBmsComponent::decode_data_(std::vector data) { case DALY_REQUEST_CELL_VOLTAGE: switch (it[4]) { case 1: - if (this->cell_1_voltage_) { - this->cell_1_voltage_->publish_state((float) encode_uint16(it[5], it[6]) / 1000); + if (this->cell_1_voltage_sensor_) { + this->cell_1_voltage_sensor_->publish_state((float) encode_uint16(it[5], it[6]) / 1000); } - if (this->cell_2_voltage_) { - this->cell_2_voltage_->publish_state((float) encode_uint16(it[7], it[8]) / 1000); + if (this->cell_2_voltage_sensor_) { + this->cell_2_voltage_sensor_->publish_state((float) encode_uint16(it[7], it[8]) / 1000); } - if (this->cell_3_voltage_) { - this->cell_3_voltage_->publish_state((float) encode_uint16(it[9], it[10]) / 1000); + if (this->cell_3_voltage_sensor_) { + this->cell_3_voltage_sensor_->publish_state((float) encode_uint16(it[9], it[10]) / 1000); } break; case 2: - if (this->cell_4_voltage_) { - this->cell_4_voltage_->publish_state((float) encode_uint16(it[5], it[6]) / 1000); + if (this->cell_4_voltage_sensor_) { + this->cell_4_voltage_sensor_->publish_state((float) encode_uint16(it[5], it[6]) / 1000); } - if (this->cell_5_voltage_) { - this->cell_5_voltage_->publish_state((float) encode_uint16(it[7], it[8]) / 1000); + if (this->cell_5_voltage_sensor_) { + this->cell_5_voltage_sensor_->publish_state((float) encode_uint16(it[7], it[8]) / 1000); } - if (this->cell_6_voltage_) { - this->cell_6_voltage_->publish_state((float) encode_uint16(it[9], it[10]) / 1000); + if (this->cell_6_voltage_sensor_) { + this->cell_6_voltage_sensor_->publish_state((float) encode_uint16(it[9], it[10]) / 1000); } break; case 3: - if (this->cell_7_voltage_) { - this->cell_7_voltage_->publish_state((float) encode_uint16(it[5], it[6]) / 1000); + if (this->cell_7_voltage_sensor_) { + this->cell_7_voltage_sensor_->publish_state((float) encode_uint16(it[5], it[6]) / 1000); } - if (this->cell_8_voltage_) { - this->cell_8_voltage_->publish_state((float) encode_uint16(it[7], it[8]) / 1000); + if (this->cell_8_voltage_sensor_) { + this->cell_8_voltage_sensor_->publish_state((float) encode_uint16(it[7], it[8]) / 1000); } - if (this->cell_9_voltage_) { - this->cell_9_voltage_->publish_state((float) encode_uint16(it[9], it[10]) / 1000); + if (this->cell_9_voltage_sensor_) { + this->cell_9_voltage_sensor_->publish_state((float) encode_uint16(it[9], it[10]) / 1000); } break; case 4: - if (this->cell_10_voltage_) { - this->cell_10_voltage_->publish_state((float) encode_uint16(it[5], it[6]) / 1000); + if (this->cell_10_voltage_sensor_) { + this->cell_10_voltage_sensor_->publish_state((float) encode_uint16(it[5], it[6]) / 1000); } - if (this->cell_11_voltage_) { - this->cell_11_voltage_->publish_state((float) encode_uint16(it[7], it[8]) / 1000); + if (this->cell_11_voltage_sensor_) { + this->cell_11_voltage_sensor_->publish_state((float) encode_uint16(it[7], it[8]) / 1000); } - if (this->cell_12_voltage_) { - this->cell_12_voltage_->publish_state((float) encode_uint16(it[9], it[10]) / 1000); + if (this->cell_12_voltage_sensor_) { + this->cell_12_voltage_sensor_->publish_state((float) encode_uint16(it[9], it[10]) / 1000); } break; case 5: - if (this->cell_13_voltage_) { - this->cell_13_voltage_->publish_state((float) encode_uint16(it[5], it[6]) / 1000); + if (this->cell_13_voltage_sensor_) { + this->cell_13_voltage_sensor_->publish_state((float) encode_uint16(it[5], it[6]) / 1000); } - if (this->cell_14_voltage_) { - this->cell_14_voltage_->publish_state((float) encode_uint16(it[7], it[8]) / 1000); + if (this->cell_14_voltage_sensor_) { + this->cell_14_voltage_sensor_->publish_state((float) encode_uint16(it[7], it[8]) / 1000); } - if (this->cell_15_voltage_) { - this->cell_15_voltage_->publish_state((float) encode_uint16(it[9], it[10]) / 1000); + if (this->cell_15_voltage_sensor_) { + this->cell_15_voltage_sensor_->publish_state((float) encode_uint16(it[9], it[10]) / 1000); } break; case 6: - if (this->cell_16_voltage_) { - this->cell_16_voltage_->publish_state((float) encode_uint16(it[5], it[6]) / 1000); + if (this->cell_16_voltage_sensor_) { + this->cell_16_voltage_sensor_->publish_state((float) encode_uint16(it[5], it[6]) / 1000); } break; } break; - +#endif default: break; } + } else { + ESP_LOGW(TAG, "Checksum-Error on Packet %x", it[4]); } std::advance(it, DALY_FRAME_SIZE); } else { diff --git a/esphome/components/daly_bms/daly_bms.h b/esphome/components/daly_bms/daly_bms.h index d4fe84fe46..52ea30ecde 100644 --- a/esphome/components/daly_bms/daly_bms.h +++ b/esphome/components/daly_bms/daly_bms.h @@ -1,9 +1,16 @@ #pragma once #include "esphome/core/component.h" +#include "esphome/core/defines.h" +#ifdef USE_SENSOR #include "esphome/components/sensor/sensor.h" +#endif +#ifdef USE_TEXT_SENSOR #include "esphome/components/text_sensor/text_sensor.h" +#endif +#ifdef USE_BINARY_SENSOR #include "esphome/components/binary_sensor/binary_sensor.h" +#endif #include "esphome/components/uart/uart.h" #include @@ -15,60 +22,53 @@ class DalyBmsComponent : public PollingComponent, public uart::UARTDevice { public: DalyBmsComponent() = default; - // SENSORS - void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; } - void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_sensor; } - void set_battery_level_sensor(sensor::Sensor *battery_level_sensor) { battery_level_sensor_ = battery_level_sensor; } - void set_max_cell_voltage_sensor(sensor::Sensor *max_cell_voltage) { max_cell_voltage_ = max_cell_voltage; } - void set_max_cell_voltage_number_sensor(sensor::Sensor *max_cell_voltage_number) { - max_cell_voltage_number_ = max_cell_voltage_number; - } - void set_min_cell_voltage_sensor(sensor::Sensor *min_cell_voltage) { min_cell_voltage_ = min_cell_voltage; } - void set_min_cell_voltage_number_sensor(sensor::Sensor *min_cell_voltage_number) { - min_cell_voltage_number_ = min_cell_voltage_number; - } - void set_max_temperature_sensor(sensor::Sensor *max_temperature) { max_temperature_ = max_temperature; } - void set_max_temperature_probe_number_sensor(sensor::Sensor *max_temperature_probe_number) { - max_temperature_probe_number_ = max_temperature_probe_number; - } - void set_min_temperature_sensor(sensor::Sensor *min_temperature) { min_temperature_ = min_temperature; } - void set_min_temperature_probe_number_sensor(sensor::Sensor *min_temperature_probe_number) { - min_temperature_probe_number_ = min_temperature_probe_number; - } - void set_remaining_capacity_sensor(sensor::Sensor *remaining_capacity) { remaining_capacity_ = remaining_capacity; } - void set_cells_number_sensor(sensor::Sensor *cells_number) { cells_number_ = cells_number; } - void set_temperature_1_sensor(sensor::Sensor *temperature_1_sensor) { temperature_1_sensor_ = temperature_1_sensor; } - void set_temperature_2_sensor(sensor::Sensor *temperature_2_sensor) { temperature_2_sensor_ = temperature_2_sensor; } - void set_cell_1_voltage_sensor(sensor::Sensor *cell_1_voltage) { cell_1_voltage_ = cell_1_voltage; } - void set_cell_2_voltage_sensor(sensor::Sensor *cell_2_voltage) { cell_2_voltage_ = cell_2_voltage; } - void set_cell_3_voltage_sensor(sensor::Sensor *cell_3_voltage) { cell_3_voltage_ = cell_3_voltage; } - void set_cell_4_voltage_sensor(sensor::Sensor *cell_4_voltage) { cell_4_voltage_ = cell_4_voltage; } - void set_cell_5_voltage_sensor(sensor::Sensor *cell_5_voltage) { cell_5_voltage_ = cell_5_voltage; } - void set_cell_6_voltage_sensor(sensor::Sensor *cell_6_voltage) { cell_6_voltage_ = cell_6_voltage; } - void set_cell_7_voltage_sensor(sensor::Sensor *cell_7_voltage) { cell_7_voltage_ = cell_7_voltage; } - void set_cell_8_voltage_sensor(sensor::Sensor *cell_8_voltage) { cell_8_voltage_ = cell_8_voltage; } - void set_cell_9_voltage_sensor(sensor::Sensor *cell_9_voltage) { cell_9_voltage_ = cell_9_voltage; } - void set_cell_10_voltage_sensor(sensor::Sensor *cell_10_voltage) { cell_10_voltage_ = cell_10_voltage; } - void set_cell_11_voltage_sensor(sensor::Sensor *cell_11_voltage) { cell_11_voltage_ = cell_11_voltage; } - void set_cell_12_voltage_sensor(sensor::Sensor *cell_12_voltage) { cell_12_voltage_ = cell_12_voltage; } - void set_cell_13_voltage_sensor(sensor::Sensor *cell_13_voltage) { cell_13_voltage_ = cell_13_voltage; } - void set_cell_14_voltage_sensor(sensor::Sensor *cell_14_voltage) { cell_14_voltage_ = cell_14_voltage; } - void set_cell_15_voltage_sensor(sensor::Sensor *cell_15_voltage) { cell_15_voltage_ = cell_15_voltage; } - void set_cell_16_voltage_sensor(sensor::Sensor *cell_16_voltage) { cell_16_voltage_ = cell_16_voltage; } +#ifdef USE_SENSOR + SUB_SENSOR(voltage) + SUB_SENSOR(current) + SUB_SENSOR(battery_level) + SUB_SENSOR(max_cell_voltage) + SUB_SENSOR(max_cell_voltage_number) + SUB_SENSOR(min_cell_voltage) + SUB_SENSOR(min_cell_voltage_number) + SUB_SENSOR(max_temperature) + SUB_SENSOR(max_temperature_probe_number) + SUB_SENSOR(min_temperature) + SUB_SENSOR(min_temperature_probe_number) + SUB_SENSOR(remaining_capacity) + SUB_SENSOR(cells_number) + SUB_SENSOR(temperature_1) + SUB_SENSOR(temperature_2) + SUB_SENSOR(cell_1_voltage) + SUB_SENSOR(cell_2_voltage) + SUB_SENSOR(cell_3_voltage) + SUB_SENSOR(cell_4_voltage) + SUB_SENSOR(cell_5_voltage) + SUB_SENSOR(cell_6_voltage) + SUB_SENSOR(cell_7_voltage) + SUB_SENSOR(cell_8_voltage) + SUB_SENSOR(cell_9_voltage) + SUB_SENSOR(cell_10_voltage) + SUB_SENSOR(cell_11_voltage) + SUB_SENSOR(cell_12_voltage) + SUB_SENSOR(cell_13_voltage) + SUB_SENSOR(cell_14_voltage) + SUB_SENSOR(cell_15_voltage) + SUB_SENSOR(cell_16_voltage) +#endif - // TEXT_SENSORS - void set_status_text_sensor(text_sensor::TextSensor *status_text_sensor) { status_text_sensor_ = status_text_sensor; } - // BINARY_SENSORS - void set_charging_mos_enabled_binary_sensor(binary_sensor::BinarySensor *charging_mos_enabled) { - charging_mos_enabled_ = charging_mos_enabled; - } - void set_discharging_mos_enabled_binary_sensor(binary_sensor::BinarySensor *discharging_mos_enabled) { - discharging_mos_enabled_ = discharging_mos_enabled; - } +#ifdef USE_TEXT_SENSOR + SUB_TEXT_SENSOR(status) +#endif + +#ifdef USE_BINARY_SENSOR + SUB_BINARY_SENSOR(charging_mos_enabled) + SUB_BINARY_SENSOR(discharging_mos_enabled) +#endif void setup() override; void dump_config() override; void update() override; + void loop() override; float get_setup_priority() const override; void set_address(uint8_t address) { this->addr_ = address; } @@ -79,42 +79,12 @@ class DalyBmsComponent : public PollingComponent, public uart::UARTDevice { uint8_t addr_; - sensor::Sensor *voltage_sensor_{nullptr}; - sensor::Sensor *current_sensor_{nullptr}; - sensor::Sensor *battery_level_sensor_{nullptr}; - sensor::Sensor *max_cell_voltage_{nullptr}; - sensor::Sensor *max_cell_voltage_number_{nullptr}; - sensor::Sensor *min_cell_voltage_{nullptr}; - sensor::Sensor *min_cell_voltage_number_{nullptr}; - sensor::Sensor *max_temperature_{nullptr}; - sensor::Sensor *max_temperature_probe_number_{nullptr}; - sensor::Sensor *min_temperature_{nullptr}; - sensor::Sensor *min_temperature_probe_number_{nullptr}; - sensor::Sensor *remaining_capacity_{nullptr}; - sensor::Sensor *cells_number_{nullptr}; - sensor::Sensor *temperature_1_sensor_{nullptr}; - sensor::Sensor *temperature_2_sensor_{nullptr}; - sensor::Sensor *cell_1_voltage_{nullptr}; - sensor::Sensor *cell_2_voltage_{nullptr}; - sensor::Sensor *cell_3_voltage_{nullptr}; - sensor::Sensor *cell_4_voltage_{nullptr}; - sensor::Sensor *cell_5_voltage_{nullptr}; - sensor::Sensor *cell_6_voltage_{nullptr}; - sensor::Sensor *cell_7_voltage_{nullptr}; - sensor::Sensor *cell_8_voltage_{nullptr}; - sensor::Sensor *cell_9_voltage_{nullptr}; - sensor::Sensor *cell_10_voltage_{nullptr}; - sensor::Sensor *cell_11_voltage_{nullptr}; - sensor::Sensor *cell_12_voltage_{nullptr}; - sensor::Sensor *cell_13_voltage_{nullptr}; - sensor::Sensor *cell_14_voltage_{nullptr}; - sensor::Sensor *cell_15_voltage_{nullptr}; - sensor::Sensor *cell_16_voltage_{nullptr}; - - text_sensor::TextSensor *status_text_sensor_{nullptr}; - - binary_sensor::BinarySensor *charging_mos_enabled_{nullptr}; - binary_sensor::BinarySensor *discharging_mos_enabled_{nullptr}; + std::vector data_; + bool receiving_{false}; + uint8_t data_count_; + uint32_t last_transmission_{0}; + bool trigger_next_; + uint8_t next_request_; }; } // namespace daly_bms diff --git a/esphome/components/daly_bms/sensor.py b/esphome/components/daly_bms/sensor.py index 2274a2153a..c447fbd8a2 100644 --- a/esphome/components/daly_bms/sensor.py +++ b/esphome/components/daly_bms/sensor.py @@ -218,9 +218,8 @@ CONFIG_SCHEMA = cv.All( async def setup_conf(config, key, hub): - if key in config: - conf = config[key] - sens = await sensor.new_sensor(conf) + if sensor_config := config.get(key): + sens = await sensor.new_sensor(sensor_config) cg.add(getattr(hub, f"set_{key}_sensor")(sens)) diff --git a/esphome/components/daly_bms/text_sensor.py b/esphome/components/daly_bms/text_sensor.py index 9f23e5f373..fcd5ee531b 100644 --- a/esphome/components/daly_bms/text_sensor.py +++ b/esphome/components/daly_bms/text_sensor.py @@ -23,9 +23,8 @@ CONFIG_SCHEMA = cv.All( async def setup_conf(config, key, hub): - if key in config: - conf = config[key] - sens = await text_sensor.new_text_sensor(conf) + if sensor_config := config.get(key): + sens = await text_sensor.new_text_sensor(sensor_config) cg.add(getattr(hub, f"set_{key}_text_sensor")(sens)) From f457269a68d1353ef3c822bd0d66dcbe591b8dcb Mon Sep 17 00:00:00 2001 From: Francesco Ciocchetti <2085772+primeroz@users.noreply.github.com> Date: Thu, 10 Aug 2023 07:09:21 +0200 Subject: [PATCH 80/84] Add missing `on_(arming|pending|armed_home|armed_night|armed_away|disarmed)` triggers to alarm_control_panel (#5219) --- .../alarm_control_panel/__init__.py | 72 +++++++++++++++++++ .../alarm_control_panel.cpp | 37 ++++++++++ .../alarm_control_panel/alarm_control_panel.h | 48 +++++++++++++ .../alarm_control_panel/automation.h | 42 +++++++++++ tests/test3.yaml | 39 ++++++++++ 5 files changed, 238 insertions(+) diff --git a/esphome/components/alarm_control_panel/__init__.py b/esphome/components/alarm_control_panel/__init__.py index 52f8ac7894..d9cafb4f30 100644 --- a/esphome/components/alarm_control_panel/__init__.py +++ b/esphome/components/alarm_control_panel/__init__.py @@ -16,6 +16,12 @@ IS_PLATFORM_COMPONENT = True CONF_ON_TRIGGERED = "on_triggered" CONF_ON_CLEARED = "on_cleared" +CONF_ON_ARMING = "on_arming" +CONF_ON_PENDING = "on_pending" +CONF_ON_ARMED_HOME = "on_armed_home" +CONF_ON_ARMED_NIGHT = "on_armed_night" +CONF_ON_ARMED_AWAY = "on_armed_away" +CONF_ON_DISARMED = "on_disarmed" alarm_control_panel_ns = cg.esphome_ns.namespace("alarm_control_panel") AlarmControlPanel = alarm_control_panel_ns.class_("AlarmControlPanel", cg.EntityBase) @@ -29,6 +35,24 @@ TriggeredTrigger = alarm_control_panel_ns.class_( ClearedTrigger = alarm_control_panel_ns.class_( "ClearedTrigger", automation.Trigger.template() ) +ArmingTrigger = alarm_control_panel_ns.class_( + "ArmingTrigger", automation.Trigger.template() +) +PendingTrigger = alarm_control_panel_ns.class_( + "PendingTrigger", automation.Trigger.template() +) +ArmedHomeTrigger = alarm_control_panel_ns.class_( + "ArmedHomeTrigger", automation.Trigger.template() +) +ArmedNightTrigger = alarm_control_panel_ns.class_( + "ArmedNightTrigger", automation.Trigger.template() +) +ArmedAwayTrigger = alarm_control_panel_ns.class_( + "ArmedAwayTrigger", automation.Trigger.template() +) +DisarmedTrigger = alarm_control_panel_ns.class_( + "DisarmedTrigger", automation.Trigger.template() +) ArmAwayAction = alarm_control_panel_ns.class_("ArmAwayAction", automation.Action) ArmHomeAction = alarm_control_panel_ns.class_("ArmHomeAction", automation.Action) ArmNightAction = alarm_control_panel_ns.class_("ArmNightAction", automation.Action) @@ -52,6 +76,36 @@ ALARM_CONTROL_PANEL_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend( cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(TriggeredTrigger), } ), + cv.Optional(CONF_ON_ARMING): automation.validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ArmingTrigger), + } + ), + cv.Optional(CONF_ON_PENDING): automation.validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(PendingTrigger), + } + ), + cv.Optional(CONF_ON_ARMED_HOME): automation.validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ArmedHomeTrigger), + } + ), + cv.Optional(CONF_ON_ARMED_NIGHT): automation.validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ArmedNightTrigger), + } + ), + cv.Optional(CONF_ON_ARMED_AWAY): automation.validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ArmedAwayTrigger), + } + ), + cv.Optional(CONF_ON_DISARMED): automation.validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(DisarmedTrigger), + } + ), cv.Optional(CONF_ON_CLEARED): automation.validate_automation( { cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ClearedTrigger), @@ -82,6 +136,24 @@ async def setup_alarm_control_panel_core_(var, config): for conf in config.get(CONF_ON_TRIGGERED, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) await automation.build_automation(trigger, [], conf) + for conf in config.get(CONF_ON_ARMING, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) + await automation.build_automation(trigger, [], conf) + for conf in config.get(CONF_ON_PENDING, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) + await automation.build_automation(trigger, [], conf) + for conf in config.get(CONF_ON_ARMED_HOME, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) + await automation.build_automation(trigger, [], conf) + for conf in config.get(CONF_ON_ARMED_NIGHT, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) + await automation.build_automation(trigger, [], conf) + for conf in config.get(CONF_ON_ARMED_AWAY, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) + await automation.build_automation(trigger, [], conf) + for conf in config.get(CONF_ON_DISARMED, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) + await automation.build_automation(trigger, [], conf) for conf in config.get(CONF_ON_CLEARED, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) await automation.build_automation(trigger, [], conf) diff --git a/esphome/components/alarm_control_panel/alarm_control_panel.cpp b/esphome/components/alarm_control_panel/alarm_control_panel.cpp index 74c9a502df..9dc083c004 100644 --- a/esphome/components/alarm_control_panel/alarm_control_panel.cpp +++ b/esphome/components/alarm_control_panel/alarm_control_panel.cpp @@ -36,7 +36,20 @@ void AlarmControlPanel::publish_state(AlarmControlPanelState state) { this->state_callback_.call(); if (state == ACP_STATE_TRIGGERED) { this->triggered_callback_.call(); + } else if (state == ACP_STATE_ARMING) { + this->arming_callback_.call(); + } else if (state == ACP_STATE_PENDING) { + this->pending_callback_.call(); + } else if (state == ACP_STATE_ARMED_HOME) { + this->armed_home_callback_.call(); + } else if (state == ACP_STATE_ARMED_NIGHT) { + this->armed_night_callback_.call(); + } else if (state == ACP_STATE_ARMED_AWAY) { + this->armed_away_callback_.call(); + } else if (state == ACP_STATE_DISARMED) { + this->disarmed_callback_.call(); } + if (prev_state == ACP_STATE_TRIGGERED) { this->cleared_callback_.call(); } @@ -55,6 +68,30 @@ void AlarmControlPanel::add_on_triggered_callback(std::function &&callba this->triggered_callback_.add(std::move(callback)); } +void AlarmControlPanel::add_on_arming_callback(std::function &&callback) { + this->arming_callback_.add(std::move(callback)); +} + +void AlarmControlPanel::add_on_armed_home_callback(std::function &&callback) { + this->armed_home_callback_.add(std::move(callback)); +} + +void AlarmControlPanel::add_on_armed_night_callback(std::function &&callback) { + this->armed_night_callback_.add(std::move(callback)); +} + +void AlarmControlPanel::add_on_armed_away_callback(std::function &&callback) { + this->armed_away_callback_.add(std::move(callback)); +} + +void AlarmControlPanel::add_on_pending_callback(std::function &&callback) { + this->pending_callback_.add(std::move(callback)); +} + +void AlarmControlPanel::add_on_disarmed_callback(std::function &&callback) { + this->disarmed_callback_.add(std::move(callback)); +} + void AlarmControlPanel::add_on_cleared_callback(std::function &&callback) { this->cleared_callback_.add(std::move(callback)); } diff --git a/esphome/components/alarm_control_panel/alarm_control_panel.h b/esphome/components/alarm_control_panel/alarm_control_panel.h index 4f15ccb45a..dc0b92df76 100644 --- a/esphome/components/alarm_control_panel/alarm_control_panel.h +++ b/esphome/components/alarm_control_panel/alarm_control_panel.h @@ -47,6 +47,42 @@ class AlarmControlPanel : public EntityBase { */ void add_on_triggered_callback(std::function &&callback); + /** Add a callback for when the state of the alarm_control_panel chanes to arming + * + * @param callback The callback function + */ + void add_on_arming_callback(std::function &&callback); + + /** Add a callback for when the state of the alarm_control_panel changes to pending + * + * @param callback The callback function + */ + void add_on_pending_callback(std::function &&callback); + + /** Add a callback for when the state of the alarm_control_panel changes to armed_home + * + * @param callback The callback function + */ + void add_on_armed_home_callback(std::function &&callback); + + /** Add a callback for when the state of the alarm_control_panel changes to armed_night + * + * @param callback The callback function + */ + void add_on_armed_night_callback(std::function &&callback); + + /** Add a callback for when the state of the alarm_control_panel changes to armed_away + * + * @param callback The callback function + */ + void add_on_armed_away_callback(std::function &&callback); + + /** Add a callback for when the state of the alarm_control_panel changes to disarmed + * + * @param callback The callback function + */ + void add_on_disarmed_callback(std::function &&callback); + /** Add a callback for when the state of the alarm_control_panel clears from triggered * * @param callback The callback function @@ -128,6 +164,18 @@ class AlarmControlPanel : public EntityBase { CallbackManager state_callback_{}; // trigger callback CallbackManager triggered_callback_{}; + // arming callback + CallbackManager arming_callback_{}; + // pending callback + CallbackManager pending_callback_{}; + // armed_home callback + CallbackManager armed_home_callback_{}; + // armed_night callback + CallbackManager armed_night_callback_{}; + // armed_away callback + CallbackManager armed_away_callback_{}; + // disarmed callback + CallbackManager disarmed_callback_{}; // clear callback CallbackManager cleared_callback_{}; }; diff --git a/esphome/components/alarm_control_panel/automation.h b/esphome/components/alarm_control_panel/automation.h index 81ac584f71..8538020c53 100644 --- a/esphome/components/alarm_control_panel/automation.h +++ b/esphome/components/alarm_control_panel/automation.h @@ -20,6 +20,48 @@ class TriggeredTrigger : public Trigger<> { } }; +class ArmingTrigger : public Trigger<> { + public: + explicit ArmingTrigger(AlarmControlPanel *alarm_control_panel) { + alarm_control_panel->add_on_arming_callback([this]() { this->trigger(); }); + } +}; + +class PendingTrigger : public Trigger<> { + public: + explicit PendingTrigger(AlarmControlPanel *alarm_control_panel) { + alarm_control_panel->add_on_pending_callback([this]() { this->trigger(); }); + } +}; + +class ArmedHomeTrigger : public Trigger<> { + public: + explicit ArmedHomeTrigger(AlarmControlPanel *alarm_control_panel) { + alarm_control_panel->add_on_armed_home_callback([this]() { this->trigger(); }); + } +}; + +class ArmedNightTrigger : public Trigger<> { + public: + explicit ArmedNightTrigger(AlarmControlPanel *alarm_control_panel) { + alarm_control_panel->add_on_armed_night_callback([this]() { this->trigger(); }); + } +}; + +class ArmedAwayTrigger : public Trigger<> { + public: + explicit ArmedAwayTrigger(AlarmControlPanel *alarm_control_panel) { + alarm_control_panel->add_on_armed_away_callback([this]() { this->trigger(); }); + } +}; + +class DisarmedTrigger : public Trigger<> { + public: + explicit DisarmedTrigger(AlarmControlPanel *alarm_control_panel) { + alarm_control_panel->add_on_disarmed_callback([this]() { this->trigger(); }); + } +}; + class ClearedTrigger : public Trigger<> { public: explicit ClearedTrigger(AlarmControlPanel *alarm_control_panel) { diff --git a/tests/test3.yaml b/tests/test3.yaml index 3ab1d561b3..5bda0afb1b 100644 --- a/tests/test3.yaml +++ b/tests/test3.yaml @@ -1206,3 +1206,42 @@ alarm_control_panel: then: - lambda: !lambda |- ESP_LOGD("TEST", "State change %s", alarm_control_panel_state_to_string(id(alarmcontrolpanel1)->get_state())); + - platform: template + id: alarmcontrolpanel2 + name: Alarm Panel + codes: + - "1234" + requires_code_to_arm: true + arming_home_time: 1s + arming_night_time: 1s + arming_away_time: 15s + pending_time: 15s + trigger_time: 30s + binary_sensors: + - input: bin1 + bypass_armed_home: true + bypass_armed_night: true + on_disarmed: + then: + - logger.log: "### DISARMED ###" + on_pending: + then: + - logger.log: "### PENDING ###" + on_arming: + then: + - logger.log: "### ARMING ###" + on_armed_home: + then: + - logger.log: "### ARMED HOME ###" + on_armed_night: + then: + - logger.log: "### ARMED NIGHT ###" + on_armed_away: + then: + - logger.log: "### ARMED AWAY ###" + on_triggered: + then: + - logger.log: "### TRIGGERED ###" + on_cleared: + then: + - logger.log: "### CLEARED ###" From b56c606523c1e34ade52ac0117aa42167ad02be4 Mon Sep 17 00:00:00 2001 From: Samuel Sieb Date: Wed, 9 Aug 2023 22:11:03 -0700 Subject: [PATCH 81/84] add value option to timeout filter (#5222) Co-authored-by: Samuel Sieb --- esphome/components/sensor/__init__.py | 14 +++++++++++--- esphome/components/sensor/filter.cpp | 4 ++-- esphome/components/sensor/filter.h | 4 +++- tests/test3.1.yaml | 3 +++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/esphome/components/sensor/__init__.py b/esphome/components/sensor/__init__.py index bbcc730943..8f7d581b2d 100644 --- a/esphome/components/sensor/__init__.py +++ b/esphome/components/sensor/__init__.py @@ -23,6 +23,7 @@ from esphome.const import ( CONF_SEND_EVERY, CONF_SEND_FIRST_AT, CONF_STATE_CLASS, + CONF_TIMEOUT, CONF_TO, CONF_TRIGGER_ID, CONF_TYPE, @@ -543,11 +544,18 @@ async def heartbeat_filter_to_code(config, filter_id): return var -@FILTER_REGISTRY.register( - "timeout", TimeoutFilter, cv.positive_time_period_milliseconds +TIMEOUT_SCHEMA = cv.maybe_simple_value( + { + cv.Required(CONF_TIMEOUT): cv.positive_time_period_milliseconds, + cv.Optional(CONF_VALUE, default="nan"): cv.float_, + }, + key=CONF_TIMEOUT, ) + + +@FILTER_REGISTRY.register("timeout", TimeoutFilter, TIMEOUT_SCHEMA) async def timeout_filter_to_code(config, filter_id): - var = cg.new_Pvariable(filter_id, config) + var = cg.new_Pvariable(filter_id, config[CONF_TIMEOUT], config[CONF_VALUE]) await cg.register_component(var, {}) return var diff --git a/esphome/components/sensor/filter.cpp b/esphome/components/sensor/filter.cpp index cd5ab5f9cd..6323023d50 100644 --- a/esphome/components/sensor/filter.cpp +++ b/esphome/components/sensor/filter.cpp @@ -375,13 +375,13 @@ void OrFilter::initialize(Sensor *parent, Filter *next) { // TimeoutFilter optional TimeoutFilter::new_value(float value) { - this->set_timeout("timeout", this->time_period_, [this]() { this->output(NAN); }); + this->set_timeout("timeout", this->time_period_, [this]() { this->output(this->value_); }); this->output(value); return {}; } -TimeoutFilter::TimeoutFilter(uint32_t time_period) : time_period_(time_period) {} +TimeoutFilter::TimeoutFilter(uint32_t time_period, float new_value) : time_period_(time_period), value_(new_value) {} float TimeoutFilter::get_setup_priority() const { return setup_priority::HARDWARE; } // DebounceFilter diff --git a/esphome/components/sensor/filter.h b/esphome/components/sensor/filter.h index 0141b73267..46aeefac56 100644 --- a/esphome/components/sensor/filter.h +++ b/esphome/components/sensor/filter.h @@ -315,7 +315,8 @@ class ThrottleFilter : public Filter { class TimeoutFilter : public Filter, public Component { public: - explicit TimeoutFilter(uint32_t time_period); + explicit TimeoutFilter(uint32_t time_period, float new_value); + void set_value(float new_value) { this->value_ = new_value; } optional new_value(float value) override; @@ -323,6 +324,7 @@ class TimeoutFilter : public Filter, public Component { protected: uint32_t time_period_; + float value_; }; class DebounceFilter : public Filter, public Component { diff --git a/tests/test3.1.yaml b/tests/test3.1.yaml index 42c1e1e1ab..46bc014204 100644 --- a/tests/test3.1.yaml +++ b/tests/test3.1.yaml @@ -87,6 +87,9 @@ sensor: - throttle: 100ms - debounce: 500s - timeout: 10min + - timeout: + timeout: 10min + value: 0 - calibrate_linear: method: exact datapoints: From 8e7e8da4a3dada0761b4b90e33f5535878cb599e Mon Sep 17 00:00:00 2001 From: Keith Burzinski Date: Thu, 10 Aug 2023 00:11:57 -0500 Subject: [PATCH 82/84] Tweak Color init because IDF 5+ (#5221) --- esphome/components/color/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/color/__init__.py b/esphome/components/color/__init__.py index 9a85eace75..4a55beef38 100644 --- a/esphome/components/color/__init__.py +++ b/esphome/components/color/__init__.py @@ -82,5 +82,5 @@ async def to_code(config): cg.new_variable( config[CONF_ID], - cg.StructInitializer(ColorStruct, ("r", r), ("g", g), ("b", b), ("w", w)), + cg.ArrayInitializer(r, g, b, w), ) From 02ed2c0ebeaa975e1d869b05f1b0fd669e02e5b7 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Thu, 10 Aug 2023 17:30:26 +1200 Subject: [PATCH 83/84] Bump version to 2023.8.0b1 --- esphome/const.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/const.py b/esphome/const.py index 4977726361..6b442dd633 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -1,6 +1,6 @@ """Constants used by esphome.""" -__version__ = "2023.8.0-dev" +__version__ = "2023.8.0b1" ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_" VALID_SUBSTITUTIONS_CHARACTERS = ( From 72e72d7d4b4556876817a0e4af5eef6536eb1c89 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Thu, 10 Aug 2023 18:40:13 +1200 Subject: [PATCH 84/84] Fix duplicate --- esphome/components/sensor/__init__.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/esphome/components/sensor/__init__.py b/esphome/components/sensor/__init__.py index c8cd2f9f7f..8f7d581b2d 100644 --- a/esphome/components/sensor/__init__.py +++ b/esphome/components/sensor/__init__.py @@ -560,15 +560,6 @@ async def timeout_filter_to_code(config, filter_id): return var -@FILTER_REGISTRY.register( - "timeout", TimeoutFilter, cv.positive_time_period_milliseconds -) -async def timeout_filter_to_code(config, filter_id): - var = cg.new_Pvariable(filter_id, config) - await cg.register_component(var, {}) - return var - - @FILTER_REGISTRY.register( "debounce", DebounceFilter, cv.positive_time_period_milliseconds )