ESPHome 2025.5.0
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: %d (should be >= %d)", 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, " Protocol version: hOn");
343 ESP_LOGCONFIG(TAG, " Control method: %d", (uint8_t) this->control_method_);
344 if (this->hvac_hardware_info_.has_value()) {
345 ESP_LOGCONFIG(TAG, " Device protocol version: %s", this->hvac_hardware_info_.value().protocol_version_.c_str());
346 ESP_LOGCONFIG(TAG, " Device software version: %s", this->hvac_hardware_info_.value().software_version_.c_str());
347 ESP_LOGCONFIG(TAG, " Device hardware version: %s", this->hvac_hardware_info_.value().hardware_version_.c_str());
348 ESP_LOGCONFIG(TAG, " Device name: %s", this->hvac_hardware_info_.value().device_name_.c_str());
349 ESP_LOGCONFIG(TAG, " Device features:%s%s%s%s%s",
350 (this->hvac_hardware_info_.value().functions_[0] ? " interactive" : ""),
351 (this->hvac_hardware_info_.value().functions_[1] ? " controller-device" : ""),
352 (this->hvac_hardware_info_.value().functions_[2] ? " crc" : ""),
353 (this->hvac_hardware_info_.value().functions_[3] ? " multinode" : ""),
354 (this->hvac_hardware_info_.value().functions_[4] ? " role" : ""));
355 ESP_LOGCONFIG(TAG, " Active alarms: %s", buf_to_hex(this->active_alarms_, sizeof(this->active_alarms_)).c_str());
356 }
357}
358
359void HonClimate::process_phase(std::chrono::steady_clock::time_point now) {
360 switch (this->protocol_phase_) {
363 // Indicate device capabilities:
364 // bit 0 - if 1 module support interactive mode
365 // bit 1 - if 1 module support controller-device mode
366 // bit 2 - if 1 module support crc
367 // bit 3 - if 1 module support multiple devices
368 // bit 4..bit 15 - not used
369 uint8_t module_capabilities[2] = {0b00000000, 0b00000111};
370 static const haier_protocol::HaierMessage DEVICE_VERSION_REQUEST(
371 haier_protocol::FrameType::GET_DEVICE_VERSION, module_capabilities, sizeof(module_capabilities));
372 this->send_message_(DEVICE_VERSION_REQUEST, this->use_crc_);
373 }
374 break;
376 if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
377 static const haier_protocol::HaierMessage DEVICEID_REQUEST(haier_protocol::FrameType::GET_DEVICE_ID);
378 this->send_message_(DEVICEID_REQUEST, this->use_crc_);
379 }
380 break;
383 if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
384 static const haier_protocol::HaierMessage STATUS_REQUEST(
385 haier_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::GET_USER_DATA);
386 static const haier_protocol::HaierMessage BIG_DATA_REQUEST(
387 haier_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::GET_BIG_DATA);
389 (!this->should_get_big_data_())) {
390 this->send_message_(STATUS_REQUEST, this->use_crc_);
391 } else {
392 this->send_message_(BIG_DATA_REQUEST, this->use_crc_);
393 }
394 this->last_status_request_ = now;
395 }
396 break;
397#ifdef USE_WIFI
399 if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
400 static const haier_protocol::HaierMessage UPDATE_SIGNAL_REQUEST(
401 haier_protocol::FrameType::GET_MANAGEMENT_INFORMATION);
402 this->send_message_(UPDATE_SIGNAL_REQUEST, this->use_crc_);
403 this->last_signal_request_ = now;
404 }
405 break;
407 if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
408 this->send_message_(this->get_wifi_signal_message_(), this->use_crc_);
409 }
410 break;
411#else
415 break;
416#endif
419 if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
420 static const haier_protocol::HaierMessage ALARM_STATUS_REQUEST(haier_protocol::FrameType::GET_ALARM_STATUS);
421 this->send_message_(ALARM_STATUS_REQUEST, this->use_crc_);
422 this->last_alarm_request_ = now;
423 }
424 break;
426 if (this->control_messages_queue_.empty()) {
427 switch (this->control_method_) {
429 haier_protocol::HaierMessage control_message = this->get_control_message();
430 this->control_messages_queue_.push(control_message);
431 } break;
434 break;
436 ESP_LOGI(TAG, "AC control is disabled, monitor only");
437 this->reset_to_idle_();
438 return;
439 default:
440 ESP_LOGW(TAG, "Unsupported control method for hOn protocol!");
441 this->reset_to_idle_();
442 return;
443 }
444 }
445 if (this->control_messages_queue_.empty()) {
446 ESP_LOGW(TAG, "Control message queue is empty!");
447 this->reset_to_idle_();
448 } else if (this->can_send_message() && this->is_control_message_interval_exceeded_(now)) {
449 ESP_LOGI(TAG, "Sending control packet, queue size %d", this->control_messages_queue_.size());
450 this->send_message_(this->control_messages_queue_.front(), this->use_crc_, CONTROL_MESSAGE_RETRIES,
452 }
453 break;
455 if (this->action_request_.has_value()) {
456 if (this->action_request_.value().message.has_value()) {
457 this->send_message_(this->action_request_.value().message.value(), this->use_crc_);
458 this->action_request_.value().message.reset();
459 } else {
460 // Message already sent, reseting request and return to idle
461 this->action_request_.reset();
463 }
464 } else {
465 ESP_LOGW(TAG, "SENDING_ACTION_COMMAND phase without action request!");
467 }
468 break;
472 this->forced_request_status_ = false;
473 } else if (std::chrono::duration_cast<std::chrono::milliseconds>(now - this->last_alarm_request_).count() >
476 }
477#ifdef USE_WIFI
478 else if (this->send_wifi_signal_ &&
479 (std::chrono::duration_cast<std::chrono::milliseconds>(now - this->last_signal_request_).count() >
482 }
483#endif
484 } break;
485 default:
486 // Shouldn't get here
487 ESP_LOGE(TAG, "Wrong protocol handler state: %s (%d), resetting communication",
490 break;
491 }
492}
493
494haier_protocol::HaierMessage HonClimate::get_power_message(bool state) {
495 if (state) {
496 static haier_protocol::HaierMessage power_on_message(
497 haier_protocol::FrameType::CONTROL, ((uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER) + 1,
498 std::initializer_list<uint8_t>({0x00, 0x01}).begin(), 2);
499 return power_on_message;
500 } else {
501 static haier_protocol::HaierMessage power_off_message(
502 haier_protocol::FrameType::CONTROL, ((uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER) + 1,
503 std::initializer_list<uint8_t>({0x00, 0x00}).begin(), 2);
504 return power_off_message;
505 }
506}
507
510 constexpr uint32_t restore_settings_version = 0x57EB59DDUL;
511 this->hon_rtc_ =
512 global_preferences->make_preference<HonSettings>(this->get_object_id_hash() ^ restore_settings_version);
513 HonSettings recovered;
514 if (this->hon_rtc_.load(&recovered)) {
515 this->settings_ = recovered;
516 } else {
518 }
522}
523
524haier_protocol::HaierMessage HonClimate::get_control_message() {
525 uint8_t control_out_buffer[haier_protocol::MAX_FRAME_SIZE];
526 memcpy(control_out_buffer, this->last_status_message_.get(), this->real_control_packet_size_);
528 control_out_buffer[4] = 0; // This byte should be cleared before setting values
529 bool has_hvac_settings = false;
530 if (this->current_hvac_settings_.valid) {
531 has_hvac_settings = true;
532 HvacSettings &climate_control = this->current_hvac_settings_;
533 if (climate_control.mode.has_value()) {
534 switch (climate_control.mode.value()) {
535 case CLIMATE_MODE_OFF:
536 out_data->ac_power = 0;
537 break;
539 out_data->ac_power = 1;
540 out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::AUTO;
541 out_data->fan_mode = this->other_modes_fan_speed_;
542 break;
544 out_data->ac_power = 1;
545 out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::HEAT;
546 out_data->fan_mode = this->other_modes_fan_speed_;
547 break;
548 case CLIMATE_MODE_DRY:
549 out_data->ac_power = 1;
550 out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::DRY;
551 out_data->fan_mode = this->other_modes_fan_speed_;
552 break;
554 out_data->ac_power = 1;
555 out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::FAN;
556 out_data->fan_mode = this->fan_mode_speed_; // Auto doesn't work in fan only mode
557 // Disabling boost for Fan only
558 out_data->fast_mode = 0;
559 break;
561 out_data->ac_power = 1;
562 out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::COOL;
563 out_data->fan_mode = this->other_modes_fan_speed_;
564 break;
565 default:
566 ESP_LOGE("Control", "Unsupported climate mode");
567 break;
568 }
569 }
570 // Set fan speed, if we are in fan mode, reject auto in fan mode
571 if (climate_control.fan_mode.has_value()) {
572 switch (climate_control.fan_mode.value()) {
573 case CLIMATE_FAN_LOW:
574 out_data->fan_mode = (uint8_t) hon_protocol::FanMode::FAN_LOW;
575 break;
577 out_data->fan_mode = (uint8_t) hon_protocol::FanMode::FAN_MID;
578 break;
579 case CLIMATE_FAN_HIGH:
580 out_data->fan_mode = (uint8_t) hon_protocol::FanMode::FAN_HIGH;
581 break;
582 case CLIMATE_FAN_AUTO:
583 if (mode != CLIMATE_MODE_FAN_ONLY) // if we are not in fan only mode
584 out_data->fan_mode = (uint8_t) hon_protocol::FanMode::FAN_AUTO;
585 break;
586 default:
587 ESP_LOGE("Control", "Unsupported fan mode");
588 break;
589 }
590 }
591 // Set swing mode
592 if (climate_control.swing_mode.has_value()) {
593 switch (climate_control.swing_mode.value()) {
595 out_data->horizontal_swing_mode = (uint8_t) this->settings_.last_horizontal_swing;
596 out_data->vertical_swing_mode = (uint8_t) this->settings_.last_vertiacal_swing;
597 break;
599 out_data->horizontal_swing_mode = (uint8_t) this->settings_.last_horizontal_swing;
601 break;
604 out_data->vertical_swing_mode = (uint8_t) this->settings_.last_vertiacal_swing;
605 break;
609 break;
610 }
611 }
612 if (climate_control.target_temperature.has_value()) {
613 float target_temp = climate_control.target_temperature.value();
614 out_data->set_point = ((int) target_temp) - 16; // set the temperature with offset 16
615 out_data->half_degree = (target_temp - ((int) target_temp) >= 0.49) ? 1 : 0;
616 }
617 if (out_data->ac_power == 0) {
618 // If AC is off - no presets allowed
619 out_data->fast_mode = 0;
620 out_data->sleep_mode = 0;
621 } else if (climate_control.preset.has_value()) {
622 switch (climate_control.preset.value()) {
624 out_data->fast_mode = 0;
625 out_data->sleep_mode = 0;
626 out_data->ten_degree = 0;
627 break;
629 // Boost is not supported in Fan only mode
630 out_data->fast_mode = (this->mode != CLIMATE_MODE_FAN_ONLY) ? 1 : 0;
631 out_data->sleep_mode = 0;
632 out_data->ten_degree = 0;
633 break;
635 out_data->fast_mode = 0;
636 out_data->sleep_mode = 0;
637 // 10 degrees allowed only in heat mode
638 out_data->ten_degree = (this->mode == CLIMATE_MODE_HEAT) ? 1 : 0;
639 break;
641 out_data->fast_mode = 0;
642 out_data->sleep_mode = 1;
643 out_data->ten_degree = 0;
644 break;
645 default:
646 ESP_LOGE("Control", "Unsupported preset");
647 out_data->fast_mode = 0;
648 out_data->sleep_mode = 0;
649 out_data->ten_degree = 0;
650 break;
651 }
652 }
653 }
655 out_data->vertical_swing_mode = (uint8_t) this->pending_vertical_direction_.value();
657 }
659 out_data->horizontal_swing_mode = (uint8_t) this->pending_horizontal_direction_.value();
661 }
662 {
663 // Quiet mode
664 if ((out_data->ac_power == 0) || (out_data->ac_mode == (uint8_t) hon_protocol::ConditioningMode::FAN)) {
665 // If AC is off or in fan only mode - no quiet mode allowed
666 out_data->quiet_mode = 0;
667 } else {
668 out_data->quiet_mode = this->get_quiet_mode_state() ? 1 : 0;
669 }
670 // Clean quiet mode state pending flag
671 this->quiet_mode_state_ = (SwitchState) ((uint8_t) this->quiet_mode_state_ & 0b01);
672 }
673 out_data->beeper_status = ((!this->get_beeper_state()) || (!has_hvac_settings)) ? 1 : 0;
674 control_out_buffer[4] = 0; // This byte should be cleared before setting values
675 out_data->display_status = this->get_display_state() ? 1 : 0;
676 this->display_status_ = (SwitchState) ((uint8_t) this->display_status_ & 0b01);
677 out_data->health_mode = this->get_health_mode() ? 1 : 0;
678 this->health_mode_ = (SwitchState) ((uint8_t) this->health_mode_ & 0b01);
679 return haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
681 control_out_buffer, this->real_control_packet_size_);
682}
683
684void HonClimate::process_alarm_message_(const uint8_t *packet, uint8_t size, bool check_new) {
685 constexpr size_t active_alarms_size = sizeof(this->active_alarms_);
686 if (size >= active_alarms_size + 2) {
687 if (check_new) {
688 size_t alarm_code = 0;
689 for (int i = active_alarms_size - 1; i >= 0; i--) {
690 if (packet[2 + i] != active_alarms_[i]) {
691 uint8_t alarm_bit = 1;
692 for (int b = 0; b < 8; b++) {
693 if ((packet[2 + i] & alarm_bit) != (this->active_alarms_[i] & alarm_bit)) {
694 bool alarm_status = (packet[2 + i] & alarm_bit) != 0;
695 int log_level = alarm_status ? ESPHOME_LOG_LEVEL_WARN : ESPHOME_LOG_LEVEL_INFO;
696 const char *alarm_message = alarm_code < esphome::haier::hon_protocol::HON_ALARM_COUNT
698 : "Unknown";
699 esp_log_printf_(log_level, TAG, __LINE__, "Alarm %s (%d): %s", alarm_status ? "activated" : "deactivated",
700 alarm_code, alarm_message);
701 if (alarm_status) {
702 this->alarm_start_callback_.call(alarm_code, alarm_message);
703 this->active_alarm_count_ += 1.0f;
704 } else {
705 this->alarm_end_callback_.call(alarm_code, alarm_message);
706 this->active_alarm_count_ -= 1.0f;
707 }
708 }
709 alarm_bit <<= 1;
710 alarm_code++;
711 }
712 active_alarms_[i] = packet[2 + i];
713 } else {
714 alarm_code += 8;
715 }
716 }
717 } else {
718 float alarm_count = 0.0f;
719 static uint8_t nibble_bits_count[] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
720 for (size_t i = 0; i < sizeof(this->active_alarms_); i++) {
721 alarm_count += (float) (nibble_bits_count[packet[2 + i] & 0x0F] + nibble_bits_count[packet[2 + i] >> 4]);
722 }
723 this->active_alarm_count_ = alarm_count;
724 memcpy(this->active_alarms_, packet + 2, sizeof(this->active_alarms_));
725 }
726 }
727}
728
729#ifdef USE_SENSOR
733 if ((this->sub_sensors_[(size_t) type] != nullptr) && (sens == nullptr)) {
734 this->big_data_sensors_--;
735 } else if ((this->sub_sensors_[(size_t) type] == nullptr) && (sens != nullptr)) {
736 this->big_data_sensors_++;
737 }
738 }
739 this->sub_sensors_[(size_t) type] = sens;
740 }
741}
742
745 size_t index = (size_t) type;
746 if ((this->sub_sensors_[index] != nullptr) &&
747 ((!this->sub_sensors_[index]->has_state()) || (this->sub_sensors_[index]->raw_state != value)))
748 this->sub_sensors_[index]->publish_state(value);
749 }
750}
751#endif // USE_SENSOR
752
753#ifdef USE_BINARY_SENSOR
756 if ((this->sub_binary_sensors_[(size_t) type] != nullptr) && (sens == nullptr)) {
757 this->big_data_sensors_--;
758 } else if ((this->sub_binary_sensors_[(size_t) type] == nullptr) && (sens != nullptr)) {
759 this->big_data_sensors_++;
760 }
761 this->sub_binary_sensors_[(size_t) type] = sens;
762 }
763}
764
766 if (value < 2) {
767 bool converted_value = value == 1;
768 size_t index = (size_t) type;
769 if ((this->sub_binary_sensors_[index] != nullptr) && ((!this->sub_binary_sensors_[index]->has_state()) ||
770 (this->sub_binary_sensors_[index]->state != converted_value)))
771 this->sub_binary_sensors_[index]->publish_state(converted_value);
772 }
773}
774#endif // USE_BINARY_SENSOR
775
776#ifdef USE_TEXT_SENSOR
778 this->sub_text_sensors_[(size_t) type] = sens;
779 switch (type) {
781 if (this->hvac_hardware_info_.has_value())
782 sens->publish_state(this->hvac_hardware_info_.value().device_name_);
783 break;
785 if (this->hvac_hardware_info_.has_value())
786 sens->publish_state(this->hvac_hardware_info_.value().protocol_version_);
787 break;
790 break;
791 default:
792 break;
793 }
794}
795
797 size_t index = (size_t) type;
798 if (this->sub_text_sensors_[index] != nullptr)
799 this->sub_text_sensors_[index]->publish_state(value);
800}
801#endif // USE_TEXT_SENSOR
802
803#ifdef USE_SWITCH
805 this->beeper_switch_ = sw;
806 if (this->beeper_switch_ != nullptr) {
808 }
809}
810
817#endif // USE_SWITCH
818
819haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *packet_buffer, uint8_t size) {
820 size_t expected_size =
822 if (size < expected_size) {
823 ESP_LOGW(TAG, "Unexpected message size %d (expexted >= %d)", size, expected_size);
824 return haier_protocol::HandlerError::WRONG_MESSAGE_STRUCTURE;
825 }
826 uint16_t subtype = (((uint16_t) packet_buffer[0]) << 8) + packet_buffer[1];
827 if ((subtype == 0x7D01) && (size >= expected_size + sizeof(hon_protocol::HaierPacketBigData))) {
828 // Got BigData packet
829 const hon_protocol::HaierPacketBigData *bd_packet =
830 (const hon_protocol::HaierPacketBigData *) (&packet_buffer[expected_size]);
831#ifdef USE_SENSOR
837 this->update_sub_sensor_(SubSensorType::POWER, encode_uint16(bd_packet->power[0], bd_packet->power[1]));
840 encode_uint16(bd_packet->compressor_current[0], bd_packet->compressor_current[1]) / 10.0);
841 this->update_sub_sensor_(
843 encode_uint16(bd_packet->expansion_valve_open_degree[0], bd_packet->expansion_valve_open_degree[1]) / 4095.0);
844#endif // USE_SENSOR
845#ifdef USE_BINARY_SENSOR
853#endif // USE_BINARY_SENSOR
854 }
855 struct {
858 } packet;
859 memcpy(&packet.control, packet_buffer + 2 + this->status_message_header_size_,
861 memcpy(&packet.sensors, packet_buffer + 2 + this->status_message_header_size_ + this->real_control_packet_size_,
863 if (packet.sensors.error_status != 0) {
864 ESP_LOGW(TAG, "HVAC error, code=0x%02X", packet.sensors.error_status);
865 }
866#ifdef USE_SENSOR
867 if ((this->sub_sensors_[(size_t) SubSensorType::OUTDOOR_TEMPERATURE] != nullptr) &&
868 (this->got_valid_outdoor_temp_ || (packet.sensors.outdoor_temperature > 0))) {
869 this->got_valid_outdoor_temp_ = true;
871 (float) (packet.sensors.outdoor_temperature + PROTOCOL_OUTDOOR_TEMPERATURE_OFFSET));
872 }
873 if ((this->sub_sensors_[(size_t) SubSensorType::HUMIDITY] != nullptr) && (packet.sensors.room_humidity <= 100)) {
874 this->update_sub_sensor_(SubSensorType::HUMIDITY, (float) packet.sensors.room_humidity);
875 }
876#endif // USE_SENSOR
877 bool should_publish = false;
878 {
879 // Extra modes/presets
880 optional<ClimatePreset> old_preset = this->preset;
881 if (packet.control.fast_mode != 0) {
883 } else if (packet.control.sleep_mode != 0) {
885 } else if (packet.control.ten_degree != 0) {
887 } else {
889 }
890 should_publish = should_publish || (!old_preset.has_value()) || (old_preset.value() != this->preset.value());
891 }
892 {
893 // Target temperature
894 float old_target_temperature = this->target_temperature;
895 this->target_temperature = packet.control.set_point + 16.0f + ((packet.control.half_degree == 1) ? 0.5f : 0.0f);
896 should_publish = should_publish || (old_target_temperature != this->target_temperature);
897 }
898 {
899 // Current temperature
900 float old_current_temperature = this->current_temperature;
901 this->current_temperature = packet.sensors.room_temperature / 2.0f;
902 should_publish = should_publish || (old_current_temperature != this->current_temperature);
903 }
904 {
905 // Fan mode
906 optional<ClimateFanMode> old_fan_mode = this->fan_mode;
907 // remember the fan speed we last had for climate vs fan
908 if (packet.control.ac_mode == (uint8_t) hon_protocol::ConditioningMode::FAN) {
909 if (packet.control.fan_mode != (uint8_t) hon_protocol::FanMode::FAN_AUTO)
910 this->fan_mode_speed_ = packet.control.fan_mode;
911 } else {
912 this->other_modes_fan_speed_ = packet.control.fan_mode;
913 }
914 switch (packet.control.fan_mode) {
915 case (uint8_t) hon_protocol::FanMode::FAN_AUTO:
916 if (packet.control.ac_mode != (uint8_t) hon_protocol::ConditioningMode::FAN) {
918 } else {
919 // Shouldn't accept fan speed auto in fan-only mode even if AC reports it
920 ESP_LOGI(TAG, "Fan speed Auto is not supported in Fan only AC mode, ignoring");
921 }
922 break;
923 case (uint8_t) hon_protocol::FanMode::FAN_MID:
925 break;
926 case (uint8_t) hon_protocol::FanMode::FAN_LOW:
928 break;
929 case (uint8_t) hon_protocol::FanMode::FAN_HIGH:
931 break;
932 }
933 should_publish = should_publish || (!old_fan_mode.has_value()) || (old_fan_mode.value() != fan_mode.value());
934 }
935 // Display status
936 // should be before "Climate mode" because it is changing this->mode
937 if (packet.control.ac_power != 0) {
938 // if AC is off display status always ON so process it only when AC is on
939 bool disp_status = packet.control.display_status != 0;
940 if (disp_status != this->get_display_state()) {
941 // Do something only if display status changed
942 if (this->mode == CLIMATE_MODE_OFF) {
943 // AC just turned on from remote need to turn off display
944 this->force_send_control_ = true;
945 } else if ((((uint8_t) this->display_status_) & 0b10) == 0) {
946 this->display_status_ = disp_status ? SwitchState::ON : SwitchState::OFF;
947 }
948 }
949 }
950 // Health mode
951 if ((((uint8_t) this->health_mode_) & 0b10) == 0) {
952 bool old_health_mode = this->get_health_mode();
953 this->health_mode_ = packet.control.health_mode == 1 ? SwitchState::ON : SwitchState::OFF;
954 should_publish = should_publish || (old_health_mode != this->get_health_mode());
955 }
956 {
957 CleaningState new_cleaning;
958 if (packet.control.steri_clean == 1) {
959 // Steri-cleaning
960 new_cleaning = CleaningState::STERI_CLEAN;
961 } else if (packet.control.self_cleaning_status == 1) {
962 // Self-cleaning
963 new_cleaning = CleaningState::SELF_CLEAN;
964 } else {
965 // No cleaning
966 new_cleaning = CleaningState::NO_CLEANING;
967 }
968 if (new_cleaning != this->cleaning_status_) {
969 ESP_LOGD(TAG, "Cleaning status change: %d => %d", (uint8_t) this->cleaning_status_, (uint8_t) new_cleaning);
970 if (new_cleaning == CleaningState::NO_CLEANING) {
971 // Turning AC off after cleaning
972 this->action_request_ =
974 }
975 this->cleaning_status_ = new_cleaning;
976#ifdef USE_TEXT_SENSOR
978#endif // USE_TEXT_SENSOR
979 }
980 }
981 {
982 // Climate mode
983 ClimateMode old_mode = this->mode;
984 if (packet.control.ac_power == 0) {
985 this->mode = CLIMATE_MODE_OFF;
986 } else {
987 // Check current hvac mode
988 switch (packet.control.ac_mode) {
990 this->mode = CLIMATE_MODE_COOL;
991 break;
993 this->mode = CLIMATE_MODE_HEAT;
994 break;
996 this->mode = CLIMATE_MODE_DRY;
997 break;
1000 break;
1003 break;
1004 }
1005 }
1006 should_publish = should_publish || (old_mode != this->mode);
1007 }
1008 {
1009 // Quiet mode, should be after climate mode
1010 if ((this->mode != CLIMATE_MODE_FAN_ONLY) && (this->mode != CLIMATE_MODE_OFF) &&
1011 ((((uint8_t) this->quiet_mode_state_) & 0b10) == 0)) {
1012 // In proper mode and not in pending state
1013 bool new_quiet_mode = packet.control.quiet_mode != 0;
1014 if (new_quiet_mode != this->get_quiet_mode_state()) {
1015 this->quiet_mode_state_ = new_quiet_mode ? SwitchState::ON : SwitchState::OFF;
1016 this->settings_.quiet_mode_state = new_quiet_mode;
1017#ifdef USE_SWITCH
1018 if (this->quiet_mode_switch_ != nullptr) {
1019 this->quiet_mode_switch_->publish_state(new_quiet_mode);
1020 }
1021#endif // USE_SWITCH
1022 this->hon_rtc_.save(&this->settings_);
1023 }
1024 }
1025 }
1026 {
1027 // Swing mode
1028 ClimateSwingMode old_swing_mode = this->swing_mode;
1029 const std::set<ClimateSwingMode> &swing_modes = traits_.get_supported_swing_modes();
1030 bool vertical_swing_supported = swing_modes.find(CLIMATE_SWING_VERTICAL) != swing_modes.end();
1031 bool horizontal_swing_supported = swing_modes.find(CLIMATE_SWING_HORIZONTAL) != swing_modes.end();
1032 if (horizontal_swing_supported &&
1033 (packet.control.horizontal_swing_mode == (uint8_t) hon_protocol::HorizontalSwingMode::AUTO)) {
1034 if (vertical_swing_supported &&
1035 (packet.control.vertical_swing_mode == (uint8_t) hon_protocol::VerticalSwingMode::AUTO)) {
1037 } else {
1039 }
1040 } else {
1041 if (vertical_swing_supported &&
1042 (packet.control.vertical_swing_mode == (uint8_t) hon_protocol::VerticalSwingMode::AUTO)) {
1044 } else {
1046 }
1047 }
1048 // Saving last known non auto mode for vertical and horizontal swing
1049 this->current_vertical_swing_ = (hon_protocol::VerticalSwingMode) packet.control.vertical_swing_mode;
1050 this->current_horizontal_swing_ = (hon_protocol::HorizontalSwingMode) packet.control.horizontal_swing_mode;
1053 (this->current_vertical_swing_.value() != this->settings_.last_vertiacal_swing)) ||
1054 ((this->current_horizontal_swing_.value() != hon_protocol::HorizontalSwingMode::AUTO) &&
1055 (this->current_horizontal_swing_.value() != this->settings_.last_horizontal_swing));
1056 if (save_settings) {
1059 this->hon_rtc_.save(&this->settings_);
1060 }
1061 should_publish = should_publish || (old_swing_mode != this->swing_mode);
1062 }
1063 this->last_valid_status_timestamp_ = std::chrono::steady_clock::now();
1064 if (should_publish) {
1065 this->publish_state();
1066 }
1067 if (should_publish) {
1068 ESP_LOGI(TAG, "HVAC values changed");
1069 }
1070 int log_level = should_publish ? ESPHOME_LOG_LEVEL_INFO : ESPHOME_LOG_LEVEL_DEBUG;
1071 esp_log_printf_(log_level, TAG, __LINE__, "HVAC Mode = 0x%X", packet.control.ac_mode);
1072 esp_log_printf_(log_level, TAG, __LINE__, "Fan speed Status = 0x%X", packet.control.fan_mode);
1073 esp_log_printf_(log_level, TAG, __LINE__, "Horizontal Swing Status = 0x%X", packet.control.horizontal_swing_mode);
1074 esp_log_printf_(log_level, TAG, __LINE__, "Vertical Swing Status = 0x%X", packet.control.vertical_swing_mode);
1075 esp_log_printf_(log_level, TAG, __LINE__, "Set Point Status = 0x%X", packet.control.set_point);
1076 return haier_protocol::HandlerError::HANDLER_OK;
1077}
1078
1080 if (!this->current_hvac_settings_.valid && !this->force_send_control_)
1081 return;
1083 HvacSettings climate_control;
1084 climate_control = this->current_hvac_settings_;
1085 // Beeper command
1086 {
1087 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1090 this->get_beeper_state() ? ZERO_BUF : ONE_BUF, 2);
1091 }
1092 // Health mode
1093 {
1094 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1097 this->get_health_mode() ? ONE_BUF : ZERO_BUF, 2);
1098 this->health_mode_ = (SwitchState) ((uint8_t) this->health_mode_ & 0b01);
1099 }
1100 // Climate mode
1101 ClimateMode climate_mode = this->mode;
1102 bool new_power = this->mode != CLIMATE_MODE_OFF;
1103 uint8_t fan_mode_buf[] = {0x00, 0xFF};
1104 uint8_t quiet_mode_buf[] = {0x00, 0xFF};
1105 if (climate_control.mode.has_value()) {
1106 climate_mode = climate_control.mode.value();
1107 uint8_t buffer[2] = {0x00, 0x00};
1108 switch (climate_control.mode.value()) {
1109 case CLIMATE_MODE_OFF:
1110 new_power = false;
1111 break;
1113 new_power = true;
1114 buffer[1] = (uint8_t) hon_protocol::ConditioningMode::AUTO;
1115 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1118 buffer, 2);
1119 fan_mode_buf[1] = this->other_modes_fan_speed_;
1120 break;
1121 case CLIMATE_MODE_HEAT:
1122 new_power = true;
1123 buffer[1] = (uint8_t) hon_protocol::ConditioningMode::HEAT;
1124 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1127 buffer, 2);
1128 fan_mode_buf[1] = this->other_modes_fan_speed_;
1129 break;
1130 case CLIMATE_MODE_DRY:
1131 new_power = true;
1132 buffer[1] = (uint8_t) hon_protocol::ConditioningMode::DRY;
1133 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1136 buffer, 2);
1137 fan_mode_buf[1] = this->other_modes_fan_speed_;
1138 break;
1140 new_power = true;
1141 buffer[1] = (uint8_t) hon_protocol::ConditioningMode::FAN;
1142 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1145 buffer, 2);
1146 fan_mode_buf[1] = this->other_modes_fan_speed_; // Auto doesn't work in fan only mode
1147 break;
1148 case CLIMATE_MODE_COOL:
1149 new_power = true;
1150 buffer[1] = (uint8_t) hon_protocol::ConditioningMode::COOL;
1151 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1154 buffer, 2);
1155 fan_mode_buf[1] = this->other_modes_fan_speed_;
1156 break;
1157 default:
1158 ESP_LOGE("Control", "Unsupported climate mode");
1159 break;
1160 }
1161 }
1162 // Climate power
1163 {
1164 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1167 new_power ? ONE_BUF : ZERO_BUF, 2);
1168 }
1169 // CLimate preset
1170 {
1171 uint8_t fast_mode_buf[] = {0x00, 0xFF};
1172 uint8_t away_mode_buf[] = {0x00, 0xFF};
1173 if (!new_power) {
1174 // If AC is off - no presets allowed
1175 fast_mode_buf[1] = 0x00;
1176 away_mode_buf[1] = 0x00;
1177 } else if (climate_control.preset.has_value()) {
1178 switch (climate_control.preset.value()) {
1180 fast_mode_buf[1] = 0x00;
1181 away_mode_buf[1] = 0x00;
1182 break;
1184 // Boost is not supported in Fan only mode
1185 fast_mode_buf[1] = (this->mode != CLIMATE_MODE_FAN_ONLY) ? 0x01 : 0x00;
1186 away_mode_buf[1] = 0x00;
1187 break;
1189 fast_mode_buf[1] = 0x00;
1190 away_mode_buf[1] = (this->mode == CLIMATE_MODE_HEAT) ? 0x01 : 0x00;
1191 break;
1192 default:
1193 ESP_LOGE("Control", "Unsupported preset");
1194 break;
1195 }
1196 }
1197 {
1198 // Quiet mode
1199 if (new_power && (climate_mode != CLIMATE_MODE_FAN_ONLY) && this->get_quiet_mode_state()) {
1200 quiet_mode_buf[1] = 0x01;
1201 } else {
1202 quiet_mode_buf[1] = 0x00;
1203 }
1204 // Clean quiet mode state pending flag
1205 this->quiet_mode_state_ = (SwitchState) ((uint8_t) this->quiet_mode_state_ & 0b01);
1206 }
1207 auto presets = this->traits_.get_supported_presets();
1208 if (quiet_mode_buf[1] != 0xFF) {
1209 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1212 quiet_mode_buf, 2);
1213 }
1214 if ((fast_mode_buf[1] != 0xFF) && ((presets.find(climate::ClimatePreset::CLIMATE_PRESET_BOOST) != presets.end()))) {
1215 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1218 fast_mode_buf, 2);
1219 }
1220 if ((away_mode_buf[1] != 0xFF) && ((presets.find(climate::ClimatePreset::CLIMATE_PRESET_AWAY) != presets.end()))) {
1221 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1224 away_mode_buf, 2);
1225 }
1226 }
1227 // Target temperature
1228 if (climate_control.target_temperature.has_value() && (this->mode != ClimateMode::CLIMATE_MODE_FAN_ONLY)) {
1229 uint8_t buffer[2] = {0x00, 0x00};
1230 buffer[1] = ((uint8_t) climate_control.target_temperature.value()) - 16;
1231 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1234 buffer, 2);
1235 }
1236 // Vertical swing mode
1237 if (climate_control.swing_mode.has_value()) {
1238 uint8_t vertical_swing_buf[] = {0x00, (uint8_t) hon_protocol::VerticalSwingMode::AUTO};
1239 uint8_t horizontal_swing_buf[] = {0x00, (uint8_t) hon_protocol::HorizontalSwingMode::AUTO};
1240 switch (climate_control.swing_mode.value()) {
1241 case CLIMATE_SWING_OFF:
1242 horizontal_swing_buf[1] = (uint8_t) this->settings_.last_horizontal_swing;
1243 vertical_swing_buf[1] = (uint8_t) this->settings_.last_vertiacal_swing;
1244 break;
1246 horizontal_swing_buf[1] = (uint8_t) this->settings_.last_horizontal_swing;
1247 break;
1249 vertical_swing_buf[1] = (uint8_t) this->settings_.last_vertiacal_swing;
1250 break;
1251 case CLIMATE_SWING_BOTH:
1252 break;
1253 }
1254 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1257 horizontal_swing_buf, 2);
1258 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1261 vertical_swing_buf, 2);
1262 }
1263 // Fan mode
1264 if (climate_control.fan_mode.has_value()) {
1265 switch (climate_control.fan_mode.value()) {
1266 case CLIMATE_FAN_LOW:
1267 fan_mode_buf[1] = (uint8_t) hon_protocol::FanMode::FAN_LOW;
1268 break;
1269 case CLIMATE_FAN_MEDIUM:
1270 fan_mode_buf[1] = (uint8_t) hon_protocol::FanMode::FAN_MID;
1271 break;
1272 case CLIMATE_FAN_HIGH:
1273 fan_mode_buf[1] = (uint8_t) hon_protocol::FanMode::FAN_HIGH;
1274 break;
1275 case CLIMATE_FAN_AUTO:
1276 if (mode != CLIMATE_MODE_FAN_ONLY) // if we are not in fan only mode
1277 fan_mode_buf[1] = (uint8_t) hon_protocol::FanMode::FAN_AUTO;
1278 break;
1279 default:
1280 ESP_LOGE("Control", "Unsupported fan mode");
1281 break;
1282 }
1283 if (fan_mode_buf[1] != 0xFF) {
1284 this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1287 fan_mode_buf, 2);
1288 }
1289 }
1290}
1291
1293 while (!this->control_messages_queue_.empty())
1294 this->control_messages_queue_.pop();
1295}
1296
1298 switch (this->action_request_.value().action) {
1301 uint8_t control_out_buffer[haier_protocol::MAX_FRAME_SIZE];
1302 memcpy(control_out_buffer, this->last_status_message_.get(), this->real_control_packet_size_);
1303 hon_protocol::HaierPacketControl *out_data = (hon_protocol::HaierPacketControl *) control_out_buffer;
1304 out_data->self_cleaning_status = 1;
1305 out_data->steri_clean = 0;
1306 out_data->set_point = 0x06;
1309 out_data->ac_power = 1;
1310 out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::DRY;
1311 out_data->light_status = 0;
1312 this->action_request_.value().message = haier_protocol::HaierMessage(
1313 haier_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::SET_GROUP_PARAMETERS,
1314 control_out_buffer, this->real_control_packet_size_);
1315 return true;
1317 this->action_request_.value().message =
1318 haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
1321 ONE_BUF, 2);
1322 return true;
1323 } else {
1324 this->action_request_.reset();
1325 return false;
1326 }
1329 uint8_t control_out_buffer[haier_protocol::MAX_FRAME_SIZE];
1330 memcpy(control_out_buffer, this->last_status_message_.get(), this->real_control_packet_size_);
1331 hon_protocol::HaierPacketControl *out_data = (hon_protocol::HaierPacketControl *) control_out_buffer;
1332 out_data->self_cleaning_status = 0;
1333 out_data->steri_clean = 1;
1334 out_data->set_point = 0x06;
1337 out_data->ac_power = 1;
1338 out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::DRY;
1339 out_data->light_status = 0;
1340 this->action_request_.value().message = haier_protocol::HaierMessage(
1341 haier_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::SET_GROUP_PARAMETERS,
1342 control_out_buffer, this->real_control_packet_size_);
1343 return true;
1344 } else {
1345 // No Steri clean support (yet?) in SET_SINGLE_PARAMETER
1346 this->action_request_.reset();
1347 return false;
1348 }
1349 default:
1351 }
1352}
1353
1356#ifdef USE_SENSOR
1357 for (auto &sub_sensor : this->sub_sensors_) {
1358 if ((sub_sensor != nullptr) && sub_sensor->has_state())
1359 sub_sensor->publish_state(NAN);
1360 }
1361#endif // USE_SENSOR
1362 this->got_valid_outdoor_temp_ = false;
1363 this->hvac_hardware_info_.reset();
1364 this->last_status_message_.reset(nullptr);
1365}
1366
1368 if (this->big_data_sensors_ > 0) {
1369 static uint8_t counter = 0;
1370 counter = (counter + 1) % 3;
1371 return counter == 1;
1372 }
1373 return false;
1374}
1375
1376} // namespace haier
1377} // namespace esphome
send_message_t send_message_
bool save(const T *src)
Definition preferences.h:21
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
uint32_t get_object_id_hash()
Base class for all binary_sensor-type classes.
void publish_state(bool state)
Publish a new state to the front-end.
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
const std::set< climate::ClimatePreset > & get_supported_presets() const
const std::set< ClimateSwingMode > & get_supported_swing_modes() const
esphome::climate::ClimateTraits traits_
Definition haier_base.h:167
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)
void control(const esphome::climate::ClimateCall &call) override
std::chrono::steady_clock::time_point last_valid_status_timestamp_
Definition haier_base.h:172
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: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
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:93
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:62
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:79
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:87
value_type const & value() const
Definition optional.h:89
Base-class for all sensors.
Definition sensor.h:57
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
Base class for all switches.
Definition switch.h:39
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:47
void publish_state(const std::string &state)
uint8_t type
FanDirection direction
Definition fan.h:3
bool state
Definition fan.h:0
@ 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.
@ 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
ESPPreferences * global_preferences
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:191
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
hon_protocol::HorizontalSwingMode last_horizontal_swing
Definition hon_climate.h:33
hon_protocol::VerticalSwingMode last_vertiacal_swing
Definition hon_climate.h:32