ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
airton.cpp
Go to the documentation of this file.
1#include "airton.h"
3#include "esphome/core/log.h"
4
5namespace esphome::airton {
6
7ESPHOME_LOG_TAG(TAG, "airton.climate");
8
9// Distinct from the climate's own RESTORE_STATE_VERSION, which shares the entity's preference key
10static constexpr uint32_t RESTORE_SETTINGS_VERSION = 0x2E86A1F3UL;
11
12// From IRutils.h of IRremoteESP8266 library
13static uint8_t checksum(const uint8_t *frame) {
14 return static_cast<uint8_t>(0x7F - sum_bytes(frame, AIRTON_STATE_FRAME_SIZE - 1)) ^ 0x2C;
15}
16
18 climate_ir::ClimateIR::setup();
19 // The flap position is the one source of truth for the swing mode the climate state restored
20 this->settings_pref_ = this->make_entity_preference<AirtonSettings>(RESTORE_SETTINGS_VERSION);
21 this->settings_pref_.load(&this->settings_);
22 this->sync_swing_mode_();
23 this->publish_settings_();
24 this->publish_state();
25}
26
27void AirtonClimate::publish_settings_() {
28#ifdef USE_SWITCH
29 PUBLISH_STATE(this->sleep_mode_switch_, this->settings_.sleep_state);
30 PUBLISH_STATE(this->display_switch_, this->settings_.display_state);
31#endif
32#ifdef USE_SELECT
33 PUBLISH_STATE(this->vertical_direction_select_, static_cast<size_t>(this->settings_.vertical_direction));
34#endif
35}
36
38
44
45// The unit has no sleep mode while it only fans or picks the mode itself
49
50// Each setter stores and saves before it publishes: an automation on the entity runs inside
51// publish_state() and may set the value again, which must not be undone afterwards
53 state = state && this->sleep_mode_allowed_();
54 const bool changed = state != this->settings_.sleep_state;
55 if (changed) {
57 this->save_settings_();
58 }
59#ifdef USE_SWITCH
60 PUBLISH_STATE(this->sleep_mode_switch_, state);
61#endif
62 return changed;
63}
64
66 const bool changed = state != this->settings_.display_state;
67 if (changed) {
69 this->save_settings_();
70 }
71#ifdef USE_SWITCH
72 PUBLISH_STATE(this->display_switch_, state);
73#endif
74 return changed;
75}
76
78 const bool changed = direction != this->settings_.vertical_direction;
79 if (changed) {
81 this->sync_swing_mode_();
82 this->save_settings_();
83 }
84#ifdef USE_SELECT
85 PUBLISH_STATE(this->vertical_direction_select_, static_cast<size_t>(direction));
86#endif
87 return changed;
88}
89
91 this->transmit_state();
92 this->publish_state();
93}
94
96 // ClimateIR::control() sends and publishes the whole state afterwards
97 // Only a change of swing mode moves the flap, so a repeated swing-off keeps a fixed position
98 if (auto swing_mode = call.get_swing_mode(); swing_mode.has_value() && *swing_mode != this->swing_mode) {
102 }
103 ClimateIR::control(call);
104}
105
107 // Entering fan-only or automatic mode drops sleep mode, as the remote does
108 if (!this->sleep_mode_allowed_())
109 this->set_sleep_mode_state(false);
110 // Sampled valid state
111 // Power: On, Mode: 2 (Dry), Fan: 1 (Quiet), Temp: 20C, Swing(V): On, Econo: Off, Turbo: Off, Light: On, Health: On,
112 // Sleep: Off. 0x74C461041A11D3
113 // The turbo bit above the fan speed stays clear, as the remote sends it
114 uint8_t remote_state[AIRTON_STATE_FRAME_SIZE] = {
115 AIRTON_HEADER_BYTE0,
116 AIRTON_HEADER_BYTE1,
117 static_cast<uint8_t>(this->operation_mode_() | (this->fan_speed_() << 4)),
118 this->temperature_(),
119 static_cast<uint8_t>(this->settings_.vertical_direction),
120 this->operation_settings_(),
121 0,
122 };
123 remote_state[6] = checksum(remote_state);
124
125 ESP_LOGV(TAG, "Sending: %02X %02X %02X %02X %02X %02X %02X", remote_state[6], remote_state[5], remote_state[4],
126 remote_state[3], remote_state[2], remote_state[1], remote_state[0]);
127
128 auto transmit = this->transmitter_->transmit();
129 auto *data = transmit.get_data();
130 data->set_carrier_frequency(AIRTON_IR_FREQUENCY);
131 data->mark(AIRTON_HEADER_MARK);
132 data->space(AIRTON_HEADER_SPACE);
133 data->item_bytes<AIRTON_BIT_TIMINGS>(remote_state, AIRTON_STATE_FRAME_SIZE);
134
135 data->mark(AIRTON_BIT_MARK);
136 data->space(AIRTON_MESSAGE_SPACE);
137
138 transmit.perform();
139}
140
142 uint8_t operating_mode = 0b1000; // First bit is for power state
143 switch (this->mode) {
145 operating_mode |= AIRTON_MODE_COOL;
146 break;
148 operating_mode |= AIRTON_MODE_DRY;
149 break;
151 operating_mode |= AIRTON_MODE_HEAT;
152 break;
154 operating_mode |= AIRTON_MODE_AUTO;
155 break;
157 operating_mode |= AIRTON_MODE_FAN;
158 break;
160 default:
161 operating_mode = 0b0111 & this->previous_mode_; // Set previous mode with power state bit off
162 }
163 this->previous_mode_ = operating_mode;
164 return operating_mode;
165}
166
168 uint8_t fan_speed;
169 switch (this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO)) {
171 fan_speed = AIRTON_FAN_1;
172 break;
174 fan_speed = AIRTON_FAN_3;
175 break;
177 fan_speed = AIRTON_FAN_5;
178 break;
180 default:
181 fan_speed = AIRTON_FAN_AUTO;
182 }
183 return fan_speed;
184}
185
187 switch (this->mode) {
189 return AIRTON_AUTO_MODE_TEMP - AIRTON_TEMP_MIN;
190 default:
191 return static_cast<uint8_t>(roundf(clamp<float>(this->target_temperature, AIRTON_TEMP_MIN, AIRTON_TEMP_MAX))) -
192 AIRTON_TEMP_MIN;
193 }
194}
195
196// The bits of this packet's byte have the following meanings (from MSB to LSB)
197// Light, Health, Unknown, HeatOn, Unknown, NotAutoOn, Sleep, Econo
199 uint8_t settings = 0;
200 if (this->mode == climate::CLIMATE_MODE_HEAT) { // Set heating bit if on the corresponding mode
201 settings |= (1 << 4);
202 }
203 if (this->settings_.display_state) {
204 settings |= (1 << 7);
205 }
206 if (this->settings_.sleep_state) {
207 settings |= (1 << 1);
208 }
209 settings |= 0b01000000; // Health, as the remote always sends it
211 settings |= 0b00000100; // NotAutoOn: clear only while the unit picks the mode itself
212 }
213 return settings;
214}
215
216void AirtonClimate::parse_state_frame_(const uint8_t *frame) {
217 uint8_t mode = frame[2];
218 // A later "off" repeats the mode the remote last sent, as the remote itself does
219 this->previous_mode_ = mode & 0b00001111;
220 if (mode & 0b00001000) { // The power bit
221 switch (mode & 0b00000111) {
222 case AIRTON_MODE_COOL:
223 this->mode = climate::CLIMATE_MODE_COOL;
224 break;
225 case AIRTON_MODE_DRY:
226 this->mode = climate::CLIMATE_MODE_DRY;
227 break;
228 case AIRTON_MODE_HEAT:
229 this->mode = climate::CLIMATE_MODE_HEAT;
230 break;
231 case AIRTON_MODE_AUTO:
233 break;
234 case AIRTON_MODE_FAN:
236 break;
237 }
238 } else {
239 this->mode = climate::CLIMATE_MODE_OFF;
240 }
241 uint8_t fan_mode = (frame[2] & 0b01110000) >> 4;
242
243 switch (fan_mode) {
244 case AIRTON_FAN_1:
245 case AIRTON_FAN_2:
246 this->fan_mode = climate::CLIMATE_FAN_LOW;
247 break;
248 case AIRTON_FAN_3:
249 this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
250 break;
251 case AIRTON_FAN_4:
252 case AIRTON_FAN_5:
253 this->fan_mode = climate::CLIMATE_FAN_HIGH;
254 break;
255 case AIRTON_FAN_AUTO:
256 this->fan_mode = climate::CLIMATE_FAN_AUTO;
257 break;
258 default:
259 break;
260 }
261
262 this->target_temperature = (frame[3] & 0b00001111) + AIRTON_TEMP_MIN;
263
264 const uint8_t direction = frame[4] & 0b00001111;
265 if (direction <= static_cast<uint8_t>(VerticalDirection::VERTICAL_DIRECTION_DOWN)) {
267 } else {
268 ESP_LOGV(TAG, "Unknown vertical direction %u", direction);
269 }
270 this->set_display_state((frame[5] & 0b10000000) != 0);
271 this->set_sleep_mode_state((frame[5] & 0b00000010) != 0);
272
273 this->publish_state();
274}
275
277 uint8_t remote_state[AIRTON_STATE_FRAME_SIZE];
278 if (!data.expect_item(AIRTON_HEADER_MARK, AIRTON_HEADER_SPACE)) {
279 ESP_LOGV(TAG, "Wrong header encoding detected!");
280 return false;
281 }
282 if (!data.expect_bytes<AIRTON_BIT_TIMINGS>(remote_state, AIRTON_STATE_FRAME_SIZE)) {
283 ESP_LOGV(TAG, "Wrong modulation encoding");
284 return false;
285 }
286
287 if (remote_state[0] != AIRTON_HEADER_BYTE0 || remote_state[1] != AIRTON_HEADER_BYTE1) {
288 ESP_LOGV(TAG, "Wrong header contents: %02X %02X", remote_state[1], remote_state[0]);
289 return false;
290 }
291 const uint8_t expected = checksum(remote_state);
292 if (remote_state[AIRTON_STATE_FRAME_SIZE - 1] != expected) {
293 ESP_LOGV(TAG, "Checksum error: calculated %02X, received %02X", expected,
294 remote_state[AIRTON_STATE_FRAME_SIZE - 1]);
295 return false;
296 }
297 ESP_LOGV(TAG, "Received: %02X %02X %02X %02X %02X %02X %02X", remote_state[6], remote_state[5], remote_state[4],
298 remote_state[3], remote_state[2], remote_state[1], remote_state[0]);
299 this->parse_state_frame_(remote_state);
300 return true;
301}
302
303} // namespace esphome::airton
uint8_t checksum
Definition bl0906.h:3
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
bool set_display_state(bool state)
Definition airton.cpp:65
bool sleep_mode_allowed_() const
Definition airton.cpp:46
void control(const climate::ClimateCall &call) override
Definition airton.cpp:95
bool set_sleep_mode_state(bool state)
Definition airton.cpp:52
void transmit_state() override
Definition airton.cpp:106
void parse_state_frame_(const uint8_t *frame)
Definition airton.cpp:216
bool set_vertical_direction_state(VerticalDirection direction)
Definition airton.cpp:77
SUB_SWITCH(sleep_mode) SUB_SWITCH(display) SUB_SELECT(vertical_direction) protected void save_settings_()
Definition airton.cpp:37
ESPPreferenceObject settings_pref_
Definition airton.h:127
bool on_receive(remote_base::RemoteReceiveData data) override
Definition airton.cpp:276
This class is used to encode all control actions on a climate device.
Definition climate.h:31
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
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:323
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:432
FanDirection direction
Definition fan.h:5
bool state
Definition fan.h:2
ESPHOME_LOG_TAG(TAG, "airton.climate")
@ CLIMATE_SWING_OFF
The swing mode is set to Off.
@ CLIMATE_SWING_VERTICAL
The fan mode is set to Vertical.
@ 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.
@ 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 sum_bytes(const uint8_t *data, size_t len, uint8_t sum=0)
Add up the bytes of data with size len, truncated to 8 bits: the checksum many serial and IR protocol...
Definition helpers.h:939
static void uint32_t
VerticalDirection vertical_direction
Definition airton.h:70