ESPHome 2025.5.0
Loading...
Searching...
No Matches
smartair2_climate.cpp
Go to the documentation of this file.
1#include <chrono>
4#include "smartair2_climate.h"
5#include "smartair2_packet.h"
6
7using namespace esphome::climate;
8using namespace esphome::uart;
9
10namespace esphome {
11namespace haier {
12
13static const char *const TAG = "haier.climate";
14constexpr size_t SIGNAL_LEVEL_UPDATE_INTERVAL_MS = 10000;
15constexpr uint8_t CONTROL_MESSAGE_RETRIES = 5;
16constexpr std::chrono::milliseconds CONTROL_MESSAGE_RETRIES_INTERVAL = std::chrono::milliseconds(500);
17constexpr uint8_t INIT_REQUESTS_RETRY = 2;
18constexpr std::chrono::milliseconds INIT_REQUESTS_RETRY_INTERVAL = std::chrono::milliseconds(2000);
19
21 last_status_message_ = std::unique_ptr<uint8_t[]>(new uint8_t[sizeof(smartair2_protocol::HaierPacketControl)]);
22}
23
24haier_protocol::HandlerError Smartair2Climate::status_handler_(haier_protocol::FrameType request_type,
25 haier_protocol::FrameType message_type,
26 const uint8_t *data, size_t data_size) {
27 haier_protocol::HandlerError result =
28 this->answer_preprocess_(request_type, haier_protocol::FrameType::CONTROL, message_type,
29 haier_protocol::FrameType::STATUS, ProtocolPhases::UNKNOWN);
30 if (result == haier_protocol::HandlerError::HANDLER_OK) {
31 result = this->process_status_message_(data, data_size);
32 if (result != haier_protocol::HandlerError::HANDLER_OK) {
33 ESP_LOGW(TAG, "Error %d while parsing Status packet", (int) result);
34 this->reset_phase_();
35 this->action_request_.reset();
36 this->force_send_control_ = false;
37 } else {
38 if (data_size >= sizeof(smartair2_protocol::HaierPacketControl) + 2) {
39 memcpy(this->last_status_message_.get(), data + 2, sizeof(smartair2_protocol::HaierPacketControl));
40 this->status_message_callback_.call((const char *) data, data_size);
41 } else {
42 ESP_LOGW(TAG, "Status packet too small: %d (should be >= %d)", data_size,
44 }
45 switch (this->protocol_phase_) {
47 ESP_LOGI(TAG, "First HVAC status received");
49 break;
51 // Do nothing, phase will be changed in process_phase
52 break;
55 break;
58 this->force_send_control_ = false;
61 break;
62 default:
63 break;
64 }
65 }
66 return result;
67 } else {
68 this->action_request_.reset();
69 this->force_send_control_ = false;
70 this->reset_phase_();
71 return result;
72 }
73}
74
76 haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data,
77 size_t data_size) {
78 if (request_type != haier_protocol::FrameType::GET_DEVICE_VERSION)
79 return haier_protocol::HandlerError::UNSUPPORTED_MESSAGE;
81 return haier_protocol::HandlerError::UNEXPECTED_MESSAGE;
82 // Invalid packet is expected answer
83 if ((message_type == haier_protocol::FrameType::GET_DEVICE_VERSION_RESPONSE) && (data_size >= 39) &&
84 ((data[37] & 0x04) != 0)) {
85 ESP_LOGW(TAG, "It looks like your ESPHome Haier climate configuration is wrong. You should use the hOn protocol "
86 "instead of smartAir2");
87 }
89 return haier_protocol::HandlerError::HANDLER_OK;
90}
91
93 haier_protocol::FrameType message_type) {
96 ESP_LOGI(TAG, "Answer timeout for command %02X, phase %s", (uint8_t) message_type,
98 ProtocolPhases new_phase = (ProtocolPhases) ((int) this->protocol_phase_ + 1);
101 this->set_phase(new_phase);
102 return haier_protocol::HandlerError::HANDLER_OK;
103}
104
106 // Set handlers
107 this->haier_protocol_.set_answer_handler(
108 haier_protocol::FrameType::GET_DEVICE_VERSION,
109 std::bind(&Smartair2Climate::get_device_version_answer_handler_, this, std::placeholders::_1,
110 std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
111 this->haier_protocol_.set_answer_handler(
112 haier_protocol::FrameType::CONTROL,
113 std::bind(&Smartair2Climate::status_handler_, this, std::placeholders::_1, std::placeholders::_2,
114 std::placeholders::_3, std::placeholders::_4));
115 this->haier_protocol_.set_answer_handler(
116 haier_protocol::FrameType::REPORT_NETWORK_STATUS,
117 std::bind(&Smartair2Climate::report_network_status_answer_handler_, this, std::placeholders::_1,
118 std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
119 this->haier_protocol_.set_default_timeout_handler(
120 std::bind(&Smartair2Climate::messages_timeout_handler_with_cycle_for_init_, this, std::placeholders::_1));
121}
122
125 ESP_LOGCONFIG(TAG, " Protocol version: smartAir2");
126}
127
128void Smartair2Climate::process_phase(std::chrono::steady_clock::time_point now) {
129 switch (this->protocol_phase_) {
132 // Indicate device capabilities:
133 // bit 0 - if 1 module support interactive mode
134 // bit 1 - if 1 module support controller-device mode
135 // bit 2 - if 1 module support crc
136 // bit 3 - if 1 module support multiple devices
137 // bit 4..bit 15 - not used
138 uint8_t module_capabilities[2] = {0b00000000, 0b00000111};
139 static const haier_protocol::HaierMessage DEVICE_VERSION_REQUEST(
140 haier_protocol::FrameType::GET_DEVICE_VERSION, module_capabilities, sizeof(module_capabilities));
141 this->send_message_(DEVICE_VERSION_REQUEST, this->use_crc_, INIT_REQUESTS_RETRY, INIT_REQUESTS_RETRY_INTERVAL);
142 }
143 break;
146 break;
149 if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
150 static const haier_protocol::HaierMessage STATUS_REQUEST(haier_protocol::FrameType::CONTROL, 0x4D01);
152 this->send_message_(STATUS_REQUEST, this->use_crc_, INIT_REQUESTS_RETRY, INIT_REQUESTS_RETRY_INTERVAL);
153 } else {
154 this->send_message_(STATUS_REQUEST, this->use_crc_);
155 }
156 this->last_status_request_ = now;
157 }
158 break;
159#ifdef USE_WIFI
161 if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
162 this->send_message_(this->get_wifi_signal_message_(), this->use_crc_);
163 this->last_signal_request_ = now;
164 }
165 break;
166#else
169 break;
170#endif
173 break;
176 break;
179 break;
182 ESP_LOGI(TAG, "Sending control packet");
183 this->send_message_(get_control_message(), this->use_crc_, CONTROL_MESSAGE_RETRIES,
185 }
186 break;
188 if (this->action_request_.has_value()) {
189 if (this->action_request_.value().message.has_value()) {
190 this->send_message_(this->action_request_.value().message.value(), this->use_crc_);
191 this->action_request_.value().message.reset();
192 } else {
193 // Message already sent, reseting request and return to idle
194 this->action_request_.reset();
196 }
197 } else {
198 ESP_LOGW(TAG, "SENDING_ACTION_COMMAND phase without action request!");
200 }
201 break;
205 this->forced_request_status_ = false;
206 }
207#ifdef USE_WIFI
208 else if (this->send_wifi_signal_ &&
209 (std::chrono::duration_cast<std::chrono::milliseconds>(now - this->last_signal_request_).count() >
212#endif
213 } break;
214 default:
215 // Shouldn't get here
216 ESP_LOGE(TAG, "Wrong protocol handler state: %s (%d), resetting communication",
219 break;
220 }
221}
222
223haier_protocol::HaierMessage Smartair2Climate::get_power_message(bool state) {
224 if (state) {
225 static haier_protocol::HaierMessage power_on_message(haier_protocol::FrameType::CONTROL, 0x4D02);
226 return power_on_message;
227 } else {
228 static haier_protocol::HaierMessage power_off_message(haier_protocol::FrameType::CONTROL, 0x4D03);
229 return power_off_message;
230 }
231}
232
233haier_protocol::HaierMessage Smartair2Climate::get_control_message() {
234 uint8_t control_out_buffer[sizeof(smartair2_protocol::HaierPacketControl)];
235 memcpy(control_out_buffer, this->last_status_message_.get(), sizeof(smartair2_protocol::HaierPacketControl));
237 out_data->cntrl = 0;
238 if (this->current_hvac_settings_.valid) {
239 HvacSettings &climate_control = this->current_hvac_settings_;
240 if (climate_control.mode.has_value()) {
241 switch (climate_control.mode.value()) {
242 case CLIMATE_MODE_OFF:
243 out_data->ac_power = 0;
244 break;
246 out_data->ac_power = 1;
248 out_data->fan_mode = this->other_modes_fan_speed_;
249 break;
251 out_data->ac_power = 1;
253 out_data->fan_mode = this->other_modes_fan_speed_;
254 break;
255 case CLIMATE_MODE_DRY:
256 out_data->ac_power = 1;
258 out_data->fan_mode = this->other_modes_fan_speed_;
259 break;
261 out_data->ac_power = 1;
263 out_data->fan_mode = this->fan_mode_speed_; // Auto doesn't work in fan only mode
264 break;
266 out_data->ac_power = 1;
268 out_data->fan_mode = this->other_modes_fan_speed_;
269 break;
270 default:
271 ESP_LOGE("Control", "Unsupported climate mode");
272 break;
273 }
274 }
275 // Set fan speed, if we are in fan mode, reject auto in fan mode
276 if (climate_control.fan_mode.has_value()) {
277 switch (climate_control.fan_mode.value()) {
278 case CLIMATE_FAN_LOW:
280 break;
283 break;
284 case CLIMATE_FAN_HIGH:
286 break;
287 case CLIMATE_FAN_AUTO:
288 if (this->mode != CLIMATE_MODE_FAN_ONLY) // if we are not in fan only mode
290 break;
291 default:
292 ESP_LOGE("Control", "Unsupported fan mode");
293 break;
294 }
295 }
296 // Set swing mode
297 if (climate_control.swing_mode.has_value()) {
299 switch (climate_control.swing_mode.value()) {
301 out_data->swing_mode = 0;
302 break;
304 out_data->swing_mode = 1;
305 break;
307 out_data->swing_mode = 2;
308 break;
310 out_data->swing_mode = 3;
311 break;
312 }
313 } else {
314 switch (climate_control.swing_mode.value()) {
316 out_data->use_swing_bits = 0;
317 out_data->swing_mode = 0;
318 break;
320 out_data->swing_mode = 0;
321 out_data->vertical_swing = 1;
322 out_data->horizontal_swing = 0;
323 break;
325 out_data->swing_mode = 0;
326 out_data->vertical_swing = 0;
327 out_data->horizontal_swing = 1;
328 break;
330 out_data->swing_mode = 1;
331 out_data->use_swing_bits = 0;
332 out_data->vertical_swing = 0;
333 out_data->horizontal_swing = 0;
334 break;
335 }
336 }
337 }
338 if (climate_control.target_temperature.has_value()) {
339 float target_temp = climate_control.target_temperature.value();
340 out_data->set_point = ((int) target_temp) - 16; // set the temperature with offset 16
341 out_data->half_degree = (target_temp - ((int) target_temp) >= 0.49) ? 1 : 0;
342 }
343 if (out_data->ac_power == 0) {
344 // If AC is off - no presets allowed
345 out_data->turbo_mode = 0;
346 out_data->quiet_mode = 0;
347 } else if (climate_control.preset.has_value()) {
348 switch (climate_control.preset.value()) {
350 out_data->ten_degree = 0;
351 out_data->turbo_mode = 0;
352 out_data->quiet_mode = 0;
353 break;
355 out_data->ten_degree = 0;
356 out_data->turbo_mode = 1;
357 out_data->quiet_mode = 0;
358 break;
360 out_data->ten_degree = 0;
361 out_data->turbo_mode = 0;
362 out_data->quiet_mode = 1;
363 break;
365 // Only allowed in heat mode
366 out_data->ten_degree = (this->mode == CLIMATE_MODE_HEAT) ? 1 : 0;
367 out_data->turbo_mode = 0;
368 out_data->quiet_mode = 0;
369 break;
370 default:
371 ESP_LOGE("Control", "Unsupported preset");
372 out_data->ten_degree = 0;
373 out_data->turbo_mode = 0;
374 out_data->quiet_mode = 0;
375 break;
376 }
377 }
378 }
379 out_data->display_status = this->get_display_state() ? 0 : 1;
380 this->display_status_ = (SwitchState) ((uint8_t) this->display_status_ & 0b01);
381 out_data->health_mode = this->get_health_mode() ? 1 : 0;
382 this->health_mode_ = (SwitchState) ((uint8_t) this->health_mode_ & 0b01);
383 return haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL, 0x4D5F, control_out_buffer,
385}
386
387haier_protocol::HandlerError Smartair2Climate::process_status_message_(const uint8_t *packet_buffer, uint8_t size) {
388 if (size < sizeof(smartair2_protocol::HaierStatus))
389 return haier_protocol::HandlerError::WRONG_MESSAGE_STRUCTURE;
391 memcpy(&packet, packet_buffer, size);
392 bool should_publish = false;
393 {
394 // Extra modes/presets
395 optional<ClimatePreset> old_preset = this->preset;
396 if (packet.control.turbo_mode != 0) {
398 } else if (packet.control.quiet_mode != 0) {
400 } else if (packet.control.ten_degree != 0) {
402 } else {
404 }
405 should_publish = should_publish || (!old_preset.has_value()) || (old_preset.value() != this->preset.value());
406 }
407 {
408 // Target temperature
409 float old_target_temperature = this->target_temperature;
410 this->target_temperature = packet.control.set_point + 16.0f + ((packet.control.half_degree == 1) ? 0.5f : 0.0f);
411 should_publish = should_publish || (old_target_temperature != this->target_temperature);
412 }
413 {
414 // Current temperature
415 float old_current_temperature = this->current_temperature;
417 should_publish = should_publish || (old_current_temperature != this->current_temperature);
418 }
419 {
420 // Fan mode
421 optional<ClimateFanMode> old_fan_mode = this->fan_mode;
422 // remember the fan speed we last had for climate vs fan
425 this->fan_mode_speed_ = packet.control.fan_mode;
426 } else {
428 }
429 switch (packet.control.fan_mode) {
431 // Sometimes AC reports in fan only mode that fan speed is auto
432 // but never accept this value back
435 } else {
436 should_publish = true;
437 }
438 break;
441 break;
444 break;
447 break;
448 }
449 should_publish = should_publish || (!old_fan_mode.has_value()) || (old_fan_mode.value() != fan_mode.value());
450 }
451 // Display status
452 // should be before "Climate mode" because it is changing this->mode
453 if (packet.control.ac_power != 0) {
454 // if AC is off display status always ON so process it only when AC is on
455 bool disp_status = packet.control.display_status == 0;
456 if (disp_status != this->get_display_state()) {
457 // Do something only if display status changed
458 if (this->mode == CLIMATE_MODE_OFF) {
459 // AC just turned on from remote need to turn off display
460 this->force_send_control_ = true;
461 } else if ((((uint8_t) this->health_mode_) & 0b10) == 0) {
462 this->display_status_ = disp_status ? SwitchState::ON : SwitchState::OFF;
463 }
464 }
465 }
466 // Health mode
467 if ((((uint8_t) this->health_mode_) & 0b10) == 0) {
468 bool old_health_mode = this->get_health_mode();
469 this->health_mode_ = packet.control.health_mode == 1 ? SwitchState::ON : SwitchState::OFF;
470 should_publish = should_publish || (old_health_mode != this->get_health_mode());
471 }
472 {
473 // Climate mode
474 ClimateMode old_mode = this->mode;
475 if (packet.control.ac_power == 0) {
476 this->mode = CLIMATE_MODE_OFF;
477 } else {
478 // Check current hvac mode
479 switch (packet.control.ac_mode) {
481 this->mode = CLIMATE_MODE_COOL;
482 break;
484 this->mode = CLIMATE_MODE_HEAT;
485 break;
487 this->mode = CLIMATE_MODE_DRY;
488 break;
491 break;
494 break;
495 }
496 }
497 should_publish = should_publish || (old_mode != this->mode);
498 }
499 {
500 // Swing mode
501 ClimateSwingMode old_swing_mode = this->swing_mode;
503 switch (packet.control.swing_mode) {
504 case 1:
506 break;
507 case 2:
509 break;
510 case 3:
512 break;
513 default:
515 break;
516 }
517 } else {
518 if (packet.control.swing_mode == 0) {
519 if (packet.control.vertical_swing != 0) {
521 } else if (packet.control.horizontal_swing != 0) {
523 } else {
525 }
526 } else {
528 }
529 }
530 should_publish = should_publish || (old_swing_mode != this->swing_mode);
531 }
532 this->last_valid_status_timestamp_ = std::chrono::steady_clock::now();
533 if (should_publish) {
534 this->publish_state();
535 }
536 if (should_publish) {
537 ESP_LOGI(TAG, "HVAC values changed");
538 }
539 int log_level = should_publish ? ESPHOME_LOG_LEVEL_INFO : ESPHOME_LOG_LEVEL_DEBUG;
540 esp_log_printf_(log_level, TAG, __LINE__, "HVAC Mode = 0x%X", packet.control.ac_mode);
541 esp_log_printf_(log_level, TAG, __LINE__, "Fan speed Status = 0x%X", packet.control.fan_mode);
542 esp_log_printf_(log_level, TAG, __LINE__, "Horizontal Swing Status = 0x%X", packet.control.horizontal_swing);
543 esp_log_printf_(log_level, TAG, __LINE__, "Vertical Swing Status = 0x%X", packet.control.vertical_swing);
544 esp_log_printf_(log_level, TAG, __LINE__, "Set Point Status = 0x%X", packet.control.set_point);
545 return haier_protocol::HandlerError::HANDLER_OK;
546}
547
549 this->use_alternative_swing_control_ = swing_control;
550}
551
552} // namespace haier
553} // namespace esphome
send_message_t send_message_
ClimateMode mode
The active mode of the climate device.
Definition climate.h:173
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:199
float target_temperature
The target temperature of the climate device.
Definition climate.h:186
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:202
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:179
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:395
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:208
bool is_message_interval_exceeded_(std::chrono::steady_clock::time_point now)
CallbackManager< void(const char *, size_t)> status_message_callback_
Definition haier_base.h:175
bool is_protocol_initialisation_interval_exceeded_(std::chrono::steady_clock::time_point now)
bool is_status_request_interval_exceeded_(std::chrono::steady_clock::time_point now)
haier_protocol::HandlerError answer_preprocess_(haier_protocol::FrameType request_message_type, haier_protocol::FrameType expected_request_message_type, haier_protocol::FrameType answer_message_type, haier_protocol::FrameType expected_answer_message_type, ProtocolPhases expected_phase)
haier_protocol::ProtocolHandler haier_protocol_
Definition haier_base.h:155
const char * phase_to_string_(ProtocolPhases phase)
std::unique_ptr< uint8_t[]> last_status_message_
Definition haier_base.h:170
haier_protocol::HandlerError report_network_status_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data, size_t data_size)
std::chrono::steady_clock::time_point last_valid_status_timestamp_
Definition haier_base.h:172
haier_protocol::HaierMessage get_wifi_signal_message_()
haier_protocol::HandlerError timeout_default_handler_(haier_protocol::FrameType request_type)
bool is_control_message_interval_exceeded_(std::chrono::steady_clock::time_point now)
esphome::optional< PendingAction > action_request_
Definition haier_base.h:157
std::chrono::steady_clock::time_point last_status_request_
Definition haier_base.h:173
virtual void set_phase(ProtocolPhases phase)
std::chrono::steady_clock::time_point last_signal_request_
Definition haier_base.h:174
haier_protocol::HandlerError status_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data, size_t data_size)
haier_protocol::HaierMessage get_control_message() override
haier_protocol::HandlerError get_device_version_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data, size_t data_size)
haier_protocol::HandlerError messages_timeout_handler_with_cycle_for_init_(haier_protocol::FrameType message_type)
haier_protocol::HandlerError process_status_message_(const uint8_t *packet, uint8_t size)
haier_protocol::HaierMessage get_power_message(bool state) override
void set_alternative_swing_control(bool swing_control)
void process_phase(std::chrono::steady_clock::time_point now) override
bool has_value() const
Definition optional.h:87
value_type const & value() const
Definition optional.h:89
bool state
Definition fan.h:0
@ CLIMATE_PRESET_NONE
No preset is active.
@ CLIMATE_PRESET_COMFORT
Device is in comfort preset.
@ CLIMATE_PRESET_AWAY
Device is in away preset.
@ CLIMATE_PRESET_BOOST
Device is in boost preset.
ClimateSwingMode
Enum for all modes a climate swing can be in.
@ CLIMATE_SWING_OFF
The swing mode is set to Off.
@ CLIMATE_SWING_HORIZONTAL
The fan mode is set to Horizontal.
@ CLIMATE_SWING_VERTICAL
The fan mode is set to Vertical.
@ CLIMATE_SWING_BOTH
The fan mode is set to Both.
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.
@ 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 CONTROL_MESSAGE_RETRIES
constexpr std::chrono::milliseconds CONTROL_MESSAGE_RETRIES_INTERVAL
constexpr uint8_t INIT_REQUESTS_RETRY
constexpr std::chrono::milliseconds INIT_REQUESTS_RETRY_INTERVAL
constexpr size_t SIGNAL_LEVEL_UPDATE_INTERVAL_MS
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void HOT esp_log_printf_(int level, const char *tag, int line, const char *format,...)
Definition log.cpp:11
esphome::optional< esphome::climate::ClimateFanMode > fan_mode
Definition haier_base.h:135
esphome::optional< esphome::climate::ClimateSwingMode > swing_mode
Definition haier_base.h:136
esphome::optional< esphome::climate::ClimateMode > mode
Definition haier_base.h:134
esphome::optional< esphome::climate::ClimatePreset > preset
Definition haier_base.h:138
esphome::optional< float > target_temperature
Definition haier_base.h:137