Compare commits

..
Author SHA1 Message Date
J. Nick Koston 55f15f45ed [whirlpool] Move mode and fan codes to an IRCode table 2026-11-14 14:26:56 -06:00
J. Nick Koston 0f691dcca3 Merge branch 'climate-ir-code-table' into climate-ir-code-table-migrate 2026-11-14 14:25:38 -06:00
J. Nick Koston 2d7a8ef338 Merge remote-tracking branch 'origin/dev' into climate-ir-code-table 2026-11-14 14:25:37 -06:00
Rodrigoah 904f0df920 [whirlpool] Add the Hitachi HCRA31NEWH model (#22704) 2026-11-14 14:23:51 -06:00
Chris 7128d22f07 [ethernet] Notify IP state listeners from the main loop (#22657) 2026-11-14 13:22:11 -06:00
Miguel Angel Nubla 825e2c3dd0 [wifi] Leave the AP before restarting on ESP32 (#22715) 2026-11-14 18:05:42 +00:00
J. Nick Koston db4c9ba24e [climate_ir] Declare from_ir_code where it is used and note the ON bit in the daikin tables 2026-11-14 10:07:57 -06:00
J. Nick Koston acf4da7bc8 [climate_ir] Use the explicit fallbacks in the converted components 2026-11-14 09:50:10 -06:00
J. Nick Koston b6dbc9f2f0 Merge branch 'climate-ir-code-table' into climate-ir-code-table-migrate 2026-11-14 09:48:30 -06:00
J. Nick Koston 170a3b7374 [climate_ir] Take explicit fallbacks and keep the lookups free of per-size copies 2026-11-14 09:48:23 -06:00
J. Nick Koston d53db6af87 [zhlt01] Use the climate_ir code table for modes and fans 2026-11-14 09:38:43 -06:00
J. Nick Koston 2e5e080df1 [whynter] Use the climate_ir code table for modes and fans 2026-11-14 09:38:42 -06:00
J. Nick Koston 0221318335 [tcl112] Use the climate_ir code table for modes and fans 2026-11-14 09:38:41 -06:00
J. Nick Koston f2468415e4 [noblex] Use the climate_ir code table for modes and fans 2026-11-14 09:38:41 -06:00
J. Nick Koston b83255590a [hitachi_ac424] Use the climate_ir code table for modes and fans 2026-11-14 09:38:40 -06:00
J. Nick Koston f738157382 [hitachi_ac344] Use the climate_ir code table for modes and fans 2026-11-14 09:38:39 -06:00
J. Nick Koston 85e7a22ee8 [delonghi] Use the climate_ir code table for modes and fans 2026-11-14 09:38:38 -06:00
J. Nick Koston 101ee872cb [daikin_brc] Use the climate_ir code table for modes and fans 2026-11-14 09:38:38 -06:00
J. Nick Koston 3b546cc5fa [daikin_arc] Use the climate_ir code table for modes and fans 2026-11-14 09:38:37 -06:00
J. Nick Koston 8acd9df0cf [climate_ir_lg] Use the climate_ir code table for modes and fans 2026-11-14 09:38:36 -06:00
J. Nick Koston 6f3270f318 [ballu] Use the climate_ir code table for modes and fans 2026-11-14 09:38:35 -06:00
J. Nick Koston 822c0f6e49 [airton] Use the climate_ir code table for modes and fans 2026-11-14 09:38:34 -06:00
J. Nick Koston fd76602635 [daikin] Use the climate_ir code table for modes and fans 2026-11-14 09:24:07 -06:00
J. Nick Koston 0eee107373 [climate_ir] Check that from_ir_code targets a one-byte type 2026-11-14 09:19:03 -06:00
J. Nick Koston 2347e0dcef [climate_ir] Add a shared PROGMEM code table for mode and fan mappings 2026-11-14 09:11:16 -06:00
57 changed files with 1035 additions and 1413 deletions

No files matched your search

+1 -1
View File
@@ -741,7 +741,7 @@ esphome/components/web_server_idf/* @dentra
esphome/components/weikai/* @DrCoolZic
esphome/components/weikai_i2c/* @DrCoolZic
esphome/components/weikai_spi/* @DrCoolZic
esphome/components/whirlpool/* @arpena @glmnet
esphome/components/whirlpool/* @arpena @glmnet @Rodrigoah
esphome/components/whynter/* @aeonsablaze
esphome/components/wiegand/* @ssieb
esphome/components/wireguard/* @droscy @lhoracek @thomas0bernard
+32 -82
View File
@@ -9,6 +9,27 @@ ESPHOME_LOG_TAG(TAG, "airton.climate");
// Distinct from the climate's own RESTORE_STATE_VERSION, which shares the entity's preference key
static constexpr uint32_t RESTORE_SETTINGS_VERSION = 0x2E86A1F3UL;
using climate_ir::find_ir_code;
using climate_ir::from_ir_code;
using climate_ir::IRCode;
using climate_ir::to_ir_code;
static constexpr uint8_t AIRTON_POWER_ON = 0b1000;
// A received code that is not listed is ignored
static constexpr IRCode MODES[] PROGMEM = {
{climate::CLIMATE_MODE_COOL, AIRTON_POWER_ON | AIRTON_MODE_COOL},
{climate::CLIMATE_MODE_DRY, AIRTON_POWER_ON | AIRTON_MODE_DRY},
{climate::CLIMATE_MODE_HEAT, AIRTON_POWER_ON | AIRTON_MODE_HEAT},
{climate::CLIMATE_MODE_HEAT_COOL, AIRTON_POWER_ON | AIRTON_MODE_AUTO},
{climate::CLIMATE_MODE_FAN_ONLY, AIRTON_POWER_ON | AIRTON_MODE_FAN},
};
// Fan speeds 2 and 4 are only received
static constexpr IRCode FANS[] PROGMEM = {
{climate::CLIMATE_FAN_AUTO, AIRTON_FAN_AUTO}, {climate::CLIMATE_FAN_LOW, AIRTON_FAN_1},
{climate::CLIMATE_FAN_MEDIUM, AIRTON_FAN_3}, {climate::CLIMATE_FAN_HIGH, AIRTON_FAN_5},
{climate::CLIMATE_FAN_LOW, AIRTON_FAN_2}, {climate::CLIMATE_FAN_HIGH, AIRTON_FAN_4},
};
// From IRutils.h of IRremoteESP8266 library
static uint8_t checksum(const uint8_t *frame) {
return static_cast<uint8_t>(0x7F - sum_bytes(frame, AIRTON_STATE_FRAME_SIZE - 1)) ^ 0x2C;
@@ -114,7 +135,9 @@ void AirtonClimate::transmit_state() {
uint8_t remote_state[AIRTON_STATE_FRAME_SIZE] = {
AIRTON_HEADER_BYTE0,
AIRTON_HEADER_BYTE1,
static_cast<uint8_t>(this->operation_mode_() | (this->fan_speed_() << 4)),
static_cast<uint8_t>(
this->operation_mode_() |
(to_ir_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO), AIRTON_FAN_AUTO) << 4)),
this->temperature_(),
static_cast<uint8_t>(this->settings_.vertical_direction),
this->operation_settings_(),
@@ -139,48 +162,9 @@ void AirtonClimate::transmit_state() {
}
uint8_t AirtonClimate::operation_mode_() {
uint8_t operating_mode = 0b1000; // First bit is for power state
switch (this->mode) {
case climate::CLIMATE_MODE_COOL:
operating_mode |= AIRTON_MODE_COOL;
break;
case climate::CLIMATE_MODE_DRY:
operating_mode |= AIRTON_MODE_DRY;
break;
case climate::CLIMATE_MODE_HEAT:
operating_mode |= AIRTON_MODE_HEAT;
break;
case climate::CLIMATE_MODE_HEAT_COOL:
operating_mode |= AIRTON_MODE_AUTO;
break;
case climate::CLIMATE_MODE_FAN_ONLY:
operating_mode |= AIRTON_MODE_FAN;
break;
case climate::CLIMATE_MODE_OFF:
default:
operating_mode = 0b0111 & this->previous_mode_; // Set previous mode with power state bit off
}
this->previous_mode_ = operating_mode;
return operating_mode;
}
uint8_t AirtonClimate::fan_speed_() {
uint8_t fan_speed;
switch (this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO)) {
case climate::CLIMATE_FAN_LOW:
fan_speed = AIRTON_FAN_1;
break;
case climate::CLIMATE_FAN_MEDIUM:
fan_speed = AIRTON_FAN_3;
break;
case climate::CLIMATE_FAN_HIGH:
fan_speed = AIRTON_FAN_5;
break;
case climate::CLIMATE_FAN_AUTO:
default:
fan_speed = AIRTON_FAN_AUTO;
}
return fan_speed;
// Off, or a mode not listed, repeats the previous mode with the power bit clear
this->previous_mode_ = to_ir_code(MODES, this->mode, this->previous_mode_ & 0b0111);
return this->previous_mode_;
}
uint8_t AirtonClimate::temperature_() {
@@ -217,47 +201,13 @@ void AirtonClimate::parse_state_frame_(const uint8_t *frame) {
uint8_t mode = frame[2];
// A later "off" repeats the mode the remote last sent, as the remote itself does
this->previous_mode_ = mode & 0b00001111;
if (mode & 0b00001000) { // The power bit
switch (mode & 0b00000111) {
case AIRTON_MODE_COOL:
this->mode = climate::CLIMATE_MODE_COOL;
break;
case AIRTON_MODE_DRY:
this->mode = climate::CLIMATE_MODE_DRY;
break;
case AIRTON_MODE_HEAT:
this->mode = climate::CLIMATE_MODE_HEAT;
break;
case AIRTON_MODE_AUTO:
this->mode = climate::CLIMATE_MODE_HEAT_COOL;
break;
case AIRTON_MODE_FAN:
this->mode = climate::CLIMATE_MODE_FAN_ONLY;
break;
}
} else {
if (!(mode & AIRTON_POWER_ON)) {
this->mode = climate::CLIMATE_MODE_OFF;
} else {
this->mode = from_ir_code(MODES, mode & 0b00001111, this->mode);
}
uint8_t fan_mode = (frame[2] & 0b01110000) >> 4;
switch (fan_mode) {
case AIRTON_FAN_1:
case AIRTON_FAN_2:
this->fan_mode = climate::CLIMATE_FAN_LOW;
break;
case AIRTON_FAN_3:
this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
break;
case AIRTON_FAN_4:
case AIRTON_FAN_5:
this->fan_mode = climate::CLIMATE_FAN_HIGH;
break;
case AIRTON_FAN_AUTO:
this->fan_mode = climate::CLIMATE_FAN_AUTO;
break;
default:
break;
}
if (const auto entry = find_ir_code(FANS, (frame[2] & 0b01110000) >> 4, true))
this->fan_mode = static_cast<climate::ClimateFanMode>(entry->value);
this->target_temperature = (frame[3] & 0b00001111) + AIRTON_TEMP_MIN;
-1
View File
@@ -117,7 +117,6 @@ class AirtonClimate : public climate_ir::ClimateIR {
void transmit_state() override;
uint8_t operation_mode_();
uint8_t fan_speed_();
uint8_t temperature_();
uint8_t operation_settings_();
+21 -74
View File
@@ -31,6 +31,23 @@ const uint8_t BALLU_SWING_VER = 0x07;
const uint8_t BALLU_SWING_HOR = 0xe0;
const uint8_t BALLU_POWER = 0x20;
using climate_ir::from_ir_code;
using climate_ir::IRCode;
using climate_ir::to_ir_code;
// A fan mode not listed sends no fan bits and a received mode not listed is ignored
static constexpr IRCode MODES[] PROGMEM = {
{climate::CLIMATE_MODE_AUTO, BALLU_AUTO}, {climate::CLIMATE_MODE_HEAT, BALLU_HEAT},
{climate::CLIMATE_MODE_COOL, BALLU_COOL}, {climate::CLIMATE_MODE_DRY, BALLU_DRY},
{climate::CLIMATE_MODE_FAN_ONLY, BALLU_FAN},
};
static constexpr IRCode FANS[] PROGMEM = {
{climate::CLIMATE_FAN_AUTO, BALLU_FAN_AUTO},
{climate::CLIMATE_FAN_HIGH, BALLU_FAN_HIGH},
{climate::CLIMATE_FAN_MEDIUM, BALLU_FAN_MED},
{climate::CLIMATE_FAN_LOW, BALLU_FAN_LOW},
};
void BalluClimate::transmit_state() {
uint8_t remote_state[BALLU_STATE_LENGTH] = {0};
@@ -46,46 +63,8 @@ void BalluClimate::transmit_state() {
remote_state[9] = (this->mode == climate::CLIMATE_MODE_OFF) ? 0 : BALLU_POWER;
remote_state[11] = 0x1e;
// Fan speed
switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
case climate::CLIMATE_FAN_HIGH:
remote_state[4] |= BALLU_FAN_HIGH;
break;
case climate::CLIMATE_FAN_MEDIUM:
remote_state[4] |= BALLU_FAN_MED;
break;
case climate::CLIMATE_FAN_LOW:
remote_state[4] |= BALLU_FAN_LOW;
break;
case climate::CLIMATE_FAN_AUTO:
remote_state[4] |= BALLU_FAN_AUTO;
break;
default:
break;
}
// Mode
switch (this->mode) {
case climate::CLIMATE_MODE_AUTO:
remote_state[6] |= BALLU_AUTO;
break;
case climate::CLIMATE_MODE_HEAT:
remote_state[6] |= BALLU_HEAT;
break;
case climate::CLIMATE_MODE_COOL:
remote_state[6] |= BALLU_COOL;
break;
case climate::CLIMATE_MODE_DRY:
remote_state[6] |= BALLU_DRY;
break;
case climate::CLIMATE_MODE_FAN_ONLY:
remote_state[6] |= BALLU_FAN;
break;
case climate::CLIMATE_MODE_OFF:
remote_state[6] |= BALLU_AUTO;
default:
break;
}
remote_state[4] = to_ir_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_ON), 0);
remote_state[6] = to_ir_code(MODES, this->mode, BALLU_AUTO);
// Checksum
for (uint8_t i = 0; i < BALLU_STATE_LENGTH - 1; i++)
@@ -149,25 +128,7 @@ bool BalluClimate::on_receive(remote_base::RemoteReceiveData data) {
} else {
auto mode = remote_state[6] & 0xe0;
ESP_LOGV(TAG, "Mode: %02X", mode);
switch (mode) {
case BALLU_HEAT:
this->mode = climate::CLIMATE_MODE_HEAT;
break;
case BALLU_COOL:
this->mode = climate::CLIMATE_MODE_COOL;
break;
case BALLU_DRY:
this->mode = climate::CLIMATE_MODE_DRY;
break;
case BALLU_FAN:
this->mode = climate::CLIMATE_MODE_FAN_ONLY;
break;
case BALLU_AUTO:
this->mode = climate::CLIMATE_MODE_AUTO;
break;
default:
break;
}
this->mode = from_ir_code(MODES, mode, this->mode);
}
// Set received temp
@@ -180,21 +141,7 @@ bool BalluClimate::on_receive(remote_base::RemoteReceiveData data) {
// Set received fan speed
auto fan = remote_state[4] & 0xe0;
ESP_LOGVV(TAG, "Fan: %02X", fan);
switch (fan) {
case BALLU_FAN_HIGH:
this->fan_mode = climate::CLIMATE_FAN_HIGH;
break;
case BALLU_FAN_MED:
this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
break;
case BALLU_FAN_LOW:
this->fan_mode = climate::CLIMATE_FAN_LOW;
break;
case BALLU_FAN_AUTO:
default:
this->fan_mode = climate::CLIMATE_FAN_AUTO;
break;
}
this->fan_mode = from_ir_code(FANS, fan, climate::CLIMATE_FAN_AUTO);
// Set received swing status
ESP_LOGVV(TAG, "Swing status: %02X %02X", remote_state[1] & BALLU_SWING_VER, remote_state[2] & BALLU_SWING_HOR);
@@ -95,4 +95,19 @@ void ClimateIR::dump_config() {
YESNO(this->modes_.count(climate::CLIMATE_MODE_HEAT_COOL)));
}
int find_ir_code_index(const IRCode *table, size_t count, uint8_t key, bool match_code) {
for (size_t i = 0; i < count; i++) {
if (progmem_read_byte(match_code ? &table[i].code : &table[i].value) == key)
return static_cast<int>(i);
}
return -1;
}
uint8_t lookup_ir_code(const IRCode *table, size_t count, uint8_t key, bool match_code, uint8_t fallback) {
const int index = find_ir_code_index(table, count, key, match_code);
if (index < 0)
return fallback;
return progmem_read_byte(match_code ? &table[index].value : &table[index].code);
}
} // namespace esphome::climate_ir
@@ -6,9 +6,42 @@
#include "esphome/components/remote_base/remote_base.h"
#include "esphome/components/remote_transmitter/remote_transmitter.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/core/hal.h"
#include "esphome/core/optional.h"
namespace esphome::climate_ir {
/// One entry of a PROGMEM table that maps a climate enum value to the unit's code.
struct IRCode {
uint8_t value; // a climate enum; they are all uint8_t
uint8_t code;
};
/// Index of the entry whose value (or, with match_code, code) is key, or -1. Reads the table from PROGMEM.
int find_ir_code_index(const IRCode *table, size_t count, uint8_t key, bool match_code);
/// The entry whose value (or, with match_code, code) is key, read from PROGMEM.
template<size_t N>
ESPHOME_ALWAYS_INLINE optional<IRCode> find_ir_code(const IRCode (&table)[N], uint8_t key, bool match_code) {
const int index = find_ir_code_index(table, N, key, match_code);
if (index < 0)
return {};
return IRCode{progmem_read_byte(&table[index].value), progmem_read_byte(&table[index].code)};
}
/// The code for value (or, with match_code, the value for code), or fallback when the table has no such entry.
uint8_t lookup_ir_code(const IRCode *table, size_t count, uint8_t key, bool match_code, uint8_t fallback);
/// The code for value, or fallback when the table has no such value.
template<size_t N> uint8_t to_ir_code(const IRCode (&table)[N], uint8_t value, uint8_t fallback) {
return lookup_ir_code(table, N, value, false, fallback);
}
/// The value for code, or fallback when the table has no such code; pass the current state to keep it.
template<typename T, size_t N> T from_ir_code(const IRCode (&table)[N], uint8_t code, T fallback) {
static_assert(sizeof(T) == sizeof(uint8_t), "IRCode values are one byte");
return static_cast<T>(lookup_ir_code(table, N, code, true, static_cast<uint8_t>(fallback)));
}
/* A base for climate which works by sending (and receiving) IR codes
To send IR codes implement
@@ -112,6 +112,27 @@ const uint32_t TEMP_SHIFT = 8;
const uint16_t BITS = 28;
using climate_ir::find_ir_code;
using climate_ir::IRCode;
using climate_ir::to_ir_code;
// Mode commands sit in bits 12-15; the command that also powers the unit on clears the top one
static constexpr uint8_t MODE_SHIFT = 12;
static constexpr uint8_t MODE_NOT_POWER_ON = COMMAND_COOL >> MODE_SHIFT;
// A mode not listed is sent as off
static constexpr IRCode MODES[] PROGMEM = {
{climate::CLIMATE_MODE_COOL, COMMAND_COOL >> MODE_SHIFT},
{climate::CLIMATE_MODE_DRY, COMMAND_DRY >> MODE_SHIFT},
{climate::CLIMATE_MODE_FAN_ONLY, COMMAND_FAN_ONLY >> MODE_SHIFT},
{climate::CLIMATE_MODE_HEAT_COOL, COMMAND_AI >> MODE_SHIFT},
{climate::CLIMATE_MODE_HEAT, COMMAND_HEAT >> MODE_SHIFT},
};
// F2 and F4 are only received
static constexpr IRCode FANS[] PROGMEM = {
{climate::CLIMATE_FAN_AUTO, FAN_AUTO}, {climate::CLIMATE_FAN_HIGH, FAN_MAX}, {climate::CLIMATE_FAN_MEDIUM, FAN_MED},
{climate::CLIMATE_FAN_LOW, FAN_MIN}, {climate::CLIMATE_FAN_LOW, FAN_F2}, {climate::CLIMATE_FAN_MEDIUM, FAN_F4},
};
void LgIrClimate::transmit_state() {
uint32_t remote_state = LG_HEADER;
@@ -144,27 +165,13 @@ void LgIrClimate::transmit_state() {
this->transmit_(remote_state);
return;
} else { // Mode commands
const bool climate_is_off = (this->mode_before_ == climate::CLIMATE_MODE_OFF);
switch (this->mode) {
case climate::CLIMATE_MODE_COOL:
remote_state |= climate_is_off ? COMMAND_ON_COOL : COMMAND_COOL;
break;
case climate::CLIMATE_MODE_DRY:
remote_state |= climate_is_off ? COMMAND_ON_DRY : COMMAND_DRY;
break;
case climate::CLIMATE_MODE_FAN_ONLY:
remote_state |= climate_is_off ? COMMAND_ON_FAN_ONLY : COMMAND_FAN_ONLY;
break;
case climate::CLIMATE_MODE_HEAT_COOL:
remote_state |= climate_is_off ? COMMAND_ON_AI : COMMAND_AI;
break;
case climate::CLIMATE_MODE_HEAT:
remote_state |= climate_is_off ? COMMAND_ON_HEAT : COMMAND_HEAT;
break;
case climate::CLIMATE_MODE_OFF:
default:
remote_state |= CommandSys::HEADER_SYS;
remote_state |= CommandSys::COMMAND_OFF;
if (const auto entry = find_ir_code(MODES, this->mode, false)) {
const bool climate_is_off = (this->mode_before_ == climate::CLIMATE_MODE_OFF);
remote_state |= static_cast<uint32_t>(climate_is_off ? entry->code & ~MODE_NOT_POWER_ON : entry->code)
<< MODE_SHIFT;
} else {
remote_state |= CommandSys::HEADER_SYS;
remote_state |= CommandSys::COMMAND_OFF;
}
}
@@ -175,21 +182,7 @@ void LgIrClimate::transmit_state() {
// Set fan speed
if (this->mode !=
climate::CLIMATE_MODE_OFF) { // https://github.com/esphome/esphome/pull/10875#issuecomment-5042765948
switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
case climate::CLIMATE_FAN_HIGH:
remote_state |= FAN_MAX;
break;
case climate::CLIMATE_FAN_MEDIUM:
remote_state |= FAN_MED;
break;
case climate::CLIMATE_FAN_LOW:
remote_state |= FAN_MIN;
break;
case climate::CLIMATE_FAN_AUTO:
default:
remote_state |= FAN_AUTO;
break;
}
remote_state |= to_ir_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO), FAN_AUTO);
}
uint8_t temp;
@@ -294,52 +287,25 @@ bool LgIrClimate::on_receive(remote_base::RemoteReceiveData data) {
}
this->publish_state();
return true;
// Following commands also contain fan speed and temperature, so no 'return' in these cases
case COMMAND_DRY:
case COMMAND_ON_DRY:
this->mode = climate::CLIMATE_MODE_DRY;
default: {
// Mode commands also contain fan speed and temperature, so no 'return' here
const uint8_t command = (remote_state & COMMAND_HEADER_MASK) >> MODE_SHIFT;
const auto mode = find_ir_code(MODES, command | MODE_NOT_POWER_ON, true);
if (!mode.has_value()) {
ESP_LOGD(TAG, "Got unknown command! Ignoring!");
return false;
}
this->mode = static_cast<climate::ClimateMode>(mode->value);
break;
case COMMAND_FAN_ONLY:
case COMMAND_ON_FAN_ONLY:
this->mode = climate::CLIMATE_MODE_FAN_ONLY;
break;
case COMMAND_AI:
case COMMAND_ON_AI:
this->mode = climate::CLIMATE_MODE_HEAT_COOL;
break;
case COMMAND_HEAT:
case COMMAND_ON_HEAT:
this->mode = climate::CLIMATE_MODE_HEAT;
break;
case COMMAND_COOL:
case COMMAND_ON_COOL:
this->mode = climate::CLIMATE_MODE_COOL;
break;
default:
ESP_LOGD(TAG, "Got unknown command! Ignoring!");
return false;
}
}
// Decode fan speed
switch (remote_state & FAN_SPEED_MASK) {
case FAN_AUTO:
this->fan_mode = climate::CLIMATE_FAN_AUTO;
break;
case FAN_MIN:
case FAN_F2:
this->fan_mode = climate::CLIMATE_FAN_LOW;
break;
case FAN_MED:
case FAN_F4:
this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
break;
case FAN_MAX:
this->fan_mode = climate::CLIMATE_FAN_HIGH;
break;
default:
ESP_LOGD(TAG, "Got unknown fan speed! Ignoring!");
return false;
const auto fan = find_ir_code(FANS, remote_state & FAN_SPEED_MASK, true);
if (!fan.has_value()) {
ESP_LOGD(TAG, "Got unknown fan speed! Ignoring!");
return false;
}
this->fan_mode = static_cast<climate::ClimateFanMode>(fan->value);
// Keep previous behavior
if (this->mode == climate::CLIMATE_MODE_HEAT_COOL && !(this->advanced_commands_support_)) {
+30 -87
View File
@@ -17,6 +17,27 @@ static constexpr uint8_t DAIKIN_FRAME_TYPE_INDEX = 4;
static constexpr uint8_t DAIKIN_FRAME_HEADER_SIZE = DAIKIN_FRAME_TYPE_INDEX + 1;
static constexpr uint8_t DAIKIN_STATE_FRAME_OFFSET = 2 * DAIKIN_COMFORT_FRAME_SIZE;
using climate_ir::find_ir_code;
using climate_ir::IRCode;
using climate_ir::to_ir_code;
// Codes include the ON bit so the OFF entry never matches a running unit; an unlisted received code is ignored
static constexpr IRCode MODES[] PROGMEM = {
{climate::CLIMATE_MODE_OFF, DAIKIN_MODE_OFF},
{climate::CLIMATE_MODE_COOL, DAIKIN_MODE_ON | DAIKIN_MODE_COOL},
{climate::CLIMATE_MODE_DRY, DAIKIN_MODE_ON | DAIKIN_MODE_DRY},
{climate::CLIMATE_MODE_HEAT, DAIKIN_MODE_ON | DAIKIN_MODE_HEAT},
{climate::CLIMATE_MODE_HEAT_COOL, DAIKIN_MODE_ON | DAIKIN_MODE_AUTO},
{climate::CLIMATE_MODE_FAN_ONLY, DAIKIN_MODE_ON | DAIKIN_MODE_FAN},
};
// Fan speeds 2 and 4 are only received
static constexpr IRCode FANS[] PROGMEM = {
{climate::CLIMATE_FAN_AUTO, DAIKIN_FAN_AUTO}, {climate::CLIMATE_FAN_QUIET, DAIKIN_FAN_SILENT},
{climate::CLIMATE_FAN_LOW, DAIKIN_FAN_1}, {climate::CLIMATE_FAN_MEDIUM, DAIKIN_FAN_3},
{climate::CLIMATE_FAN_HIGH, DAIKIN_FAN_5}, {climate::CLIMATE_FAN_LOW, DAIKIN_FAN_2},
{climate::CLIMATE_FAN_HIGH, DAIKIN_FAN_4},
};
void DaikinClimate::setup() {
climate_ir::ClimateIR::setup();
// A boost preset restored from flash gets its timer too
@@ -35,7 +56,7 @@ void DaikinClimate::transmit_state() {
0x00, 0x00, 0x00, 0x06, 0x60, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00};
this->set_presets_(remote_state);
remote_state[21] = this->operation_mode_();
remote_state[21] = to_ir_code(MODES, this->mode, DAIKIN_MODE_OFF);
remote_state[22] = this->temperature_();
put_be16(remote_state + 24, this->fan_speed_());
@@ -67,52 +88,8 @@ void DaikinClimate::transmit_state() {
transmit.perform();
}
uint8_t DaikinClimate::operation_mode_() const {
uint8_t operating_mode = DAIKIN_MODE_ON;
switch (this->mode) {
case climate::CLIMATE_MODE_COOL:
operating_mode |= DAIKIN_MODE_COOL;
break;
case climate::CLIMATE_MODE_DRY:
operating_mode |= DAIKIN_MODE_DRY;
break;
case climate::CLIMATE_MODE_HEAT:
operating_mode |= DAIKIN_MODE_HEAT;
break;
case climate::CLIMATE_MODE_HEAT_COOL:
operating_mode |= DAIKIN_MODE_AUTO;
break;
case climate::CLIMATE_MODE_FAN_ONLY:
operating_mode |= DAIKIN_MODE_FAN;
break;
case climate::CLIMATE_MODE_OFF:
default:
operating_mode = DAIKIN_MODE_OFF;
break;
}
return operating_mode;
}
uint16_t DaikinClimate::fan_speed_() const {
uint16_t fan_speed;
switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
case climate::CLIMATE_FAN_QUIET:
fan_speed = DAIKIN_FAN_SILENT << 8;
break;
case climate::CLIMATE_FAN_LOW:
fan_speed = DAIKIN_FAN_1 << 8;
break;
case climate::CLIMATE_FAN_MEDIUM:
fan_speed = DAIKIN_FAN_3 << 8;
break;
case climate::CLIMATE_FAN_HIGH:
fan_speed = DAIKIN_FAN_5 << 8;
break;
case climate::CLIMATE_FAN_AUTO:
default:
fan_speed = DAIKIN_FAN_AUTO << 8;
}
uint16_t fan_speed = to_ir_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO), DAIKIN_FAN_AUTO) << 8;
// If swing is enabled switch first 4 bits to 1111
switch (this->swing_mode) {
@@ -189,29 +166,12 @@ bool DaikinClimate::parse_state_frame_(const uint8_t frame[]) {
// Temperature is given in degrees celsius * 2
// only update for states that use the temperature
uint8_t temperature = frame[6];
if (mode & DAIKIN_MODE_ON) {
switch (mode & 0xF0) {
case DAIKIN_MODE_COOL:
this->mode = climate::CLIMATE_MODE_COOL;
this->target_temperature = static_cast<float>(temperature * 0.5f);
break;
case DAIKIN_MODE_DRY:
this->mode = climate::CLIMATE_MODE_DRY;
break;
case DAIKIN_MODE_HEAT:
this->mode = climate::CLIMATE_MODE_HEAT;
this->target_temperature = static_cast<float>(temperature * 0.5f);
break;
case DAIKIN_MODE_AUTO:
this->mode = climate::CLIMATE_MODE_HEAT_COOL;
this->target_temperature = static_cast<float>(temperature * 0.5f);
break;
case DAIKIN_MODE_FAN:
this->mode = climate::CLIMATE_MODE_FAN_ONLY;
break;
}
} else {
if (!(mode & DAIKIN_MODE_ON)) {
this->mode = climate::CLIMATE_MODE_OFF;
} else if (const auto entry = find_ir_code(MODES, mode & (0xF0 | DAIKIN_MODE_ON), true)) {
this->mode = static_cast<climate::ClimateMode>(entry->value);
if (this->mode != climate::CLIMATE_MODE_DRY && this->mode != climate::CLIMATE_MODE_FAN_ONLY)
this->target_temperature = static_cast<float>(temperature * 0.5f);
}
uint8_t fan_mode = frame[8];
uint8_t swing_mode = frame[9];
@@ -224,25 +184,8 @@ bool DaikinClimate::parse_state_frame_(const uint8_t frame[]) {
} else {
this->swing_mode = climate::CLIMATE_SWING_OFF;
}
switch (fan_mode & 0xF0) {
case DAIKIN_FAN_1:
case DAIKIN_FAN_2:
this->fan_mode = climate::CLIMATE_FAN_LOW;
break;
case DAIKIN_FAN_3:
this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
break;
case DAIKIN_FAN_4:
case DAIKIN_FAN_5:
this->fan_mode = climate::CLIMATE_FAN_HIGH;
break;
case DAIKIN_FAN_AUTO:
this->fan_mode = climate::CLIMATE_FAN_AUTO;
break;
case DAIKIN_FAN_SILENT:
this->fan_mode = climate::CLIMATE_FAN_QUIET;
break;
}
if (const auto entry = find_ir_code(FANS, fan_mode & 0xF0, true))
this->fan_mode = static_cast<climate::ClimateFanMode>(entry->value);
// Comfort lives in the first frame, recorded as pending until this frame proves the transmission valid
// A comfort frame whose own state frame never came must not attach to a later transmission
-1
View File
@@ -72,7 +72,6 @@ class DaikinClimate final : public climate_ir::ClimateIR {
void control(const climate::ClimateCall &call) override;
// Transmit via IR the state of this climate controller.
void transmit_state() override;
uint8_t operation_mode_() const;
uint16_t fan_speed_() const;
uint8_t temperature_() const;
void set_presets_(uint8_t *remote_state) const;
+29 -79
View File
@@ -10,6 +10,28 @@ namespace esphome::daikin_arc {
ESPHOME_LOG_TAG(TAG, "daikin.climate");
using climate_ir::find_ir_code;
using climate_ir::from_ir_code;
using climate_ir::IRCode;
using climate_ir::to_ir_code;
// Codes include the ON bit so the OFF entry never matches a running unit; an unlisted received code is ignored
static constexpr IRCode MODES[] PROGMEM = {
{climate::CLIMATE_MODE_OFF, DAIKIN_MODE_OFF},
{climate::CLIMATE_MODE_COOL, DAIKIN_MODE_ON | DAIKIN_MODE_COOL},
{climate::CLIMATE_MODE_DRY, DAIKIN_MODE_ON | DAIKIN_MODE_DRY},
{climate::CLIMATE_MODE_HEAT, DAIKIN_MODE_ON | DAIKIN_MODE_HEAT},
{climate::CLIMATE_MODE_HEAT_COOL, DAIKIN_MODE_ON | DAIKIN_MODE_AUTO},
{climate::CLIMATE_MODE_FAN_ONLY, DAIKIN_MODE_ON | DAIKIN_MODE_FAN},
};
// Fan speeds 2 and 4 and silent are only received
static constexpr IRCode FANS[] PROGMEM = {
{climate::CLIMATE_FAN_AUTO, DAIKIN_FAN_AUTO}, {climate::CLIMATE_FAN_LOW, DAIKIN_FAN_1},
{climate::CLIMATE_FAN_MEDIUM, DAIKIN_FAN_3}, {climate::CLIMATE_FAN_HIGH, DAIKIN_FAN_5},
{climate::CLIMATE_FAN_LOW, DAIKIN_FAN_2}, {climate::CLIMATE_FAN_LOW, DAIKIN_FAN_SILENT},
{climate::CLIMATE_FAN_HIGH, DAIKIN_FAN_4},
};
void DaikinArcClimate::setup() {
climate_ir::ClimateIR::setup();
@@ -82,7 +104,7 @@ void DaikinArcClimate::transmit_state() {
/* OF 0x80 */
};
remote_state[5] = this->operation_mode_() | 0x08;
remote_state[5] = to_ir_code(MODES, this->mode, DAIKIN_MODE_OFF) | 0x08;
remote_state[6] = this->temperature_();
remote_state[7] = this->humidity_();
if (remote_state[7] != this->last_humidity_ && this->mode != climate::CLIMATE_MODE_OFF) {
@@ -126,49 +148,8 @@ void DaikinArcClimate::transmit_state() {
transmit.perform();
}
uint8_t DaikinArcClimate::operation_mode_() {
uint8_t operating_mode = DAIKIN_MODE_ON;
switch (this->mode) {
case climate::CLIMATE_MODE_COOL:
operating_mode |= DAIKIN_MODE_COOL;
break;
case climate::CLIMATE_MODE_DRY:
operating_mode |= DAIKIN_MODE_DRY;
break;
case climate::CLIMATE_MODE_HEAT:
operating_mode |= DAIKIN_MODE_HEAT;
break;
case climate::CLIMATE_MODE_HEAT_COOL:
operating_mode |= DAIKIN_MODE_AUTO;
break;
case climate::CLIMATE_MODE_FAN_ONLY:
operating_mode |= DAIKIN_MODE_FAN;
break;
case climate::CLIMATE_MODE_OFF:
default:
operating_mode = DAIKIN_MODE_OFF;
break;
}
return operating_mode;
}
uint16_t DaikinArcClimate::fan_speed_() {
uint16_t fan_speed;
switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
case climate::CLIMATE_FAN_LOW:
fan_speed = DAIKIN_FAN_1 << 8;
break;
case climate::CLIMATE_FAN_MEDIUM:
fan_speed = DAIKIN_FAN_3 << 8;
break;
case climate::CLIMATE_FAN_HIGH:
fan_speed = DAIKIN_FAN_5 << 8;
break;
case climate::CLIMATE_FAN_AUTO:
default:
fan_speed = DAIKIN_FAN_AUTO << 8;
}
uint16_t fan_speed = to_ir_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO), DAIKIN_FAN_AUTO) << 8;
// If swing is enabled switch first 4 bits to 1111
switch (this->swing_mode) {
@@ -241,26 +222,10 @@ bool DaikinArcClimate::parse_state_frame_(const uint8_t frame[]) {
ESP_LOGD(TAG, "FRAME %s", buf);
uint8_t mode = frame[5];
if (mode & DAIKIN_MODE_ON) {
switch (mode & 0xF0) {
case DAIKIN_MODE_COOL:
this->mode = climate::CLIMATE_MODE_COOL;
break;
case DAIKIN_MODE_DRY:
this->mode = climate::CLIMATE_MODE_DRY;
break;
case DAIKIN_MODE_HEAT:
this->mode = climate::CLIMATE_MODE_HEAT;
break;
case DAIKIN_MODE_AUTO:
this->mode = climate::CLIMATE_MODE_HEAT_COOL;
break;
case DAIKIN_MODE_FAN:
this->mode = climate::CLIMATE_MODE_FAN_ONLY;
break;
}
} else {
if (!(mode & DAIKIN_MODE_ON)) {
this->mode = climate::CLIMATE_MODE_OFF;
} else {
this->mode = from_ir_code(MODES, mode & (0xF0 | DAIKIN_MODE_ON), this->mode);
}
uint8_t temperature = frame[6];
if (!(temperature & 0xC0)) {
@@ -279,23 +244,8 @@ bool DaikinArcClimate::parse_state_frame_(const uint8_t frame[]) {
} else {
this->swing_mode = climate::CLIMATE_SWING_OFF;
}
switch (fan_mode & 0xF0) {
case DAIKIN_FAN_1:
case DAIKIN_FAN_2:
case DAIKIN_FAN_SILENT:
this->fan_mode = climate::CLIMATE_FAN_LOW;
break;
case DAIKIN_FAN_3:
this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
break;
case DAIKIN_FAN_4:
case DAIKIN_FAN_5:
this->fan_mode = climate::CLIMATE_FAN_HIGH;
break;
case DAIKIN_FAN_AUTO:
this->fan_mode = climate::CLIMATE_FAN_AUTO;
break;
}
if (const auto entry = find_ir_code(FANS, fan_mode & 0xF0, true))
this->fan_mode = static_cast<climate::ClimateFanMode>(entry->value);
/*
05 0 [1:3]MODE 1 [OFF TMR] [ON TMR] Power
06-07 TEMP
@@ -64,7 +64,6 @@ class DaikinArcClimate final : public climate_ir::ClimateIR {
void transmit_query_();
void transmit_state() override;
climate::ClimateTraits traits() override;
uint8_t operation_mode_();
uint16_t fan_speed_();
uint8_t temperature_();
uint8_t humidity_();
+27 -72
View File
@@ -12,6 +12,26 @@ static constexpr uint8_t DAIKIN_BRC_HEADER_BYTE2 = 0x17;
static constexpr uint8_t DAIKIN_BRC_HEADER_BYTE3 = 0x18;
static constexpr uint8_t DAIKIN_BRC_FRAME_TYPE_STATE = 0x00;
using climate_ir::find_ir_code;
using climate_ir::from_ir_code;
using climate_ir::IRCode;
using climate_ir::to_ir_code;
// Codes include the ON bit so the OFF entry never matches a running unit; an unlisted received code is ignored
static constexpr IRCode MODES[] PROGMEM = {
{climate::CLIMATE_MODE_OFF, DAIKIN_BRC_MODE_OFF},
{climate::CLIMATE_MODE_COOL, DAIKIN_BRC_MODE_ON | DAIKIN_BRC_MODE_COOL},
{climate::CLIMATE_MODE_DRY, DAIKIN_BRC_MODE_ON | DAIKIN_BRC_MODE_DRY},
{climate::CLIMATE_MODE_HEAT, DAIKIN_BRC_MODE_ON | DAIKIN_BRC_MODE_HEAT},
{climate::CLIMATE_MODE_HEAT_COOL, DAIKIN_BRC_MODE_ON | DAIKIN_BRC_MODE_AUTO},
{climate::CLIMATE_MODE_FAN_ONLY, DAIKIN_BRC_MODE_ON | DAIKIN_BRC_MODE_FAN},
};
static constexpr IRCode FANS[] PROGMEM = {
{climate::CLIMATE_FAN_LOW, DAIKIN_BRC_FAN_1},
{climate::CLIMATE_FAN_MEDIUM, DAIKIN_BRC_FAN_2},
{climate::CLIMATE_FAN_HIGH, DAIKIN_BRC_FAN_3},
};
void DaikinBrcClimate::control(const climate::ClimateCall &call) {
this->mode_button_ = 0x00;
if (call.get_mode().has_value()) {
@@ -33,7 +53,7 @@ void DaikinBrcClimate::transmit_state() {
remote_state[12] = this->alt_mode_();
remote_state[13] = this->mode_button_;
remote_state[14] = this->operation_mode_();
remote_state[14] = to_ir_code(MODES, this->mode, DAIKIN_BRC_MODE_OFF);
remote_state[17] = this->temperature_();
remote_state[18] = this->fan_speed_swing_();
@@ -82,48 +102,8 @@ uint8_t DaikinBrcClimate::alt_mode_() {
return alt_mode;
}
uint8_t DaikinBrcClimate::operation_mode_() {
uint8_t operating_mode = DAIKIN_BRC_MODE_ON;
switch (this->mode) {
case climate::CLIMATE_MODE_COOL:
operating_mode |= DAIKIN_BRC_MODE_COOL;
break;
case climate::CLIMATE_MODE_DRY:
operating_mode |= DAIKIN_BRC_MODE_DRY;
break;
case climate::CLIMATE_MODE_HEAT:
operating_mode |= DAIKIN_BRC_MODE_HEAT;
break;
case climate::CLIMATE_MODE_HEAT_COOL:
operating_mode |= DAIKIN_BRC_MODE_AUTO;
break;
case climate::CLIMATE_MODE_FAN_ONLY:
operating_mode |= DAIKIN_BRC_MODE_FAN;
break;
case climate::CLIMATE_MODE_OFF:
default:
operating_mode = DAIKIN_BRC_MODE_OFF;
break;
}
return operating_mode;
}
uint8_t DaikinBrcClimate::fan_speed_swing_() {
uint16_t fan_speed;
switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
case climate::CLIMATE_FAN_LOW:
fan_speed = DAIKIN_BRC_FAN_1;
break;
case climate::CLIMATE_FAN_MEDIUM:
fan_speed = DAIKIN_BRC_FAN_2;
break;
case climate::CLIMATE_FAN_HIGH:
fan_speed = DAIKIN_BRC_FAN_3;
break;
default:
fan_speed = DAIKIN_BRC_FAN_1;
}
uint16_t fan_speed = to_ir_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_LOW), DAIKIN_BRC_FAN_1);
// If swing is enabled switch first 4 bits to 1111
switch (this->swing_mode) {
@@ -168,26 +148,10 @@ bool DaikinBrcClimate::parse_state_frame_(const uint8_t frame[]) {
}
uint8_t mode = frame[7];
if (mode & DAIKIN_BRC_MODE_ON) {
switch (mode & 0xF0) {
case DAIKIN_BRC_MODE_COOL:
this->mode = climate::CLIMATE_MODE_COOL;
break;
case DAIKIN_BRC_MODE_DRY:
this->mode = climate::CLIMATE_MODE_DRY;
break;
case DAIKIN_BRC_MODE_HEAT:
this->mode = climate::CLIMATE_MODE_HEAT;
break;
case DAIKIN_BRC_MODE_AUTO:
this->mode = climate::CLIMATE_MODE_HEAT_COOL;
break;
case DAIKIN_BRC_MODE_FAN:
this->mode = climate::CLIMATE_MODE_FAN_ONLY;
break;
}
} else {
if (!(mode & DAIKIN_BRC_MODE_ON)) {
this->mode = climate::CLIMATE_MODE_OFF;
} else {
this->mode = from_ir_code(MODES, mode & (0xF0 | DAIKIN_BRC_MODE_ON), this->mode);
}
uint8_t temperature = frame[10];
@@ -211,17 +175,8 @@ bool DaikinBrcClimate::parse_state_frame_(const uint8_t frame[]) {
break;
}
switch (fan_mode & 0xF0) {
case DAIKIN_BRC_FAN_1:
this->fan_mode = climate::CLIMATE_FAN_LOW;
break;
case DAIKIN_BRC_FAN_2:
this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
break;
case DAIKIN_BRC_FAN_3:
this->fan_mode = climate::CLIMATE_FAN_HIGH;
break;
}
if (const auto entry = find_ir_code(FANS, fan_mode & 0xF0, true))
this->fan_mode = static_cast<climate::ClimateFanMode>(entry->value);
this->publish_state();
return true;
}
@@ -70,7 +70,6 @@ class DaikinBrcClimate final : public climate_ir::ClimateIR {
// Transmit via IR the state of this climate controller.
void transmit_state() override;
uint8_t alt_mode_();
uint8_t operation_mode_();
uint8_t fan_speed_swing_();
uint8_t temperature_();
// Handle received IR Buffer
+28 -83
View File
@@ -6,12 +6,33 @@ namespace esphome::delonghi {
ESPHOME_LOG_TAG(TAG, "delonghi.climate");
using climate_ir::find_ir_code;
using climate_ir::from_ir_code;
using climate_ir::IRCode;
using climate_ir::to_ir_code;
// A received code that is not listed is ignored
static constexpr IRCode MODES[] PROGMEM = {
{climate::CLIMATE_MODE_OFF, DELONGHI_MODE_OFF},
{climate::CLIMATE_MODE_COOL, DELONGHI_MODE_ON | DELONGHI_MODE_COOL},
{climate::CLIMATE_MODE_DRY, DELONGHI_MODE_ON | DELONGHI_MODE_DRY},
{climate::CLIMATE_MODE_HEAT, DELONGHI_MODE_ON | DELONGHI_MODE_HEAT},
{climate::CLIMATE_MODE_HEAT_COOL, DELONGHI_MODE_ON | DELONGHI_MODE_AUTO},
{climate::CLIMATE_MODE_FAN_ONLY, DELONGHI_MODE_ON | DELONGHI_MODE_FAN},
};
static constexpr IRCode FANS[] PROGMEM = {
{climate::CLIMATE_FAN_AUTO, DELONGHI_FAN_AUTO},
{climate::CLIMATE_FAN_LOW, DELONGHI_FAN_LOW},
{climate::CLIMATE_FAN_MEDIUM, DELONGHI_FAN_MEDIUM},
{climate::CLIMATE_FAN_HIGH, DELONGHI_FAN_HIGH},
};
void DelonghiClimate::transmit_state() {
uint8_t remote_state[DELONGHI_STATE_FRAME_SIZE] = {0};
remote_state[0] = DELONGHI_ADDRESS;
remote_state[1] = this->temperature_();
remote_state[1] |= (this->fan_speed_()) << 5;
remote_state[2] = this->operation_mode_();
remote_state[1] |= to_ir_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO), DELONGHI_FAN_AUTO) << 5;
remote_state[2] = to_ir_code(MODES, this->mode, DELONGHI_MODE_OFF);
// Calculate checksum
for (int i = 0; i < DELONGHI_STATE_FRAME_SIZE - 1; i++) {
remote_state[DELONGHI_STATE_FRAME_SIZE - 1] += remote_state[i];
@@ -30,51 +51,6 @@ void DelonghiClimate::transmit_state() {
transmit.perform();
}
uint8_t DelonghiClimate::operation_mode_() {
uint8_t operating_mode = DELONGHI_MODE_ON;
switch (this->mode) {
case climate::CLIMATE_MODE_COOL:
operating_mode |= DELONGHI_MODE_COOL;
break;
case climate::CLIMATE_MODE_DRY:
operating_mode |= DELONGHI_MODE_DRY;
break;
case climate::CLIMATE_MODE_HEAT:
operating_mode |= DELONGHI_MODE_HEAT;
break;
case climate::CLIMATE_MODE_HEAT_COOL:
operating_mode |= DELONGHI_MODE_AUTO;
break;
case climate::CLIMATE_MODE_FAN_ONLY:
operating_mode |= DELONGHI_MODE_FAN;
break;
case climate::CLIMATE_MODE_OFF:
default:
operating_mode = DELONGHI_MODE_OFF;
break;
}
return operating_mode;
}
uint16_t DelonghiClimate::fan_speed_() {
uint16_t fan_speed;
switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
case climate::CLIMATE_FAN_LOW:
fan_speed = DELONGHI_FAN_LOW;
break;
case climate::CLIMATE_FAN_MEDIUM:
fan_speed = DELONGHI_FAN_MEDIUM;
break;
case climate::CLIMATE_FAN_HIGH:
fan_speed = DELONGHI_FAN_HIGH;
break;
case climate::CLIMATE_FAN_AUTO:
default:
fan_speed = DELONGHI_FAN_AUTO;
}
return fan_speed;
}
uint8_t DelonghiClimate::temperature_() {
// Force special temperatures depending on the mode
uint8_t temperature = 0b0001;
@@ -102,26 +78,10 @@ bool DelonghiClimate::parse_state_frame_(const uint8_t frame[]) {
return false;
}
uint8_t mode = frame[2] & 0x0F;
if (mode & DELONGHI_MODE_ON) {
switch (mode & 0x0E) {
case DELONGHI_MODE_COOL:
this->mode = climate::CLIMATE_MODE_COOL;
break;
case DELONGHI_MODE_DRY:
this->mode = climate::CLIMATE_MODE_DRY;
break;
case DELONGHI_MODE_HEAT:
this->mode = climate::CLIMATE_MODE_HEAT;
break;
case DELONGHI_MODE_AUTO:
this->mode = climate::CLIMATE_MODE_HEAT_COOL;
break;
case DELONGHI_MODE_FAN:
this->mode = climate::CLIMATE_MODE_FAN_ONLY;
break;
}
} else {
if (!(mode & DELONGHI_MODE_ON)) {
this->mode = climate::CLIMATE_MODE_OFF;
} else {
this->mode = from_ir_code(MODES, mode, this->mode);
}
uint8_t temperature = frame[1] & 0x0F;
if (this->mode == climate::CLIMATE_MODE_HEAT) {
@@ -129,23 +89,8 @@ bool DelonghiClimate::parse_state_frame_(const uint8_t frame[]) {
} else {
this->target_temperature = temperature + DELONGHI_TEMP_OFFSET_COOL;
}
uint8_t fan_mode = frame[1] >> 5;
switch (fan_mode) {
case DELONGHI_FAN_LOW:
this->fan_mode = climate::CLIMATE_FAN_LOW;
break;
case DELONGHI_FAN_MEDIUM:
this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
break;
case DELONGHI_FAN_HIGH:
this->fan_mode = climate::CLIMATE_FAN_HIGH;
break;
case DELONGHI_FAN_AUTO:
this->fan_mode = climate::CLIMATE_FAN_AUTO;
break;
default:
break;
}
if (const auto entry = find_ir_code(FANS, frame[1] >> 5, true))
this->fan_mode = static_cast<climate::ClimateFanMode>(entry->value);
this->publish_state();
return true;
}
-2
View File
@@ -51,8 +51,6 @@ class DelonghiClimate final : public climate_ir::ClimateIR {
protected:
// Transmit via IR the state of this climate controller.
void transmit_state() override;
uint8_t operation_mode_();
uint16_t fan_speed_();
uint8_t temperature_();
// Handle received IR Buffer
bool on_receive(remote_base::RemoteReceiveData data) override;
@@ -41,7 +41,7 @@ from esphome.const import (
__version__ as ESPHOME_VERSION,
)
from esphome.core import CORE, ID, MACAddress
from esphome.cpp_generator import MockObj, StaticCastExpression, UnaryOpExpression
from esphome.cpp_generator import MockObj, StaticCastExpression
import esphome.final_validate as fv
from esphome.schema_extractors import SCHEMA_EXTRACT
from esphome.types import ConfigType, TemplateArgsType
@@ -704,8 +704,7 @@ async def to_code_descriptor(descriptor_conf, char_conf, char_var):
descriptor_conf[CONF_MAX_LENGTH],
descriptor_permissions(descriptor_conf, char_conf),
)
# Static storage, so the characteristic must not free it
cg.add(char_var.add_descriptor(UnaryOpExpression("*", desc_var)))
cg.add(char_var.add_descriptor(desc_var))
cg.add(desc_var.set_value(value))
if (on_write_conf := descriptor_conf.get(CONF_ON_WRITE)) is not None:
await _build_on_write_automation(desc_var, on_write_conf)
@@ -2,8 +2,6 @@
#include "ble_server.h"
#include "ble_service.h"
#include <vector>
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
@@ -15,12 +13,7 @@ ESPHOME_LOG_TAG(TAG, "esp32_ble_server.characteristic");
BLECharacteristic::~BLECharacteristic() {
for (auto *descriptor : this->descriptors_) {
if (descriptor->owned_) {
delete descriptor; // NOLINT(cppcoreguidelines-owning-memory)
} else if (descriptor->characteristic_ == this) {
// A caller-owned descriptor outlives this characteristic, and may already belong to another one
descriptor->characteristic_ = nullptr;
}
delete descriptor; // NOLINT(cppcoreguidelines-owning-memory)
}
}
@@ -86,27 +79,10 @@ void BLECharacteristic::notify() {
}
}
void BLECharacteristic::do_delete() {
this->clients_to_notify_.clear();
// The stack may reuse the handles, so a deleted characteristic must not match events until it is created again
this->handle_ = 0xFFFF;
this->state_ = INIT;
for (auto *descriptor : this->descriptors_) {
descriptor->handle_ = 0xFFFF;
descriptor->state_ = BLEDescriptor::INIT;
}
}
bool BLECharacteristic::add_descriptor(BLEDescriptor &descriptor) {
// Bound until removed or the parent is destroyed
if (descriptor.characteristic_ != nullptr) {
ESP_LOGE(TAG, "Already added");
return false;
}
descriptor.characteristic_ = this;
void BLECharacteristic::add_descriptor(BLEDescriptor *descriptor) {
// If the descriptor is the CCCD descriptor, listen to its write event to know if the client wants to be notified
if (descriptor.get_uuid() == ESPBTUUID::from_uint16(ESP_GATT_UUID_CHAR_CLIENT_CONFIG)) {
descriptor.on_write([this](std::span<const uint8_t> value, uint16_t conn_id) {
if (descriptor->get_uuid() == ESPBTUUID::from_uint16(ESP_GATT_UUID_CHAR_CLIENT_CONFIG)) {
descriptor->on_write([this](std::span<const uint8_t> value, uint16_t conn_id) {
if (value.size() != 2)
return;
uint16_t cccd = encode_uint16(value[1], value[0]);
@@ -120,18 +96,16 @@ bool BLECharacteristic::add_descriptor(BLEDescriptor &descriptor) {
}
});
}
this->descriptors_.push_back(&descriptor);
return true;
this->descriptors_.push_back(descriptor);
}
void BLECharacteristic::remove_descriptor(BLEDescriptor *descriptor) {
if (std::erase(this->descriptors_, descriptor) == 0)
return;
descriptor->owned_ = false; // the caller owns it again
descriptor->characteristic_ = nullptr;
this->descriptors_.erase(std::remove(this->descriptors_.begin(), this->descriptors_.end(), descriptor),
this->descriptors_.end());
}
void BLECharacteristic::do_create() {
void BLECharacteristic::do_create(BLEService *service) {
this->service_ = service;
esp_attr_control_t control;
control.auto_rsp = ESP_GATT_RSP_BY_APP;
@@ -142,9 +116,8 @@ void BLECharacteristic::do_create() {
#endif
esp_bt_uuid_t uuid = this->uuid_.get_uuid();
esp_err_t err =
esp_ble_gatts_add_char(this->service_->get_handle(), &uuid, static_cast<esp_gatt_perm_t>(this->permissions_),
this->properties_, nullptr, &control);
esp_err_t err = esp_ble_gatts_add_char(service->get_handle(), &uuid, static_cast<esp_gatt_perm_t>(this->permissions_),
this->properties_, nullptr, &control);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_ble_gatts_add_char failed: %d", err);
@@ -215,15 +188,11 @@ void BLECharacteristic::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt
break;
}
case ESP_GATTS_ADD_CHAR_EVT: {
// Only while creating, in its own service: a late event for a removed service or a same-UUID
// characteristic elsewhere must not set the handle or start the descriptors
if (this->state_ == CREATING && this->service_ != nullptr &&
this->service_->get_handle() == param->add_char.service_handle &&
this->uuid_ == ESPBTUUID::from_uuid(param->add_char.char_uuid)) {
if (this->uuid_ == ESPBTUUID::from_uuid(param->add_char.char_uuid)) {
this->handle_ = param->add_char.attr_handle;
for (auto *descriptor : this->descriptors_) {
descriptor->do_create();
descriptor->do_create(this);
}
this->state_ = CREATING_DEPENDENTS;
@@ -25,8 +25,6 @@ using namespace bytebuffer;
class BLEService;
class BLECharacteristic final {
friend class BLEService;
public:
BLECharacteristic(ESPBTUUID uuid, uint32_t properties, esp_gatt_perm_t permissions);
~BLECharacteristic();
@@ -46,18 +44,11 @@ class BLECharacteristic final {
void notify();
void do_create();
void do_delete();
void do_create(BLEService *service);
void do_delete() { this->clients_to_notify_.clear(); }
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
/// Takes ownership of a heap-allocated descriptor and frees it with this characteristic.
void add_descriptor(BLEDescriptor *descriptor) {
if (this->add_descriptor(*descriptor))
descriptor->owned_ = true;
}
/// Adds a descriptor the caller keeps alive (such as a component member); it is never freed here.
/// False when it already belongs to a characteristic.
bool add_descriptor(BLEDescriptor &descriptor);
void add_descriptor(BLEDescriptor *descriptor);
void remove_descriptor(BLEDescriptor *descriptor);
BLEService *get_service() { return this->service_; }
@@ -115,8 +106,6 @@ class BLECharacteristic final {
CREATING_DEPENDENTS,
CREATED,
} state_{INIT};
// Set by BLEService::create_characteristic(), which allocates it; the service frees it
bool owned_{false};
};
} // namespace esphome::esp32_ble_server
@@ -27,7 +27,8 @@ BLEDescriptor::BLEDescriptor(ESPBTUUID uuid, uint16_t max_len, esp_gatt_perm_t p
BLEDescriptor::~BLEDescriptor() { free(this->value_.attr_value); } // NOLINT
void BLEDescriptor::do_create() {
void BLEDescriptor::do_create(BLECharacteristic *characteristic) {
this->characteristic_ = characteristic;
esp_attr_control_t control;
control.auto_rsp = ESP_GATT_AUTO_RSP;
@@ -65,8 +66,7 @@ void BLEDescriptor::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_
esp_ble_gatts_cb_param_t *param) {
switch (event) {
case ESP_GATTS_ADD_CHAR_DESCR_EVT: {
// Bound by add_descriptor(), so only a descriptor do_create() is creating may claim the event
if (this->state_ == CREATING && this->uuid_ == ESPBTUUID::from_uuid(param->add_char_descr.descr_uuid) &&
if (this->characteristic_ != nullptr && this->uuid_ == ESPBTUUID::from_uuid(param->add_char_descr.descr_uuid) &&
this->characteristic_->get_service()->get_handle() == param->add_char_descr.service_handle &&
this->characteristic_ == this->characteristic_->get_service()->get_last_created_characteristic()) {
this->handle_ = param->add_char_descr.attr_handle;
@@ -20,13 +20,11 @@ class BLECharacteristic;
// Base class for BLE descriptors
class BLEDescriptor {
friend class BLECharacteristic;
public:
BLEDescriptor(ESPBTUUID uuid, uint16_t max_len = 100, bool read = true, bool write = true);
BLEDescriptor(ESPBTUUID uuid, uint16_t max_len, esp_gatt_perm_t permissions);
virtual ~BLEDescriptor();
void do_create();
void do_create(BLECharacteristic *characteristic);
ESPBTUUID get_uuid() const { return this->uuid_; }
void set_value(const uint8_t *data, size_t length);
@@ -62,8 +60,6 @@ class BLEDescriptor {
CREATING,
CREATED,
} state_{INIT};
// Set when the characteristic took ownership through add_descriptor(BLEDescriptor *) and frees it
bool owned_{false};
};
} // namespace esphome::esp32_ble_server
@@ -1,8 +1,5 @@
#include "ble_server.h"
#include <algorithm>
#include <vector>
#include "esphome/components/ble_device_base/ble_device.h"
#include "esphome/components/esp32_ble/ble.h"
#include "esphome/core/log.h"
@@ -79,11 +76,11 @@ void BLEServer::loop() {
// it is at the top of the GATT table
this->device_information_service_->do_create(this);
// Create all services previously created
for (auto *service : this->services_) {
if (service == this->device_information_service_) {
for (auto &entry : this->services_) {
if (entry.service == this->device_information_service_) {
continue;
}
service->do_create(this);
entry.service->do_create(this);
}
this->state_ = STARTING_SERVICE;
}
@@ -126,52 +123,31 @@ void BLEServer::release_advertising_() {
}
BLEService *BLEServer::create_service(ESPBTUUID uuid, bool advertise, uint16_t num_handles) {
// Checked before allocating, so a failure never deletes a service and pulls in its destructor
const uint8_t inst_id = this->free_inst_id_(uuid);
if (inst_id == INVALID_INST_ID)
return nullptr;
auto *service = new BLEService(uuid, num_handles, advertise); // NOLINT(cppcoreguidelines-owning-memory)
service->owned_ = true;
this->register_service_(*service, inst_id);
return service;
}
bool BLEServer::add_service(BLEService &service) {
// Adding it twice would free or start it twice
if (std::ranges::find(this->services_, &service) != this->services_.end()) {
ESP_LOGE(TAG, "Already added");
return false;
}
const uint8_t inst_id = this->free_inst_id_(service.get_uuid());
if (inst_id == INVALID_INST_ID)
return false;
this->register_service_(service, inst_id);
return true;
}
uint8_t BLEServer::free_inst_id_(const ESPBTUUID &uuid) {
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
char uuid_buf[esp32_ble::UUID_STR_LEN];
uuid.to_str(uuid_buf);
ESP_LOGV(TAG, "Adding BLE service - %s", uuid_buf);
ESP_LOGV(TAG, "Creating BLE service - %s", uuid_buf);
#endif
for (uint8_t inst_id = 0; inst_id < INVALID_INST_ID; inst_id++) {
if (this->get_service(uuid, inst_id) == nullptr)
return inst_id;
// Calculate the inst_id for the service
uint8_t inst_id = 0;
for (; inst_id < 0xFF; inst_id++) {
if (this->get_service(uuid, inst_id) == nullptr) {
break;
}
}
char warn_uuid_buf[esp32_ble::UUID_STR_LEN];
uuid.to_str(warn_uuid_buf);
ESP_LOGW(TAG, "Could not create BLE service %s, too many instances", warn_uuid_buf);
return INVALID_INST_ID;
}
void BLEServer::register_service_(BLEService &service, uint8_t inst_id) {
service.inst_id_ = inst_id;
BLEService *ptr = &service; // an lvalue reuses the push_back already linked for services_to_start_
this->services_.push_back(ptr);
if (inst_id == 0xFF) {
char warn_uuid_buf[esp32_ble::UUID_STR_LEN];
uuid.to_str(warn_uuid_buf);
ESP_LOGW(TAG, "Could not create BLE service %s, too many instances", warn_uuid_buf);
return nullptr;
}
BLEService *service = // NOLINT(cppcoreguidelines-owning-memory)
new BLEService(uuid, num_handles, inst_id, advertise);
this->services_.push_back({uuid, inst_id, service});
if (this->parent_->is_active() && this->registered_) {
service.do_create(this);
service->do_create(this);
}
return service;
}
void BLEServer::remove_service(ESPBTUUID uuid, uint8_t inst_id) {
@@ -181,13 +157,9 @@ void BLEServer::remove_service(ESPBTUUID uuid, uint8_t inst_id) {
ESP_LOGV(TAG, "Removing BLE service - %s %d", uuid_buf, inst_id);
#endif
for (auto it = this->services_.begin(); it != this->services_.end(); ++it) {
BLEService *service = *it;
if (service->get_inst_id() == inst_id && service->get_uuid() == uuid) {
service->do_delete();
// A service still waiting to start must not stay queued once it is unregistered or freed
std::erase(this->services_to_start_, service);
if (service->owned_)
delete service; // NOLINT(cppcoreguidelines-owning-memory)
if (it->uuid == uuid && it->inst_id == inst_id) {
it->service->do_delete();
delete it->service; // NOLINT(cppcoreguidelines-owning-memory)
this->services_.erase(it);
return;
}
@@ -198,9 +170,9 @@ void BLEServer::remove_service(ESPBTUUID uuid, uint8_t inst_id) {
}
BLEService *BLEServer::get_service(ESPBTUUID uuid, uint8_t inst_id) {
for (auto *service : this->services_) {
if (service->get_inst_id() == inst_id && service->get_uuid() == uuid) {
return service;
for (auto &entry : this->services_) {
if (entry.uuid == uuid && entry.inst_id == inst_id) {
return entry.service;
}
}
return nullptr;
@@ -243,17 +215,8 @@ void BLEServer::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t ga
break;
}
for (auto *service : this->services_) {
service->gatts_event_handler(event, gatts_if, param);
}
// A service this server asked for but removed while the stack was still creating it is deleted once its handle
// arrives; services of other GATT applications are left alone
if (event == ESP_GATTS_CREATE_EVT && gatts_if == this->gatts_if_ && param->create.status == ESP_GATT_OK) {
for (auto *service : this->services_) {
if (service->get_handle() == param->create.service_handle)
return;
}
esp_ble_gatts_delete_service(param->create.service_handle);
for (auto &entry : this->services_) {
entry.service->gatts_event_handler(event, gatts_if, param);
}
}
@@ -315,8 +278,8 @@ void BLEServer::ble_before_disabled_event_handler() {
// Delete all clients
this->client_count_ = 0;
// Delete all services
for (auto *service : this->services_) {
service->do_delete();
for (auto &entry : this->services_) {
entry.service->do_delete();
}
this->registered_ = false;
this->state_ = INIT;
@@ -49,10 +49,6 @@ class BLEServer final : public Component, public Parented<ESP32BLE> {
uint8_t get_max_clients() const { return this->max_clients_; }
BLEService *create_service(ESPBTUUID uuid, bool advertise = false, uint16_t num_handles = 15);
/// Adds a service the caller keeps alive (such as a component member); remove_service() never frees it.
/// Do not add it again after remove_service(). Returns false when it is already added or the UUID already has
/// 255 instances.
bool add_service(BLEService &service);
void remove_service(ESPBTUUID uuid, uint8_t inst_id = 0);
BLEService *get_service(ESPBTUUID uuid, uint8_t inst_id = 0);
void enqueue_start_service(BLEService *service) { this->services_to_start_.push_back(service); }
@@ -115,9 +111,12 @@ class BLEServer final : public Component, public Parented<ESP32BLE> {
std::function<void(uint16_t)> callback;
};
static constexpr uint8_t INVALID_INST_ID = 0xFF;
uint8_t free_inst_id_(const ESPBTUUID &uuid);
void register_service_(BLEService &service, uint8_t inst_id);
struct ServiceEntry {
ESPBTUUID uuid;
uint8_t inst_id;
BLEService *service;
};
void restart_advertising_();
void request_advertising_();
void release_advertising_();
@@ -160,7 +159,7 @@ class BLEServer final : public Component, public Parented<ESP32BLE> {
#endif
uint8_t client_count_{0};
uint8_t max_clients_{1};
std::vector<BLEService *> services_{};
std::vector<ServiceEntry> services_{};
std::vector<BLEService *> services_to_start_{};
BLEService *device_information_service_{};
@@ -12,25 +12,8 @@ BLEService::BLEService(ESPBTUUID uuid, uint16_t num_handles, uint8_t inst_id, bo
: uuid_(uuid), num_handles_(num_handles), inst_id_(inst_id), advertise_(advertise) {}
BLEService::~BLEService() {
for (auto *chr : this->characteristics_) {
if (chr->owned_) {
delete chr; // NOLINT(cppcoreguidelines-owning-memory)
} else if (chr->service_ == this) {
// A caller-owned characteristic outlives this service, and may already belong to another one
chr->service_ = nullptr;
}
}
}
bool BLEService::add_characteristic(BLECharacteristic &characteristic) {
// Bound until the parent is destroyed
if (characteristic.service_ != nullptr) {
ESP_LOGE(TAG, "Already added");
return false;
}
characteristic.service_ = this;
this->characteristics_.push_back(&characteristic);
return true;
for (auto &chr : this->characteristics_)
delete chr; // NOLINT(cppcoreguidelines-owning-memory)
}
BLECharacteristic *BLEService::get_characteristic(ESPBTUUID uuid) {
@@ -56,8 +39,7 @@ BLECharacteristic *BLEService::create_characteristic(ESPBTUUID uuid, esp_gatt_ch
esp_gatt_perm_t permissions) {
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
BLECharacteristic *characteristic = new BLECharacteristic(uuid, properties, permissions);
characteristic->owned_ = true;
this->add_characteristic(*characteristic);
this->characteristics_.push_back(characteristic);
return characteristic;
}
@@ -87,14 +69,8 @@ void BLEService::do_delete() {
// Call all characteristics to delete
for (auto *characteristic : this->characteristics_)
characteristic->do_delete();
// Not created on the stack yet; if a create is in flight, the server deletes it when its handle arrives
if (this->handle_ == 0xFFFF) {
this->state_ = DELETED;
return;
}
this->stop_();
esp_err_t err = esp_ble_gatts_delete_service(this->handle_);
this->handle_ = 0xFFFF; // a later add creates it again with a new handle
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_ble_gatts_delete_service failed: %d", err);
return;
@@ -111,7 +87,7 @@ bool BLEService::do_create_characteristics_() {
auto *characteristic = this->characteristics_[this->created_characteristic_count_++];
this->last_created_characteristic_ = characteristic;
characteristic->do_create();
characteristic->do_create(this);
return true;
}
@@ -164,7 +140,7 @@ void BLEService::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t g
esp_ble_gatts_cb_param_t *param) {
switch (event) {
case ESP_GATTS_CREATE_EVT: {
if (this->state_ == CREATING && this->uuid_ == ESPBTUUID::from_uuid(param->create.service_id.id.uuid) &&
if (this->uuid_ == ESPBTUUID::from_uuid(param->create.service_id.id.uuid) &&
this->inst_id_ == param->create.service_id.id.inst_id) {
this->handle_ = param->create.service_handle;
this->state_ = CREATED;
@@ -20,12 +20,8 @@ class BLEServer;
using namespace esp32_ble;
class BLEService final {
friend class BLEServer;
public:
BLEService(ESPBTUUID uuid, uint16_t num_handles, uint8_t inst_id, bool advertise);
/// The server picks the instance id when the service is added.
BLEService(ESPBTUUID uuid, uint16_t num_handles, bool advertise) : BLEService(uuid, num_handles, 0, advertise) {}
~BLEService();
BLECharacteristic *get_characteristic(ESPBTUUID uuid);
BLECharacteristic *get_characteristic(uint16_t uuid);
@@ -36,11 +32,9 @@ class BLEService final {
esp_gatt_perm_t permissions = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE);
BLECharacteristic *create_characteristic(ESPBTUUID uuid, esp_gatt_char_prop_t properties,
esp_gatt_perm_t permissions = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE);
/// Adds a characteristic the caller keeps alive; it is never freed here. False when it already belongs to a service.
bool add_characteristic(BLECharacteristic &characteristic);
const ESPBTUUID &get_uuid() const { return this->uuid_; }
uint8_t get_inst_id() const { return this->inst_id_; }
ESPBTUUID get_uuid() { return this->uuid_; }
uint8_t get_inst_id() { return this->inst_id_; }
BLECharacteristic *get_last_created_characteristic() { return this->last_created_characteristic_; }
uint16_t get_handle() { return this->handle_; }
@@ -63,9 +57,7 @@ class BLEService final {
protected:
std::vector<BLECharacteristic *> characteristics_;
BLECharacteristic *last_created_characteristic_{nullptr};
uint16_t created_characteristic_count_{0}; // never above num_handles_, so owned_ fits beside it
// Set by BLEServer::create_service(), which allocates it; remove_service() frees it
bool owned_{false};
uint32_t created_characteristic_count_{0};
BLEServer *server_ = nullptr;
ESPBTUUID uuid_;
uint16_t num_handles_;
@@ -7,6 +7,8 @@
#include "esphome/core/automation.h"
#include "esphome/components/network/ip_address.h"
#include <atomic>
#ifdef USE_ETHERNET
#ifdef USE_ESP32
@@ -366,6 +368,9 @@ class EthernetComponent final : public Component {
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
StaticVector<EthernetIPStateListener *, ESPHOME_ETHERNET_IP_STATE_LISTENERS> ip_state_listeners_;
#ifdef USE_ESP32
std::atomic<uint8_t> ip_state_pending_{0};
#endif
#endif
#ifdef USE_ETHERNET_CONNECT_TRIGGER
@@ -129,6 +129,14 @@ void EthernetComponent::log_error_and_mark_failed_(esp_err_t err, const char *me
void EthernetComponent::loop() {
const uint32_t now = App.get_loop_component_start_time();
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
// The IP events arrive on the event task; the listeners publish states, so notify them here
if (this->ip_state_pending_.load(std::memory_order_acquire) != 0) {
this->ip_state_pending_.store(0, std::memory_order_relaxed);
this->notify_ip_state_listeners_();
}
#endif
switch (this->state_) {
case EthernetComponentState::STOPPED:
if (this->started_) {
@@ -770,7 +778,8 @@ void EthernetComponent::eth_event_handler(void *arg, esp_event_base_t event_base
event_name = "ETH connected";
// For static IP configurations, GOT_IP event may not fire, so notify IP listeners here
#if defined(USE_ETHERNET_IP_STATE_LISTENERS) && defined(USE_ETHERNET_MANUAL_IP)
global_eth_component->notify_ip_state_listeners_();
global_eth_component->ip_state_pending_.store(1, std::memory_order_release);
global_eth_component->enable_loop_soon_any_context();
#endif
#if USE_NETWORK_IPV6
// Start SLAAC on link-up, not after the DHCPv4 lease. This also restores the
@@ -798,6 +807,9 @@ void EthernetComponent::got_ip_event_handler(void *arg, esp_event_base_t event_b
const esp_netif_ip_info_t *ip_info = &event->ip_info;
ESP_LOGV(TAG, "[Ethernet event] ETH Got IP " IPSTR, IP2STR(&ip_info->ip));
global_eth_component->got_ipv4_address_ = true;
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
global_eth_component->ip_state_pending_.store(1, std::memory_order_release);
#endif
#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
global_eth_component->connected_ = global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT;
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
@@ -805,9 +817,6 @@ void EthernetComponent::got_ip_event_handler(void *arg, esp_event_base_t event_b
global_eth_component->connected_ = true;
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
#endif /* USE_NETWORK_IPV6 */
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
global_eth_component->notify_ip_state_listeners_();
#endif
}
#if USE_NETWORK_IPV6
@@ -819,6 +828,9 @@ void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_
// after a link flap fires another event for the same address.
struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
global_eth_component->ipv6_count_ = esp_netif_get_all_ip6(global_eth_component->eth_netif_, if_ip6s);
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
global_eth_component->ip_state_pending_.store(1, std::memory_order_release);
#endif
#if (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
global_eth_component->connected_ =
global_eth_component->got_ipv4_address_ && (global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT);
@@ -827,9 +839,6 @@ void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_
global_eth_component->connected_ = global_eth_component->got_ipv4_address_;
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
#endif
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
global_eth_component->notify_ip_state_listeners_();
#endif
}
#endif /* USE_NETWORK_IPV6 */
+12 -25
View File
@@ -50,34 +50,25 @@ static constexpr uint8_t STATE_TEMPLATE[STATE_MESSAGE_LENGTH] PROGMEM = {
static constexpr uint8_t OFF_FRAME[STATE_MESSAGE_LENGTH_UTIL] PROGMEM = {0x00, 0x08, 0x08, BYTE3_POWER_OFF,
255 - BYTE3_POWER_OFF};
struct Code {
uint8_t value; // a climate enum, all of which are uint8_t
uint8_t code;
};
static constexpr Code MODES[] PROGMEM = {
using climate_ir::find_ir_code;
using climate_ir::from_ir_code;
using climate_ir::IRCode;
using climate_ir::to_ir_code;
// A received mode that is not listed is ignored
static constexpr IRCode MODES[] PROGMEM = {
{climate::CLIMATE_MODE_HEAT_COOL, BYTE7_MODE_AUTO},
{climate::CLIMATE_MODE_COOL, 0x80},
{climate::CLIMATE_MODE_DRY, 0x40},
{climate::CLIMATE_MODE_FAN_ONLY, 0xC0},
{climate::CLIMATE_MODE_HEAT, 0x20},
};
static constexpr Code FANS[] PROGMEM = {
static constexpr IRCode FANS[] PROGMEM = {
{climate::CLIMATE_FAN_AUTO, BYTE8_FAN_AUTO}, {climate::CLIMATE_FAN_HIGH, 0x80},
{climate::CLIMATE_FAN_MEDIUM, 0x40}, {climate::CLIMATE_FAN_LOW, 0xC0},
{climate::CLIMATE_FAN_QUIET, 0x20},
};
// The entry whose value (or code) matches key. Entries are copied out of PROGMEM.
template<size_t N> static optional<Code> find_entry(const Code (&table)[N], uint8_t key, bool match_code) {
for (const Code &slot : table) {
Code entry;
progmem_memcpy(&entry, &slot, sizeof(entry));
if ((match_code ? entry.code : entry.value) == key)
return entry;
}
return {};
}
// The temperature code is the 4-bit reversal of (F - TEMP_MIN) / TEMP_STEP; the mapping is its own inverse.
static uint8_t reverse_nibble(uint8_t value) { return reverse_bits(value) >> 4; }
@@ -151,14 +142,11 @@ void FriedrichClimate::transmit_state() {
temperature += temperature % 2;
this->target_temperature = temperature;
const auto mode = find_entry(MODES, this->mode, false);
const auto fan = find_entry(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO), false);
uint8_t frame[STATE_MESSAGE_LENGTH];
progmem_memcpy(frame, STATE_TEMPLATE, sizeof(frame));
frame[6] |= reverse_nibble((temperature - TEMP_MIN) / TEMP_STEP);
frame[7] = mode.has_value() ? mode->code : BYTE7_MODE_AUTO;
frame[8] = fan.has_value() ? fan->code : BYTE8_FAN_AUTO;
frame[7] = to_ir_code(MODES, this->mode, BYTE7_MODE_AUTO);
frame[8] = to_ir_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO), BYTE8_FAN_AUTO);
if (this->swing_mode != climate::CLIMATE_SWING_OFF)
frame[8] |= BYTE8_FAN_SWING;
frame[STATE_MESSAGE_LENGTH - 1] = checksum_state(frame);
@@ -209,7 +197,7 @@ bool FriedrichClimate::on_receive(remote_base::RemoteReceiveData src) {
return false;
// Ignore frames with a mode this component does not expose (e.g. MIN_HEAT, COIL_DRY)
const auto mode = find_entry(MODES, d[7], true);
const auto mode = find_ir_code(MODES, d[7], true);
if (!mode.has_value()) {
ESP_LOGV(TAG, "Ignoring unknown mode 0x%02X", d[7]);
return false;
@@ -224,8 +212,7 @@ bool FriedrichClimate::on_receive(remote_base::RemoteReceiveData src) {
this->target_temperature = TEMP_MIN + temp_index * TEMP_STEP;
this->swing_mode = (d[8] & BYTE8_FAN_SWING) ? climate::CLIMATE_SWING_VERTICAL : climate::CLIMATE_SWING_OFF;
const auto fan = find_entry(FANS, d[8] & ~BYTE8_FAN_SWING, true);
this->fan_mode = fan.has_value() ? static_cast<climate::ClimateFanMode>(fan->value) : climate::CLIMATE_FAN_AUTO;
this->fan_mode = from_ir_code(FANS, d[8] & ~BYTE8_FAN_SWING, climate::CLIMATE_FAN_AUTO);
this->publish_state();
return true;
@@ -7,6 +7,26 @@ namespace esphome::hitachi_ac344 {
ESPHOME_LOG_TAG(TAG, "climate.hitachi_ac344");
using climate_ir::find_ir_code;
using climate_ir::from_ir_code;
using climate_ir::IRCode;
using climate_ir::to_ir_code;
// A received code that is not listed is ignored
static constexpr IRCode MODES[] PROGMEM = {
{climate::CLIMATE_MODE_COOL, HITACHI_AC344_MODE_COOL},
{climate::CLIMATE_MODE_DRY, HITACHI_AC344_MODE_DRY},
{climate::CLIMATE_MODE_HEAT, HITACHI_AC344_MODE_HEAT},
{climate::CLIMATE_MODE_HEAT_COOL, HITACHI_AC344_MODE_AUTO},
{climate::CLIMATE_MODE_FAN_ONLY, HITACHI_AC344_MODE_FAN},
};
// The max speed is only received
static constexpr IRCode FANS[] PROGMEM = {
{climate::CLIMATE_FAN_AUTO, HITACHI_AC344_FAN_AUTO}, {climate::CLIMATE_FAN_QUIET, HITACHI_AC344_FAN_MIN},
{climate::CLIMATE_FAN_LOW, HITACHI_AC344_FAN_LOW}, {climate::CLIMATE_FAN_MEDIUM, HITACHI_AC344_FAN_MEDIUM},
{climate::CLIMATE_FAN_HIGH, HITACHI_AC344_FAN_HIGH}, {climate::CLIMATE_FAN_HIGH, HITACHI_AC344_FAN_MAX},
};
void set_bits(uint8_t *const dst, const uint8_t offset, const uint8_t nbits, const uint8_t data) {
if (offset >= 8 || !nbits)
return; // Short circuit as it won't change.
@@ -166,49 +186,17 @@ void HitachiClimate::setup() {
void HitachiClimate::transmit_state() { this->transmit_state_(HITACHI_AC344_BUTTON_POWER); }
void HitachiClimate::transmit_state_(uint8_t button) {
switch (this->mode) {
case climate::CLIMATE_MODE_COOL:
set_mode_(HITACHI_AC344_MODE_COOL);
break;
case climate::CLIMATE_MODE_DRY:
set_mode_(HITACHI_AC344_MODE_DRY);
break;
case climate::CLIMATE_MODE_HEAT:
set_mode_(HITACHI_AC344_MODE_HEAT);
break;
case climate::CLIMATE_MODE_HEAT_COOL:
set_mode_(HITACHI_AC344_MODE_AUTO);
break;
case climate::CLIMATE_MODE_FAN_ONLY:
set_mode_(HITACHI_AC344_MODE_FAN);
break;
case climate::CLIMATE_MODE_OFF:
set_power_(false);
break;
default:
ESP_LOGW(TAG, "Unsupported mode: %s", LOG_STR_ARG(climate_mode_to_string(this->mode)));
if (const auto entry = find_ir_code(MODES, this->mode, false)) {
set_mode_(entry->code);
} else if (this->mode == climate::CLIMATE_MODE_OFF) {
set_power_(false);
} else {
ESP_LOGW(TAG, "Unsupported mode: %s", LOG_STR_ARG(climate_mode_to_string(this->mode)));
}
set_temp_(static_cast<uint8_t>(this->target_temperature));
switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
case climate::CLIMATE_FAN_QUIET:
set_fan_(HITACHI_AC344_FAN_MIN);
break;
case climate::CLIMATE_FAN_LOW:
set_fan_(HITACHI_AC344_FAN_LOW);
break;
case climate::CLIMATE_FAN_MEDIUM:
set_fan_(HITACHI_AC344_FAN_MEDIUM);
break;
case climate::CLIMATE_FAN_HIGH:
set_fan_(HITACHI_AC344_FAN_HIGH);
break;
case climate::CLIMATE_FAN_ON:
case climate::CLIMATE_FAN_AUTO:
default:
set_fan_(HITACHI_AC344_FAN_AUTO);
}
set_fan_(to_ir_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO), HITACHI_AC344_FAN_AUTO));
const bool swing_v = swing_vertical(this->swing_mode);
const bool swing_h =
@@ -254,28 +242,10 @@ bool HitachiClimate::parse_mode_(const uint8_t remote_state[]) {
ESP_LOGV(TAG, "Power: %02X %02X", remote_state[HITACHI_AC344_POWER_BYTE], power);
uint8_t mode = remote_state[HITACHI_AC344_MODE_BYTE] & 0xF;
ESP_LOGV(TAG, "Mode: %02X %02X", remote_state[HITACHI_AC344_MODE_BYTE], mode);
if (power == HITACHI_AC344_POWER_ON) {
switch (mode) {
case HITACHI_AC344_MODE_COOL:
this->mode = climate::CLIMATE_MODE_COOL;
break;
case HITACHI_AC344_MODE_DRY:
this->mode = climate::CLIMATE_MODE_DRY;
break;
case HITACHI_AC344_MODE_HEAT:
this->mode = climate::CLIMATE_MODE_HEAT;
break;
case HITACHI_AC344_MODE_AUTO:
this->mode = climate::CLIMATE_MODE_HEAT_COOL;
break;
case HITACHI_AC344_MODE_FAN:
this->mode = climate::CLIMATE_MODE_FAN_ONLY;
break;
default:
break;
}
} else {
if (power != HITACHI_AC344_POWER_ON) {
this->mode = climate::CLIMATE_MODE_OFF;
} else {
this->mode = from_ir_code(MODES, mode, this->mode);
}
return true;
}
@@ -292,26 +262,8 @@ bool HitachiClimate::parse_temperature_(const uint8_t remote_state[]) {
bool HitachiClimate::parse_fan_(const uint8_t remote_state[]) {
uint8_t fan_mode = remote_state[HITACHI_AC344_FAN_BYTE] >> 4 & 0xF;
ESP_LOGV(TAG, "Fan: %02X %02X", remote_state[HITACHI_AC344_FAN_BYTE], fan_mode);
switch (fan_mode) {
case HITACHI_AC344_FAN_MIN:
this->fan_mode = climate::CLIMATE_FAN_QUIET;
break;
case HITACHI_AC344_FAN_LOW:
this->fan_mode = climate::CLIMATE_FAN_LOW;
break;
case HITACHI_AC344_FAN_MEDIUM:
this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
break;
case HITACHI_AC344_FAN_HIGH:
case HITACHI_AC344_FAN_MAX:
this->fan_mode = climate::CLIMATE_FAN_HIGH;
break;
case HITACHI_AC344_FAN_AUTO:
this->fan_mode = climate::CLIMATE_FAN_AUTO;
break;
default:
break;
}
if (const auto entry = find_ir_code(FANS, fan_mode, true))
this->fan_mode = static_cast<climate::ClimateFanMode>(entry->value);
return true;
}
@@ -5,6 +5,26 @@ namespace esphome::hitachi_ac424 {
ESPHOME_LOG_TAG(TAG, "climate.hitachi_ac424");
using climate_ir::find_ir_code;
using climate_ir::from_ir_code;
using climate_ir::IRCode;
using climate_ir::to_ir_code;
// A received code that is not listed is ignored
static constexpr IRCode MODES[] PROGMEM = {
{climate::CLIMATE_MODE_COOL, HITACHI_AC424_MODE_COOL},
{climate::CLIMATE_MODE_DRY, HITACHI_AC424_MODE_DRY},
{climate::CLIMATE_MODE_HEAT, HITACHI_AC424_MODE_HEAT},
{climate::CLIMATE_MODE_HEAT_COOL, HITACHI_AC424_MODE_AUTO},
{climate::CLIMATE_MODE_FAN_ONLY, HITACHI_AC424_MODE_FAN},
};
// The min and max speeds are only received
static constexpr IRCode FANS[] PROGMEM = {
{climate::CLIMATE_FAN_AUTO, HITACHI_AC424_FAN_AUTO}, {climate::CLIMATE_FAN_LOW, HITACHI_AC424_FAN_LOW},
{climate::CLIMATE_FAN_MEDIUM, HITACHI_AC424_FAN_MEDIUM}, {climate::CLIMATE_FAN_HIGH, HITACHI_AC424_FAN_HIGH},
{climate::CLIMATE_FAN_HIGH, HITACHI_AC424_FAN_MAX}, {climate::CLIMATE_FAN_LOW, HITACHI_AC424_FAN_MIN},
};
void set_bits(uint8_t *const dst, const uint8_t offset, const uint8_t nbits, const uint8_t data) {
if (offset >= 8 || !nbits)
return; // Short circuit as it won't change.
@@ -142,46 +162,17 @@ uint8_t HitachiClimate::get_button_() { return remote_state_[HITACHI_AC424_BUTTO
void HitachiClimate::set_button_(uint8_t button) { remote_state_[HITACHI_AC424_BUTTON_BYTE] = button; }
void HitachiClimate::transmit_state() {
switch (this->mode) {
case climate::CLIMATE_MODE_COOL:
set_mode_(HITACHI_AC424_MODE_COOL);
break;
case climate::CLIMATE_MODE_DRY:
set_mode_(HITACHI_AC424_MODE_DRY);
break;
case climate::CLIMATE_MODE_HEAT:
set_mode_(HITACHI_AC424_MODE_HEAT);
break;
case climate::CLIMATE_MODE_HEAT_COOL:
set_mode_(HITACHI_AC424_MODE_AUTO);
break;
case climate::CLIMATE_MODE_FAN_ONLY:
set_mode_(HITACHI_AC424_MODE_FAN);
break;
case climate::CLIMATE_MODE_OFF:
set_power_(false);
break;
default:
ESP_LOGW(TAG, "Unsupported mode: %s", LOG_STR_ARG(climate_mode_to_string(this->mode)));
if (const auto entry = find_ir_code(MODES, this->mode, false)) {
set_mode_(entry->code);
} else if (this->mode == climate::CLIMATE_MODE_OFF) {
set_power_(false);
} else {
ESP_LOGW(TAG, "Unsupported mode: %s", LOG_STR_ARG(climate_mode_to_string(this->mode)));
}
set_temp_(static_cast<uint8_t>(this->target_temperature));
switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
case climate::CLIMATE_FAN_LOW:
set_fan_(HITACHI_AC424_FAN_LOW);
break;
case climate::CLIMATE_FAN_MEDIUM:
set_fan_(HITACHI_AC424_FAN_MEDIUM);
break;
case climate::CLIMATE_FAN_HIGH:
set_fan_(HITACHI_AC424_FAN_HIGH);
break;
case climate::CLIMATE_FAN_ON:
case climate::CLIMATE_FAN_AUTO:
default:
set_fan_(HITACHI_AC424_FAN_AUTO);
}
set_fan_(to_ir_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO), HITACHI_AC424_FAN_AUTO));
switch (this->swing_mode) {
case climate::CLIMATE_SWING_BOTH:
@@ -230,28 +221,10 @@ bool HitachiClimate::parse_mode_(const uint8_t remote_state[]) {
ESP_LOGV(TAG, "Power: %02X %02X", remote_state[HITACHI_AC424_POWER_BYTE], power);
uint8_t mode = remote_state[HITACHI_AC424_MODE_BYTE] & 0xF;
ESP_LOGV(TAG, "Mode: %02X %02X", remote_state[HITACHI_AC424_MODE_BYTE], mode);
if (power == HITACHI_AC424_POWER_ON) {
switch (mode) {
case HITACHI_AC424_MODE_COOL:
this->mode = climate::CLIMATE_MODE_COOL;
break;
case HITACHI_AC424_MODE_DRY:
this->mode = climate::CLIMATE_MODE_DRY;
break;
case HITACHI_AC424_MODE_HEAT:
this->mode = climate::CLIMATE_MODE_HEAT;
break;
case HITACHI_AC424_MODE_AUTO:
this->mode = climate::CLIMATE_MODE_HEAT_COOL;
break;
case HITACHI_AC424_MODE_FAN:
this->mode = climate::CLIMATE_MODE_FAN_ONLY;
break;
default:
break;
}
} else {
if (power != HITACHI_AC424_POWER_ON) {
this->mode = climate::CLIMATE_MODE_OFF;
} else {
this->mode = from_ir_code(MODES, mode, this->mode);
}
return true;
}
@@ -268,24 +241,8 @@ bool HitachiClimate::parse_temperature_(const uint8_t remote_state[]) {
bool HitachiClimate::parse_fan_(const uint8_t remote_state[]) {
uint8_t fan_mode = remote_state[HITACHI_AC424_FAN_BYTE] >> 4 & 0xF;
ESP_LOGV(TAG, "Fan: %02X %02X", remote_state[HITACHI_AC424_FAN_BYTE], fan_mode);
switch (fan_mode) {
case HITACHI_AC424_FAN_MIN:
case HITACHI_AC424_FAN_LOW:
this->fan_mode = climate::CLIMATE_FAN_LOW;
break;
case HITACHI_AC424_FAN_MEDIUM:
this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
break;
case HITACHI_AC424_FAN_HIGH:
case HITACHI_AC424_FAN_MAX:
this->fan_mode = climate::CLIMATE_FAN_HIGH;
break;
case HITACHI_AC424_FAN_AUTO:
this->fan_mode = climate::CLIMATE_FAN_AUTO;
break;
default:
break;
}
if (const auto entry = find_ir_code(FANS, fan_mode, true))
this->fan_mode = static_cast<climate::ClimateFanMode>(entry->value);
return true;
}
+7 -11
View File
@@ -21,19 +21,16 @@ constexpr uint8_t MISTRAL_POWER_ON = 0x20;
constexpr uint8_t MISTRAL_TEMP_OFFSET = 0x9F;
constexpr uint8_t MISTRAL_MODE_AUTO = 0x10;
// The tables live in flash, so entries are copied out before use
struct ModeCode {
climate::ClimateMode mode;
uint8_t code;
};
static const ModeCode MODES[] PROGMEM = {
// OFF sends the auto mode code and only clears the power byte
static constexpr climate_ir::IRCode MODES[] PROGMEM = {
{climate::CLIMATE_MODE_HEAT_COOL, MISTRAL_MODE_AUTO},
{climate::CLIMATE_MODE_COOL, 0xC0},
{climate::CLIMATE_MODE_DRY, 0x40},
{climate::CLIMATE_MODE_FAN_ONLY, 0x80},
{climate::CLIMATE_MODE_HEAT, 0x60},
{climate::CLIMATE_MODE_HEAT_COOL, MISTRAL_MODE_AUTO},
};
// Each fan speed sets two bytes; the table lives in flash, so entries are copied out before use
struct FanCode {
climate::ClimateFanMode fan;
uint8_t byte1;
@@ -78,7 +75,6 @@ static bool is_valid_frame(const uint8_t *frame) {
}
void MistralIR::transmit_state() {
const auto mode = find_entry(MODES, [this](const ModeCode &m) { return m.mode == this->mode; });
const climate::ClimateFanMode fan_mode = this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO);
const FanCode fan =
find_entry(FANS, [fan_mode](const FanCode &f) { return f.fan == fan_mode; }).value_or(load_entry(FANS[0]));
@@ -88,7 +84,7 @@ void MistralIR::transmit_state() {
progmem_memcpy(frame, FRAME_TEMPLATE, sizeof(frame));
frame[1] = fan.byte1;
frame[3] = this->mode == climate::CLIMATE_MODE_OFF ? MISTRAL_POWER_OFF : MISTRAL_POWER_ON;
frame[4] = mode.has_value() ? mode->code : MISTRAL_MODE_AUTO;
frame[4] = climate_ir::to_ir_code(MODES, this->mode, MISTRAL_MODE_AUTO);
frame[5] = reverse_bits(static_cast<uint8_t>(MISTRAL_TEMP_OFFSET - temp));
frame[6] = fan.byte6;
frame[11] = compute_checksum(frame);
@@ -116,12 +112,12 @@ bool MistralIR::on_receive(remote_base::RemoteReceiveData data) {
return false;
// Decode everything before touching the state so an unknown code leaves it untouched
const auto mode = find_entry(MODES, [frame](const ModeCode &m) { return m.code == frame[4]; });
const auto mode = climate_ir::find_ir_code(MODES, frame[4], true);
const auto fan = find_entry(FANS, [frame](const FanCode &f) { return f.byte1 == frame[1] && f.byte6 == frame[6]; });
if (!mode.has_value() || !fan.has_value())
return false;
this->mode = mode->mode;
this->mode = static_cast<climate::ClimateMode>(mode->value);
this->target_temperature =
clamp<float>(MISTRAL_TEMP_OFFSET - reverse_bits(frame[5]), MISTRAL_TEMP_MIN, MISTRAL_TEMP_MAX);
this->fan_mode = fan->fan;
+29 -72
View File
@@ -29,6 +29,24 @@ using IRNoblexFan = enum IRNoblexFan {
IR_NOBLEX_FAN_HIGH = 0b11,
};
using climate_ir::find_ir_code;
using climate_ir::from_ir_code;
using climate_ir::IRCode;
using climate_ir::to_ir_code;
// A mode not listed is sent as power off, and ignored when received
static constexpr IRCode MODES[] PROGMEM = {
{climate::CLIMATE_MODE_HEAT_COOL, IR_NOBLEX_MODE_AUTO}, {climate::CLIMATE_MODE_COOL, IR_NOBLEX_MODE_COOL},
{climate::CLIMATE_MODE_DRY, IR_NOBLEX_MODE_DRY}, {climate::CLIMATE_MODE_FAN_ONLY, IR_NOBLEX_MODE_FAN},
{climate::CLIMATE_MODE_HEAT, IR_NOBLEX_MODE_HEAT},
};
static constexpr IRCode FANS[] PROGMEM = {
{climate::CLIMATE_FAN_AUTO, IR_NOBLEX_FAN_AUTO},
{climate::CLIMATE_FAN_LOW, IR_NOBLEX_FAN_LOW},
{climate::CLIMATE_FAN_MEDIUM, IR_NOBLEX_FAN_MEDIUM},
{climate::CLIMATE_FAN_HIGH, IR_NOBLEX_FAN_HIGH},
};
// Transmit via IR the state of this climate controller.
void NoblexClimate::transmit_state() {
uint8_t remote_state[8] = {0x80, 0x10, 0x00, 0x0A, 0x50, 0x00, 0x20, 0x00}; // OFF, COOL, 24C, FAN_AUTO
@@ -44,47 +62,18 @@ void NoblexClimate::transmit_state() {
auto temp = (uint8_t) roundf(clamp<float>(this->target_temperature, NOBLEX_TEMP_MIN, NOBLEX_TEMP_MAX));
remote_state[1] = reverse_bits(uint8_t((temp - NOBLEX_TEMP_MIN) & 0x0F));
switch (this->mode) {
case climate::CLIMATE_MODE_HEAT_COOL:
remote_state[0] |= (IRNoblexMode::IR_NOBLEX_MODE_AUTO << 5);
if (const auto entry = find_ir_code(MODES, this->mode, false)) {
remote_state[0] |= entry->code << 5;
if (this->mode == climate::CLIMATE_MODE_HEAT_COOL)
remote_state[1] = 0x90; // NOBLEX_TEMP_MAP 25C
break;
case climate::CLIMATE_MODE_COOL:
remote_state[0] |= (IRNoblexMode::IR_NOBLEX_MODE_COOL << 5);
break;
case climate::CLIMATE_MODE_DRY:
remote_state[0] |= (IRNoblexMode::IR_NOBLEX_MODE_DRY << 5);
break;
case climate::CLIMATE_MODE_FAN_ONLY:
remote_state[0] |= (IRNoblexMode::IR_NOBLEX_MODE_FAN << 5);
break;
case climate::CLIMATE_MODE_HEAT:
remote_state[0] |= (IRNoblexMode::IR_NOBLEX_MODE_HEAT << 5);
break;
case climate::CLIMATE_MODE_OFF:
default:
powered_on = false;
this->powered_on_assumed = powered_on;
remote_state[0] &= 0xEF;
remote_state[2] = 0x00;
break;
} else {
powered_on = false;
this->powered_on_assumed = powered_on;
remote_state[0] &= 0xEF;
remote_state[2] = 0x00;
}
switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
case climate::CLIMATE_FAN_LOW:
remote_state[0] |= (IRNoblexFan::IR_NOBLEX_FAN_LOW << 2);
break;
case climate::CLIMATE_FAN_MEDIUM:
remote_state[0] |= (IRNoblexFan::IR_NOBLEX_FAN_MEDIUM << 2);
break;
case climate::CLIMATE_FAN_HIGH:
remote_state[0] |= (IRNoblexFan::IR_NOBLEX_FAN_HIGH << 2);
break;
case climate::CLIMATE_FAN_AUTO:
default:
remote_state[0] |= (IRNoblexFan::IR_NOBLEX_FAN_AUTO << 2);
break;
}
remote_state[0] |= to_ir_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO), IR_NOBLEX_FAN_AUTO) << 2;
switch (this->swing_mode) {
case climate::CLIMATE_SWING_VERTICAL:
@@ -236,25 +225,7 @@ bool NoblexClimate::on_receive(remote_base::RemoteReceiveData data) {
if (powered_on_assumed) {
auto mode = (this->remote_state_[0] & 0xE0) >> 5;
ESP_LOGV(TAG, "Mode: %02X", mode);
switch (mode) {
case IRNoblexMode::IR_NOBLEX_MODE_AUTO:
this->mode = climate::CLIMATE_MODE_HEAT_COOL;
break;
case IRNoblexMode::IR_NOBLEX_MODE_COOL:
this->mode = climate::CLIMATE_MODE_COOL;
break;
case IRNoblexMode::IR_NOBLEX_MODE_DRY:
this->mode = climate::CLIMATE_MODE_DRY;
break;
case IRNoblexMode::IR_NOBLEX_MODE_FAN:
this->mode = climate::CLIMATE_MODE_FAN_ONLY;
break;
case IRNoblexMode::IR_NOBLEX_MODE_HEAT:
this->mode = climate::CLIMATE_MODE_HEAT;
break;
default:
break;
}
this->mode = from_ir_code(MODES, mode, this->mode);
}
// Set received temp
@@ -269,21 +240,7 @@ bool NoblexClimate::on_receive(remote_base::RemoteReceiveData data) {
// Set received fan speed
auto fan = (this->remote_state_[0] & 0x0C) >> 2;
ESP_LOGV(TAG, "Fan: %02X", fan);
switch (fan) {
case IRNoblexFan::IR_NOBLEX_FAN_HIGH:
this->fan_mode = climate::CLIMATE_FAN_HIGH;
break;
case IRNoblexFan::IR_NOBLEX_FAN_MEDIUM:
this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
break;
case IRNoblexFan::IR_NOBLEX_FAN_LOW:
this->fan_mode = climate::CLIMATE_FAN_LOW;
break;
case IRNoblexFan::IR_NOBLEX_FAN_AUTO:
default:
this->fan_mode = climate::CLIMATE_FAN_AUTO;
break;
}
this->fan_mode = from_ir_code(FANS, fan, climate::CLIMATE_FAN_AUTO);
// Set received swing status
if (this->remote_state_[0] & 0x02) {
+15 -33
View File
@@ -47,46 +47,28 @@ static bool is_powered_on(const uint8_t *state) {
return get_field(state, POWER_1) == POWER_ON && get_field(state, POWER_2) == POWER_ON;
}
struct Code {
uint8_t value; // a climate enum, all of which are uint8_t
uint8_t code;
};
// The first entry of each table is the fallback in both directions
static constexpr Code MODES[] PROGMEM = {
using climate_ir::from_ir_code;
using climate_ir::IRCode;
using climate_ir::to_ir_code;
// Codes not listed fall back to auto (mode and fan) and to swing off
static constexpr IRCode MODES[] PROGMEM = {
{climate::CLIMATE_MODE_HEAT_COOL, 0}, {climate::CLIMATE_MODE_COOL, 1}, {climate::CLIMATE_MODE_DRY, 2},
{climate::CLIMATE_MODE_FAN_ONLY, 3}, {climate::CLIMATE_MODE_HEAT, 4},
};
static constexpr Code FANS[] PROGMEM = {
static constexpr IRCode FANS[] PROGMEM = {
{climate::CLIMATE_FAN_AUTO, 0},
{climate::CLIMATE_FAN_LOW, 2},
{climate::CLIMATE_FAN_MEDIUM, 4},
{climate::CLIMATE_FAN_HIGH, 5},
};
static constexpr Code SWINGS[] PROGMEM = {
static constexpr IRCode SWINGS[] PROGMEM = {
{climate::CLIMATE_SWING_OFF, 0b111},
{climate::CLIMATE_SWING_VERTICAL, 0b010},
{climate::CLIMATE_SWING_HORIZONTAL, 0b011},
{climate::CLIMATE_SWING_BOTH, 0b100},
};
// Returns the entry whose value (or code) matches key, else the first entry. Entries are copied out of PROGMEM.
static Code find_entry(const Code *table, size_t count, uint8_t key, bool match_code) {
Code entry{};
for (size_t i = 0; i < count; i++) {
progmem_memcpy(&entry, &table[i], sizeof(entry));
if ((match_code ? entry.code : entry.value) == key)
return entry;
}
progmem_memcpy(&entry, table, sizeof(entry));
return entry;
}
template<size_t N> static uint8_t to_code(const Code (&table)[N], uint8_t value) {
return find_entry(table, N, value, false).code;
}
template<typename T, size_t N> static T from_code(const Code (&table)[N], uint8_t code) {
return static_cast<T>(find_entry(table, N, code, true).value);
}
// Inverted count of the one bits in a section, skipping the two nibbles that hold the checksum itself.
// See https://github.com/crankyoldgit/IRremoteESP8266/issues/1538#issuecomment-894645947
static uint8_t section_checksum(const uint8_t *section) {
@@ -123,13 +105,13 @@ void SamsungClimate::transmit_state() {
if (on) {
progmem_memcpy(state, RESET_STATE, SAMSUNG_AC_STATE_LENGTH);
set_field(state, MODE, to_code(MODES, this->mode));
set_field(state, MODE, to_ir_code(MODES, this->mode, 0));
} // else keep the last mode so the power off frame still carries a valid state
const uint8_t temp = clamp<uint8_t>(this->target_temperature, SAMSUNG_AC_TEMP_MIN, SAMSUNG_AC_TEMP_MAX);
set_field(state, TEMP, temp - SAMSUNG_AC_TEMP_MIN);
set_field(state, SWING, to_code(SWINGS, this->swing_mode));
set_field(state, FAN, to_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO)));
set_field(state, SWING, to_ir_code(SWINGS, this->swing_mode, 0b111));
set_field(state, FAN, to_ir_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO), 0));
set_field(state, POWER_1, on ? POWER_ON : 0);
set_field(state, POWER_2, on ? POWER_ON : 0);
@@ -212,11 +194,11 @@ bool SamsungClimate::on_receive(remote_base::RemoteReceiveData data) {
memcpy(state, frame, SAMSUNG_AC_SECTION_LENGTH);
memcpy(state + SAMSUNG_AC_SECTION_LENGTH, frame + length - SAMSUNG_AC_SECTION_LENGTH, SAMSUNG_AC_SECTION_LENGTH);
this->swing_mode = from_code<climate::ClimateSwingMode>(SWINGS, get_field(state, SWING));
this->swing_mode = from_ir_code(SWINGS, get_field(state, SWING), climate::CLIMATE_SWING_OFF);
this->target_temperature = get_field(state, TEMP) + SAMSUNG_AC_TEMP_MIN;
this->fan_mode = from_code<climate::ClimateFanMode>(FANS, get_field(state, FAN));
this->mode =
is_powered_on(state) ? from_code<climate::ClimateMode>(MODES, get_field(state, MODE)) : climate::CLIMATE_MODE_OFF;
this->fan_mode = from_ir_code(FANS, get_field(state, FAN), climate::CLIMATE_FAN_AUTO);
this->mode = is_powered_on(state) ? from_ir_code(MODES, get_field(state, MODE), climate::CLIMATE_MODE_HEAT_COOL)
: climate::CLIMATE_MODE_OFF;
ESP_LOGD(TAG, "Received %u byte frame, power %s", length, ONOFF(is_powered_on(state)));
this->publish_state();
@@ -6,70 +6,32 @@ namespace esphome::siemens_ira211 {
using namespace remote_base;
struct PresetMapping {
climate::ClimatePreset preset;
IRA211Mode mode;
// Presets map to IRA211Mode and fan modes to IRA211Fan. Values the other side does not know fall back to comfort
// and auto.
static constexpr climate_ir::IRCode PRESETS[] PROGMEM = {
{climate::CLIMATE_PRESET_COMFORT, static_cast<uint8_t>(IRA211Mode::IRA211_MODE_COMFORT)},
{climate::CLIMATE_PRESET_ECO, static_cast<uint8_t>(IRA211Mode::IRA211_MODE_TIMER)},
};
static constexpr climate_ir::IRCode FANS[] PROGMEM = {
{climate::CLIMATE_FAN_AUTO, static_cast<uint8_t>(IRA211Fan::IRA211_FAN_AUTO)},
{climate::CLIMATE_FAN_LOW, static_cast<uint8_t>(IRA211Fan::IRA211_FAN_LOW)},
{climate::CLIMATE_FAN_MEDIUM, static_cast<uint8_t>(IRA211Fan::IRA211_FAN_MEDIUM)},
{climate::CLIMATE_FAN_HIGH, static_cast<uint8_t>(IRA211Fan::IRA211_FAN_HIGH)},
};
static const PresetMapping PRESETS[] PROGMEM = {
{climate::CLIMATE_PRESET_COMFORT, IRA211Mode::IRA211_MODE_COMFORT},
{climate::CLIMATE_PRESET_ECO, IRA211Mode::IRA211_MODE_TIMER},
};
struct FanMapping {
climate::ClimateFanMode fan_mode;
IRA211Fan fan;
};
static const FanMapping FANS[] PROGMEM = {
{climate::CLIMATE_FAN_AUTO, IRA211Fan::IRA211_FAN_AUTO},
{climate::CLIMATE_FAN_LOW, IRA211Fan::IRA211_FAN_LOW},
{climate::CLIMATE_FAN_MEDIUM, IRA211Fan::IRA211_FAN_MEDIUM},
{climate::CLIMATE_FAN_HIGH, IRA211Fan::IRA211_FAN_HIGH},
};
// The tables live in flash, so entries are copied out before use
template<typename T> static T load_entry(const T &slot) {
T entry;
progmem_memcpy(&entry, &slot, sizeof(T));
return entry;
}
// The first entry of each table is the fallback for values the other side does not know
static IRA211Mode to_ira211_mode(climate::ClimatePreset preset) {
for (const auto &slot : PRESETS) {
const auto entry = load_entry(slot);
if (entry.preset == preset)
return entry.mode;
}
return load_entry(PRESETS[0]).mode;
return static_cast<IRA211Mode>(
climate_ir::to_ir_code(PRESETS, preset, static_cast<uint8_t>(IRA211Mode::IRA211_MODE_COMFORT)));
}
static climate::ClimatePreset to_climate_preset(IRA211Mode mode) {
for (const auto &slot : PRESETS) {
const auto entry = load_entry(slot);
if (entry.mode == mode)
return entry.preset;
}
return load_entry(PRESETS[0]).preset;
return climate_ir::from_ir_code(PRESETS, static_cast<uint8_t>(mode), climate::CLIMATE_PRESET_COMFORT);
}
static IRA211Fan to_ira211_fan(climate::ClimateFanMode fan_mode) {
for (const auto &slot : FANS) {
const auto entry = load_entry(slot);
if (entry.fan_mode == fan_mode)
return entry.fan;
}
return load_entry(FANS[0]).fan;
return static_cast<IRA211Fan>(
climate_ir::to_ir_code(FANS, fan_mode, static_cast<uint8_t>(IRA211Fan::IRA211_FAN_AUTO)));
}
static climate::ClimateFanMode to_climate_fan_mode(IRA211Fan fan) {
for (const auto &slot : FANS) {
const auto entry = load_entry(slot);
if (entry.fan == fan)
return entry.fan_mode;
}
return load_entry(FANS[0]).fan_mode;
return climate_ir::from_ir_code(FANS, static_cast<uint8_t>(fan), climate::CLIMATE_FAN_AUTO);
}
void SiemensIRA211Climate::setup() {
+26 -77
View File
@@ -32,6 +32,24 @@ static constexpr remote_base::BitTimings TCL112_BIT_TIMINGS{
.bit_mark = TCL112_BIT_MARK, .one_space = 1100, .zero_space = 350};
const uint32_t TCL112_GAP = TCL112_HEADER_SPACE;
using climate_ir::find_ir_code;
using climate_ir::from_ir_code;
using climate_ir::IRCode;
using climate_ir::to_ir_code;
// A mode not listed is sent as power off, and ignored when received
static constexpr IRCode MODES[] PROGMEM = {
{climate::CLIMATE_MODE_HEAT_COOL, TCL112_AUTO}, {climate::CLIMATE_MODE_COOL, TCL112_COOL},
{climate::CLIMATE_MODE_HEAT, TCL112_HEAT}, {climate::CLIMATE_MODE_DRY, TCL112_DRY},
{climate::CLIMATE_MODE_FAN_ONLY, TCL112_FAN},
};
static constexpr IRCode FANS[] PROGMEM = {
{climate::CLIMATE_FAN_AUTO, TCL112_FAN_AUTO},
{climate::CLIMATE_FAN_HIGH, TCL112_FAN_HIGH},
{climate::CLIMATE_FAN_MEDIUM, TCL112_FAN_MED},
{climate::CLIMATE_FAN_LOW, TCL112_FAN_LOW},
};
void Tcl112Climate::transmit_state() {
uint8_t remote_state[TCL112_STATE_LENGTH] = {0};
@@ -46,31 +64,11 @@ void Tcl112Climate::transmit_state() {
remote_state[8] = 0x40;
// Set mode
switch (this->mode) {
case climate::CLIMATE_MODE_HEAT_COOL:
remote_state[6] &= 0xF0;
remote_state[6] |= TCL112_AUTO;
break;
case climate::CLIMATE_MODE_COOL:
remote_state[6] &= 0xF0;
remote_state[6] |= TCL112_COOL;
break;
case climate::CLIMATE_MODE_HEAT:
remote_state[6] &= 0xF0;
remote_state[6] |= TCL112_HEAT;
break;
case climate::CLIMATE_MODE_DRY:
remote_state[6] &= 0xF0;
remote_state[6] |= TCL112_DRY;
break;
case climate::CLIMATE_MODE_FAN_ONLY:
remote_state[6] &= 0xF0;
remote_state[6] |= TCL112_FAN;
break;
case climate::CLIMATE_MODE_OFF:
default:
remote_state[5] &= ~TCL112_POWER_MASK;
break;
if (const auto entry = find_ir_code(MODES, this->mode, false)) {
remote_state[6] &= 0xF0;
remote_state[6] |= entry->code;
} else {
remote_state[5] &= ~TCL112_POWER_MASK;
}
// Set temperature
@@ -88,22 +86,7 @@ void Tcl112Climate::transmit_state() {
remote_state[7] |= ((uint8_t) TCL112_TEMP_MAX - half_degrees / 2);
// Set fan
uint8_t selected_fan;
switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
case climate::CLIMATE_FAN_HIGH:
selected_fan = TCL112_FAN_HIGH;
break;
case climate::CLIMATE_FAN_MEDIUM:
selected_fan = TCL112_FAN_MED;
break;
case climate::CLIMATE_FAN_LOW:
selected_fan = TCL112_FAN_LOW;
break;
case climate::CLIMATE_FAN_AUTO:
default:
selected_fan = TCL112_FAN_AUTO;
}
remote_state[8] |= selected_fan;
remote_state[8] |= to_ir_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO), TCL112_FAN_AUTO);
// Set swing
if (this->swing_mode == climate::CLIMATE_SWING_VERTICAL) {
@@ -169,47 +152,13 @@ bool Tcl112Climate::on_receive(remote_base::RemoteReceiveData data) {
if ((remote_state[5] & TCL112_POWER_MASK) == 0) {
this->mode = climate::CLIMATE_MODE_OFF;
} else {
auto mode = remote_state[6] & 0x0F;
switch (mode) {
case TCL112_HEAT:
this->mode = climate::CLIMATE_MODE_HEAT;
break;
case TCL112_COOL:
this->mode = climate::CLIMATE_MODE_COOL;
break;
case TCL112_DRY:
this->mode = climate::CLIMATE_MODE_DRY;
break;
case TCL112_FAN:
this->mode = climate::CLIMATE_MODE_FAN_ONLY;
break;
case TCL112_AUTO:
this->mode = climate::CLIMATE_MODE_HEAT_COOL;
break;
default:
break;
}
this->mode = from_ir_code(MODES, remote_state[6] & 0x0F, this->mode);
}
auto temp = TCL112_TEMP_MAX - remote_state[7];
if (remote_state[12] & TCL112_HALF_DEGREE)
temp += .5f;
this->target_temperature = temp;
auto fan = remote_state[8] & 0x7;
switch (fan) {
case TCL112_FAN_HIGH:
this->fan_mode = climate::CLIMATE_FAN_HIGH;
break;
case TCL112_FAN_MED:
this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
break;
case TCL112_FAN_LOW:
this->fan_mode = climate::CLIMATE_FAN_LOW;
break;
case TCL112_FAN_AUTO:
default:
this->fan_mode = climate::CLIMATE_FAN_AUTO;
break;
}
this->fan_mode = from_ir_code(FANS, remote_state[8] & 0x7, climate::CLIMATE_FAN_AUTO);
if ((remote_state[8] & TCL112_VSWING_MASK) == TCL112_VSWING_MASK) {
this->swing_mode = climate::CLIMATE_SWING_VERTICAL;
} else {
+6 -2
View File
@@ -5,13 +5,15 @@ from esphome.const import CONF_MODEL
from esphome.types import ConfigType
AUTO_LOAD = ["climate_ir"]
CODEOWNERS = ["@glmnet", "@arpena"]
CODEOWNERS = ["@glmnet", "@arpena", "@Rodrigoah"]
whirlpool_ns = cg.esphome_ns.namespace("whirlpool")
WhirlpoolClimate = whirlpool_ns.class_("WhirlpoolClimate", climate_ir.ClimateIR)
Model = whirlpool_ns.enum("Model")
MODELS = Model.options("DG11J1-3A", "DG11J1-91", "HISENSE_ARG", prefix="MODEL_")
MODELS = Model.options(
"DG11J1-3A", "DG11J1-91", "HISENSE_ARG", "HITACHI_HCRA31NEWH", prefix="MODEL_"
)
CONFIG_SCHEMA = climate_ir.climate_ir_with_receiver_schema(WhirlpoolClimate).extend(
{
@@ -26,3 +28,5 @@ async def to_code(config: ConfigType) -> None:
# cv.enum keeps the YAML key as a str subclass; the MockObj value cannot be compared
if config[CONF_MODEL] == "HISENSE_ARG":
cg.add_define("USE_WHIRLPOOL_HISENSE_ARG")
elif config[CONF_MODEL] == "HITACHI_HCRA31NEWH":
cg.add_define("USE_WHIRLPOOL_HITACHI_HCRA31NEWH")
+85 -89
View File
@@ -2,6 +2,8 @@
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include <algorithm>
namespace esphome::whirlpool {
ESPHOME_LOG_TAG(TAG, "whirlpool.climate");
@@ -44,6 +46,35 @@ static constexpr uint8_t HISENSE_ARG_POWER_TOGGLE = 0x01; // byte 15
static constexpr uint8_t HISENSE_ARG_SETTINGS_ONLY = 0x02; // byte 15
static constexpr uint8_t HISENSE_ARG_FIXED_TEMP_STEP = 7; // auto and fan-only send 23 C
// Hitachi HCRA31NEWH model: power is a state in every frame
static constexpr uint8_t HITACHI_BYTE10 = 0x20; // byte 10
static constexpr uint8_t HITACHI_COOL = 0x02; // byte 15
static constexpr uint8_t HITACHI_OFF = 0x01; // byte 15
static constexpr uint8_t HITACHI_BYTE18_ON = 0x38; // byte 18
static constexpr uint8_t HITACHI_BYTE18_OFF = 0x28; // byte 18
static constexpr uint8_t HITACHI_BYTE19 = 0x08; // byte 19
// Header, 21 bytes of 8 bits, two dividers and the footer mark
static constexpr uint32_t WHIRLPOOL_FRAME_TIMINGS = 2 + WHIRLPOOL_STATE_LENGTH * 16 + 2 * 2 + 1;
using climate_ir::find_ir_code;
using climate_ir::from_ir_code;
using climate_ir::IRCode;
using climate_ir::to_ir_code;
// Off is not listed: it is sent as a power toggle or state
static constexpr IRCode MODES[] PROGMEM = {
{climate::CLIMATE_MODE_HEAT_COOL, WHIRLPOOL_AUTO}, {climate::CLIMATE_MODE_HEAT, WHIRLPOOL_HEAT},
{climate::CLIMATE_MODE_COOL, WHIRLPOOL_COOL}, {climate::CLIMATE_MODE_DRY, WHIRLPOOL_DRY},
{climate::CLIMATE_MODE_FAN_ONLY, WHIRLPOOL_FAN},
};
static constexpr IRCode FANS[] PROGMEM = {
{climate::CLIMATE_FAN_AUTO, WHIRLPOOL_FAN_AUTO},
{climate::CLIMATE_FAN_HIGH, WHIRLPOOL_FAN_HIGH},
{climate::CLIMATE_FAN_MEDIUM, WHIRLPOOL_FAN_MED},
{climate::CLIMATE_FAN_LOW, WHIRLPOOL_FAN_LOW},
};
WhirlpoolClimate::WhirlpoolClimate()
: climate_ir::ClimateIR(
WHIRLPOOL_DG11J1_3A_TEMP_MIN, WHIRLPOOL_DG11J1_3A_TEMP_MAX, 1.0f, true, true,
@@ -55,46 +86,26 @@ void WhirlpoolClimate::transmit_state() {
this->last_transmit_time_ = millis(); // setting the time of the last transmission.
uint8_t remote_state[WHIRLPOOL_STATE_LENGTH];
memset(remote_state, 0, sizeof(remote_state));
remote_state[0] = 0x83;
remote_state[1] = 0x06;
put_be16(remote_state, this->remote_id_());
remote_state[6] = 0x80;
remote_state[18] = 0x08; // the same on every supported model
remote_state[18] = 0x08;
auto powered_on = this->mode != climate::CLIMATE_MODE_OFF;
const bool power_toggle = powered_on != this->powered_on_assumed;
const bool powered_on = this->mode != climate::CLIMATE_MODE_OFF;
// Hitachi sends power as a state instead of a toggle
const bool power_toggle = !this->is_hitachi_hcra31newh_() && powered_on != this->powered_on_assumed;
if (power_toggle) {
// Set power toggle command
remote_state[2] = 4;
remote_state[15] = 1;
this->powered_on_assumed = powered_on;
}
switch (this->mode) {
case climate::CLIMATE_MODE_HEAT_COOL:
// set fan auto
// set temp auto temp
// set sleep false
remote_state[3] = WHIRLPOOL_AUTO;
remote_state[15] = 0x17;
break;
case climate::CLIMATE_MODE_HEAT:
remote_state[3] = WHIRLPOOL_HEAT;
remote_state[15] = 6;
break;
case climate::CLIMATE_MODE_COOL:
remote_state[3] = WHIRLPOOL_COOL;
remote_state[15] = 6;
break;
case climate::CLIMATE_MODE_DRY:
remote_state[3] = WHIRLPOOL_DRY;
remote_state[15] = 6;
break;
case climate::CLIMATE_MODE_FAN_ONLY:
remote_state[3] = WHIRLPOOL_FAN;
remote_state[15] = 6;
break;
case climate::CLIMATE_MODE_OFF:
default:
break;
this->powered_on_assumed = powered_on;
if (const auto entry = find_ir_code(MODES, this->mode, false)) {
remote_state[3] = entry->code;
// Auto also sets fan auto, auto temperature and sleep off
remote_state[15] = this->mode == climate::CLIMATE_MODE_HEAT_COOL ? 0x17 : 6;
} else if (this->mode == climate::CLIMATE_MODE_OFF && (this->is_hisense_arg_() || this->is_hitachi_hcra31newh_())) {
// These remotes keep the cool code in the power-off frame
remote_state[3] = WHIRLPOOL_COOL;
}
const bool hisense_arg = this->is_hisense_arg_();
@@ -102,6 +113,7 @@ void WhirlpoolClimate::transmit_state() {
// Temperature
auto temp = (uint8_t) roundf(clamp(this->target_temperature, this->temperature_min_(), this->temperature_max_()));
const auto temp_step = (uint8_t) (temp - this->temperature_min_());
this->target_temperature = temp; // publish what the unit was sent
if (hisense_arg && this->fixed_temp_mode_()) {
remote_state[3] |= HISENSE_ARG_FIXED_TEMP_STEP << 4;
} else {
@@ -109,19 +121,7 @@ void WhirlpoolClimate::transmit_state() {
}
// Fan speed
switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
case climate::CLIMATE_FAN_HIGH:
remote_state[2] |= WHIRLPOOL_FAN_HIGH;
break;
case climate::CLIMATE_FAN_MEDIUM:
remote_state[2] |= WHIRLPOOL_FAN_MED;
break;
case climate::CLIMATE_FAN_LOW:
remote_state[2] |= WHIRLPOOL_FAN_LOW;
break;
default:
break;
}
remote_state[2] |= to_ir_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO), WHIRLPOOL_FAN_AUTO);
// Swing
ESP_LOGV(TAG, "send swing %s", this->send_swing_cmd_ ? LOG_STR_LITERAL("true") : LOG_STR_LITERAL("false"));
@@ -131,7 +131,6 @@ void WhirlpoolClimate::transmit_state() {
remote_state[2] |= WHIRLPOOL_SWING_MASK;
remote_state[8] = WHIRLPOOL_SWING_BYTE8;
}
this->encode_hisense_arg_(remote_state, power_toggle, temp_step);
} else if (this->send_swing_cmd_) {
if (this->swing_mode == climate::CLIMATE_SWING_VERTICAL || this->swing_mode == climate::CLIMATE_SWING_OFF) {
remote_state[2] |= WHIRLPOOL_SWING_MASK;
@@ -139,6 +138,11 @@ void WhirlpoolClimate::transmit_state() {
}
}
if (hisense_arg)
this->encode_hisense_arg_(remote_state, power_toggle, temp_step);
if (this->is_hitachi_hcra31newh_())
this->encode_hitachi_hcra31newh_(remote_state);
// Checksum
remote_state[13] = xor_bytes(&remote_state[2], 11, remote_state[13]);
remote_state[20] = xor_bytes(&remote_state[14], 6, remote_state[20]);
@@ -148,6 +152,7 @@ void WhirlpoolClimate::transmit_state() {
// Send code
auto transmit = this->transmitter_->transmit();
auto *data = transmit.get_data();
data->reserve(WHIRLPOOL_FRAME_TIMINGS);
data->set_carrier_frequency(38000);
@@ -180,10 +185,6 @@ void WhirlpoolClimate::encode_hisense_arg_(uint8_t *remote_state, bool power_tog
remote_state[15] = power_toggle ? HISENSE_ARG_POWER_TOGGLE : HISENSE_ARG_SETTINGS_ONLY;
switch (this->mode) {
case climate::CLIMATE_MODE_OFF:
// The remote keeps the cool code in the power-off frame
remote_state[3] = (remote_state[3] & 0xF0) | WHIRLPOOL_COOL;
break;
case climate::CLIMATE_MODE_DRY:
// The unit runs these fan speeds itself; the user's choice is kept for the next mode change
remote_state[2] &= ~WHIRLPOOL_FAN_MASK;
@@ -212,6 +213,27 @@ void WhirlpoolClimate::encode_hisense_arg_(uint8_t *remote_state, bool power_tog
#endif
}
void WhirlpoolClimate::encode_hitachi_hcra31newh_(uint8_t *remote_state) const {
#ifdef USE_WHIRLPOOL_HITACHI_HCRA31NEWH
remote_state[10] = HITACHI_BYTE10;
remote_state[18] = HITACHI_BYTE18_ON;
remote_state[19] = HITACHI_BYTE19;
// The unit takes the whole state from every frame: only the off frame carries the power bit
switch (this->mode) {
case climate::CLIMATE_MODE_COOL:
remote_state[15] = HITACHI_COOL;
break;
case climate::CLIMATE_MODE_OFF:
remote_state[2] |= WHIRLPOOL_POWER;
remote_state[15] = HITACHI_OFF;
remote_state[18] = HITACHI_BYTE18_OFF;
break;
default:
break;
}
#endif
}
bool WhirlpoolClimate::on_receive(remote_base::RemoteReceiveData data) {
// Check if the esp isn't currently transmitting.
if (millis() - this->last_transmit_time_ < 500) {
@@ -234,7 +256,8 @@ bool WhirlpoolClimate::on_receive(remote_base::RemoteReceiveData data) {
if (!data.expect_item(WHIRLPOOL_BIT_MARK, WHIRLPOOL_GAP))
return false;
}
if (i == WHIRLPOOL_SECOND_DIVIDER && !data.is_valid()) {
// The Hitachi remote always sends all 21 bytes; byte 18 holds its power state
if (i == WHIRLPOOL_SECOND_DIVIDER && !data.is_valid() && !this->is_hitachi_hcra31newh_()) {
// Remote control only sent 14 bytes, nothing more to read, not even the footer
ESP_LOGV(TAG, "Remote control only sent %d bytes", i);
skip_footer = true;
@@ -261,13 +284,18 @@ bool WhirlpoolClimate::on_receive(remote_base::RemoteReceiveData data) {
ESP_LOGV_HEX(TAG, remote_state, WHIRLPOOL_STATE_LENGTH, WHIRLPOOL_STATE_LENGTH, "Received: %s");
// verify header remote code
if (remote_state[0] != 0x83 || remote_state[1] != 0x06)
if (encode_uint16(remote_state[0], remote_state[1]) != this->remote_id_())
return false;
// powr on/off button
ESP_LOGV(TAG, "Power: %02X", (remote_state[2] & WHIRLPOOL_POWER));
if ((remote_state[2] & WHIRLPOOL_POWER) == WHIRLPOOL_POWER) {
if (this->is_hitachi_hcra31newh_()) {
// Power is a state: byte 18 says whether the unit is off
this->powered_on_assumed = remote_state[18] != HITACHI_BYTE18_OFF;
if (!this->powered_on_assumed)
this->mode = climate::CLIMATE_MODE_OFF;
} else if ((remote_state[2] & WHIRLPOOL_POWER) == WHIRLPOOL_POWER) {
auto powered_on = this->mode != climate::CLIMATE_MODE_OFF;
if (powered_on) {
@@ -282,25 +310,7 @@ bool WhirlpoolClimate::on_receive(remote_base::RemoteReceiveData data) {
if (powered_on_assumed) {
auto mode = remote_state[3] & 0x7;
ESP_LOGV(TAG, "Mode: %02X", mode);
switch (mode) {
case WHIRLPOOL_HEAT:
this->mode = climate::CLIMATE_MODE_HEAT;
break;
case WHIRLPOOL_COOL:
this->mode = climate::CLIMATE_MODE_COOL;
break;
case WHIRLPOOL_DRY:
this->mode = climate::CLIMATE_MODE_DRY;
break;
case WHIRLPOOL_FAN:
this->mode = climate::CLIMATE_MODE_FAN_ONLY;
break;
case WHIRLPOOL_AUTO:
this->mode = climate::CLIMATE_MODE_HEAT_COOL;
break;
default:
break;
}
this->mode = from_ir_code(MODES, mode, this->mode);
}
// Set received temp; Hisense ARG sends fixed values in auto and fan-only
@@ -309,7 +319,7 @@ bool WhirlpoolClimate::on_receive(remote_base::RemoteReceiveData data) {
int temp = remote_state[3] & 0xF0;
ESP_LOGVV(TAG, "Temperature Raw: %02X", temp);
temp = (uint8_t) temp >> 4;
temp += static_cast<int>(this->temperature_min_());
temp = std::min(temp + static_cast<int>(this->temperature_min_()), static_cast<int>(this->temperature_max_()));
ESP_LOGVV(TAG, "Temperature Climate: %u", temp);
this->target_temperature = temp;
}
@@ -317,21 +327,7 @@ bool WhirlpoolClimate::on_receive(remote_base::RemoteReceiveData data) {
// Set received fan speed
auto fan = remote_state[2] & WHIRLPOOL_FAN_MASK;
ESP_LOGVV(TAG, "Fan: %02X", fan);
switch (fan) {
case WHIRLPOOL_FAN_HIGH:
this->fan_mode = climate::CLIMATE_FAN_HIGH;
break;
case WHIRLPOOL_FAN_MED:
this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
break;
case WHIRLPOOL_FAN_LOW:
this->fan_mode = climate::CLIMATE_FAN_LOW;
break;
case WHIRLPOOL_FAN_AUTO:
default:
this->fan_mode = climate::CLIMATE_FAN_AUTO;
break;
}
this->fan_mode = from_ir_code(FANS, fan, climate::CLIMATE_FAN_AUTO);
// Set received swing status
if (hisense_arg) {
+19 -4
View File
@@ -6,9 +6,10 @@ namespace esphome::whirlpool {
/// Simple enum to represent models.
enum Model {
MODEL_DG11J1_3A = 0, /// Temperature range is from 18 to 32
MODEL_DG11J1_91 = 1, /// Temperature range is from 16 to 30
MODEL_HISENSE_ARG = 2, /// Hisense/BGH (Argentina), 16 to 30
MODEL_DG11J1_3A = 0, /// Temperature range is from 18 to 32
MODEL_DG11J1_91 = 1, /// Temperature range is from 16 to 30
MODEL_HISENSE_ARG = 2, /// Hisense/BGH (Argentina), 16 to 30
MODEL_HITACHI_HCRA31NEWH = 3, /// Hitachi with the HCRA31NEWH remote, 16 to 30
};
// Temperature
@@ -29,7 +30,9 @@ class WhirlpoolClimate final : public climate_ir::ClimateIR {
/// Override control to change settings of the climate device.
void control(const climate::ClimateCall &call) override {
send_swing_cmd_ = call.get_swing_mode().has_value();
// The toggle remotes flip swing on every press, so only send one when the mode really changes
auto swing = call.get_swing_mode();
this->send_swing_cmd_ = swing.has_value() && *swing != this->swing_mode;
climate_ir::ClimateIR::control(call);
}
@@ -55,10 +58,22 @@ class WhirlpoolClimate final : public climate_ir::ClimateIR {
return false;
#endif
}
/// False when the model is compiled out, so the compiler drops the Hitachi branches.
bool is_hitachi_hcra31newh_() const {
#ifdef USE_WHIRLPOOL_HITACHI_HCRA31NEWH
return this->model_ == MODEL_HITACHI_HCRA31NEWH;
#else
return false;
#endif
}
/// The remote ID sent in bytes 0 and 1.
uint16_t remote_id_() const { return this->is_hitachi_hcra31newh_() ? 0xA959 : 0x8306; }
/// Modes in which the Hisense ARG model sends a fixed temperature.
bool fixed_temp_mode_() const;
/// Add the Hisense ARG bytes on top of the common whirlpool frame.
void encode_hisense_arg_(uint8_t *remote_state, bool power_toggle, uint8_t temp_step) const;
/// Add the Hitachi HCRA31NEWH bytes on top of the common whirlpool frame.
void encode_hitachi_hcra31newh_(uint8_t *remote_state) const;
/// Set the time of the last transmission.
uint32_t last_transmit_time_{};
+30 -51
View File
@@ -39,48 +39,40 @@ const uint32_t UNIT_MASK = 1 << UNIT_SHIFT;
const uint32_t TEMP_MASK = 0xFF;
const uint32_t TEMP_OFFSET_C = 16;
using climate_ir::find_ir_code;
using climate_ir::from_ir_code;
using climate_ir::IRCode;
using climate_ir::to_ir_code;
// Codes are stored shifted down to one byte. A mode not listed is sent as power off, and ignored when received.
static constexpr IRCode MODES[] PROGMEM = {
{climate::CLIMATE_MODE_FAN_ONLY, MODE_FAN >> MODE_SHIFT},
{climate::CLIMATE_MODE_DRY, MODE_DRY >> MODE_SHIFT},
{climate::CLIMATE_MODE_HEAT, MODE_HEAT >> MODE_SHIFT},
{climate::CLIMATE_MODE_COOL, MODE_COOL >> MODE_SHIFT},
};
// A received code that is not listed is ignored
static constexpr IRCode FANS[] PROGMEM = {
{climate::CLIMATE_FAN_HIGH, FAN_HIGH >> FAN_SHIFT},
{climate::CLIMATE_FAN_LOW, FAN_LOW >> FAN_SHIFT},
{climate::CLIMATE_FAN_MEDIUM, FAN_MED >> FAN_SHIFT},
};
void Whynter::transmit_state() {
uint32_t remote_state = COMMAND_CODE;
if (this->mode == climate::CLIMATE_MODE_HEAT_COOL)
this->mode = climate::CLIMATE_MODE_COOL;
switch (this->mode) {
case climate::CLIMATE_MODE_FAN_ONLY:
remote_state |= POWER_MASK;
remote_state |= MODE_FAN;
break;
case climate::CLIMATE_MODE_DRY:
remote_state |= POWER_MASK;
remote_state |= MODE_DRY;
break;
case climate::CLIMATE_MODE_HEAT:
remote_state |= POWER_MASK;
remote_state |= MODE_HEAT;
break;
case climate::CLIMATE_MODE_COOL:
remote_state |= POWER_MASK;
remote_state |= MODE_COOL;
break;
case climate::CLIMATE_MODE_OFF:
remote_state |= POWER_OFF;
break;
default:
remote_state |= POWER_OFF;
if (const auto entry = find_ir_code(MODES, this->mode, false)) {
remote_state |= POWER_MASK;
remote_state |= static_cast<uint32_t>(entry->code) << MODE_SHIFT;
} else {
remote_state |= POWER_OFF;
}
mode_before_ = this->mode;
switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
case climate::CLIMATE_FAN_LOW:
remote_state |= FAN_LOW;
break;
case climate::CLIMATE_FAN_MEDIUM:
remote_state |= FAN_MED;
break;
case climate::CLIMATE_FAN_HIGH:
remote_state |= FAN_HIGH;
break;
default:
remote_state |= FAN_HIGH;
}
remote_state |=
static_cast<uint32_t>(to_ir_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_HIGH), FAN_HIGH >> FAN_SHIFT))
<< FAN_SHIFT;
if (fahrenheit_) {
remote_state |= UNIT_MASK;
@@ -123,15 +115,7 @@ bool Whynter::on_receive(remote_base::RemoteReceiveData data) {
if ((remote_state & POWER_MASK) != POWER_MASK) {
this->mode = climate::CLIMATE_MODE_OFF;
} else {
if ((remote_state & MODE_MASK) == MODE_FAN) {
this->mode = climate::CLIMATE_MODE_FAN_ONLY;
} else if ((remote_state & MODE_MASK) == MODE_DRY) {
this->mode = climate::CLIMATE_MODE_DRY;
} else if ((remote_state & MODE_MASK) == MODE_HEAT) {
this->mode = climate::CLIMATE_MODE_HEAT;
} else if ((remote_state & MODE_MASK) == MODE_COOL) {
this->mode = climate::CLIMATE_MODE_COOL;
}
this->mode = from_ir_code(MODES, (remote_state & MODE_MASK) >> MODE_SHIFT, this->mode);
// Temperature
if ((remote_state & UNIT_MASK) == UNIT_MASK) { // Fahrenheit
@@ -141,13 +125,8 @@ bool Whynter::on_receive(remote_base::RemoteReceiveData data) {
}
// Fan Speed
if ((remote_state & FAN_MASK) == FAN_LOW) {
this->fan_mode = climate::CLIMATE_FAN_LOW;
} else if ((remote_state & FAN_MASK) == FAN_MED) {
this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
} else if ((remote_state & FAN_MASK) == FAN_HIGH) {
this->fan_mode = climate::CLIMATE_FAN_HIGH;
}
if (const auto entry = find_ir_code(FANS, (remote_state & FAN_MASK) >> FAN_SHIFT, true))
this->fan_mode = static_cast<climate::ClimateFanMode>(entry->value);
}
this->publish_state();
+1 -1
View File
@@ -563,7 +563,7 @@ class WiFiComponent final : public Component {
// (In most use cases you won't need these)
/// Setup WiFi interface.
void setup() override;
#if defined(USE_LN882X) || defined(USE_BK72XX)
#if defined(USE_ESP32) || defined(USE_LN882X) || defined(USE_BK72XX)
void on_powerdown() override;
#endif
void start();
@@ -1635,6 +1635,21 @@ bool WiFiComponent::wifi_dpp_start_listen_() {
void WiFiComponent::wifi_dpp_stop_listen_() { esp_supp_dpp_stop_listen(); }
#endif
void WiFiComponent::on_powerdown() {
// Leave the AP before esp_restart() or deep sleep stops Wi-Fi. esp_wifi_stop() while associated
// makes the ESP32 retransmit its Disassociation up to the retry limit although the AP ACKs it
// (32 copies with protected management frames), which some APs handle badly.
// Upstream bug: https://github.com/espressif/esp-idf/issues/19443 - remove this once it is fixed
// in the ESP-IDF versions we support.
// Ask the driver: s_sta_connected lags until the main loop has handled the connect event.
wifi_ap_record_t ap_info;
if (esp_wifi_sta_get_ap_info(&ap_info) != ESP_OK)
return;
ESP_LOGD(TAG, "Disconnecting before powerdown");
esp_wifi_disconnect();
delay(100); // NOLINT: once per reboot or deep sleep, lets the disassociation go out
}
bool WiFiComponent::wifi_disconnect_() { return esp_wifi_disconnect(); }
bssid_t WiFiComponent::wifi_bssid() {
+23 -55
View File
@@ -7,6 +7,24 @@ ESPHOME_LOG_TAG(TAG, "zhlt01.climate");
static constexpr uint8_t ZHLT01_MESSAGE_LENGTH = 12;
using climate_ir::find_ir_code;
using climate_ir::from_ir_code;
using climate_ir::IRCode;
using climate_ir::to_ir_code;
static constexpr IRCode MODES[] PROGMEM = {
{climate::CLIMATE_MODE_AUTO, AC1_MODE_AUTO}, {climate::CLIMATE_MODE_COOL, AC1_MODE_COOL},
{climate::CLIMATE_MODE_HEAT, AC1_MODE_HEAT}, {climate::CLIMATE_MODE_DRY, AC1_MODE_DRY},
{climate::CLIMATE_MODE_FAN_ONLY, AC1_MODE_FAN},
};
// A received fan code that is not listed is ignored
static constexpr IRCode FANS[] PROGMEM = {
{climate::CLIMATE_FAN_AUTO, AC1_FAN_AUTO},
{climate::CLIMATE_FAN_LOW, AC1_FAN1},
{climate::CLIMATE_FAN_MEDIUM, AC1_FAN2},
{climate::CLIMATE_FAN_HIGH, AC1_FAN3},
};
void ZHLT01Climate::transmit_state() {
uint8_t ir_message[ZHLT01_MESSAGE_LENGTH] = {0};
@@ -52,46 +70,12 @@ void ZHLT01Climate::transmit_state() {
ir_message[7] |= AC1_FAN_SILENT;
break;
default:
switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
case climate::CLIMATE_FAN_LOW:
ir_message[7] |= AC1_FAN1;
break;
case climate::CLIMATE_FAN_MEDIUM:
ir_message[7] |= AC1_FAN2;
break;
case climate::CLIMATE_FAN_HIGH:
ir_message[7] |= AC1_FAN3;
break;
case climate::CLIMATE_FAN_AUTO:
ir_message[7] |= AC1_FAN_AUTO;
break;
default:
break;
}
ir_message[7] |= to_ir_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO), AC1_FAN_AUTO);
}
// Byte 9 : AC Mode | Temperature
// -- AC Mode
switch (this->mode) {
case climate::CLIMATE_MODE_AUTO:
case climate::CLIMATE_MODE_HEAT_COOL:
ir_message[9] = AC1_MODE_AUTO;
break;
case climate::CLIMATE_MODE_COOL:
ir_message[9] = AC1_MODE_COOL;
break;
case climate::CLIMATE_MODE_HEAT:
ir_message[9] = AC1_MODE_HEAT;
break;
case climate::CLIMATE_MODE_DRY:
ir_message[9] = AC1_MODE_DRY;
break;
case climate::CLIMATE_MODE_FAN_ONLY:
ir_message[9] = AC1_MODE_FAN;
break;
default:
break;
}
ir_message[9] = to_ir_code(MODES, this->mode, AC1_MODE_AUTO);
// -- Temperature
ir_message[9] |= (uint8_t) (this->target_temperature - 16.0f);
@@ -185,28 +169,12 @@ bool ZHLT01Climate::on_receive(remote_base::RemoteReceiveData data) {
} else if ((ir_message[7] & 0xE1) == AC1_FAN_SILENT) {
this->preset = climate::CLIMATE_PRESET_SLEEP;
this->fan_mode = climate::CLIMATE_FAN_LOW;
} else if ((ir_message[7] & 0xE1) == AC1_FAN_AUTO) {
this->fan_mode = climate::CLIMATE_FAN_AUTO;
} else if ((ir_message[7] & 0xE1) == AC1_FAN1) {
this->fan_mode = climate::CLIMATE_FAN_LOW;
} else if ((ir_message[7] & 0xE1) == AC1_FAN2) {
this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
} else if ((ir_message[7] & 0xE1) == AC1_FAN3) {
this->fan_mode = climate::CLIMATE_FAN_HIGH;
} else if (const auto entry = find_ir_code(FANS, ir_message[7] & 0xE1, true)) {
this->fan_mode = static_cast<climate::ClimateFanMode>(entry->value);
}
// AC Mode
if ((ir_message[9] & 0xE0) == AC1_MODE_COOL) {
this->mode = climate::CLIMATE_MODE_COOL;
} else if ((ir_message[9] & 0xE0) == AC1_MODE_HEAT) {
this->mode = climate::CLIMATE_MODE_HEAT;
} else if ((ir_message[9] & 0xE0) == AC1_MODE_DRY) {
this->mode = climate::CLIMATE_MODE_DRY;
} else if ((ir_message[9] & 0xE0) == AC1_MODE_FAN) {
this->mode = climate::CLIMATE_MODE_FAN_ONLY;
} else {
this->mode = climate::CLIMATE_MODE_AUTO;
}
this->mode = from_ir_code(MODES, ir_message[9] & 0xE0, climate::CLIMATE_MODE_AUTO);
// Target Temperature
this->target_temperature = (ir_message[9] & 0x1F) + 16.0f;
+2
View File
@@ -294,6 +294,8 @@ void Application::reboot() {
for (auto &component : std::ranges::reverse_view(this->components_)) {
component->on_shutdown();
}
// No teardown on a forced reboot, but still power hardware down (Wi-Fi leaves the AP)
this->run_powerdown_hooks();
arch_restart();
}
void Application::safe_reboot() {
+4 -2
View File
@@ -185,10 +185,12 @@ class Component {
*/
virtual bool teardown() { return true; }
/** Called after teardown is complete to power down hardware.
/** Called last before a reboot or deep sleep to power down hardware.
*
* This is called after all components have finished their teardown process,
* On a safe shutdown this is called after all components have finished their teardown process,
* making it safe to power down hardware like ethernet PHY.
* On a forced reboot (Application::reboot()) it is called right after on_shutdown() with no
* teardown, so implementations must not assume teardown() has run.
*/
virtual void on_powerdown() {}
+1
View File
@@ -387,6 +387,7 @@
#define USE_WATER_HEATER
#define USE_WATER_HEATER_VISUAL_OVERRIDES
#define USE_WHIRLPOOL_HISENSE_ARG
#define USE_WHIRLPOOL_HITACHI_HCRA31NEWH
#define USE_ZWAVE_PROXY
// Feature flags which do not work for zephyr
@@ -147,17 +147,6 @@ def test_set_value_constants_share_a_flash_table(
assert "ByteBuffer::wrap" not in main_cpp
def test_yaml_descriptors_are_added_by_reference(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
main_cpp = generate_main(component_config_path("set_value.yaml"))
# Descriptors live in static storage, so the characteristic must not take ownership
assert "->add_descriptor((*test_descriptor));" in main_cpp
assert "->add_descriptor(test_descriptor);" not in main_cpp
def test_float32_rejects_out_of_range() -> None:
for value in (1e40, -1e40):
with pytest.raises(cv.Invalid, match="out of range for a float"):
@@ -0,0 +1,43 @@
#include <gtest/gtest.h>
#include "esphome/components/climate_ir/climate_ir.h"
namespace esphome::climate_ir::testing {
static constexpr IRCode FANS[] PROGMEM = {
{climate::CLIMATE_FAN_AUTO, 0x10},
{climate::CLIMATE_FAN_LOW, 0x20},
{climate::CLIMATE_FAN_HIGH, 0x30},
};
TEST(ClimateIRCodeTest, FindByValue) {
const auto entry = find_ir_code(FANS, climate::CLIMATE_FAN_LOW, false);
ASSERT_TRUE(entry.has_value());
EXPECT_EQ(entry->value, climate::CLIMATE_FAN_LOW);
EXPECT_EQ(entry->code, 0x20);
}
TEST(ClimateIRCodeTest, FindByCode) {
const auto entry = find_ir_code(FANS, 0x30, true);
ASSERT_TRUE(entry.has_value());
EXPECT_EQ(entry->value, climate::CLIMATE_FAN_HIGH);
}
TEST(ClimateIRCodeTest, FindMissing) {
EXPECT_FALSE(find_ir_code(FANS, climate::CLIMATE_FAN_MEDIUM, false).has_value());
// A code that is also a listed value must not match when looking up codes
EXPECT_FALSE(find_ir_code(FANS, climate::CLIMATE_FAN_LOW, true).has_value());
}
TEST(ClimateIRCodeTest, ToCode) {
EXPECT_EQ(to_ir_code(FANS, climate::CLIMATE_FAN_HIGH, 0x00), 0x30);
EXPECT_EQ(to_ir_code(FANS, climate::CLIMATE_FAN_MEDIUM, 0x00), 0x00);
}
TEST(ClimateIRCodeTest, FromCode) {
EXPECT_EQ(from_ir_code(FANS, 0x20, climate::CLIMATE_FAN_AUTO), climate::CLIMATE_FAN_LOW);
EXPECT_EQ(from_ir_code(FANS, 0x99, climate::CLIMATE_FAN_AUTO), climate::CLIMATE_FAN_AUTO);
// The current state as the fallback leaves it unchanged on an unknown code
EXPECT_EQ(from_ir_code(FANS, 0x99, climate::CLIMATE_FAN_MEDIUM), climate::CLIMATE_FAN_MEDIUM);
}
} // namespace esphome::climate_ir::testing
@@ -1,18 +1,3 @@
esphome:
on_boot:
- lambda: |-
// Objects the caller keeps alive, registered without heap allocation
static esp32_ble_server::BLEService battery(esp32_ble::ESPBTUUID::from_uint16(0x180F), 6, false);
static esp32_ble_server::BLECharacteristic level(
esp32_ble::ESPBTUUID::from_uint16(0x2A19),
esp32_ble_server::BLECharacteristic::PROPERTY_READ | esp32_ble_server::BLECharacteristic::PROPERTY_NOTIFY,
ESP_GATT_PERM_READ);
static esp32_ble_server::BLE2902 level_cccd;
level.add_descriptor(level_cccd);
battery.add_characteristic(level);
if (id(ble_server).add_service(battery))
id(ble_server).enqueue_start_service(&battery);
esp32_ble_server:
id: ble_server
manufacturer_data: [0x72, 0x4, 0x00, 0x23]
@@ -2,3 +2,7 @@ packages:
i2c: !include ../../test_build_components/common/i2c/esp32-idf.yaml
<<: !include common.yaml
# IPv6 with an IP sensor: compiles the IPv6 event handler's listener notification
network:
enable_ipv6: true
+2 -1
View File
@@ -10,7 +10,8 @@ def override_manifest(manifest: ComponentManifestOverride) -> None:
manifest.auto_load = ["climate_ir", "climate"]
async def to_code_testing(config: ConfigType) -> None:
# Compiles in the Hisense ARG model, which climate.py adds only when it is configured
# Compiles in the Hisense ARG and Hitachi models, which climate.py adds only when configured
cg.add_define("USE_WHIRLPOOL_HISENSE_ARG")
cg.add_define("USE_WHIRLPOOL_HITACHI_HCRA31NEWH")
manifest.to_code = to_code_testing
+5
View File
@@ -8,3 +8,8 @@ climate:
model: HISENSE_ARG
transmitter_id: xmitr
receiver_id: rcvr
- platform: whirlpool
name: Whirlpool Hitachi HCRA31NEWH Climate
model: HITACHI_HCRA31NEWH
transmitter_id: xmitr
receiver_id: rcvr
@@ -252,6 +252,9 @@ TEST(WhirlpoolHisenseArg, SwingIsSentOnEveryTransmit) {
TEST(WhirlpoolDG11J13A, SwingIsSentOnlyWhenItChanges) {
Fixture f(MODEL_DG11J1_3A);
f.command(climate::CLIMATE_MODE_COOL, 24, climate::CLIMATE_FAN_LOW, climate::CLIMATE_SWING_OFF);
EXPECT_EQ(f.last_frame()[2] & 0x80, 0) << "swing is already off";
f.command(climate::CLIMATE_MODE_COOL, 24, climate::CLIMATE_FAN_LOW, climate::CLIMATE_SWING_VERTICAL);
Frame frame = f.last_frame();
EXPECT_EQ(frame[2] & 0x80, 0x80);
@@ -261,6 +264,9 @@ TEST(WhirlpoolDG11J13A, SwingIsSentOnlyWhenItChanges) {
frame = f.last_frame();
EXPECT_EQ(frame[2] & 0x80, 0);
EXPECT_EQ(frame[8], 0x00);
f.command(climate::CLIMATE_MODE_COOL, 25, climate::CLIMATE_FAN_LOW, climate::CLIMATE_SWING_VERTICAL);
EXPECT_EQ(f.last_frame()[2] & 0x80, 0) << "swing is already vertical";
}
// Cool 24 C (step 8), fan low, powered on assumed
@@ -385,4 +391,90 @@ TEST(WhirlpoolHisenseArg, EveryTransmitHasValidChecksums) {
}
}
// Cool 24 C (step 8), fan low, unit on
static Frame hitachi_cool_frame() {
return with_checksums({0xA9, 0x59, 0x03, 0x82, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x20,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x38, 0x08, 0x00});
}
// The off frame: power bit, cool code, byte 15 0x01 and byte 18 0x28
static Frame hitachi_off_frame() {
return with_checksums({0xA9, 0x59, 0x07, 0x82, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x20,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x28, 0x08, 0x00});
}
TEST(WhirlpoolHitachiHCRA31NEWH, CoolAndOffMatchKnownFrames) {
Fixture f(MODEL_HITACHI_HCRA31NEWH);
f.command(climate::CLIMATE_MODE_COOL, 24, climate::CLIMATE_FAN_LOW);
// Turning on sends the full state with no power bit
expect_frame(f.last_frame(), hitachi_cool_frame());
f.command(climate::CLIMATE_MODE_OFF, 24, climate::CLIMATE_FAN_LOW);
expect_frame(f.last_frame(), hitachi_off_frame());
// A second off command still sends the power bit
f.command(climate::CLIMATE_MODE_OFF, 24, climate::CLIMATE_FAN_LOW);
EXPECT_EQ(f.last_frame()[2] & 0x04, 0x04);
f.command(climate::CLIMATE_MODE_HEAT, 24, climate::CLIMATE_FAN_LOW);
const Frame frame = f.last_frame();
EXPECT_EQ(frame[2] & 0x04, 0);
EXPECT_EQ(frame[3], 0x80);
EXPECT_EQ(frame[15], 0x06);
EXPECT_EQ(frame[18], 0x38);
}
TEST(WhirlpoolHitachiHCRA31NEWH, ReceivePowerIsAState) {
Fixture f(MODEL_HITACHI_HCRA31NEWH);
ASSERT_EQ(f.climate.mode, climate::CLIMATE_MODE_OFF);
// An on frame restores the mode even though it has no power bit
EXPECT_TRUE(receive(f.climate, hitachi_cool_frame()));
EXPECT_EQ(f.climate.mode, climate::CLIMATE_MODE_COOL);
EXPECT_FLOAT_EQ(f.climate.target_temperature, 24);
EXPECT_EQ(f.climate.fan_mode, climate::CLIMATE_FAN_LOW);
// The same frame again keeps it on
EXPECT_TRUE(receive(f.climate, hitachi_cool_frame()));
EXPECT_EQ(f.climate.mode, climate::CLIMATE_MODE_COOL);
EXPECT_TRUE(receive(f.climate, hitachi_off_frame()));
EXPECT_EQ(f.climate.mode, climate::CLIMATE_MODE_OFF);
// A repeated off frame does not toggle
EXPECT_TRUE(receive(f.climate, hitachi_off_frame()));
EXPECT_EQ(f.climate.mode, climate::CLIMATE_MODE_OFF);
}
TEST(WhirlpoolHitachiHCRA31NEWH, ReceiveChecksRemoteId) {
Fixture hitachi(MODEL_HITACHI_HCRA31NEWH);
EXPECT_FALSE(receive(hitachi.climate, hisense_cool_frame()));
Fixture whirlpool(MODEL_DG11J1_91);
EXPECT_FALSE(receive(whirlpool.climate, hitachi_cool_frame()));
}
TEST(WhirlpoolHitachiHCRA31NEWH, ReceivedTemperatureIsClamped) {
Fixture f(MODEL_HITACHI_HCRA31NEWH);
Frame frame = hitachi_cool_frame();
frame[3] = 0xF2; // step 15 would be 31 C
EXPECT_TRUE(receive(f.climate, with_checksums(frame)));
EXPECT_FLOAT_EQ(f.climate.target_temperature, 30);
}
TEST(WhirlpoolDG11J13A, TargetIsClampedAndRoundedToWhatWasSent) {
Fixture f(MODEL_DG11J1_3A);
f.command(climate::CLIMATE_MODE_COOL, 35, climate::CLIMATE_FAN_LOW);
EXPECT_FLOAT_EQ(f.climate.target_temperature, 32);
f.command(climate::CLIMATE_MODE_COOL, 24.4f, climate::CLIMATE_FAN_LOW);
EXPECT_FLOAT_EQ(f.climate.target_temperature, 24);
}
TEST(WhirlpoolHitachiHCRA31NEWH, ReceiveRejectsShortFrame) {
Fixture f(MODEL_HITACHI_HCRA31NEWH);
remote_base::RawTimings raw = waveform(hitachi_cool_frame());
// Cut after the second divider: 14 bytes, no footer
raw.resize(2 + 14 * 16 + 2 * 2);
remote_base::RemoteReceiveData data(raw, 25, remote_base::TOLERANCE_MODE_PERCENTAGE);
EXPECT_FALSE(static_cast<remote_base::RemoteReceiverListener &>(f.climate).on_receive(data));
EXPECT_EQ(f.climate.mode, climate::CLIMATE_MODE_OFF);
}
} // namespace esphome::whirlpool::testing
@@ -0,0 +1,23 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_NAME
from esphome.types import ConfigType
CODEOWNERS = ["@esphome/tests"]
MULTI_CONF = True
reboot_hook_recorder_ns = cg.esphome_ns.namespace("reboot_hook_recorder")
RebootHookRecorder = reboot_hook_recorder_ns.class_("RebootHookRecorder", cg.Component)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(RebootHookRecorder),
cv.Required(CONF_NAME): cv.string,
}
).extend(cv.COMPONENT_SCHEMA)
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
cg.add(var.set_name(config[CONF_NAME]))
@@ -0,0 +1,10 @@
#include "reboot_hook_recorder.h"
#include "esphome/core/log.h"
namespace esphome::reboot_hook_recorder {
ESPHOME_LOG_TAG(TAG, "reboot_hook_recorder");
void RebootHookRecorder::record_(const char *hook) { ESP_LOGI(TAG, "HOOK %s %s", this->name_, hook); }
} // namespace esphome::reboot_hook_recorder
@@ -0,0 +1,33 @@
#pragma once
#include "esphome/core/component.h"
namespace esphome::reboot_hook_recorder {
/// Logs every shutdown lifecycle hook it receives, so a test can check their order.
class RebootHookRecorder : public Component {
public:
void set_name(const char *name) { this->name_ = name; }
void on_safe_shutdown() override { this->record_("on_safe_shutdown"); }
void on_shutdown() override { this->record_("on_shutdown"); }
// Needs a second call to finish, like a component waiting for a connection to close
bool teardown() override {
if (++this->teardown_calls_ < 2) {
this->record_("teardown_pending");
return false;
}
if (this->teardown_calls_ == 2)
this->record_("teardown_done");
return true;
}
void on_powerdown() override { this->record_("on_powerdown"); }
protected:
void record_(const char *hook);
const char *name_{""};
uint8_t teardown_calls_{0};
};
} // namespace esphome::reboot_hook_recorder
@@ -0,0 +1,19 @@
esphome:
name: reboot-hooks-forced
host:
logger:
level: DEBUG
api:
# Reboot once the test has connected, so that it sees every hook
on_client_connected:
- delay: 100ms
- lambda: App.reboot();
external_components:
- source:
type: local
path: EXTERNAL_COMPONENT_PATH
reboot_hook_recorder:
- name: first
- name: second
@@ -0,0 +1,19 @@
esphome:
name: reboot-hooks-safe
host:
logger:
level: DEBUG
api:
# Reboot once the test has connected, so that it sees every hook
on_client_connected:
- delay: 100ms
- lambda: App.safe_reboot();
external_components:
- source:
type: local
path: EXTERNAL_COMPONENT_PATH
reboot_hook_recorder:
- name: first
- name: second
@@ -0,0 +1,80 @@
"""Test the order of the shutdown lifecycle hooks on a forced and on a safe reboot."""
import re
import pytest
from .log_utils import LineWaiter
from .types import APIClientConnectedFactory, RunCompiledFunction
from .wait_utils import wait_or_fail
HOOK_PATTERN = re.compile(r"HOOK (\w+) (\w+)")
# Hooks run in reverse component order, so "first" gets each hook last.
LAST_HOOK = "HOOK first on_powerdown"
async def _record_reboot_hooks(
yaml_config: str,
run_compiled: RunCompiledFunction,
api_client_connected: APIClientConnectedFactory,
) -> list[tuple[str, str]]:
"""Connect, which makes the device reboot, and return its (component, hook) calls in order."""
waiter = LineWaiter()
hooks: list[tuple[str, str]] = []
def on_line(line: str) -> None:
if match := HOOK_PATTERN.search(line):
hooks.append((match[1], match[2]))
waiter.callback(line)
async with (
run_compiled(yaml_config, line_callback=on_line),
api_client_connected(),
):
await wait_or_fail(
waiter.wait_for(LAST_HOOK),
5.0,
"Device did not run the powerdown hooks",
hooks=hooks,
)
return hooks
@pytest.mark.asyncio
async def test_reboot_hook_order_forced(
yaml_config: str,
run_compiled: RunCompiledFunction,
api_client_connected: APIClientConnectedFactory,
) -> None:
"""A forced reboot runs on_shutdown then on_powerdown, with no safe shutdown or teardown."""
hooks = await _record_reboot_hooks(yaml_config, run_compiled, api_client_connected)
assert hooks == [
("second", "on_shutdown"),
("first", "on_shutdown"),
("second", "on_powerdown"),
("first", "on_powerdown"),
]
@pytest.mark.asyncio
async def test_reboot_hook_order_safe(
yaml_config: str,
run_compiled: RunCompiledFunction,
api_client_connected: APIClientConnectedFactory,
) -> None:
"""A safe reboot powers down only after every component has finished its teardown."""
hooks = await _record_reboot_hooks(yaml_config, run_compiled, api_client_connected)
assert hooks == [
("second", "on_safe_shutdown"),
("first", "on_safe_shutdown"),
("second", "on_shutdown"),
("first", "on_shutdown"),
("second", "teardown_pending"),
("first", "teardown_pending"),
("second", "teardown_done"),
("first", "teardown_done"),
("second", "on_powerdown"),
("first", "on_powerdown"),
]