ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
hon_climate.cpp
Go to the documentation of this file.
1#include <chrono>
2#include <string>
6#include "hon_climate.h"
7#include "hon_packet.h"
8
9using namespace esphome::climate;
10using namespace esphome::uart;
11
12namespace esphome {
13namespace haier {
14
15static const char *const TAG = "haier.climate";
16constexpr size_t SIGNAL_LEVEL_UPDATE_INTERVAL_MS = 10000;
18constexpr uint8_t CONTROL_MESSAGE_RETRIES = 5;
19constexpr std::chrono::milliseconds CONTROL_MESSAGE_RETRIES_INTERVAL = std::chrono::milliseconds(500);
20constexpr size_t ALARM_STATUS_REQUEST_INTERVAL_MS = 600000;
21const uint8_t ONE_BUF[] = {0x00, 0x01};
22const uint8_t ZERO_BUF[] = {0x00, 0x00};
23
25 : cleaning_status_(CleaningState::NO_CLEANING), got_valid_outdoor_temp_(false), active_alarms_{0x00, 0x00, 0x00,
26 0x00, 0x00, 0x00,
27 0x00, 0x00} {
30}
31
33
35 if (state != this->settings_.beeper_state) {
37#ifdef USE_SWITCH
38 if (this->beeper_switch_ != nullptr) {
39 this->beeper_switch_->publish_state(state);
40 }
41#endif
42 this->hon_rtc_.save(&this->settings_);
43 }
44}
45
47
49 if (state != this->get_quiet_mode_state()) {
50 if ((this->mode != ClimateMode::CLIMATE_MODE_OFF) && (this->mode != ClimateMode::CLIMATE_MODE_FAN_ONLY)) {
52 this->force_send_control_ = true;
53 } else {
55 }
57#ifdef USE_SWITCH
58 if (this->quiet_mode_switch_ != nullptr) {
60 }
61#endif
62 this->hon_rtc_.save(&this->settings_);
63 }
64}
65
69
73
78
82
87
89 switch (this->cleaning_status_) {
91 return "Self clean";
93 return "56°C Steri-Clean";
94 default:
95 return "No cleaning";
96 }
97}
98
100
103 ESP_LOGI(TAG, "Sending self cleaning start request");
104 this->action_request_ =
106 }
107}
108
111 ESP_LOGI(TAG, "Sending steri cleaning start request");
112 this->action_request_ =
114 }
115}
116
117void HonClimate::add_alarm_start_callback(std::function<void(uint8_t, const char *)> &&callback) {
118 this->alarm_start_callback_.add(std::move(callback));
119}
120
121void HonClimate::add_alarm_end_callback(std::function<void(uint8_t, const char *)> &&callback) {
122 this->alarm_end_callback_.add(std::move(callback));
123}
124
125haier_protocol::HandlerError HonClimate::get_device_version_answer_handler_(haier_protocol::FrameType request_type,
126 haier_protocol::FrameType message_type,
127 const uint8_t *data, size_t data_size) {
128 // Should check this before preprocess
129 if (message_type == haier_protocol::FrameType::INVALID) {
130 ESP_LOGW(TAG, "It looks like your ESPHome Haier climate configuration is wrong. You should use the smartAir2 "
131 "protocol instead of hOn");
133 return haier_protocol::HandlerError::INVALID_ANSWER;
134 }
135 haier_protocol::HandlerError result =
136 this->answer_preprocess_(request_type, haier_protocol::FrameType::GET_DEVICE_VERSION, message_type,
137 haier_protocol::FrameType::GET_DEVICE_VERSION_RESPONSE, ProtocolPhases::SENDING_INIT_1);
138 if (result == haier_protocol::HandlerError::HANDLER_OK) {
139 if (data_size < sizeof(hon_protocol::DeviceVersionAnswer)) {
140 // Wrong structure
141 return haier_protocol::HandlerError::WRONG_MESSAGE_STRUCTURE;
142 }
143 // All OK
145 char tmp[9];
146 tmp[8] = 0;
147 strncpy(tmp, answr->protocol_version, 8);
149 this->hvac_hardware_info_.value().protocol_version_ = std::string(tmp);
150 strncpy(tmp, answr->software_version, 8);
151 this->hvac_hardware_info_.value().software_version_ = std::string(tmp);
152 strncpy(tmp, answr->hardware_version, 8);
153 this->hvac_hardware_info_.value().hardware_version_ = std::string(tmp);
154 strncpy(tmp, answr->device_name, 8);
155 this->hvac_hardware_info_.value().device_name_ = std::string(tmp);
156#ifdef USE_TEXT_SENSOR
159 this->hvac_hardware_info_.value().protocol_version_);
160#endif
161 this->hvac_hardware_info_.value().functions_[0] = (answr->functions[1] & 0x01) != 0; // interactive mode support
162 this->hvac_hardware_info_.value().functions_[1] =
163 (answr->functions[1] & 0x02) != 0; // controller-device mode support
164 this->hvac_hardware_info_.value().functions_[2] = (answr->functions[1] & 0x04) != 0; // crc support
165 this->hvac_hardware_info_.value().functions_[3] = (answr->functions[1] & 0x08) != 0; // multiple AC support
166 this->hvac_hardware_info_.value().functions_[4] = (answr->functions[1] & 0x20) != 0; // roles support
167 this->use_crc_ = this->hvac_hardware_info_.value().functions_[2];
169 return result;
170 } else {
171 this->reset_phase_();
172 return result;
173 }
174}
175
176haier_protocol::HandlerError HonClimate::get_device_id_answer_handler_(haier_protocol::FrameType request_type,
177 haier_protocol::FrameType message_type,
178 const uint8_t *data, size_t data_size) {
179 haier_protocol::HandlerError result =
180 this->answer_preprocess_(request_type, haier_protocol::FrameType::GET_DEVICE_ID, message_type,
181 haier_protocol::FrameType::GET_DEVICE_ID_RESPONSE, ProtocolPhases::SENDING_INIT_2);
182 if (result == haier_protocol::HandlerError::HANDLER_OK) {
184 return result;
185 } else {
186 this->reset_phase_();
187 return result;
188 }
189}
190
191haier_protocol::HandlerError HonClimate::status_handler_(haier_protocol::FrameType request_type,
192 haier_protocol::FrameType message_type, const uint8_t *data,
193 size_t data_size) {
194 haier_protocol::HandlerError result =
195 this->answer_preprocess_(request_type, haier_protocol::FrameType::CONTROL, message_type,
196 haier_protocol::FrameType::STATUS, ProtocolPhases::UNKNOWN);
197 if (result == haier_protocol::HandlerError::HANDLER_OK) {
198 result = this->process_status_message_(data, data_size);
199 if (result != haier_protocol::HandlerError::HANDLER_OK) {
200 ESP_LOGW(TAG, "Error %d while parsing Status packet", (int) result);
201 this->reset_phase_();
202 this->action_request_.reset();
203 this->force_send_control_ = false;
204 } else {
205 if (!this->last_status_message_) {
208 this->last_status_message_.reset();
209 this->last_status_message_ = std::unique_ptr<uint8_t[]>(new uint8_t[this->real_control_packet_size_]);
210 };
211 if (data_size >= this->real_control_packet_size_ + 2) {
212 memcpy(this->last_status_message_.get(), data + 2 + this->status_message_header_size_,
213 this->real_control_packet_size_);
214 this->status_message_callback_.call((const char *) data, data_size);
215 } else {
216 ESP_LOGW(TAG, "Status packet too small: %zu (should be >= %zu)", data_size, this->real_control_packet_size_);
217 }
218 switch (this->protocol_phase_) {
220 ESP_LOGI(TAG, "First HVAC status received");
222 break;
224 // Do nothing, phase will be changed in process_phase
225 break;
228 break;
230 if (!this->control_messages_queue_.empty())
231 this->control_messages_queue_.pop();
232 if (this->control_messages_queue_.empty()) {
234 this->force_send_control_ = false;
237 } else {
239 }
240 break;
241 default:
242 break;
243 }
244 }
245 return result;
246 } else {
247 this->action_request_.reset();
248 this->force_send_control_ = false;
249 this->reset_phase_();
250 return result;
251 }
252}
253
255 haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data,
256 size_t data_size) {
257 haier_protocol::HandlerError result = this->answer_preprocess_(
258 request_type, haier_protocol::FrameType::GET_MANAGEMENT_INFORMATION, message_type,
259 haier_protocol::FrameType::GET_MANAGEMENT_INFORMATION_RESPONSE, ProtocolPhases::SENDING_UPDATE_SIGNAL_REQUEST);
260 if (result == haier_protocol::HandlerError::HANDLER_OK) {
262 return result;
263 } else {
265 return result;
266 }
267}
268
269haier_protocol::HandlerError HonClimate::get_alarm_status_answer_handler_(haier_protocol::FrameType request_type,
270 haier_protocol::FrameType message_type,
271 const uint8_t *data, size_t data_size) {
272 if (request_type == haier_protocol::FrameType::GET_ALARM_STATUS) {
273 if (message_type != haier_protocol::FrameType::GET_ALARM_STATUS_RESPONSE) {
274 // Unexpected answer to request
276 return haier_protocol::HandlerError::UNSUPPORTED_MESSAGE;
277 }
280 // Don't expect this answer now
282 return haier_protocol::HandlerError::UNEXPECTED_MESSAGE;
283 }
284 if (data_size < sizeof(active_alarms_) + 2)
285 return haier_protocol::HandlerError::WRONG_MESSAGE_STRUCTURE;
286 this->process_alarm_message_(data, data_size, this->protocol_phase_ >= ProtocolPhases::IDLE);
288 return haier_protocol::HandlerError::HANDLER_OK;
289 } else {
291 return haier_protocol::HandlerError::UNSUPPORTED_MESSAGE;
292 }
293}
294
295haier_protocol::HandlerError HonClimate::alarm_status_message_handler_(haier_protocol::FrameType type,
296 const uint8_t *buffer, size_t size) {
297 haier_protocol::HandlerError result = haier_protocol::HandlerError::HANDLER_OK;
298 if (size < sizeof(this->active_alarms_) + 2) {
299 // Log error but confirm anyway to avoid to many messages
300 result = haier_protocol::HandlerError::WRONG_MESSAGE_STRUCTURE;
301 }
302 this->process_alarm_message_(buffer, size, true);
303 this->haier_protocol_.send_answer(haier_protocol::HaierMessage(haier_protocol::FrameType::CONFIRM));
304 this->last_alarm_request_ = std::chrono::steady_clock::now();
305 return result;
306}
307
309 // Set handlers
310 this->haier_protocol_.set_answer_handler(
311 haier_protocol::FrameType::GET_DEVICE_VERSION,
312 std::bind(&HonClimate::get_device_version_answer_handler_, this, std::placeholders::_1, std::placeholders::_2,
313 std::placeholders::_3, std::placeholders::_4));
314 this->haier_protocol_.set_answer_handler(
315 haier_protocol::FrameType::GET_DEVICE_ID,
316 std::bind(&HonClimate::get_device_id_answer_handler_, this, std::placeholders::_1, std::placeholders::_2,
317 std::placeholders::_3, std::placeholders::_4));
318 this->haier_protocol_.set_answer_handler(
319 haier_protocol::FrameType::CONTROL,
320 std::bind(&HonClimate::status_handler_, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
321 std::placeholders::_4));
322 this->haier_protocol_.set_answer_handler(
323 haier_protocol::FrameType::GET_MANAGEMENT_INFORMATION,
324 std::bind(&HonClimate::get_management_information_answer_handler_, this, std::placeholders::_1,
325 std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
326 this->haier_protocol_.set_answer_handler(
327 haier_protocol::FrameType::GET_ALARM_STATUS,
328 std::bind(&HonClimate::get_alarm_status_answer_handler_, this, std::placeholders::_1, std::placeholders::_2,
329 std::placeholders::_3, std::placeholders::_4));
330 this->haier_protocol_.set_answer_handler(
331 haier_protocol::FrameType::REPORT_NETWORK_STATUS,
332 std::bind(&HonClimate::report_network_status_answer_handler_, this, std::placeholders::_1, std::placeholders::_2,
333 std::placeholders::_3, std::placeholders::_4));
334 this->haier_protocol_.set_message_handler(
335 haier_protocol::FrameType::ALARM_STATUS,
336 std::bind(&HonClimate::alarm_status_message_handler_, this, std::placeholders::_1, std::placeholders::_2,
337 std::placeholders::_3));
338}
339
342 ESP_LOGCONFIG(TAG,
343 " Protocol version: hOn\n"
344 " Control method: %d",
345 (uint8_t) this->control_method_);
346 if (this->hvac_hardware_info_.has_value()) {
347 ESP_LOGCONFIG(TAG,
348 " Device protocol version: %s\n"
349 " Device software version: %s\n"
350 " Device hardware version: %s\n"
351 " Device name: %s",
352 this->hvac_hardware_info_.value().protocol_version_.c_str(),
353 this->hvac_hardware_info_.value().software_version_.c_str(),
354 this->hvac_hardware_info_.value().hardware_version_.c_str(),
355 this->hvac_hardware_info_.value().device_name_.c_str());
356 ESP_LOGCONFIG(TAG, " Device features:%s%s%s%s%s",
357 (this->hvac_hardware_info_.value().functions_[0] ? " interactive" : ""),
358 (this->hvac_hardware_info_.value().functions_[1] ? " controller-device" : ""),
359 (this->hvac_hardware_info_.value().functions_[2] ? " crc" : ""),
360 (this->hvac_hardware_info_.value().functions_[3] ? " multinode" : ""),
361 (this->hvac_hardware_info_.value().functions_[4] ? " role" : ""));
362 ESP_LOGCONFIG(TAG, " Active alarms: %s", buf_to_hex(this->active_alarms_, sizeof(this->active_alarms_)).c_str());
363 }
364}
365
366void HonClimate::process_phase(std::chrono::steady_clock::time_point now) {
367 switch (this->protocol_phase_) {
370 // Indicate device capabilities:
371 // bit 0 - if 1 module support interactive mode
372 // bit 1 - if 1 module support controller-device mode
373 // bit 2 - if 1 module support crc
374 // bit 3 - if 1 module support multiple devices
375 // bit 4..bit 15 - not used
376 uint8_t module_capabilities[2] = {0b00000000, 0b00000111};
377 static const haier_protocol::HaierMessage DEVICE_VERSION_REQUEST(
378 haier_protocol::FrameType::GET_DEVICE_VERSION, module_capabilities, sizeof(module_capabilities));
379 this->send_message_(DEVICE_VERSION_REQUEST, this->use_crc_);
380 }
381 break;
383 if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
384 static const haier_protocol::HaierMessage DEVICEID_REQUEST(haier_protocol::FrameType::GET_DEVICE_ID);
385 this->send_message_(DEVICEID_REQUEST, this->use_crc_);
386 }
387 break;
390 if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
391 static const haier_protocol::HaierMessage STATUS_REQUEST(
392 haier_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::GET_USER_DATA);
393 static const haier_protocol::HaierMessage BIG_DATA_REQUEST(
394 haier_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::GET_BIG_DATA);
396 (!this->should_get_big_data_())) {
397 this->send_message_(STATUS_REQUEST, this->use_crc_);
398 } else {
399 this->send_message_(BIG_DATA_REQUEST, this->use_crc_);
400 }
401 this->last_status_request_ = now;
402 }
403 break;
404#ifdef USE_WIFI
406 if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
407 static const haier_protocol::HaierMessage UPDATE_SIGNAL_REQUEST(
408 haier_protocol::FrameType::GET_MANAGEMENT_INFORMATION);
409 this->send_message_(UPDATE_SIGNAL_REQUEST, this->use_crc_);
410 this->last_signal_request_ = now;
411 }
412 break;
414 if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
415 this->send_message_(this->get_wifi_signal_message_(), this->use_crc_);
416 }
417 break;
418#else
422 break;
423#endif
426 if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
427 static const haier_protocol::HaierMessage ALARM_STATUS_REQUEST(haier_protocol::FrameType::GET_ALARM_STATUS);
428 this->send_message_(ALARM_STATUS_REQUEST, this->use_crc_);
429 this->last_alarm_request_ = now;
430 }
431 break;
433 if (this->control_messages_queue_.empty()) {
434 switch (this->control_method_) {
436 haier_protocol::HaierMessage control_message = this->get_control_message();
437 this->control_messages_queue_.push(control_message);
438 } break;
441 break;
443 ESP_LOGI(TAG, "AC control is disabled, monitor only");
444 this->reset_to_idle_();
445 return;
446 default:
447 ESP_LOGW(TAG, "Unsupported control method for hOn protocol!");
448 this->reset_to_idle_();
449 return;
450 }
451 }
452 if (this->control_messages_queue_.empty()) {
453 ESP_LOGW(TAG, "Control message queue is empty!");
454 this->reset_to_idle_();
455 } else if (this->can_send_message() && this->is_control_message_interval_exceeded_(now)) {
456 ESP_LOGI(TAG, "Sending control packet, queue size %d", this->control_messages_queue_.size());
457 this->send_message_(this->control_messages_queue_.front(), this->use_crc_, CONTROL_MESSAGE_RETRIES,
459 }
460 break;
462 if (this->action_request_.has_value()) {
463 if (this->action_request_.value().message.has_value()) {
464 this->send_message_(this->action_request_.value().message.value(), this->use_crc_);
465 this->action_request_.value().message.reset();
466 } else {
467 // Message already sent, reseting request and return to idle
468 this->action_request_.reset();
470 }
471 } else {
472 ESP_LOGW(TAG, "SENDING_ACTION_COMMAND phase without action request!");
474 }
475 break;
479 this->forced_request_status_ = false;
480 } else if (std::chrono::duration_cast<std::chrono::milliseconds>(now - this->last_alarm_request_).count() >
483 }
484#ifdef USE_WIFI
485 else if (this->send_wifi_signal_ &&
486 (std::chrono::duration_cast<std::chrono::milliseconds>(now - this->last_signal_request_).count() >
489 }
490#endif
491 } break;
492 default:
493 // Shouldn't get here
494 ESP_LOGE(TAG, "Wrong protocol handler state: %s (%d), resetting communication",
497 break;
498 }
499}
500
501haier_protocol::HaierMessage HonClimate::get_power_message(bool state) {
502 if (state) {
503 static haier_protocol::HaierMessage power_on_message(
504 haier_protocol::FrameType::CONTROL, ((uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER) + 1,
505 std::initializer_list<uint8_t>({0x00, 0x01}).begin(), 2);
506 return power_on_message;
507 } else {
508 static haier_protocol::HaierMessage power_off_message(
509 haier_protocol::FrameType::CONTROL, ((uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER) + 1,
510 std::initializer_list<uint8_t>({0x00, 0x00}).begin(), 2);
511 return power_off_message;
512 }
513}
514
517 constexpr uint32_t restore_settings_version = 0x57EB59DDUL;
518 this->hon_rtc_ = this->make_entity_preference<HonSettings>(restore_settings_version);
519 HonSettings recovered;
520 if (this->hon_rtc_.load(&recovered)) {
521 this->settings_ = recovered;
522 } else {
524 }
528}
529
530haier_protocol::HaierMessage HonClimate::get_control_message() {
531 uint8_t control_out_buffer[haier_protocol::MAX_FRAME_SIZE];
532 memcpy(control_out_buffer, this->last_status_message_.get(), this->real_control_packet_size_);
534 control_out_buffer[4] = 0; // This byte should be cleared before setting values
535 bool has_hvac_settings = false;
536 if (this->current_hvac_settings_.valid) {
537 has_hvac_settings = true;
538 HvacSettings &climate_control = this->current_hvac_settings_;
539 if (climate_control.mode.has_value()) {
540 switch (climate_control.mode.value()) {
541 case CLIMATE_MODE_OFF:
542 out_data->ac_power = 0;
543 break;
545 out_data->ac_power = 1;
546 out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::AUTO;
547 out_data->fan_mode = this->other_modes_fan_speed_;
548 break;
550 out_data->ac_power = 1;
551 out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::HEAT;
552 out_data->fan_mode = this->other_modes_fan_speed_;
553 break;
554 case CLIMATE_MODE_DRY:
555 out_data->ac_power = 1;
556 out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::DRY;
557 out_data->fan_mode = this->other_modes_fan_speed_;
558 break;
560 out_data->ac_power = 1;
561 out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::FAN;
562 out_data->fan_mode = this->fan_mode_speed_; // Auto doesn't work in fan only mode
563 // Disabling boost for Fan only
564 out_data->fast_mode = 0;
565 break;
567 out_data->ac_power = 1;
568 out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::COOL;
569 out_data->fan_mode = this->other_modes_fan_speed_;
570 break;
571 default:
572 ESP_LOGE("Control", "Unsupported climate mode");
573 break;
574 }
575 }
576 // Set fan speed, if we are in fan mode, reject auto in fan mode
577 if (climate_control.fan_mode.has_value()) {
578 switch (climate_control.fan_mode.value()) {
579 case CLIMATE_FAN_LOW:
580 out_data->fan_mode = (uint8_t) hon_protocol::FanMode::FAN_LOW;
581 break;
583 out_data->fan_mode = (uint8_t) hon_protocol::FanMode::FAN_MID;
584 break;
585 case CLIMATE_FAN_HIGH:
586 out_data->fan_mode = (uint8_t) hon_protocol::FanMode::FAN_HIGH;
587 break;
588 case CLIMATE_FAN_AUTO:
589 if (mode != CLIMATE_MODE_FAN_ONLY) // if we are not in fan only mode
590 out_data->fan_mode = (uint8_t) hon_protocol::FanMode::FAN_AUTO;
591 break;
592 default:
593 ESP_LOGE("Control", "Unsupported fan mode");
594 break;
595 }
596 }
597 // Set swing mode
598 if (climate_control.swing_mode.has_value()) {
599 switch (climate_control.swing_mode.value()) {
601 out_data->horizontal_swing_mode = (uint8_t) this->settings_.last_horizontal_swing;
602 out_data->vertical_swing_mode = (uint8_t) this->settings_.last_vertiacal_swing;
603 break;
605 out_data->horizontal_swing_mode = (uint8_t) this->settings_.last_horizontal_swing;
607 break;
610 out_data->vertical_swing_mode = (uint8_t) this->settings_.last_vertiacal_swing;
611 break;
615 break;
616 }
617 }
618 if (climate_control.target_temperature.has_value()) {
619 float target_temp = climate_control.target_temperature.value();
620 out_data->set_point = ((int) target_temp) - 16; // set the temperature with offset 16
621 out_data->half_degree = (target_temp - ((int) target_temp) >= 0.49) ? 1 : 0;
622 }
623 if (out_data->ac_power == 0) {
624 // If AC is off - no presets allowed
625 out_data->fast_mode = 0;
626 out_data->sleep_mode = 0;
627 } else if (climate_control.preset.has_value()) {
628 switch (climate_control.preset.value()) {
630 out_data->fast_mode = 0;
631 out_data->sleep_mode = 0;
632 out_data->ten_degree = 0;
633 break;
635 // Boost is not supported in Fan only mode
636 out_data->fast_mode = (this->mode != CLIMATE_MODE_FAN_ONLY) ? 1 : 0;
637 out_data->sleep_mode = 0;
638 out_data->ten_degree = 0;
639 break;
641 out_data->fast_mode = 0;
642 out_data->sleep_mode = 0;
643 // 10 degrees allowed only in heat mode
644 out_data->ten_degree = (this->mode == CLIMATE_MODE_HEAT) ? 1 : 0;
645 break;
647 out_data->fast_mode = 0;
648 out_data->sleep_mode = 1;
649 out_data->ten_degree = 0;
650 break;
651 default:
652 ESP_LOGE("Control", "Unsupported preset");
653 out_data->fast_mode = 0;
654 out_data->sleep_mode = 0;
655 out_data->ten_degree = 0;
656 break;
657 }
658 }
659 }
661 out_data->vertical_swing_mode = (uint8_t) this->pending_vertical_direction_.value();
663 }
665 out_data->horizontal_swing_mode = (uint8_t) this->pending_horizontal_direction_.value();
667 }
668 {
669 // Quiet mode
670 if ((out_data->ac_power == 0) || (out_data->ac_mode == (uint8_t) hon_protocol::ConditioningMode::FAN)) {
671 // If AC is off or in fan only mode - no quiet mode allowed
672 out_data->quiet_mode = 0;
673 } else {
674 out_data->quiet_mode = this->get_quiet_mode_state() ? 1 : 0;
675 }
676 // Clean quiet mode state pending flag
677 this->quiet_mode_state_ = (SwitchState) ((uint8_t) this->quiet_mode_state_ & 0b01);
678 }
679 out_data->beeper_status = ((!this->get_beeper_state()) || (!has_hvac_settings)) ? 1 : 0;
680 control_out_buffer[4] = 0; // This byte should be cleared before setting values
681 out_data->display_status = this->get_display_state() ? 1 : 0;
682 this->display_status_ = (SwitchState) ((uint8_t) this->display_status_ & 0b01);
683 out_data->health_mode = this->get_health_mode() ? 1 : 0;
684 this->health_mode_ = (SwitchState) ((uint8_t) this->health_mode_ & 0b01);
685 return haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
687 control_out_buffer, this->real_control_packet_size_);
688}
689
690void HonClimate::process_alarm_message_(const uint8_t *packet, uint8_t size, bool check_new) {
691 constexpr size_t active_alarms_size = sizeof(this->active_alarms_);
692 if (size >= active_alarms_size + 2) {
693 if (check_new) {
694 size_t alarm_code = 0;
695 for (int i = active_alarms_size - 1; i >= 0; i--) {
696 if (packet[2 + i] != active_alarms_[i]) {
697 uint8_t alarm_bit = 1;
698 for (int b = 0; b < 8; b++) {
699 if ((packet[2 + i] & alarm_bit) != (this->active_alarms_[i] & alarm_bit)) {
700 bool alarm_status = (packet[2 + i] & alarm_bit) != 0;
701 int log_level = alarm_status ? ESPHOME_LOG_LEVEL_WARN : ESPHOME_LOG_LEVEL_INFO;
702 const char *alarm_message = alarm_code < esphome::haier::hon_protocol::HON_ALARM_COUNT
704 : "Unknown";
705 esp_log_printf_(log_level, TAG, __LINE__, "Alarm %s (%d): %s", alarm_status ? "activated" : "deactivated",
706 alarm_code, alarm_message);
707 if (alarm_status) {
708 this->alarm_start_callback_.call(alarm_code, alarm_message);
709 this->active_alarm_count_ += 1.0f;
710 } else {
711 this->alarm_end_callback_.call(alarm_code, alarm_message);
712 this->active_alarm_count_ -= 1.0f;
713 }
714 }
715 alarm_bit <<= 1;
716 alarm_code++;
717 }
718 active_alarms_[i] = packet[2 + i];
719 } else {
720 alarm_code += 8;
721 }
722 }
723 } else {
724 float alarm_count = 0.0f;
725 static uint8_t nibble_bits_count[] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
726 for (size_t i = 0; i < sizeof(this->active_alarms_); i++) {
727 alarm_count += (float) (nibble_bits_count[packet[2 + i] & 0x0F] + nibble_bits_count[packet[2 + i] >> 4]);
728 }
729 this->active_alarm_count_ = alarm_count;
730 memcpy(this->active_alarms_, packet + 2, sizeof(this->active_alarms_));
731 }
732 }
733}
734
735#ifdef USE_SENSOR
739 if ((this->sub_sensors_[(size_t) type] != nullptr) && (sens == nullptr)) {
740 this->big_data_sensors_--;
741 } else if ((this->sub_sensors_[(size_t) type] == nullptr) && (sens != nullptr)) {
742 this->big_data_sensors_++;
743 }
744 }
745 this->sub_sensors_[(size_t) type] = sens;
746 }
747}
748
751 size_t index = (size_t) type;
752 if ((this->sub_sensors_[index] != nullptr) &&
753 ((!this->sub_sensors_[index]->has_state()) || (this->sub_sensors_[index]->raw_state != value)))
754 this->sub_sensors_[index]->publish_state(value);
755 }
756}
757#endif // USE_SENSOR
758
759#ifdef USE_BINARY_SENSOR
762 if ((this->sub_binary_sensors_[(size_t) type] != nullptr) && (sens == nullptr)) {
763 this->big_data_sensors_--;
764 } else if ((this->sub_binary_sensors_[(size_t) type] == nullptr) && (sens != nullptr)) {
765 this->big_data_sensors_++;
766 }
767 this->sub_binary_sensors_[(size_t) type] = sens;
768 }
769}
770
772 if (value < 2) {
773 bool converted_value = value == 1;
774 size_t index = (size_t) type;
775 if ((this->sub_binary_sensors_[index] != nullptr) && ((!this->sub_binary_sensors_[index]->has_state()) ||
776 (this->sub_binary_sensors_[index]->state != converted_value)))
777 this->sub_binary_sensors_[index]->publish_state(converted_value);
778 }
779}
780#endif // USE_BINARY_SENSOR
781
782#ifdef USE_TEXT_SENSOR
784 this->sub_text_sensors_[(size_t) type] = sens;
785 switch (type) {
787 if (this->hvac_hardware_info_.has_value())
788 sens->publish_state(this->hvac_hardware_info_.value().device_name_);
789 break;
791 if (this->hvac_hardware_info_.has_value())
792 sens->publish_state(this->hvac_hardware_info_.value().protocol_version_);
793 break;
796 break;
797 default:
798 break;
799 }
800}
801
803 size_t index = (size_t) type;
804 if (this->sub_text_sensors_[index] != nullptr)
805 this->sub_text_sensors_[index]->publish_state(value);
806}
807#endif // USE_TEXT_SENSOR
808
809#ifdef USE_SWITCH
811 this->beeper_switch_ = sw;
812 if (this->beeper_switch_ != nullptr) {
814 }
815}
816
823#endif // USE_SWITCH
824
825haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *packet_buffer, uint8_t size) {
826 size_t expected_size =
828 if (size < expected_size) {
829 ESP_LOGW(TAG, "Unexpected message size %u (expexted >= %zu)", size, expected_size);
830 return haier_protocol::HandlerError::WRONG_MESSAGE_STRUCTURE;
831 }
832 uint16_t subtype = (((uint16_t) packet_buffer[0]) << 8) + packet_buffer[1];
833 if ((subtype == 0x7D01) && (size >= expected_size + sizeof(hon_protocol::HaierPacketBigData))) {
834 // Got BigData packet
835 const hon_protocol::HaierPacketBigData *bd_packet =
836 (const hon_protocol::HaierPacketBigData *) (&packet_buffer[expected_size]);
837#ifdef USE_SENSOR
843 this->update_sub_sensor_(SubSensorType::POWER, encode_uint16(bd_packet->power[0], bd_packet->power[1]));
846 encode_uint16(bd_packet->compressor_current[0], bd_packet->compressor_current[1]) / 10.0);
847 this->update_sub_sensor_(
849 encode_uint16(bd_packet->expansion_valve_open_degree[0], bd_packet->expansion_valve_open_degree[1]) / 4095.0);
850#endif // USE_SENSOR
851#ifdef USE_BINARY_SENSOR
859#endif // USE_BINARY_SENSOR
860 }
861 struct {
864 } packet;
865 memcpy(&packet.control, packet_buffer + 2 + this->status_message_header_size_,
867 memcpy(&packet.sensors, packet_buffer + 2 + this->status_message_header_size_ + this->real_control_packet_size_,
869 if (packet.sensors.error_status != 0) {
870 ESP_LOGW(TAG, "HVAC error, code=0x%02X", packet.sensors.error_status);
871 }
872#ifdef USE_SENSOR
873 if ((this->sub_sensors_[(size_t) SubSensorType::OUTDOOR_TEMPERATURE] != nullptr) &&
874 (this->got_valid_outdoor_temp_ || (packet.sensors.outdoor_temperature > 0))) {
875 this->got_valid_outdoor_temp_ = true;
877 (float) (packet.sensors.outdoor_temperature + PROTOCOL_OUTDOOR_TEMPERATURE_OFFSET));
878 }
879 if ((this->sub_sensors_[(size_t) SubSensorType::HUMIDITY] != nullptr) && (packet.sensors.room_humidity <= 100)) {
880 this->update_sub_sensor_(SubSensorType::HUMIDITY, (float) packet.sensors.room_humidity);
881 }
882#endif // USE_SENSOR
883 bool should_publish = false;
884 {
885 // Extra modes/presets
886 optional<ClimatePreset> old_preset = this->preset;
887 if (packet.control.fast_mode != 0) {
889 } else if (packet.control.sleep_mode != 0) {
891 } else if (packet.control.ten_degree != 0) {
893 } else {
895 }
896 should_publish = should_publish || (!old_preset.has_value()) || (old_preset.value() != this->preset.value());
897 }
898 {
899 // Target temperature
900 float old_target_temperature = this->target_temperature;
901 this->target_temperature = packet.control.set_point + 16.0f + ((packet.control.half_degree == 1) ? 0.5f : 0.0f);
902 should_publish = should_publish || (old_target_temperature != this->target_temperature);
903 }
904 {
905 // Current temperature
906 float old_current_temperature = this->current_temperature;
907 this->current_temperature = packet.sensors.room_temperature / 2.0f;
908 should_publish = should_publish || (old_current_temperature != this->current_temperature);
909 }
910 {
911 // Fan mode
912 optional<ClimateFanMode> old_fan_mode = this->fan_mode;
913 // remember the fan speed we last had for climate vs fan
914 if (packet.control.ac_mode == (uint8_t) hon_protocol::ConditioningMode::FAN) {
915 if (packet.control.fan_mode != (uint8_t) hon_protocol::FanMode::FAN_AUTO)
916 this->fan_mode_speed_ = packet.control.fan_mode;
917 } else {
918 this->other_modes_fan_speed_ = packet.control.fan_mode;
919 }
920 switch (packet.control.fan_mode) {
921 case (uint8_t) hon_protocol::FanMode::FAN_AUTO:
922 if (packet.control.ac_mode != (uint8_t) hon_protocol::ConditioningMode::FAN) {
924 } else {
925 // Shouldn't accept fan speed auto in fan-only mode even if AC reports it
926 ESP_LOGI(TAG, "Fan speed Auto is not supported in Fan only AC mode, ignoring");
927 }
928 break;
929 case (uint8_t) hon_protocol::FanMode::FAN_MID:
931 break;
932 case (uint8_t) hon_protocol::FanMode::FAN_LOW:
934 break;
935 case (uint8_t) hon_protocol::FanMode::FAN_HIGH:
937 break;
938 }
939 should_publish = should_publish || (!old_fan_mode.has_value()) || (old_fan_mode.value() != fan_mode.value());
940 }
941 // Display status
942 // should be before "Climate mode" because it is changing this->mode
943 if (packet.control.ac_power != 0) {
944 // if AC is off display status always ON so process it only when AC is on
945 bool disp_status = packet.control.display_status != 0;
946 if (disp_status != this->get_display_state()) {
947 // Do something only if display status changed
948 if (this->mode == CLIMATE_MODE_OFF) {
949 // AC just turned on from remote need to turn off display
950 this->force_send_control_ = true;
951 } else if ((((uint8_t) this->display_status_) & 0b10) == 0) {
952 this->display_status_ = disp_status ? SwitchState::ON : SwitchState::OFF;
953 }
954 }
955 }
956 // Health mode
957 if ((((uint8_t) this->health_mode_) & 0b10) == 0) {
958 bool old_health_mode = this->get_health_mode();
959 this->health_mode_ = packet.control.health_mode == 1 ? SwitchState::ON : SwitchState::OFF;
960 should_publish = should_publish || (old_health_mode != this->get_health_mode());
961 }
962 {
963 CleaningState new_cleaning;
964 if (packet.control.steri_clean == 1) {
965 // Steri-cleaning
966 new_cleaning = CleaningState::STERI_CLEAN;
967 } else if (packet.control.self_cleaning_status == 1) {
968 // Self-cleaning
969 new_cleaning = CleaningState::SELF_CLEAN;
970 } else {
971 // No cleaning
972 new_cleaning = CleaningState::NO_CLEANING;
973 }
974 if (new_cleaning != this->cleaning_status_) {
975 ESP_LOGD(TAG, "Cleaning status change: %d => %d", (uint8_t) this->cleaning_status_, (uint8_t) new_cleaning);
976 if (new_cleaning == CleaningState::NO_CLEANING) {
977 // Turning AC off after cleaning
978 this->action_request_ =
980 }
981 this->cleaning_status_ = new_cleaning;
982#ifdef USE_TEXT_SENSOR
984#endif // USE_TEXT_SENSOR
985 }
986 }
987 {
988 // Climate mode
989 ClimateMode old_mode = this->mode;
990 if (packet.control.ac_power == 0) {
991 this->mode = CLIMATE_MODE_OFF;
992 } else {
993 // Check current hvac mode
994 switch (packet.control.ac_mode) {
996 this->mode = CLIMATE_MODE_COOL;
997 break;
999 this->mode = CLIMATE_MODE_HEAT;
1000 break;
1002 this->mode = CLIMATE_MODE_DRY;
1003 break;
1006 break;
1009 break;
1010 }
1011 }
1012 should_publish = should_publish || (old_mode != this->mode);
1013 }
1014 {
1015 // Quiet mode, should be after climate mode
1016 if ((this->mode != CLIMATE_MODE_FAN_ONLY) && (this->mode != CLIMATE_MODE_OFF) &&
1017 ((((uint8_t) this->quiet_mode_state_) & 0b10) == 0)) {
1018 // In proper mode and not in pending state
1019 bool new_quiet_mode = packet.control.quiet_mode != 0;
1020 if (new_quiet_mode != this->get_quiet_mode_state()) {
1021 this->quiet_mode_state_ = new_quiet_mode ? SwitchState::ON : SwitchState::OFF;
1022 this->settings_.quiet_mode_state = new_quiet_mode;
1023#ifdef USE_SWITCH
1024 if (this->quiet_mode_switch_ != nullptr) {
1025 this->quiet_mode_switch_->publish_state(new_quiet_mode);
1026 }
1027#endif // USE_SWITCH
1028 this->hon_rtc_.save(&this->settings_);
1029 }
1030 }
1031 }
1032 {
1033 // Swing mode
1034 ClimateSwingMode old_swing_mode = this->swing_mode;
1035 const auto &swing_modes = traits_.get_supported_swing_modes();
1036 bool vertical_swing_supported = swing_modes.count(CLIMATE_SWING_VERTICAL);
1037 bool horizontal_swing_supported = swing_modes.count(CLIMATE_SWING_HORIZONTAL);
1038 if (horizontal_swing_supported &&
1039 (packet.control.horizontal_swing_mode == (uint8_t) hon_protocol::HorizontalSwingMode::AUTO)) {
1040 if (vertical_swing_supported &&
1041 (packet.control.vertical_swing_mode == (uint8_t) hon_protocol::VerticalSwingMode::AUTO)) {
1043 } else {
1045 }
1046 } else {
1047 if (vertical_swing_supported &&
1048 (packet.control.vertical_swing_mode == (uint8_t) hon_protocol::VerticalSwingMode::AUTO)) {
1050 } else {
1052 }
1053 }
1054 // Saving last known non auto mode for vertical and horizontal swing
1055 this->current_vertical_swing_ = (hon_protocol::VerticalSwingMode) packet.control.vertical_swing_mode;
1056 this->current_horizontal_swing_ = (hon_protocol::HorizontalSwingMode) packet.control.horizontal_swing_mode;
1059 (this->current_vertical_swing_.value() != this->settings_.last_vertiacal_swing)) ||
1060 ((this->current_horizontal_swing_.value() != hon_protocol::HorizontalSwingMode::AUTO) &&
1061 (this->current_horizontal_swing_.value() != this->settings_.last_horizontal_swing));
1062 if (save_settings) {
1065 this->hon_rtc_.save(&this->settings_);
1066 }
1067 should_publish = should_publish || (old_swing_mode != this->swing_mode);
1068 }
1069 this->last_valid_status_timestamp_ = std::chrono::steady_clock::now();
1070 if (should_publish) {
1071 this->publish_state();
1072 }
1073 if (should_publish) {
1074 ESP_LOGI(TAG, "HVAC values changed");
1075 }
1076 int log_level = should_publish ? ESPHOME_LOG_LEVEL_INFO : ESPHOME_LOG_LEVEL_DEBUG;
1077 esp_log_printf_(log_level, TAG, __LINE__, "HVAC Mode = 0x%X", packet.control.ac_mode);
1078 esp_log_printf_(log_level, TAG, __LINE__, "Fan speed Status = 0x%X", packet.control.fan_mode);
1079 esp_log_printf_(log_level, TAG, __LINE__, "Horizontal Swing Status = 0x%X", packet.control.horizontal_swing_mode);
1080 esp_log_printf_(log_level, TAG, __LINE__, "Vertical Swing Status = 0x%X", packet.control.vertical_swing_mode);
1081 esp_log_printf_(log_level, TAG, __LINE__, "Set Point Status = 0x%X", packet.control.set_point);
1082 return haier_protocol::HandlerError::HANDLER_OK;
1083}
1084
1086 if (!this->current_hvac_settings_.valid && !this->force_send_control_)
1087 return;
1089 HvacSettings climate_control;
1090 climate_control = this->current_hvac_settings_;
1091 // Beeper command
1092 {
1093 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1096 this->get_beeper_state() ? ZERO_BUF : ONE_BUF, 2);
1097 }
1098 // Health mode
1099 {
1100 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1103 this->get_health_mode() ? ONE_BUF : ZERO_BUF, 2);
1104 this->health_mode_ = (SwitchState) ((uint8_t) this->health_mode_ & 0b01);
1105 }
1106 // Climate mode
1107 ClimateMode climate_mode = this->mode;
1108 bool new_power = this->mode != CLIMATE_MODE_OFF;
1109 uint8_t fan_mode_buf[] = {0x00, 0xFF};
1110 uint8_t quiet_mode_buf[] = {0x00, 0xFF};
1111 if (climate_control.mode.has_value()) {
1112 climate_mode = climate_control.mode.value();
1113 uint8_t buffer[2] = {0x00, 0x00};
1114 switch (climate_control.mode.value()) {
1115 case CLIMATE_MODE_OFF:
1116 new_power = false;
1117 break;
1119 new_power = true;
1120 buffer[1] = (uint8_t) hon_protocol::ConditioningMode::AUTO;
1121 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1124 buffer, 2);
1125 fan_mode_buf[1] = this->other_modes_fan_speed_;
1126 break;
1127 case CLIMATE_MODE_HEAT:
1128 new_power = true;
1129 buffer[1] = (uint8_t) hon_protocol::ConditioningMode::HEAT;
1130 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1133 buffer, 2);
1134 fan_mode_buf[1] = this->other_modes_fan_speed_;
1135 break;
1136 case CLIMATE_MODE_DRY:
1137 new_power = true;
1138 buffer[1] = (uint8_t) hon_protocol::ConditioningMode::DRY;
1139 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1142 buffer, 2);
1143 fan_mode_buf[1] = this->other_modes_fan_speed_;
1144 break;
1146 new_power = true;
1147 buffer[1] = (uint8_t) hon_protocol::ConditioningMode::FAN;
1148 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1151 buffer, 2);
1152 fan_mode_buf[1] = this->other_modes_fan_speed_; // Auto doesn't work in fan only mode
1153 break;
1154 case CLIMATE_MODE_COOL:
1155 new_power = true;
1156 buffer[1] = (uint8_t) hon_protocol::ConditioningMode::COOL;
1157 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1160 buffer, 2);
1161 fan_mode_buf[1] = this->other_modes_fan_speed_;
1162 break;
1163 default:
1164 ESP_LOGE("Control", "Unsupported climate mode");
1165 break;
1166 }
1167 }
1168 // Climate power
1169 {
1170 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1173 new_power ? ONE_BUF : ZERO_BUF, 2);
1174 }
1175 // CLimate preset
1176 {
1177 uint8_t fast_mode_buf[] = {0x00, 0xFF};
1178 uint8_t away_mode_buf[] = {0x00, 0xFF};
1179 if (!new_power) {
1180 // If AC is off - no presets allowed
1181 fast_mode_buf[1] = 0x00;
1182 away_mode_buf[1] = 0x00;
1183 } else if (climate_control.preset.has_value()) {
1184 switch (climate_control.preset.value()) {
1186 fast_mode_buf[1] = 0x00;
1187 away_mode_buf[1] = 0x00;
1188 break;
1190 // Boost is not supported in Fan only mode
1191 fast_mode_buf[1] = (this->mode != CLIMATE_MODE_FAN_ONLY) ? 0x01 : 0x00;
1192 away_mode_buf[1] = 0x00;
1193 break;
1195 fast_mode_buf[1] = 0x00;
1196 away_mode_buf[1] = (this->mode == CLIMATE_MODE_HEAT) ? 0x01 : 0x00;
1197 break;
1198 default:
1199 ESP_LOGE("Control", "Unsupported preset");
1200 break;
1201 }
1202 }
1203 {
1204 // Quiet mode
1205 if (new_power && (climate_mode != CLIMATE_MODE_FAN_ONLY) && this->get_quiet_mode_state()) {
1206 quiet_mode_buf[1] = 0x01;
1207 } else {
1208 quiet_mode_buf[1] = 0x00;
1209 }
1210 // Clean quiet mode state pending flag
1211 this->quiet_mode_state_ = (SwitchState) ((uint8_t) this->quiet_mode_state_ & 0b01);
1212 }
1213 auto presets = this->traits_.get_supported_presets();
1214 if (quiet_mode_buf[1] != 0xFF) {
1215 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1218 quiet_mode_buf, 2);
1219 }
1220 if ((fast_mode_buf[1] != 0xFF) && presets.count(climate::ClimatePreset::CLIMATE_PRESET_BOOST)) {
1221 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1224 fast_mode_buf, 2);
1225 }
1226 if ((away_mode_buf[1] != 0xFF) && presets.count(climate::ClimatePreset::CLIMATE_PRESET_AWAY)) {
1227 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1230 away_mode_buf, 2);
1231 }
1232 }
1233 // Target temperature
1234 if (climate_control.target_temperature.has_value() && (this->mode != ClimateMode::CLIMATE_MODE_FAN_ONLY)) {
1235 uint8_t buffer[2] = {0x00, 0x00};
1236 buffer[1] = ((uint8_t) climate_control.target_temperature.value()) - 16;
1237 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1240 buffer, 2);
1241 }
1242 // Vertical swing mode
1243 if (climate_control.swing_mode.has_value()) {
1244 uint8_t vertical_swing_buf[] = {0x00, (uint8_t) hon_protocol::VerticalSwingMode::AUTO};
1245 uint8_t horizontal_swing_buf[] = {0x00, (uint8_t) hon_protocol::HorizontalSwingMode::AUTO};
1246 switch (climate_control.swing_mode.value()) {
1247 case CLIMATE_SWING_OFF:
1248 horizontal_swing_buf[1] = (uint8_t) this->settings_.last_horizontal_swing;
1249 vertical_swing_buf[1] = (uint8_t) this->settings_.last_vertiacal_swing;
1250 break;
1252 horizontal_swing_buf[1] = (uint8_t) this->settings_.last_horizontal_swing;
1253 break;
1255 vertical_swing_buf[1] = (uint8_t) this->settings_.last_vertiacal_swing;
1256 break;
1257 case CLIMATE_SWING_BOTH:
1258 break;
1259 }
1260 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1263 horizontal_swing_buf, 2);
1264 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1267 vertical_swing_buf, 2);
1268 }
1269 // Fan mode
1270 if (climate_control.fan_mode.has_value()) {
1271 switch (climate_control.fan_mode.value()) {
1272 case CLIMATE_FAN_LOW:
1273 fan_mode_buf[1] = (uint8_t) hon_protocol::FanMode::FAN_LOW;
1274 break;
1275 case CLIMATE_FAN_MEDIUM:
1276 fan_mode_buf[1] = (uint8_t) hon_protocol::FanMode::FAN_MID;
1277 break;
1278 case CLIMATE_FAN_HIGH:
1279 fan_mode_buf[1] = (uint8_t) hon_protocol::FanMode::FAN_HIGH;
1280 break;
1281 case CLIMATE_FAN_AUTO:
1282 if (mode != CLIMATE_MODE_FAN_ONLY) // if we are not in fan only mode
1283 fan_mode_buf[1] = (uint8_t) hon_protocol::FanMode::FAN_AUTO;
1284 break;
1285 default:
1286 ESP_LOGE("Control", "Unsupported fan mode");
1287 break;
1288 }
1289 if (fan_mode_buf[1] != 0xFF) {
1290 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1293 fan_mode_buf, 2);
1294 }
1295 }
1296}
1297
1299 while (!this->control_messages_queue_.empty())
1300 this->control_messages_queue_.pop();
1301}
1302
1304 switch (this->action_request_.value().action) {
1307 uint8_t control_out_buffer[haier_protocol::MAX_FRAME_SIZE];
1308 memcpy(control_out_buffer, this->last_status_message_.get(), this->real_control_packet_size_);
1309 hon_protocol::HaierPacketControl *out_data = (hon_protocol::HaierPacketControl *) control_out_buffer;
1310 out_data->self_cleaning_status = 1;
1311 out_data->steri_clean = 0;
1312 out_data->set_point = 0x06;
1315 out_data->ac_power = 1;
1316 out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::DRY;
1317 out_data->light_status = 0;
1318 this->action_request_.value().message = haier_protocol::HaierMessage(
1319 haier_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::SET_GROUP_PARAMETERS,
1320 control_out_buffer, this->real_control_packet_size_);
1321 return true;
1323 this->action_request_.value().message =
1324 haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
1327 ONE_BUF, 2);
1328 return true;
1329 } else {
1330 this->action_request_.reset();
1331 return false;
1332 }
1335 uint8_t control_out_buffer[haier_protocol::MAX_FRAME_SIZE];
1336 memcpy(control_out_buffer, this->last_status_message_.get(), this->real_control_packet_size_);
1337 hon_protocol::HaierPacketControl *out_data = (hon_protocol::HaierPacketControl *) control_out_buffer;
1338 out_data->self_cleaning_status = 0;
1339 out_data->steri_clean = 1;
1340 out_data->set_point = 0x06;
1343 out_data->ac_power = 1;
1344 out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::DRY;
1345 out_data->light_status = 0;
1346 this->action_request_.value().message = haier_protocol::HaierMessage(
1347 haier_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::SET_GROUP_PARAMETERS,
1348 control_out_buffer, this->real_control_packet_size_);
1349 return true;
1350 } else {
1351 // No Steri clean support (yet?) in SET_SINGLE_PARAMETER
1352 this->action_request_.reset();
1353 return false;
1354 }
1355 default:
1357 }
1358}
1359
1362#ifdef USE_SENSOR
1363 for (auto &sub_sensor : this->sub_sensors_) {
1364 if ((sub_sensor != nullptr) && sub_sensor->has_state())
1365 sub_sensor->publish_state(NAN);
1366 }
1367#endif // USE_SENSOR
1368 this->got_valid_outdoor_temp_ = false;
1369 this->hvac_hardware_info_.reset();
1370 this->last_status_message_.reset(nullptr);
1371}
1372
1374 if (this->big_data_sensors_ > 0) {
1375 static uint8_t counter = 0;
1376 counter = (counter + 1) % 3;
1377 return counter == 1;
1378 }
1379 return false;
1380}
1381
1382} // namespace haier
1383} // namespace esphome
bool save(const T *src)
Definition preferences.h:21
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
bool has_state() const
constexpr size_t count(ValueType value) const
Check if the set contains a specific value (std::set compatibility) Returns 1 if present,...
Base class for all binary_sensor-type classes.
void publish_state(bool new_state)
Publish a new state to the front-end.
ClimateMode mode
The active mode of the climate device.
Definition climate.h:266
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:260
float target_temperature
The target temperature of the climate device.
Definition climate.h:247
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:272
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:240
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:445
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:263
const ClimatePresetMask & get_supported_presets() const
const ClimateSwingModeMask & get_supported_swing_modes() const
esphome::climate::ClimateTraits traits_
Definition haier_base.h:166
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:174
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:154
const char * phase_to_string_(ProtocolPhases phase)
std::unique_ptr< uint8_t[]> last_status_message_
Definition haier_base.h:169
void send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_repeats=0, std::chrono::milliseconds interval=std::chrono::milliseconds::zero())
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)
void control(const esphome::climate::ClimateCall &call) override
std::chrono::steady_clock::time_point last_valid_status_timestamp_
Definition haier_base.h:171
haier_protocol::HaierMessage get_wifi_signal_message_()
bool is_control_message_interval_exceeded_(std::chrono::steady_clock::time_point now)
esphome::optional< PendingAction > action_request_
Definition haier_base.h:156
std::chrono::steady_clock::time_point last_status_request_
Definition haier_base.h:172
virtual void set_phase(ProtocolPhases phase)
std::chrono::steady_clock::time_point last_signal_request_
Definition haier_base.h:173
void set_quiet_mode_switch(switch_::Switch *sw)
esphome::optional< hon_protocol::VerticalSwingMode > get_vertical_airflow() const
void add_alarm_end_callback(std::function< void(uint8_t, const char *)> &&callback)
ESPPreferenceObject hon_rtc_
void set_sub_text_sensor(SubTextSensorType type, text_sensor::TextSensor *sens)
haier_protocol::HandlerError status_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data, size_t data_size)
HonControlMethod control_method_
void initialization() override
haier_protocol::HandlerError get_device_id_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data, size_t data_size)
CallbackManager< void(uint8_t, const char *)> alarm_end_callback_
esphome::optional< HardwareInfo > hvac_hardware_info_
void set_sub_sensor(SubSensorType type, sensor::Sensor *sens)
CallbackManager< void(uint8_t, const char *)> alarm_start_callback_
void update_sub_sensor_(SubSensorType type, float value)
void set_sub_binary_sensor(SubBinarySensorType type, binary_sensor::BinarySensor *sens)
haier_protocol::HaierMessage get_power_message(bool state) override
text_sensor::TextSensor * sub_text_sensors_[(size_t) SubTextSensorType::SUB_TEXT_SENSOR_TYPE_COUNT]
Definition hon_climate.h:94
haier_protocol::HandlerError get_alarm_status_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data, size_t data_size)
switch_::Switch * beeper_switch_
std::chrono::steady_clock::time_point last_alarm_request_
sensor::Sensor * sub_sensors_[(size_t) SubSensorType::SUB_SENSOR_TYPE_COUNT]
Definition hon_climate.h:63
void set_beeper_state(bool state)
void set_horizontal_airflow(hon_protocol::HorizontalSwingMode direction)
haier_protocol::HandlerError process_status_message_(const uint8_t *packet, uint8_t size)
void add_alarm_start_callback(std::function< void(uint8_t, const char *)> &&callback)
void set_vertical_airflow(hon_protocol::VerticalSwingMode direction)
CleaningState cleaning_status_
haier_protocol::HandlerError get_management_information_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data, size_t data_size)
std::queue< haier_protocol::HaierMessage > control_messages_queue_
esphome::optional< hon_protocol::HorizontalSwingMode > current_horizontal_swing_
void set_quiet_mode_state(bool state)
void update_sub_text_sensor_(SubTextSensorType type, const std::string &value)
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)
binary_sensor::BinarySensor * sub_binary_sensors_[(size_t) SubBinarySensorType::SUB_BINARY_SENSOR_TYPE_COUNT]
Definition hon_climate.h:80
haier_protocol::HandlerError alarm_status_message_handler_(haier_protocol::FrameType type, const uint8_t *buffer, size_t size)
void update_sub_binary_sensor_(SubBinarySensorType type, uint8_t value)
haier_protocol::HaierMessage get_control_message() override
void process_phase(std::chrono::steady_clock::time_point now) override
esphome::optional< hon_protocol::HorizontalSwingMode > pending_horizontal_direction_
bool get_quiet_mode_state() const
switch_::Switch * quiet_mode_switch_
std::string get_cleaning_status_text() const
void process_protocol_reset() override
CleaningState get_cleaning_status() const
esphome::optional< hon_protocol::HorizontalSwingMode > get_horizontal_airflow() const
void set_beeper_switch(switch_::Switch *sw)
esphome::optional< hon_protocol::VerticalSwingMode > current_vertical_swing_
void process_alarm_message_(const uint8_t *packet, uint8_t size, bool check_new)
bool prepare_pending_action() override
esphome::optional< hon_protocol::VerticalSwingMode > pending_vertical_direction_
bool has_value() const
Definition optional.h:92
value_type const & value() const
Definition optional.h:94
Base-class for all sensors.
Definition sensor.h:47
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
Base class for all switches.
Definition switch.h:38
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:56
void publish_state(const std::string &state)
uint16_t type
FanDirection direction
Definition fan.h:5
bool state
Definition fan.h:2
@ CLIMATE_PRESET_NONE
No preset is active.
@ CLIMATE_PRESET_AWAY
Device is in away preset.
@ CLIMATE_PRESET_BOOST
Device is in boost preset.
@ CLIMATE_PRESET_SLEEP
Device is prepared for sleep.
ClimateSwingMode
Enum for all modes a climate swing can be in NOTE: If adding values, update ClimateSwingModeMask in c...
@ 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.
const std::string HON_ALARM_MESSAGES[]
Definition hon_packet.h:202
constexpr size_t HON_ALARM_COUNT
Definition hon_packet.h:256
constexpr uint8_t CONTROL_MESSAGE_RETRIES
constexpr size_t ALARM_STATUS_REQUEST_INTERVAL_MS
constexpr std::chrono::milliseconds CONTROL_MESSAGE_RETRIES_INTERVAL
constexpr int PROTOCOL_OUTDOOR_TEMPERATURE_OFFSET
const uint8_t ZERO_BUF[]
const uint8_t ONE_BUF[]
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
size_t size
Definition helpers.h:854
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:653
esphome::optional< esphome::climate::ClimateFanMode > fan_mode
Definition haier_base.h:134
esphome::optional< esphome::climate::ClimateSwingMode > swing_mode
Definition haier_base.h:135
esphome::optional< esphome::climate::ClimateMode > mode
Definition haier_base.h:133
esphome::optional< esphome::climate::ClimatePreset > preset
Definition haier_base.h:137
esphome::optional< float > target_temperature
Definition haier_base.h:136
hon_protocol::HorizontalSwingMode last_horizontal_swing
Definition hon_climate.h:34
hon_protocol::VerticalSwingMode last_vertiacal_swing
Definition hon_climate.h:33