From 3eff7e76aa42d440c2f89526bfdf47773dfbe0bb Mon Sep 17 00:00:00 2001 From: Jimmy Hedman Date: Thu, 27 Jul 2023 07:18:02 +0200 Subject: [PATCH 01/26] 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 02/26] 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 03/26] 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 04/26] 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 05/26] 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 06/26] 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 07/26] 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 08/26] 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 09/26] 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 10/26] 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 11/26] 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 12/26] 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 13/26] 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 14/26] 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 15/26] 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 16/26] 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 17/26] 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 18/26] 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 19/26] 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 20/26] 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 21/26] 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 22/26] 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 23/26] 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 24/26] 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 25/26] 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 26/26] 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