From bdfbac0301b633ad234fc8696c77033715aceffd Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 16 Oct 2025 21:20:00 -1000 Subject: [PATCH 1/7] [tests] Fix ESP32-C3 component test binary size by using larger partition table (#11319) --- .../build_components_base.esp32-c3-idf.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_build_components/build_components_base.esp32-c3-idf.yaml b/tests/test_build_components/build_components_base.esp32-c3-idf.yaml index 18584497f4..73a85467d3 100644 --- a/tests/test_build_components/build_components_base.esp32-c3-idf.yaml +++ b/tests/test_build_components/build_components_base.esp32-c3-idf.yaml @@ -6,6 +6,9 @@ esp32: board: lolin_c3_mini framework: type: esp-idf + # Use custom partition table with larger app partition (3MB) + # Default IDF partitions only allow 1.75MB which is too small for grouped tests + partitions: ../partitions_testing.csv logger: level: VERY_VERBOSE From 39e23c323d2144163e62d3290ee9c644341422bf Mon Sep 17 00:00:00 2001 From: esphomebot Date: Fri, 17 Oct 2025 20:49:10 +1300 Subject: [PATCH 2/7] Synchronise Device Classes from Home Assistant (#11285) --- esphome/components/number/__init__.py | 2 ++ esphome/components/sensor/__init__.py | 2 ++ esphome/const.py | 1 + 3 files changed, 5 insertions(+) diff --git a/esphome/components/number/__init__.py b/esphome/components/number/__init__.py index 76a7b05ea1..230c3aa0c1 100644 --- a/esphome/components/number/__init__.py +++ b/esphome/components/number/__init__.py @@ -66,6 +66,7 @@ from esphome.const import ( DEVICE_CLASS_SPEED, DEVICE_CLASS_SULPHUR_DIOXIDE, DEVICE_CLASS_TEMPERATURE, + DEVICE_CLASS_TEMPERATURE_DELTA, DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS, DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS_PARTS, DEVICE_CLASS_VOLTAGE, @@ -130,6 +131,7 @@ DEVICE_CLASSES = [ DEVICE_CLASS_SPEED, DEVICE_CLASS_SULPHUR_DIOXIDE, DEVICE_CLASS_TEMPERATURE, + DEVICE_CLASS_TEMPERATURE_DELTA, DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS, DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS_PARTS, DEVICE_CLASS_VOLTAGE, diff --git a/esphome/components/sensor/__init__.py b/esphome/components/sensor/__init__.py index 2b99f68ac0..bf13217787 100644 --- a/esphome/components/sensor/__init__.py +++ b/esphome/components/sensor/__init__.py @@ -89,6 +89,7 @@ from esphome.const import ( DEVICE_CLASS_SPEED, DEVICE_CLASS_SULPHUR_DIOXIDE, DEVICE_CLASS_TEMPERATURE, + DEVICE_CLASS_TEMPERATURE_DELTA, DEVICE_CLASS_TIMESTAMP, DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS, DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS_PARTS, @@ -157,6 +158,7 @@ DEVICE_CLASSES = [ DEVICE_CLASS_SPEED, DEVICE_CLASS_SULPHUR_DIOXIDE, DEVICE_CLASS_TEMPERATURE, + DEVICE_CLASS_TEMPERATURE_DELTA, DEVICE_CLASS_TIMESTAMP, DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS, DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS_PARTS, diff --git a/esphome/const.py b/esphome/const.py index f3f177b3ae..ce1c033e41 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -1300,6 +1300,7 @@ DEVICE_CLASS_SULPHUR_DIOXIDE = "sulphur_dioxide" DEVICE_CLASS_SWITCH = "switch" DEVICE_CLASS_TAMPER = "tamper" DEVICE_CLASS_TEMPERATURE = "temperature" +DEVICE_CLASS_TEMPERATURE_DELTA = "temperature_delta" DEVICE_CLASS_TIMESTAMP = "timestamp" DEVICE_CLASS_UPDATE = "update" DEVICE_CLASS_VIBRATION = "vibration" From 661e9f9991ba1460bba3c915973944a686393320 Mon Sep 17 00:00:00 2001 From: exotime Date: Fri, 17 Oct 2025 17:33:50 +0900 Subject: [PATCH 3/7] [toshiba] Add support for RAS-2819T air conditioner (#9490) Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: Keith Burzinski --- esphome/components/toshiba/climate.py | 1 + esphome/components/toshiba/toshiba.cpp | 653 +++++++++++++++++- esphome/components/toshiba/toshiba.h | 26 +- tests/components/toshiba/common_ras2819t.yaml | 13 + .../toshiba/test_ras2819t.esp32-ard.yaml | 5 + .../toshiba/test_ras2819t.esp32-c3-ard.yaml | 5 + .../toshiba/test_ras2819t.esp32-c3-idf.yaml | 5 + .../toshiba/test_ras2819t.esp32-idf.yaml | 5 + .../toshiba/test_ras2819t.esp8266-ard.yaml | 5 + 9 files changed, 689 insertions(+), 29 deletions(-) create mode 100644 tests/components/toshiba/common_ras2819t.yaml create mode 100644 tests/components/toshiba/test_ras2819t.esp32-ard.yaml create mode 100644 tests/components/toshiba/test_ras2819t.esp32-c3-ard.yaml create mode 100644 tests/components/toshiba/test_ras2819t.esp32-c3-idf.yaml create mode 100644 tests/components/toshiba/test_ras2819t.esp32-idf.yaml create mode 100644 tests/components/toshiba/test_ras2819t.esp8266-ard.yaml diff --git a/esphome/components/toshiba/climate.py b/esphome/components/toshiba/climate.py index b8e390dd66..bdb17923fa 100644 --- a/esphome/components/toshiba/climate.py +++ b/esphome/components/toshiba/climate.py @@ -14,6 +14,7 @@ MODELS = { "GENERIC": Model.MODEL_GENERIC, "RAC-PT1411HWRU-C": Model.MODEL_RAC_PT1411HWRU_C, "RAC-PT1411HWRU-F": Model.MODEL_RAC_PT1411HWRU_F, + "RAS-2819T": Model.MODEL_RAS_2819T, } CONFIG_SCHEMA = climate_ir.climate_ir_with_receiver_schema(ToshibaClimate).extend( diff --git a/esphome/components/toshiba/toshiba.cpp b/esphome/components/toshiba/toshiba.cpp index ff4241a81f..36e5a21ffa 100644 --- a/esphome/components/toshiba/toshiba.cpp +++ b/esphome/components/toshiba/toshiba.cpp @@ -1,4 +1,5 @@ #include "toshiba.h" +#include "esphome/components/remote_base/toshiba_ac_protocol.h" #include @@ -97,6 +98,282 @@ const std::vector RAC_PT1411HWRU_TEMPERATURE_F{0x10, 0x30, 0x00, 0x20, 0x22, 0x06, 0x26, 0x07, 0x05, 0x25, 0x04, 0x24, 0x0C, 0x2C, 0x0D, 0x2D, 0x09, 0x08, 0x28, 0x0A, 0x2A, 0x0B}; +// RAS-2819T protocol constants +const uint16_t RAS_2819T_HEADER1 = 0xC23D; +const uint8_t RAS_2819T_HEADER2 = 0xD5; +const uint8_t RAS_2819T_MESSAGE_LENGTH = 6; + +// RAS-2819T fan speed codes for rc_code_1 (bytes 2-3) +const uint16_t RAS_2819T_FAN_AUTO = 0xBF40; +const uint16_t RAS_2819T_FAN_QUIET = 0xFF00; +const uint16_t RAS_2819T_FAN_LOW = 0x9F60; +const uint16_t RAS_2819T_FAN_MEDIUM = 0x5FA0; +const uint16_t RAS_2819T_FAN_HIGH = 0x3FC0; + +// RAS-2819T fan speed codes for rc_code_2 (byte 1) +const uint8_t RAS_2819T_FAN2_AUTO = 0x66; +const uint8_t RAS_2819T_FAN2_QUIET = 0x01; +const uint8_t RAS_2819T_FAN2_LOW = 0x28; +const uint8_t RAS_2819T_FAN2_MEDIUM = 0x3C; +const uint8_t RAS_2819T_FAN2_HIGH = 0x50; + +// RAS-2819T second packet suffix bytes for rc_code_2 (bytes 3-5) +// These are fixed patterns, not actual checksums +struct Ras2819tPacketSuffix { + uint8_t byte3; + uint8_t byte4; + uint8_t byte5; +}; +const Ras2819tPacketSuffix RAS_2819T_SUFFIX_AUTO{0x00, 0x02, 0x3D}; +const Ras2819tPacketSuffix RAS_2819T_SUFFIX_QUIET{0x00, 0x02, 0xD8}; +const Ras2819tPacketSuffix RAS_2819T_SUFFIX_LOW{0x00, 0x02, 0xFF}; +const Ras2819tPacketSuffix RAS_2819T_SUFFIX_MEDIUM{0x00, 0x02, 0x13}; +const Ras2819tPacketSuffix RAS_2819T_SUFFIX_HIGH{0x00, 0x02, 0x27}; + +// RAS-2819T swing toggle command +const uint64_t RAS_2819T_SWING_TOGGLE = 0xC23D6B94E01F; + +// RAS-2819T single-packet commands +const uint64_t RAS_2819T_POWER_OFF_COMMAND = 0xC23D7B84E01F; + +// RAS-2819T known valid command patterns for validation +const std::array RAS_2819T_VALID_SINGLE_COMMANDS = { + RAS_2819T_POWER_OFF_COMMAND, // Power off + RAS_2819T_SWING_TOGGLE, // Swing toggle +}; + +const uint16_t RAS_2819T_VALID_HEADER1 = 0xC23D; +const uint8_t RAS_2819T_VALID_HEADER2 = 0xD5; + +const uint8_t RAS_2819T_DRY_BYTE2 = 0x1F; +const uint8_t RAS_2819T_DRY_BYTE3 = 0xE0; +const uint8_t RAS_2819T_DRY_TEMP_OFFSET = 0x24; + +const uint8_t RAS_2819T_AUTO_BYTE2 = 0x1F; +const uint8_t RAS_2819T_AUTO_BYTE3 = 0xE0; +const uint8_t RAS_2819T_AUTO_TEMP_OFFSET = 0x08; + +const uint8_t RAS_2819T_FAN_ONLY_TEMP = 0xE4; +const uint8_t RAS_2819T_FAN_ONLY_TEMP_INV = 0x1B; + +const uint8_t RAS_2819T_HEAT_TEMP_OFFSET = 0x0C; + +// RAS-2819T second packet fixed values +const uint8_t RAS_2819T_AUTO_DRY_FAN_BYTE = 0x65; +const uint8_t RAS_2819T_AUTO_DRY_SUFFIX = 0x3A; +const uint8_t RAS_2819T_HEAT_SUFFIX = 0x3B; + +// RAS-2819T temperature codes for 18-30°C +static const uint8_t RAS_2819T_TEMP_CODES[] = { + 0x10, // 18°C + 0x30, // 19°C + 0x20, // 20°C + 0x60, // 21°C + 0x70, // 22°C + 0x50, // 23°C + 0x40, // 24°C + 0xC0, // 25°C + 0xD0, // 26°C + 0x90, // 27°C + 0x80, // 28°C + 0xA0, // 29°C + 0xB0 // 30°C +}; + +// Helper functions for RAS-2819T protocol +// +// ===== RAS-2819T PROTOCOL DOCUMENTATION ===== +// +// The RAS-2819T uses a two-packet IR protocol with some exceptions for simple commands. +// +// PACKET STRUCTURE: +// All packets are 6 bytes (48 bits) transmitted with standard Toshiba timing. +// +// TWO-PACKET COMMANDS (Mode/Temperature/Fan changes): +// +// First Packet (rc_code_1): [C2 3D] [FAN_HI FAN_LO] [TEMP] [~TEMP] +// Byte 0-1: Header (always 0xC23D) +// Byte 2-3: Fan speed encoding (varies by mode, see fan tables below) +// Byte 4: Temperature + mode encoding +// Byte 5: Bitwise complement of temperature byte +// +// Second Packet (rc_code_2): [D5] [FAN2] [00] [SUF1] [SUF2] [SUF3] +// Byte 0: Header (always 0xD5) +// Byte 1: Fan speed secondary encoding +// Byte 2: Always 0x00 +// Byte 3-5: Fixed suffix pattern (depends on fan speed and mode) +// +// TEMPERATURE ENCODING: +// Base temp codes: 18°C=0x10, 19°C=0x30, 20°C=0x20, 21°C=0x60, 22°C=0x70, +// 23°C=0x50, 24°C=0x40, 25°C=0xC0, 26°C=0xD0, 27°C=0x90, +// 28°C=0x80, 29°C=0xA0, 30°C=0xB0 +// Mode offsets added to base temp: +// COOL: No offset +// HEAT: +0x0C (e.g., 24°C heat = 0x40 | 0x0C = 0x4C) +// AUTO: +0x08 (e.g., 24°C auto = 0x40 | 0x08 = 0x48) +// DRY: +0x24 (e.g., 24°C dry = 0x40 | 0x24 = 0x64) +// +// FAN SPEED ENCODING (First packet bytes 2-3): +// AUTO: 0xBF40, QUIET: 0xFF00, LOW: 0x9F60, MEDIUM: 0x5FA0, HIGH: 0x3FC0 +// Special cases: AUTO/DRY modes use 0x1FE0 instead +// +// SINGLE-PACKET COMMANDS: +// Power Off: 0xC23D7B84E01F (6 bytes, no second packet) +// Swing Toggle: 0xC23D6B94E01F (6 bytes, no second packet) +// +// MODE DETECTION (from first packet): +// - Check bytes 2-3: if 0x7B84 → OFF mode +// - Check bytes 2-3: if 0x1FE0 → AUTO/DRY/low-temp-COOL (distinguish by temp code) +// - Otherwise: COOL/HEAT/FAN_ONLY (distinguish by temp code and byte 5) + +/** + * Get fan speed encoding for RAS-2819T first packet (rc_code_1, bytes 2-3) + */ +static uint16_t get_ras_2819t_fan_code(climate::ClimateFanMode fan_mode) { + switch (fan_mode) { + case climate::CLIMATE_FAN_QUIET: + return RAS_2819T_FAN_QUIET; + case climate::CLIMATE_FAN_LOW: + return RAS_2819T_FAN_LOW; + case climate::CLIMATE_FAN_MEDIUM: + return RAS_2819T_FAN_MEDIUM; + case climate::CLIMATE_FAN_HIGH: + return RAS_2819T_FAN_HIGH; + case climate::CLIMATE_FAN_AUTO: + default: + return RAS_2819T_FAN_AUTO; + } +} + +/** + * Get fan speed encoding for RAS-2819T rc_code_2 packet (second packet) + */ +struct Ras2819tSecondPacketCodes { + uint8_t fan_byte; + Ras2819tPacketSuffix suffix; +}; + +static Ras2819tSecondPacketCodes get_ras_2819t_second_packet_codes(climate::ClimateFanMode fan_mode) { + switch (fan_mode) { + case climate::CLIMATE_FAN_QUIET: + return {RAS_2819T_FAN2_QUIET, RAS_2819T_SUFFIX_QUIET}; + case climate::CLIMATE_FAN_LOW: + return {RAS_2819T_FAN2_LOW, RAS_2819T_SUFFIX_LOW}; + case climate::CLIMATE_FAN_MEDIUM: + return {RAS_2819T_FAN2_MEDIUM, RAS_2819T_SUFFIX_MEDIUM}; + case climate::CLIMATE_FAN_HIGH: + return {RAS_2819T_FAN2_HIGH, RAS_2819T_SUFFIX_HIGH}; + case climate::CLIMATE_FAN_AUTO: + default: + return {RAS_2819T_FAN2_AUTO, RAS_2819T_SUFFIX_AUTO}; + } +} + +/** + * Get temperature code for RAS-2819T protocol + */ +static uint8_t get_ras_2819t_temp_code(float temperature) { + int temp_index = static_cast(temperature) - 18; + if (temp_index < 0 || temp_index >= static_cast(sizeof(RAS_2819T_TEMP_CODES))) { + ESP_LOGW(TAG, "Temperature %.1f°C out of range [18-30°C], defaulting to 24°C", temperature); + return 0x40; // Default to 24°C + } + + return RAS_2819T_TEMP_CODES[temp_index]; +} + +/** + * Decode temperature from RAS-2819T temp code + */ +static float decode_ras_2819t_temperature(uint8_t temp_code) { + uint8_t base_temp_code = temp_code & 0xF0; + + // Find the code in the temperature array + for (size_t temp_index = 0; temp_index < sizeof(RAS_2819T_TEMP_CODES); temp_index++) { + if (RAS_2819T_TEMP_CODES[temp_index] == base_temp_code) { + return static_cast(temp_index + 18); // 18°C is the minimum + } + } + + ESP_LOGW(TAG, "Unknown temp code: 0x%02X, defaulting to 24°C", base_temp_code); + return 24.0f; // Default to 24°C +} + +/** + * Decode fan speed from RAS-2819T IR codes + */ +static climate::ClimateFanMode decode_ras_2819t_fan_mode(uint16_t fan_code) { + switch (fan_code) { + case RAS_2819T_FAN_QUIET: + return climate::CLIMATE_FAN_QUIET; + case RAS_2819T_FAN_LOW: + return climate::CLIMATE_FAN_LOW; + case RAS_2819T_FAN_MEDIUM: + return climate::CLIMATE_FAN_MEDIUM; + case RAS_2819T_FAN_HIGH: + return climate::CLIMATE_FAN_HIGH; + case RAS_2819T_FAN_AUTO: + default: + return climate::CLIMATE_FAN_AUTO; + } +} + +/** + * Validate RAS-2819T IR command structure and content + */ +static bool is_valid_ras_2819t_command(uint64_t rc_code_1, uint64_t rc_code_2 = 0) { + // Check header of first packet + uint16_t header1 = (rc_code_1 >> 32) & 0xFFFF; + if (header1 != RAS_2819T_VALID_HEADER1) { + return false; + } + + // Single packet commands + if (rc_code_2 == 0) { + for (uint64_t valid_cmd : RAS_2819T_VALID_SINGLE_COMMANDS) { + if (rc_code_1 == valid_cmd) { + return true; + } + } + // Additional validation for unknown single packets + return false; + } + + // Two-packet commands - validate second packet header + uint8_t header2 = (rc_code_2 >> 40) & 0xFF; + if (header2 != RAS_2819T_VALID_HEADER2) { + return false; + } + + // Validate temperature complement in first packet (byte 4 should be ~byte 5) + uint8_t temp_byte = (rc_code_1 >> 8) & 0xFF; + uint8_t temp_complement = rc_code_1 & 0xFF; + if (temp_byte != static_cast(~temp_complement)) { + return false; + } + + // Validate fan speed combinations make sense + uint16_t fan_code = (rc_code_1 >> 16) & 0xFFFF; + uint8_t fan2_byte = (rc_code_2 >> 32) & 0xFF; + + // Check if fan codes are from known valid patterns + bool valid_fan_combo = false; + if (fan_code == RAS_2819T_FAN_AUTO && fan2_byte == RAS_2819T_FAN2_AUTO) + valid_fan_combo = true; + if (fan_code == RAS_2819T_FAN_QUIET && fan2_byte == RAS_2819T_FAN2_QUIET) + valid_fan_combo = true; + if (fan_code == RAS_2819T_FAN_LOW && fan2_byte == RAS_2819T_FAN2_LOW) + valid_fan_combo = true; + if (fan_code == RAS_2819T_FAN_MEDIUM && fan2_byte == RAS_2819T_FAN2_MEDIUM) + valid_fan_combo = true; + if (fan_code == RAS_2819T_FAN_HIGH && fan2_byte == RAS_2819T_FAN2_HIGH) + valid_fan_combo = true; + if (fan_code == 0x1FE0 && fan2_byte == RAS_2819T_AUTO_DRY_FAN_BYTE) + valid_fan_combo = true; // AUTO/DRY + + return valid_fan_combo; +} + void ToshibaClimate::setup() { if (this->sensor_) { this->sensor_->add_on_state_callback([this](float state) { @@ -126,16 +403,43 @@ void ToshibaClimate::setup() { this->minimum_temperature_ = this->temperature_min_(); this->maximum_temperature_ = this->temperature_max_(); this->swing_modes_ = this->toshiba_swing_modes_(); + + // Ensure swing mode is always initialized to a valid value + if (this->swing_modes_.empty() || this->swing_modes_.find(this->swing_mode) == this->swing_modes_.end()) { + // No swing support for this model or current swing mode not supported, reset to OFF + this->swing_mode = climate::CLIMATE_SWING_OFF; + } + + // Ensure mode is valid - ESPHome should only use standard climate modes + if (this->mode != climate::CLIMATE_MODE_OFF && this->mode != climate::CLIMATE_MODE_HEAT && + this->mode != climate::CLIMATE_MODE_COOL && this->mode != climate::CLIMATE_MODE_HEAT_COOL && + this->mode != climate::CLIMATE_MODE_DRY && this->mode != climate::CLIMATE_MODE_FAN_ONLY) { + ESP_LOGW(TAG, "Invalid mode detected during setup, resetting to OFF"); + this->mode = climate::CLIMATE_MODE_OFF; + } + + // Ensure fan mode is valid + if (!this->fan_mode.has_value()) { + ESP_LOGW(TAG, "Fan mode not set during setup, defaulting to AUTO"); + this->fan_mode = climate::CLIMATE_FAN_AUTO; + } + // Never send nan to HA if (std::isnan(this->target_temperature)) this->target_temperature = 24; + // Log final state for debugging HA errors + ESP_LOGV(TAG, "Setup complete - Mode: %d, Fan: %s, Swing: %d, Temp: %.1f", static_cast(this->mode), + this->fan_mode.has_value() ? std::to_string(static_cast(this->fan_mode.value())).c_str() : "NONE", + static_cast(this->swing_mode), this->target_temperature); } void ToshibaClimate::transmit_state() { if (this->model_ == MODEL_RAC_PT1411HWRU_C || this->model_ == MODEL_RAC_PT1411HWRU_F) { - transmit_rac_pt1411hwru_(); + this->transmit_rac_pt1411hwru_(); + } else if (this->model_ == MODEL_RAS_2819T) { + this->transmit_ras_2819t_(); } else { - transmit_generic_(); + this->transmit_generic_(); } } @@ -230,7 +534,7 @@ void ToshibaClimate::transmit_generic_() { auto transmit = this->transmitter_->transmit(); auto *data = transmit.get_data(); - encode_(data, message, message_length, 1); + this->encode_(data, message, message_length, 1); transmit.perform(); } @@ -348,15 +652,12 @@ void ToshibaClimate::transmit_rac_pt1411hwru_() { message[11] += message[index]; } } - ESP_LOGV(TAG, "*** Generated codes: 0x%.2X%.2X%.2X%.2X%.2X%.2X 0x%.2X%.2X%.2X%.2X%.2X%.2X", message[0], message[1], - message[2], message[3], message[4], message[5], message[6], message[7], message[8], message[9], message[10], - message[11]); // load first block of IR code and repeat it once - encode_(data, &message[0], RAC_PT1411HWRU_MESSAGE_LENGTH, 1); + this->encode_(data, &message[0], RAC_PT1411HWRU_MESSAGE_LENGTH, 1); // load second block of IR code, if present if (message[6] != 0) { - encode_(data, &message[6], RAC_PT1411HWRU_MESSAGE_LENGTH, 0); + this->encode_(data, &message[6], RAC_PT1411HWRU_MESSAGE_LENGTH, 0); } transmit.perform(); @@ -366,19 +667,19 @@ void ToshibaClimate::transmit_rac_pt1411hwru_() { data->space(TOSHIBA_PACKET_SPACE); switch (this->swing_mode) { case climate::CLIMATE_SWING_VERTICAL: - encode_(data, &RAC_PT1411HWRU_SWING_VERTICAL[0], RAC_PT1411HWRU_MESSAGE_LENGTH, 1); + this->encode_(data, &RAC_PT1411HWRU_SWING_VERTICAL[0], RAC_PT1411HWRU_MESSAGE_LENGTH, 1); break; case climate::CLIMATE_SWING_OFF: default: - encode_(data, &RAC_PT1411HWRU_SWING_OFF[0], RAC_PT1411HWRU_MESSAGE_LENGTH, 1); + this->encode_(data, &RAC_PT1411HWRU_SWING_OFF[0], RAC_PT1411HWRU_MESSAGE_LENGTH, 1); } data->space(TOSHIBA_PACKET_SPACE); transmit.perform(); if (this->sensor_) { - transmit_rac_pt1411hwru_temp_(true, false); + this->transmit_rac_pt1411hwru_temp_(true, false); } } @@ -430,15 +731,217 @@ void ToshibaClimate::transmit_rac_pt1411hwru_temp_(const bool cs_state, const bo // Byte 5: Footer lower/bitwise complement of byte 4 message[5] = ~message[4]; - ESP_LOGV(TAG, "*** Generated code: 0x%.2X%.2X%.2X%.2X%.2X%.2X", message[0], message[1], message[2], message[3], - message[4], message[5]); // load IR code and repeat it once - encode_(data, message, RAC_PT1411HWRU_MESSAGE_LENGTH, 1); + this->encode_(data, message, RAC_PT1411HWRU_MESSAGE_LENGTH, 1); transmit.perform(); } } +void ToshibaClimate::transmit_ras_2819t_() { + // Handle swing mode transmission for RAS-2819T + // Note: RAS-2819T uses a toggle command, so we need to track state changes + + // Check if ONLY swing mode changed (and no other climate parameters) + bool swing_changed = (this->swing_mode != this->last_swing_mode_); + bool mode_changed = (this->mode != this->last_mode_); + bool fan_changed = (this->fan_mode != this->last_fan_mode_); + bool temp_changed = (abs(this->target_temperature - this->last_target_temperature_) > 0.1f); + + bool only_swing_changed = swing_changed && !mode_changed && !fan_changed && !temp_changed; + + if (only_swing_changed) { + // Send ONLY swing toggle command (like the physical remote does) + auto swing_transmit = this->transmitter_->transmit(); + auto *swing_data = swing_transmit.get_data(); + + // Convert toggle command to bytes for transmission + uint8_t swing_message[RAS_2819T_MESSAGE_LENGTH]; + swing_message[0] = (RAS_2819T_SWING_TOGGLE >> 40) & 0xFF; + swing_message[1] = (RAS_2819T_SWING_TOGGLE >> 32) & 0xFF; + swing_message[2] = (RAS_2819T_SWING_TOGGLE >> 24) & 0xFF; + swing_message[3] = (RAS_2819T_SWING_TOGGLE >> 16) & 0xFF; + swing_message[4] = (RAS_2819T_SWING_TOGGLE >> 8) & 0xFF; + swing_message[5] = RAS_2819T_SWING_TOGGLE & 0xFF; + + // Use single packet transmission WITH repeat (like regular commands) + this->encode_(swing_data, swing_message, RAS_2819T_MESSAGE_LENGTH, 1); + swing_transmit.perform(); + + // Update all state tracking + this->last_swing_mode_ = this->swing_mode; + this->last_mode_ = this->mode; + this->last_fan_mode_ = this->fan_mode; + this->last_target_temperature_ = this->target_temperature; + + // Immediately publish the state change to Home Assistant + this->publish_state(); + + return; // Exit early - don't send climate command + } + + // If we get here, send the regular climate command (temperature/mode/fan) + uint8_t message1[RAS_2819T_MESSAGE_LENGTH] = {0}; + uint8_t message2[RAS_2819T_MESSAGE_LENGTH] = {0}; + float temperature = + clamp(this->target_temperature, TOSHIBA_RAS_2819T_TEMP_C_MIN, TOSHIBA_RAS_2819T_TEMP_C_MAX); + + // Build first packet (RAS_2819T_HEADER1 + 4 bytes) + message1[0] = (RAS_2819T_HEADER1 >> 8) & 0xFF; + message1[1] = RAS_2819T_HEADER1 & 0xFF; + + // Handle OFF mode + if (this->mode == climate::CLIMATE_MODE_OFF) { + // Extract bytes from power off command constant + message1[2] = (RAS_2819T_POWER_OFF_COMMAND >> 24) & 0xFF; + message1[3] = (RAS_2819T_POWER_OFF_COMMAND >> 16) & 0xFF; + message1[4] = (RAS_2819T_POWER_OFF_COMMAND >> 8) & 0xFF; + message1[5] = RAS_2819T_POWER_OFF_COMMAND & 0xFF; + // No second packet for OFF + } else { + // Get temperature and fan encoding + uint8_t temp_code = get_ras_2819t_temp_code(temperature); + + // Get fan speed encoding for rc_code_1 + climate::ClimateFanMode effective_fan_mode = this->fan_mode.value(); + + // Dry mode only supports AUTO fan speed + if (this->mode == climate::CLIMATE_MODE_DRY) { + effective_fan_mode = climate::CLIMATE_FAN_AUTO; + if (this->fan_mode.value() != climate::CLIMATE_FAN_AUTO) { + ESP_LOGW(TAG, "Dry mode only supports AUTO fan speed, forcing AUTO"); + } + } + + uint16_t fan_code = get_ras_2819t_fan_code(effective_fan_mode); + + // Mode and temperature encoding + switch (this->mode) { + case climate::CLIMATE_MODE_COOL: + // All cooling temperatures support fan speed control + message1[2] = (fan_code >> 8) & 0xFF; + message1[3] = fan_code & 0xFF; + message1[4] = temp_code; + message1[5] = ~temp_code; + break; + + case climate::CLIMATE_MODE_HEAT: + // Heating supports fan speed control + message1[2] = (fan_code >> 8) & 0xFF; + message1[3] = fan_code & 0xFF; + // Heat mode adds offset to temperature code + message1[4] = temp_code | RAS_2819T_HEAT_TEMP_OFFSET; + message1[5] = ~(temp_code | RAS_2819T_HEAT_TEMP_OFFSET); + break; + + case climate::CLIMATE_MODE_HEAT_COOL: + // Auto mode uses fixed encoding + message1[2] = RAS_2819T_AUTO_BYTE2; + message1[3] = RAS_2819T_AUTO_BYTE3; + message1[4] = temp_code | RAS_2819T_AUTO_TEMP_OFFSET; + message1[5] = ~(temp_code | RAS_2819T_AUTO_TEMP_OFFSET); + break; + + case climate::CLIMATE_MODE_DRY: + // Dry mode uses fixed encoding and forces AUTO fan + message1[2] = RAS_2819T_DRY_BYTE2; + message1[3] = RAS_2819T_DRY_BYTE3; + message1[4] = temp_code | RAS_2819T_DRY_TEMP_OFFSET; + message1[5] = ~message1[4]; + break; + + case climate::CLIMATE_MODE_FAN_ONLY: + // Fan only mode supports fan speed control + message1[2] = (fan_code >> 8) & 0xFF; + message1[3] = fan_code & 0xFF; + message1[4] = RAS_2819T_FAN_ONLY_TEMP; + message1[5] = RAS_2819T_FAN_ONLY_TEMP_INV; + break; + + default: + // Default case supports fan speed control + message1[2] = (fan_code >> 8) & 0xFF; + message1[3] = fan_code & 0xFF; + message1[4] = temp_code; + message1[5] = ~temp_code; + break; + } + + // Build second packet (RAS_2819T_HEADER2 + 4 bytes) + message2[0] = RAS_2819T_HEADER2; + + // Get fan speed encoding for rc_code_2 + Ras2819tSecondPacketCodes second_packet_codes = get_ras_2819t_second_packet_codes(effective_fan_mode); + + // Determine header byte 2 and fan encoding based on mode + switch (this->mode) { + case climate::CLIMATE_MODE_COOL: + message2[1] = second_packet_codes.fan_byte; + message2[2] = 0x00; + message2[3] = second_packet_codes.suffix.byte3; + message2[4] = second_packet_codes.suffix.byte4; + message2[5] = second_packet_codes.suffix.byte5; + break; + + case climate::CLIMATE_MODE_HEAT: + message2[1] = second_packet_codes.fan_byte; + message2[2] = 0x00; + message2[3] = second_packet_codes.suffix.byte3; + message2[4] = 0x00; + message2[5] = RAS_2819T_HEAT_SUFFIX; + break; + + case climate::CLIMATE_MODE_HEAT_COOL: + case climate::CLIMATE_MODE_DRY: + // Auto/Dry modes use fixed values regardless of fan setting + message2[1] = RAS_2819T_AUTO_DRY_FAN_BYTE; + message2[2] = 0x00; + message2[3] = 0x00; + message2[4] = 0x00; + message2[5] = RAS_2819T_AUTO_DRY_SUFFIX; + break; + + case climate::CLIMATE_MODE_FAN_ONLY: + message2[1] = second_packet_codes.fan_byte; + message2[2] = 0x00; + message2[3] = second_packet_codes.suffix.byte3; + message2[4] = 0x00; + message2[5] = RAS_2819T_HEAT_SUFFIX; + break; + + default: + message2[1] = second_packet_codes.fan_byte; + message2[2] = 0x00; + message2[3] = second_packet_codes.suffix.byte3; + message2[4] = second_packet_codes.suffix.byte4; + message2[5] = second_packet_codes.suffix.byte5; + break; + } + } + + // Log final messages being transmitted + + // Transmit using proper Toshiba protocol timing + auto transmit = this->transmitter_->transmit(); + auto *data = transmit.get_data(); + + // Use existing Toshiba encode function for proper timing + this->encode_(data, message1, RAS_2819T_MESSAGE_LENGTH, 1); + + if (this->mode != climate::CLIMATE_MODE_OFF) { + // Send second packet with gap + this->encode_(data, message2, RAS_2819T_MESSAGE_LENGTH, 0); + } + + transmit.perform(); + + // Update all state tracking after successful transmission + this->last_swing_mode_ = this->swing_mode; + this->last_mode_ = this->mode; + this->last_fan_mode_ = this->fan_mode; + this->last_target_temperature_ = this->target_temperature; +} + uint8_t ToshibaClimate::is_valid_rac_pt1411hwru_header_(const uint8_t *message) { const std::vector header{RAC_PT1411HWRU_MESSAGE_HEADER0, RAC_PT1411HWRU_CS_HEADER, RAC_PT1411HWRU_SWING_HEADER}; @@ -464,11 +967,11 @@ bool ToshibaClimate::compare_rac_pt1411hwru_packets_(const uint8_t *message1, co bool ToshibaClimate::is_valid_rac_pt1411hwru_message_(const uint8_t *message) { uint8_t checksum = 0; - switch (is_valid_rac_pt1411hwru_header_(message)) { + switch (this->is_valid_rac_pt1411hwru_header_(message)) { case RAC_PT1411HWRU_MESSAGE_HEADER0: case RAC_PT1411HWRU_CS_HEADER: case RAC_PT1411HWRU_SWING_HEADER: - if (is_valid_rac_pt1411hwru_header_(message) && (message[2] == static_cast(~message[3])) && + if (this->is_valid_rac_pt1411hwru_header_(message) && (message[2] == static_cast(~message[3])) && (message[4] == static_cast(~message[5]))) { return true; } @@ -490,7 +993,103 @@ bool ToshibaClimate::is_valid_rac_pt1411hwru_message_(const uint8_t *message) { return false; } +bool ToshibaClimate::process_ras_2819t_command_(const remote_base::ToshibaAcData &toshiba_data) { + // Check for power-off command (single packet) + if (toshiba_data.rc_code_2 == 0 && toshiba_data.rc_code_1 == RAS_2819T_POWER_OFF_COMMAND) { + this->mode = climate::CLIMATE_MODE_OFF; + ESP_LOGI(TAG, "Mode: OFF"); + this->publish_state(); + return true; + } + + // Check for swing toggle command (single packet) + if (toshiba_data.rc_code_2 == 0 && toshiba_data.rc_code_1 == RAS_2819T_SWING_TOGGLE) { + // Toggle swing mode + if (this->swing_mode == climate::CLIMATE_SWING_VERTICAL) { + this->swing_mode = climate::CLIMATE_SWING_OFF; + ESP_LOGI(TAG, "Swing: OFF"); + } else { + this->swing_mode = climate::CLIMATE_SWING_VERTICAL; + ESP_LOGI(TAG, "Swing: VERTICAL"); + } + this->publish_state(); + return true; + } + + // Handle regular two-packet commands (mode/temperature/fan changes) + if (toshiba_data.rc_code_2 != 0) { + // Convert to byte array for easier processing + uint8_t message1[6], message2[6]; + for (uint8_t i = 0; i < 6; i++) { + message1[i] = (toshiba_data.rc_code_1 >> (40 - i * 8)) & 0xFF; + message2[i] = (toshiba_data.rc_code_2 >> (40 - i * 8)) & 0xFF; + } + + // Decode the protocol using message1 (rc_code_1) + uint8_t temp_code = message1[4]; + + // Decode mode - check bytes 2-3 pattern and temperature code + if ((message1[2] == 0x7B) && (message1[3] == 0x84)) { + // OFF mode has specific pattern + this->mode = climate::CLIMATE_MODE_OFF; + ESP_LOGI(TAG, "Mode: OFF"); + } else if ((message1[2] == 0x1F) && (message1[3] == 0xE0)) { + // 0x1FE0 pattern is used for AUTO, DRY, and low-temp COOL + if ((temp_code & 0x0F) == 0x08) { + this->mode = climate::CLIMATE_MODE_HEAT_COOL; + ESP_LOGI(TAG, "Mode: AUTO"); + } else if ((temp_code & 0x0F) == 0x04) { + this->mode = climate::CLIMATE_MODE_DRY; + ESP_LOGI(TAG, "Mode: DRY"); + } else { + this->mode = climate::CLIMATE_MODE_COOL; + ESP_LOGI(TAG, "Mode: COOL (low temp)"); + } + } else { + // Variable fan speed patterns - decode by temperature code + if ((temp_code & 0x0F) == 0x0C) { + this->mode = climate::CLIMATE_MODE_HEAT; + ESP_LOGI(TAG, "Mode: HEAT"); + } else if (message1[5] == 0x1B) { + this->mode = climate::CLIMATE_MODE_FAN_ONLY; + ESP_LOGI(TAG, "Mode: FAN_ONLY"); + } else { + this->mode = climate::CLIMATE_MODE_COOL; + ESP_LOGI(TAG, "Mode: COOL"); + } + } + + // Decode fan speed from rc_code_1 + uint16_t fan_code = (message1[2] << 8) | message1[3]; + this->fan_mode = decode_ras_2819t_fan_mode(fan_code); + + // Decode temperature + if (this->mode != climate::CLIMATE_MODE_OFF && this->mode != climate::CLIMATE_MODE_FAN_ONLY) { + this->target_temperature = decode_ras_2819t_temperature(temp_code); + } + + this->publish_state(); + return true; + } else { + ESP_LOGD(TAG, "Unknown single-packet RAS-2819T command: 0x%" PRIX64, toshiba_data.rc_code_1); + return false; + } +} + bool ToshibaClimate::on_receive(remote_base::RemoteReceiveData data) { + // Try modern ToshibaAcProtocol decoder first (handles RAS-2819T and potentially others) + remote_base::ToshibaAcProtocol toshiba_protocol; + auto decode_result = toshiba_protocol.decode(data); + + if (decode_result.has_value()) { + auto toshiba_data = decode_result.value(); + // Validate and process RAS-2819T commands + if (is_valid_ras_2819t_command(toshiba_data.rc_code_1, toshiba_data.rc_code_2)) { + return this->process_ras_2819t_command_(toshiba_data); + } + } + + // Fall back to generic processing for older protocols uint8_t message[18] = {0}; uint8_t message_length = TOSHIBA_HEADER_LENGTH, temperature_code = 0; @@ -499,11 +1098,11 @@ bool ToshibaClimate::on_receive(remote_base::RemoteReceiveData data) { return false; } // Read incoming bits into buffer - if (!decode_(&data, message, message_length)) { + if (!this->decode_(&data, message, message_length)) { return false; } // Determine incoming message protocol version and/or length - if (is_valid_rac_pt1411hwru_header_(message)) { + if (this->is_valid_rac_pt1411hwru_header_(message)) { // We already received four bytes message_length = RAC_PT1411HWRU_MESSAGE_LENGTH - 4; } else if ((message[0] ^ message[1] ^ message[2]) != message[3]) { @@ -514,11 +1113,11 @@ bool ToshibaClimate::on_receive(remote_base::RemoteReceiveData data) { message_length = message[2] + 2; } // Decode the remaining bytes - if (!decode_(&data, &message[4], message_length)) { + if (!this->decode_(&data, &message[4], message_length)) { return false; } // If this is a RAC-PT1411HWRU message, we expect the first packet a second time and also possibly a third packet - if (is_valid_rac_pt1411hwru_header_(message)) { + if (this->is_valid_rac_pt1411hwru_header_(message)) { // There is always a space between packets if (!data.expect_item(TOSHIBA_BIT_MARK, TOSHIBA_GAP_SPACE)) { return false; @@ -527,7 +1126,7 @@ bool ToshibaClimate::on_receive(remote_base::RemoteReceiveData data) { if (!data.expect_item(TOSHIBA_HEADER_MARK, TOSHIBA_HEADER_SPACE)) { return false; } - if (!decode_(&data, &message[6], RAC_PT1411HWRU_MESSAGE_LENGTH)) { + if (!this->decode_(&data, &message[6], RAC_PT1411HWRU_MESSAGE_LENGTH)) { return false; } // If this is a RAC-PT1411HWRU message, there may also be a third packet. @@ -535,25 +1134,25 @@ bool ToshibaClimate::on_receive(remote_base::RemoteReceiveData data) { if (data.expect_item(TOSHIBA_BIT_MARK, TOSHIBA_GAP_SPACE)) { // Validate header 3 data.expect_item(TOSHIBA_HEADER_MARK, TOSHIBA_HEADER_SPACE); - if (decode_(&data, &message[12], RAC_PT1411HWRU_MESSAGE_LENGTH)) { - if (!is_valid_rac_pt1411hwru_message_(&message[12])) { + if (this->decode_(&data, &message[12], RAC_PT1411HWRU_MESSAGE_LENGTH)) { + if (!this->is_valid_rac_pt1411hwru_message_(&message[12])) { // If a third packet was received but the checksum is not valid, fail return false; } } } - if (!compare_rac_pt1411hwru_packets_(&message[0], &message[6])) { + if (!this->compare_rac_pt1411hwru_packets_(&message[0], &message[6])) { // If the first two packets don't match each other, fail return false; } - if (!is_valid_rac_pt1411hwru_message_(&message[0])) { + if (!this->is_valid_rac_pt1411hwru_message_(&message[0])) { // If the first packet isn't valid, fail return false; } } // Header has been verified, now determine protocol version and set the climate component properties - switch (is_valid_rac_pt1411hwru_header_(message)) { + switch (this->is_valid_rac_pt1411hwru_header_(message)) { // Power, temperature, mode, fan speed case RAC_PT1411HWRU_MESSAGE_HEADER0: // Get the mode @@ -608,7 +1207,7 @@ bool ToshibaClimate::on_receive(remote_base::RemoteReceiveData data) { break; } // Get the target temperature - if (is_valid_rac_pt1411hwru_message_(&message[12])) { + if (this->is_valid_rac_pt1411hwru_message_(&message[12])) { temperature_code = (message[4] >> 4) | (message[14] & RAC_PT1411HWRU_FLAG_FRAC) | (message[15] & RAC_PT1411HWRU_FLAG_NEG); if (message[15] & RAC_PT1411HWRU_FLAG_FAH) { diff --git a/esphome/components/toshiba/toshiba.h b/esphome/components/toshiba/toshiba.h index 83e85c34db..d76833f406 100644 --- a/esphome/components/toshiba/toshiba.h +++ b/esphome/components/toshiba/toshiba.h @@ -1,6 +1,7 @@ #pragma once #include "esphome/components/climate_ir/climate_ir.h" +#include "esphome/components/remote_base/toshiba_ac_protocol.h" namespace esphome { namespace toshiba { @@ -10,6 +11,7 @@ enum Model { MODEL_GENERIC = 0, // Temperature range is from 17 to 30 MODEL_RAC_PT1411HWRU_C = 1, // Temperature range is from 16 to 30 MODEL_RAC_PT1411HWRU_F = 2, // Temperature range is from 16 to 30 + MODEL_RAS_2819T = 3, // RAS-2819T protocol variant, temperature range 18 to 30 }; // Supported temperature ranges @@ -19,6 +21,8 @@ const float TOSHIBA_RAC_PT1411HWRU_TEMP_C_MIN = 16.0; const float TOSHIBA_RAC_PT1411HWRU_TEMP_C_MAX = 30.0; const float TOSHIBA_RAC_PT1411HWRU_TEMP_F_MIN = 60.0; const float TOSHIBA_RAC_PT1411HWRU_TEMP_F_MAX = 86.0; +const float TOSHIBA_RAS_2819T_TEMP_C_MIN = 18.0; +const float TOSHIBA_RAS_2819T_TEMP_C_MAX = 30.0; class ToshibaClimate : public climate_ir::ClimateIR { public: @@ -35,6 +39,9 @@ class ToshibaClimate : public climate_ir::ClimateIR { void transmit_generic_(); void transmit_rac_pt1411hwru_(); void transmit_rac_pt1411hwru_temp_(bool cs_state = true, bool cs_send_update = true); + void transmit_ras_2819t_(); + // Process RAS-2819T IR command data + bool process_ras_2819t_command_(const remote_base::ToshibaAcData &toshiba_data); // Returns the header if valid, else returns zero uint8_t is_valid_rac_pt1411hwru_header_(const uint8_t *message); // Returns true if message is a valid RAC-PT1411HWRU IR message, regardless if first or second packet @@ -43,11 +50,26 @@ class ToshibaClimate : public climate_ir::ClimateIR { bool compare_rac_pt1411hwru_packets_(const uint8_t *message1, const uint8_t *message2); bool on_receive(remote_base::RemoteReceiveData data) override; + private: + // RAS-2819T state tracking for swing mode optimization + climate::ClimateSwingMode last_swing_mode_{climate::CLIMATE_SWING_OFF}; + climate::ClimateMode last_mode_{climate::CLIMATE_MODE_OFF}; + optional last_fan_mode_{}; + float last_target_temperature_{24.0f}; + float temperature_min_() { - return (this->model_ == MODEL_GENERIC) ? TOSHIBA_GENERIC_TEMP_C_MIN : TOSHIBA_RAC_PT1411HWRU_TEMP_C_MIN; + if (this->model_ == MODEL_RAC_PT1411HWRU_C || this->model_ == MODEL_RAC_PT1411HWRU_F) + return TOSHIBA_RAC_PT1411HWRU_TEMP_C_MIN; + if (this->model_ == MODEL_RAS_2819T) + return TOSHIBA_RAS_2819T_TEMP_C_MIN; + return TOSHIBA_GENERIC_TEMP_C_MIN; // Default to GENERIC for unknown models } float temperature_max_() { - return (this->model_ == MODEL_GENERIC) ? TOSHIBA_GENERIC_TEMP_C_MAX : TOSHIBA_RAC_PT1411HWRU_TEMP_C_MAX; + if (this->model_ == MODEL_RAC_PT1411HWRU_C || this->model_ == MODEL_RAC_PT1411HWRU_F) + return TOSHIBA_RAC_PT1411HWRU_TEMP_C_MAX; + if (this->model_ == MODEL_RAS_2819T) + return TOSHIBA_RAS_2819T_TEMP_C_MAX; + return TOSHIBA_GENERIC_TEMP_C_MAX; // Default to GENERIC for unknown models } std::set toshiba_swing_modes_() { return (this->model_ == MODEL_GENERIC) diff --git a/tests/components/toshiba/common_ras2819t.yaml b/tests/components/toshiba/common_ras2819t.yaml new file mode 100644 index 0000000000..32081fca98 --- /dev/null +++ b/tests/components/toshiba/common_ras2819t.yaml @@ -0,0 +1,13 @@ +remote_transmitter: + pin: ${tx_pin} + carrier_duty_percent: 50% + +remote_receiver: + id: rcvr + pin: ${rx_pin} + +climate: + - platform: toshiba + name: "RAS-2819T Climate" + model: RAS-2819T + receiver_id: rcvr diff --git a/tests/components/toshiba/test_ras2819t.esp32-ard.yaml b/tests/components/toshiba/test_ras2819t.esp32-ard.yaml new file mode 100644 index 0000000000..00805baa01 --- /dev/null +++ b/tests/components/toshiba/test_ras2819t.esp32-ard.yaml @@ -0,0 +1,5 @@ +substitutions: + tx_pin: GPIO5 + rx_pin: GPIO4 + +<<: !include common_ras2819t.yaml diff --git a/tests/components/toshiba/test_ras2819t.esp32-c3-ard.yaml b/tests/components/toshiba/test_ras2819t.esp32-c3-ard.yaml new file mode 100644 index 0000000000..00805baa01 --- /dev/null +++ b/tests/components/toshiba/test_ras2819t.esp32-c3-ard.yaml @@ -0,0 +1,5 @@ +substitutions: + tx_pin: GPIO5 + rx_pin: GPIO4 + +<<: !include common_ras2819t.yaml diff --git a/tests/components/toshiba/test_ras2819t.esp32-c3-idf.yaml b/tests/components/toshiba/test_ras2819t.esp32-c3-idf.yaml new file mode 100644 index 0000000000..00805baa01 --- /dev/null +++ b/tests/components/toshiba/test_ras2819t.esp32-c3-idf.yaml @@ -0,0 +1,5 @@ +substitutions: + tx_pin: GPIO5 + rx_pin: GPIO4 + +<<: !include common_ras2819t.yaml diff --git a/tests/components/toshiba/test_ras2819t.esp32-idf.yaml b/tests/components/toshiba/test_ras2819t.esp32-idf.yaml new file mode 100644 index 0000000000..00805baa01 --- /dev/null +++ b/tests/components/toshiba/test_ras2819t.esp32-idf.yaml @@ -0,0 +1,5 @@ +substitutions: + tx_pin: GPIO5 + rx_pin: GPIO4 + +<<: !include common_ras2819t.yaml diff --git a/tests/components/toshiba/test_ras2819t.esp8266-ard.yaml b/tests/components/toshiba/test_ras2819t.esp8266-ard.yaml new file mode 100644 index 0000000000..00805baa01 --- /dev/null +++ b/tests/components/toshiba/test_ras2819t.esp8266-ard.yaml @@ -0,0 +1,5 @@ +substitutions: + tx_pin: GPIO5 + rx_pin: GPIO4 + +<<: !include common_ras2819t.yaml From 2b832e9ee8df636076898750162c1259ead4ae5d Mon Sep 17 00:00:00 2001 From: mrtoy-me <118446898+mrtoy-me@users.noreply.github.com> Date: Fri, 17 Oct 2025 22:55:07 +1000 Subject: [PATCH 4/7] [cap1188] remove delays in setup (#11317) Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- esphome/components/cap1188/cap1188.cpp | 34 +++++++++++++++++++------- esphome/components/cap1188/cap1188.h | 2 ++ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/esphome/components/cap1188/cap1188.cpp b/esphome/components/cap1188/cap1188.cpp index 584ff896c5..683e5cf487 100644 --- a/esphome/components/cap1188/cap1188.cpp +++ b/esphome/components/cap1188/cap1188.cpp @@ -8,17 +8,30 @@ namespace cap1188 { static const char *const TAG = "cap1188"; void CAP1188Component::setup() { - // Reset device using the reset pin - if (this->reset_pin_ != nullptr) { - this->reset_pin_->setup(); - this->reset_pin_->digital_write(false); - delay(100); // NOLINT - this->reset_pin_->digital_write(true); - delay(100); // NOLINT - this->reset_pin_->digital_write(false); - delay(100); // NOLINT + this->disable_loop(); + + // no reset pin + if (this->reset_pin_ == nullptr) { + this->finish_setup_(); + return; } + // reset pin configured so reset before finishing setup + this->reset_pin_->setup(); + this->reset_pin_->digital_write(false); + // delay after reset pin write + this->set_timeout(100, [this]() { + this->reset_pin_->digital_write(true); + // delay after reset pin write + this->set_timeout(100, [this]() { + this->reset_pin_->digital_write(false); + // delay after reset pin write + this->set_timeout(100, [this]() { this->finish_setup_(); }); + }); + }); +} + +void CAP1188Component::finish_setup_() { // Check if CAP1188 is actually connected this->read_byte(CAP1188_PRODUCT_ID, &this->cap1188_product_id_); this->read_byte(CAP1188_MANUFACTURE_ID, &this->cap1188_manufacture_id_); @@ -44,6 +57,9 @@ void CAP1188Component::setup() { // Speed up a bit this->write_byte(CAP1188_STAND_BY_CONFIGURATION, 0x30); + + // Setup successful, so enable loop + this->enable_loop(); } void CAP1188Component::dump_config() { diff --git a/esphome/components/cap1188/cap1188.h b/esphome/components/cap1188/cap1188.h index baefd1c48f..297c601b05 100644 --- a/esphome/components/cap1188/cap1188.h +++ b/esphome/components/cap1188/cap1188.h @@ -49,6 +49,8 @@ class CAP1188Component : public Component, public i2c::I2CDevice { void loop() override; protected: + void finish_setup_(); + std::vector channels_{}; uint8_t touch_threshold_{0x20}; uint8_t allow_multiple_touches_{0x80}; From fe9db75c27a663bbbee5202381386ae81395a36b Mon Sep 17 00:00:00 2001 From: tomaszduda23 Date: Fri, 17 Oct 2025 15:02:37 +0200 Subject: [PATCH 5/7] [nrf52] add xiao_ble board (#10698) Co-authored-by: Jonathan Swoboda <154711427+swoboda1337@users.noreply.github.com> --- esphome/components/debug/debug_zephyr.cpp | 52 ++++++++++++++++++- esphome/components/nrf52/__init__.py | 13 +++++ esphome/components/nrf52/boards.py | 10 +++- esphome/components/zephyr/__init__.py | 27 ++++++---- .../components/nrf52/test.nrf52-xiao-ble.yaml | 7 +++ .../build_components_base.nrf52-xiao-ble.yaml | 15 ++++++ 6 files changed, 112 insertions(+), 12 deletions(-) create mode 100644 tests/components/nrf52/test.nrf52-xiao-ble.yaml create mode 100644 tests/test_build_components/build_components_base.nrf52-xiao-ble.yaml diff --git a/esphome/components/debug/debug_zephyr.cpp b/esphome/components/debug/debug_zephyr.cpp index 9a361b158f..231b39a711 100644 --- a/esphome/components/debug/debug_zephyr.cpp +++ b/esphome/components/debug/debug_zephyr.cpp @@ -25,10 +25,37 @@ static void show_reset_reason(std::string &reset_reason, bool set, const char *r reset_reason += reason; } -inline uint32_t read_mem_u32(uintptr_t addr) { +static inline uint32_t read_mem_u32(uintptr_t addr) { return *reinterpret_cast(addr); // NOLINT(performance-no-int-to-ptr) } +static inline uint8_t read_mem_u8(uintptr_t addr) { + return *reinterpret_cast(addr); // NOLINT(performance-no-int-to-ptr) +} + +// defines from https://github.com/adafruit/Adafruit_nRF52_Bootloader which prints those information +constexpr uint32_t SD_MAGIC_NUMBER = 0x51B1E5DB; +constexpr uintptr_t MBR_SIZE = 0x1000; +constexpr uintptr_t SOFTDEVICE_INFO_STRUCT_OFFSET = 0x2000; +constexpr uintptr_t SD_ID_OFFSET = SOFTDEVICE_INFO_STRUCT_OFFSET + 0x10; +constexpr uintptr_t SD_VERSION_OFFSET = SOFTDEVICE_INFO_STRUCT_OFFSET + 0x14; + +static inline bool is_sd_present() { + return read_mem_u32(SOFTDEVICE_INFO_STRUCT_OFFSET + MBR_SIZE + 4) == SD_MAGIC_NUMBER; +} +static inline uint32_t sd_id_get() { + if (read_mem_u8(MBR_SIZE + SOFTDEVICE_INFO_STRUCT_OFFSET) > (SD_ID_OFFSET - SOFTDEVICE_INFO_STRUCT_OFFSET)) { + return read_mem_u32(MBR_SIZE + SD_ID_OFFSET); + } + return 0; +} +static inline uint32_t sd_version_get() { + if (read_mem_u8(MBR_SIZE + SOFTDEVICE_INFO_STRUCT_OFFSET) > (SD_VERSION_OFFSET - SOFTDEVICE_INFO_STRUCT_OFFSET)) { + return read_mem_u32(MBR_SIZE + SD_VERSION_OFFSET); + } + return 0; +} + std::string DebugComponent::get_reset_reason_() { uint32_t cause; auto ret = hwinfo_get_reset_cause(&cause); @@ -271,6 +298,29 @@ void DebugComponent::get_device_info_(std::string &device_info) { NRF_UICR->NRFFW[0]); ESP_LOGD(TAG, "MBR param page addr 0x%08x, UICR param page addr 0x%08x", read_mem_u32(MBR_PARAM_PAGE_ADDR), NRF_UICR->NRFFW[1]); + if (is_sd_present()) { + uint32_t const sd_id = sd_id_get(); + uint32_t const sd_version = sd_version_get(); + + uint32_t ver[3]; + ver[0] = sd_version / 1000000; + ver[1] = (sd_version - ver[0] * 1000000) / 1000; + ver[2] = (sd_version - ver[0] * 1000000 - ver[1] * 1000); + + ESP_LOGD(TAG, "SoftDevice: S%u %u.%u.%u", sd_id, ver[0], ver[1], ver[2]); +#ifdef USE_SOFTDEVICE_ID +#ifdef USE_SOFTDEVICE_VERSION + if (USE_SOFTDEVICE_ID != sd_id || USE_SOFTDEVICE_VERSION != ver[0]) { + ESP_LOGE(TAG, "Built for SoftDevice S%u %u.x.y. It may crash due to mismatch of bootloader version.", + USE_SOFTDEVICE_ID, USE_SOFTDEVICE_VERSION); + } +#else + if (USE_SOFTDEVICE_ID != sd_id) { + ESP_LOGE(TAG, "Built for SoftDevice S%u. It may crash due to mismatch of bootloader version.", USE_SOFTDEVICE_ID); + } +#endif +#endif + } #endif } diff --git a/esphome/components/nrf52/__init__.py b/esphome/components/nrf52/__init__.py index 84e505a90a..727607933d 100644 --- a/esphome/components/nrf52/__init__.py +++ b/esphome/components/nrf52/__init__.py @@ -1,5 +1,6 @@ from __future__ import annotations +import logging from pathlib import Path from esphome import pins @@ -48,6 +49,7 @@ from .gpio import nrf52_pin_to_code # noqa CODEOWNERS = ["@tomaszduda23"] AUTO_LOAD = ["zephyr"] IS_TARGET_PLATFORM = True +_LOGGER = logging.getLogger(__name__) def set_core_data(config: ConfigType) -> ConfigType: @@ -127,6 +129,10 @@ def _validate_mcumgr(config): def _final_validate(config): if CONF_DFU in config: _validate_mcumgr(config) + if config[KEY_BOOTLOADER] == BOOTLOADER_ADAFRUIT: + _LOGGER.warning( + "Selected generic Adafruit bootloader. The board might crash. Consider settings `bootloader:`" + ) FINAL_VALIDATE_SCHEMA = _final_validate @@ -157,6 +163,13 @@ async def to_code(config: ConfigType) -> None: if config[KEY_BOOTLOADER] == BOOTLOADER_MCUBOOT: cg.add_define("USE_BOOTLOADER_MCUBOOT") else: + if "_sd" in config[KEY_BOOTLOADER]: + bootloader = config[KEY_BOOTLOADER].split("_") + sd_id = bootloader[2][2:] + cg.add_define("USE_SOFTDEVICE_ID", int(sd_id)) + if (len(bootloader)) > 3: + sd_version = bootloader[3][1:] + cg.add_define("USE_SOFTDEVICE_VERSION", int(sd_version)) # make sure that firmware.zip is created # for Adafruit_nRF52_Bootloader cg.add_platformio_option("board_upload.protocol", "nrfutil") diff --git a/esphome/components/nrf52/boards.py b/esphome/components/nrf52/boards.py index 8e5fb2a23d..6064fe844a 100644 --- a/esphome/components/nrf52/boards.py +++ b/esphome/components/nrf52/boards.py @@ -11,10 +11,18 @@ from .const import ( BOARDS_ZEPHYR = { "adafruit_itsybitsy_nrf52840": { KEY_BOOTLOADER: [ + BOOTLOADER_ADAFRUIT_NRF52_SD140_V6, + BOOTLOADER_ADAFRUIT, + BOOTLOADER_ADAFRUIT_NRF52_SD132, + BOOTLOADER_ADAFRUIT_NRF52_SD140_V7, + ] + }, + "xiao_ble": { + KEY_BOOTLOADER: [ + BOOTLOADER_ADAFRUIT_NRF52_SD140_V7, BOOTLOADER_ADAFRUIT, BOOTLOADER_ADAFRUIT_NRF52_SD132, BOOTLOADER_ADAFRUIT_NRF52_SD140_V6, - BOOTLOADER_ADAFRUIT_NRF52_SD140_V7, ] }, } diff --git a/esphome/components/zephyr/__init__.py b/esphome/components/zephyr/__init__.py index ff4644163e..634c99876b 100644 --- a/esphome/components/zephyr/__init__.py +++ b/esphome/components/zephyr/__init__.py @@ -222,18 +222,25 @@ def copy_files(): ] in ["xiao_ble"]: fake_board_manifest = """ { -"frameworks": [ - "zephyr" -], -"name": "esphome nrf52", -"upload": { - "maximum_ram_size": 248832, - "maximum_size": 815104 -}, -"url": "https://esphome.io/", -"vendor": "esphome" + "frameworks": [ + "zephyr" + ], + "name": "esphome nrf52", + "upload": { + "maximum_ram_size": 248832, + "maximum_size": 815104, + "speed": 115200 + }, + "url": "https://esphome.io/", + "vendor": "esphome", + "build": { + "softdevice": { + "sd_fwid": "0x00B6" + } + } } """ + write_file_if_changed( CORE.relative_build_path(f"boards/{zephyr_data()[KEY_BOARD]}.json"), fake_board_manifest, diff --git a/tests/components/nrf52/test.nrf52-xiao-ble.yaml b/tests/components/nrf52/test.nrf52-xiao-ble.yaml new file mode 100644 index 0000000000..3fe80209b6 --- /dev/null +++ b/tests/components/nrf52/test.nrf52-xiao-ble.yaml @@ -0,0 +1,7 @@ +nrf52: + dfu: + reset_pin: + number: 14 + inverted: true + mode: + output: true diff --git a/tests/test_build_components/build_components_base.nrf52-xiao-ble.yaml b/tests/test_build_components/build_components_base.nrf52-xiao-ble.yaml new file mode 100644 index 0000000000..2f3f91d957 --- /dev/null +++ b/tests/test_build_components/build_components_base.nrf52-xiao-ble.yaml @@ -0,0 +1,15 @@ +esphome: + name: componenttestnrf52 + friendly_name: $component_name + +nrf52: + board: xiao_ble + +logger: + level: VERY_VERBOSE + +packages: + component_under_test: !include + file: $component_test_file + vars: + component_test_file: $component_test_file From 6d09e68b2e9d7c13940962427f69128d62b92471 Mon Sep 17 00:00:00 2001 From: B48D81EFCC <111175947+B48D81EFCC@users.noreply.github.com> Date: Fri, 17 Oct 2025 17:11:51 +0200 Subject: [PATCH 6/7] [bh1900nux] Add bh1900nux temperature Sensor (#8631) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: Andreas Riehl Co-authored-by: Jonathan Swoboda <154711427+swoboda1337@users.noreply.github.com> --- CODEOWNERS | 1 + esphome/components/bh1900nux/__init__.py | 0 esphome/components/bh1900nux/bh1900nux.cpp | 54 +++++++++++++++++++ esphome/components/bh1900nux/bh1900nux.h | 18 +++++++ esphome/components/bh1900nux/sensor.py | 34 ++++++++++++ tests/components/bh1900nux/common.yaml | 6 +++ .../bh1900nux/test.esp32-c3-idf.yaml | 4 ++ .../components/bh1900nux/test.esp32-idf.yaml | 4 ++ .../bh1900nux/test.esp8266-ard.yaml | 4 ++ .../components/bh1900nux/test.rp2040-ard.yaml | 4 ++ 10 files changed, 129 insertions(+) create mode 100644 esphome/components/bh1900nux/__init__.py create mode 100644 esphome/components/bh1900nux/bh1900nux.cpp create mode 100644 esphome/components/bh1900nux/bh1900nux.h create mode 100644 esphome/components/bh1900nux/sensor.py create mode 100644 tests/components/bh1900nux/common.yaml create mode 100644 tests/components/bh1900nux/test.esp32-c3-idf.yaml create mode 100644 tests/components/bh1900nux/test.esp32-idf.yaml create mode 100644 tests/components/bh1900nux/test.esp8266-ard.yaml create mode 100644 tests/components/bh1900nux/test.rp2040-ard.yaml diff --git a/CODEOWNERS b/CODEOWNERS index 03ea5d0e47..b5cefa1e0c 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -62,6 +62,7 @@ esphome/components/bedjet/fan/* @jhansche esphome/components/bedjet/sensor/* @javawizard @jhansche esphome/components/beken_spi_led_strip/* @Mat931 esphome/components/bh1750/* @OttoWinter +esphome/components/bh1900nux/* @B48D81EFCC esphome/components/binary_sensor/* @esphome/core esphome/components/bk72xx/* @kuba2k2 esphome/components/bl0906/* @athom-tech @jesserockz @tarontop diff --git a/esphome/components/bh1900nux/__init__.py b/esphome/components/bh1900nux/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/esphome/components/bh1900nux/bh1900nux.cpp b/esphome/components/bh1900nux/bh1900nux.cpp new file mode 100644 index 0000000000..96a06adaa0 --- /dev/null +++ b/esphome/components/bh1900nux/bh1900nux.cpp @@ -0,0 +1,54 @@ +#include "esphome/core/log.h" +#include "bh1900nux.h" + +namespace esphome { +namespace bh1900nux { + +static const char *const TAG = "bh1900nux.sensor"; + +// I2C Registers +static const uint8_t TEMPERATURE_REG = 0x00; +static const uint8_t CONFIG_REG = 0x01; // Not used and supported yet +static const uint8_t TEMPERATURE_LOW_REG = 0x02; // Not used and supported yet +static const uint8_t TEMPERATURE_HIGH_REG = 0x03; // Not used and supported yet +static const uint8_t SOFT_RESET_REG = 0x04; + +// I2C Command payloads +static const uint8_t SOFT_RESET_PAYLOAD = 0x01; // Soft Reset value + +static const float SENSOR_RESOLUTION = 0.0625f; // Sensor resolution per bit in degrees celsius + +void BH1900NUXSensor::setup() { + // Initialize I2C device + i2c::ErrorCode result_code = + this->write_register(SOFT_RESET_REG, &SOFT_RESET_PAYLOAD, 1); // Software Reset to check communication + if (result_code != i2c::ERROR_OK) { + this->mark_failed(ESP_LOG_MSG_COMM_FAIL); + return; + } +} + +void BH1900NUXSensor::update() { + uint8_t temperature_raw[2]; + if (this->read_register(TEMPERATURE_REG, temperature_raw, 2) != i2c::ERROR_OK) { + ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL); + return; + } + + // Combined raw value, unsigned and unaligned 16 bit + // Temperature is represented in just 12 bits, shift needed + int16_t raw_temperature_register_value = encode_uint16(temperature_raw[0], temperature_raw[1]); + raw_temperature_register_value >>= 4; + float temperature_value = raw_temperature_register_value * SENSOR_RESOLUTION; // Apply sensor resolution + + this->publish_state(temperature_value); +} + +void BH1900NUXSensor::dump_config() { + LOG_SENSOR("", "BH1900NUX", this); + LOG_I2C_DEVICE(this); + LOG_UPDATE_INTERVAL(this); +} + +} // namespace bh1900nux +} // namespace esphome diff --git a/esphome/components/bh1900nux/bh1900nux.h b/esphome/components/bh1900nux/bh1900nux.h new file mode 100644 index 0000000000..fd7f8848d6 --- /dev/null +++ b/esphome/components/bh1900nux/bh1900nux.h @@ -0,0 +1,18 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/components/sensor/sensor.h" +#include "esphome/components/i2c/i2c.h" + +namespace esphome { +namespace bh1900nux { + +class BH1900NUXSensor : public sensor::Sensor, public PollingComponent, public i2c::I2CDevice { + public: + void setup() override; + void update() override; + void dump_config() override; +}; + +} // namespace bh1900nux +} // namespace esphome diff --git a/esphome/components/bh1900nux/sensor.py b/esphome/components/bh1900nux/sensor.py new file mode 100644 index 0000000000..5e1c0395af --- /dev/null +++ b/esphome/components/bh1900nux/sensor.py @@ -0,0 +1,34 @@ +import esphome.codegen as cg +from esphome.components import i2c, sensor +import esphome.config_validation as cv +from esphome.const import ( + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, + UNIT_CELSIUS, +) + +DEPENDENCIES = ["i2c"] +CODEOWNERS = ["@B48D81EFCC"] + +sensor_ns = cg.esphome_ns.namespace("bh1900nux") +BH1900NUXSensor = sensor_ns.class_( + "BH1900NUXSensor", cg.PollingComponent, i2c.I2CDevice +) + +CONFIG_SCHEMA = ( + sensor.sensor_schema( + BH1900NUXSensor, + accuracy_decimals=1, + unit_of_measurement=UNIT_CELSIUS, + device_class=DEVICE_CLASS_TEMPERATURE, + state_class=STATE_CLASS_MEASUREMENT, + ) + .extend(cv.polling_component_schema("60s")) + .extend(i2c.i2c_device_schema(0x48)) +) + + +async def to_code(config): + var = await sensor.new_sensor(config) + await cg.register_component(var, config) + await i2c.register_i2c_device(var, config) diff --git a/tests/components/bh1900nux/common.yaml b/tests/components/bh1900nux/common.yaml new file mode 100644 index 0000000000..3438418702 --- /dev/null +++ b/tests/components/bh1900nux/common.yaml @@ -0,0 +1,6 @@ +sensor: + - platform: bh1900nux + i2c_id: i2c_bus + name: Temperature Living Room + address: 0x48 + update_interval: 30s diff --git a/tests/components/bh1900nux/test.esp32-c3-idf.yaml b/tests/components/bh1900nux/test.esp32-c3-idf.yaml new file mode 100644 index 0000000000..9990d96d29 --- /dev/null +++ b/tests/components/bh1900nux/test.esp32-c3-idf.yaml @@ -0,0 +1,4 @@ +packages: + i2c: !include ../../test_build_components/common/i2c/esp32-c3-idf.yaml + +<<: !include common.yaml diff --git a/tests/components/bh1900nux/test.esp32-idf.yaml b/tests/components/bh1900nux/test.esp32-idf.yaml new file mode 100644 index 0000000000..b47e39c389 --- /dev/null +++ b/tests/components/bh1900nux/test.esp32-idf.yaml @@ -0,0 +1,4 @@ +packages: + i2c: !include ../../test_build_components/common/i2c/esp32-idf.yaml + +<<: !include common.yaml diff --git a/tests/components/bh1900nux/test.esp8266-ard.yaml b/tests/components/bh1900nux/test.esp8266-ard.yaml new file mode 100644 index 0000000000..4a98b9388a --- /dev/null +++ b/tests/components/bh1900nux/test.esp8266-ard.yaml @@ -0,0 +1,4 @@ +packages: + i2c: !include ../../test_build_components/common/i2c/esp8266-ard.yaml + +<<: !include common.yaml diff --git a/tests/components/bh1900nux/test.rp2040-ard.yaml b/tests/components/bh1900nux/test.rp2040-ard.yaml new file mode 100644 index 0000000000..319a7c71a6 --- /dev/null +++ b/tests/components/bh1900nux/test.rp2040-ard.yaml @@ -0,0 +1,4 @@ +packages: + i2c: !include ../../test_build_components/common/i2c/rp2040-ard.yaml + +<<: !include common.yaml From 6722e5c8d83912c4b388c538d63ed7fc458b02b1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 17 Oct 2025 07:43:24 -1000 Subject: [PATCH 7/7] [wifi] Optimize WiFi scanning to reduce copies and heap allocations --- esphome/components/wifi/wifi_component.cpp | 36 ++++++++++++---------- esphome/components/wifi/wifi_component.h | 2 +- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 5aa2a03a14..9fb0d8a122 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -607,10 +607,12 @@ void WiFiComponent::check_scanning_finished() { for (auto &ap : this->sta_) { if (res.matches(ap)) { res.set_matches(true); - if (!this->has_sta_priority(res.get_bssid())) { - this->set_sta_priority(res.get_bssid(), ap.get_priority()); + // Cache priority lookup - do single search instead of 2 separate searches + const bssid_t &bssid = res.get_bssid(); + if (!this->has_sta_priority(bssid)) { + this->set_sta_priority(bssid, ap.get_priority()); } - res.set_priority(this->get_sta_priority(res.get_bssid())); + res.set_priority(this->get_sta_priority(bssid)); break; } } @@ -629,8 +631,9 @@ void WiFiComponent::check_scanning_finished() { return; } - WiFiAP connect_params; - WiFiScanResult scan_res = this->scan_result_[0]; + // Build connection params directly into selected_ap_ to avoid extra copy + const WiFiScanResult &scan_res = this->scan_result_[0]; + WiFiAP &selected = this->selected_ap_; for (auto &config : this->sta_) { // search for matching STA config, at least one will match (from checks before) if (!scan_res.matches(config)) { @@ -639,37 +642,36 @@ void WiFiComponent::check_scanning_finished() { if (config.get_hidden()) { // selected network is hidden, we use the data from the config - connect_params.set_hidden(true); - connect_params.set_ssid(config.get_ssid()); + selected.set_hidden(true); + selected.set_ssid(config.get_ssid()); // don't set BSSID and channel, there might be multiple hidden networks // but we can't know which one is the correct one. Rely on probe-req with just SSID. } else { // selected network is visible, we use the data from the scan // limit the connect params to only connect to exactly this network // (network selection is done during scan phase). - connect_params.set_hidden(false); - connect_params.set_ssid(scan_res.get_ssid()); - connect_params.set_channel(scan_res.get_channel()); - connect_params.set_bssid(scan_res.get_bssid()); + selected.set_hidden(false); + selected.set_ssid(scan_res.get_ssid()); + selected.set_channel(scan_res.get_channel()); + selected.set_bssid(scan_res.get_bssid()); } // copy manual IP (if set) - connect_params.set_manual_ip(config.get_manual_ip()); + selected.set_manual_ip(config.get_manual_ip()); #ifdef USE_WIFI_WPA2_EAP // copy EAP parameters (if set) - connect_params.set_eap(config.get_eap()); + selected.set_eap(config.get_eap()); #endif // copy password (if set) - connect_params.set_password(config.get_password()); + selected.set_password(config.get_password()); break; } yield(); - this->selected_ap_ = connect_params; - this->start_connecting(connect_params, false); + this->start_connecting(this->selected_ap_, false); } void WiFiComponent::dump_config() { @@ -902,7 +904,7 @@ WiFiScanResult::WiFiScanResult(const bssid_t &bssid, std::string ssid, uint8_t c rssi_(rssi), with_auth_(with_auth), is_hidden_(is_hidden) {} -bool WiFiScanResult::matches(const WiFiAP &config) { +bool WiFiScanResult::matches(const WiFiAP &config) const { if (config.get_hidden()) { // User configured a hidden network, only match actually hidden networks // don't match SSID diff --git a/esphome/components/wifi/wifi_component.h b/esphome/components/wifi/wifi_component.h index 9d32071b2b..508024a235 100644 --- a/esphome/components/wifi/wifi_component.h +++ b/esphome/components/wifi/wifi_component.h @@ -170,7 +170,7 @@ class WiFiScanResult { public: WiFiScanResult(const bssid_t &bssid, std::string ssid, uint8_t channel, int8_t rssi, bool with_auth, bool is_hidden); - bool matches(const WiFiAP &config); + bool matches(const WiFiAP &config) const; bool get_matches() const; void set_matches(bool matches);