ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
mistral_ir.cpp
Go to the documentation of this file.
1#include "mistral_ir.h"
3#include "esphome/core/hal.h"
5#include "esphome/core/log.h"
6
7#include <cmath>
8
10
11ESPHOME_LOG_TAG(TAG, "mistral_ir.climate");
12
13// Fixed bytes of every frame; bytes 1, 3, 4, 5, 6 and 11 are filled in per state
14static const uint8_t FRAME_TEMPLATE[MISTRAL_FRAME_SIZE] PROGMEM = {0x56, 0x00, 0x08, 0x00, 0x00, 0x00,
15 0x00, 0x00, 0xE8, 0x00, 0x60, 0x00};
16static const uint8_t FIXED_BYTES[] PROGMEM = {0, 2, 7, 8, 9, 10};
17static const uint8_t CHECKSUM_BYTES[] PROGMEM = {1, 3, 4, 5, 6, 8};
18
19constexpr uint8_t MISTRAL_POWER_OFF = 0x00;
20constexpr uint8_t MISTRAL_POWER_ON = 0x20;
21constexpr uint8_t MISTRAL_TEMP_OFFSET = 0x9F;
22constexpr uint8_t MISTRAL_MODE_AUTO = 0x10;
23
24// OFF sends the auto mode code and only clears the power byte
25static constexpr climate_ir::IRCode MODES[] PROGMEM = {
31};
32
33// Each fan speed sets two bytes; the table lives in flash, so entries are copied out before use
34struct FanCode {
36 uint8_t byte1;
37 uint8_t byte6;
38};
39static const FanCode FANS[] PROGMEM = {
40 {climate::CLIMATE_FAN_AUTO, 0xA4, 0x00},
41 {climate::CLIMATE_FAN_LOW, 0x44, 0x40},
42 {climate::CLIMATE_FAN_MEDIUM, 0x24, 0xC0},
43 {climate::CLIMATE_FAN_HIGH, 0xA4, 0xA0},
44};
45
46template<typename T> static T load_entry(const T &slot) {
47 T entry;
48 progmem_memcpy(&entry, &slot, sizeof(T));
49 return entry;
50}
51
52template<typename T, size_t N, typename Pred> static optional<T> find_entry(const T (&table)[N], Pred pred) {
53 for (const T &slot : table) {
54 T entry = load_entry(slot);
55 if (pred(entry))
56 return entry;
57 }
58 return {};
59}
60
61static uint8_t compute_checksum(const uint8_t *frame) {
62 uint8_t sum = 0;
63 for (const uint8_t &idx : CHECKSUM_BYTES)
64 sum += reverse_bits(frame[progmem_read_byte(&idx)]);
65 return reverse_bits(sum);
66}
67
68static bool is_valid_frame(const uint8_t *frame) {
69 for (const uint8_t &slot : FIXED_BYTES) {
70 const uint8_t idx = progmem_read_byte(&slot);
71 if (frame[idx] != progmem_read_byte(&FRAME_TEMPLATE[idx]))
72 return false;
73 }
74 return frame[11] == compute_checksum(frame);
75}
76
78 const climate::ClimateFanMode fan_mode = this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO);
79 const FanCode fan =
80 find_entry(FANS, [fan_mode](const FanCode &f) { return f.fan == fan_mode; }).value_or(load_entry(FANS[0]));
81 const uint8_t temp = lroundf(clamp<float>(this->target_temperature, MISTRAL_TEMP_MIN, MISTRAL_TEMP_MAX));
82
83 uint8_t frame[MISTRAL_FRAME_SIZE];
84 progmem_memcpy(frame, FRAME_TEMPLATE, sizeof(frame));
85 frame[1] = fan.byte1;
87 frame[4] = climate_ir::to_ir_code(MODES, this->mode, MISTRAL_MODE_AUTO);
88 frame[5] = reverse_bits(static_cast<uint8_t>(MISTRAL_TEMP_OFFSET - temp));
89 frame[6] = fan.byte6;
90 frame[11] = compute_checksum(frame);
91
92 auto transmit = this->transmitter_->transmit();
93 remote_base::AEHAProtocol().encode(transmit.get_data(), MISTRAL_ADDRESS, frame, sizeof(frame));
94 transmit.perform();
95}
96
98 auto aeha = remote_base::AEHAProtocol().decode(data);
99 if (!aeha.has_value() || aeha->address != MISTRAL_ADDRESS || aeha->data.size() != MISTRAL_FRAME_SIZE)
100 return false;
101
102 const uint8_t *frame = aeha->data.data();
103 if (!is_valid_frame(frame))
104 return false;
105
106 if (frame[3] == MISTRAL_POWER_OFF) {
108 this->publish_state();
109 return true;
110 }
111 if (frame[3] != MISTRAL_POWER_ON)
112 return false;
113
114 // Decode everything before touching the state so an unknown code leaves it untouched
115 const auto mode = climate_ir::find_ir_code(MODES, frame[4], true);
116 const auto fan = find_entry(FANS, [frame](const FanCode &f) { return f.byte1 == frame[1] && f.byte6 == frame[6]; });
117 if (!mode.has_value() || !fan.has_value())
118 return false;
119
120 this->mode = static_cast<climate::ClimateMode>(mode->value);
121 this->target_temperature =
123 this->fan_mode = fan->fan;
124
125 ESP_LOGV(TAG, "Received Mistral frame: mode=%d temp=%.0f fan=%d", static_cast<int>(this->mode),
126 this->target_temperature, static_cast<int>(fan->fan));
127 this->publish_state();
128 return true;
129}
130
131} // namespace esphome::mistral_ir
ClimateMode mode
The active mode of the climate device.
Definition climate.h:317
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:311
float target_temperature
The target temperature of the climate device.
Definition climate.h:298
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:432
bool on_receive(remote_base::RemoteReceiveData data) override
optional< AEHAData > decode(RemoteReceiveData src)
void encode(RemoteTransmitData *dst, const AEHAData &data)
uint8_t to_ir_code(const IRCode(&table)[N], uint8_t value, uint8_t fallback)
The code for value, or fallback when the table has no such value.
Definition climate_ir.h:36
ESPHOME_ALWAYS_INLINE optional< IRCode > find_ir_code(const IRCode(&table)[N], uint8_t key, bool match_code)
The entry whose value (or, with match_code, code) is key, read from PROGMEM.
Definition climate_ir.h:25
ClimateMode
Enum for all modes a climate device can be in.
@ CLIMATE_MODE_DRY
The climate device is set to dry/humidity mode.
@ CLIMATE_MODE_FAN_ONLY
The climate device only has the fan enabled, no heating or cooling is taking place.
@ CLIMATE_MODE_HEAT
The climate device is set to heat to reach the target temperature.
@ CLIMATE_MODE_COOL
The climate device is set to cool to reach the target temperature.
@ CLIMATE_MODE_HEAT_COOL
The climate device is set to heat/cool to reach the target temperature.
@ CLIMATE_MODE_OFF
The climate device is off.
ClimateFanMode
NOTE: If adding values, update ClimateFanModeMask in climate_traits.h to use the new last value.
@ CLIMATE_FAN_MEDIUM
The fan mode is set to Medium.
@ CLIMATE_FAN_AUTO
The fan mode is set to Auto.
@ CLIMATE_FAN_LOW
The fan mode is set to Low.
@ CLIMATE_FAN_HIGH
The fan mode is set to High.
constexpr uint8_t MISTRAL_POWER_OFF
ESPHOME_LOG_TAG(TAG, "mistral_ir.climate")
constexpr uint8_t MISTRAL_TEMP_OFFSET
constexpr size_t MISTRAL_FRAME_SIZE
Definition mistral_ir.h:12
constexpr uint8_t MISTRAL_TEMP_MAX
Definition mistral_ir.h:9
constexpr uint8_t MISTRAL_POWER_ON
constexpr uint8_t MISTRAL_TEMP_MIN
Definition mistral_ir.h:8
constexpr uint8_t MISTRAL_MODE_AUTO
constexpr uint16_t MISTRAL_ADDRESS
Definition mistral_ir.h:11
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
uint8_t reverse_bits(uint8_t x)
Reverse the order of 8 bits.
Definition helpers.h:1343
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
One entry of a PROGMEM table that maps a climate enum value to the unit's code.
Definition climate_ir.h:15