ESPHome  2024.12.2
hon_climate.cpp
Go to the documentation of this file.
1 #include <chrono>
2 #include <string>
5 #include "esphome/core/helpers.h"
6 #include "hon_climate.h"
7 #include "hon_packet.h"
8 
9 using namespace esphome::climate;
10 using namespace esphome::uart;
11 
12 namespace esphome {
13 namespace haier {
14 
15 static const char *const TAG = "haier.climate";
16 constexpr size_t SIGNAL_LEVEL_UPDATE_INTERVAL_MS = 10000;
18 constexpr uint8_t CONTROL_MESSAGE_RETRIES = 5;
19 constexpr std::chrono::milliseconds CONTROL_MESSAGE_RETRIES_INTERVAL = std::chrono::milliseconds(500);
20 constexpr size_t ALARM_STATUS_REQUEST_INTERVAL_MS = 600000;
21 const uint8_t ONE_BUF[] = {0x00, 0x01};
22 const uint8_t ZERO_BUF[] = {0x00, 0x00};
23 
24 HonClimate::HonClimate()
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 
46 bool HonClimate::get_beeper_state() const { return this->settings_.beeper_state; }
47 
49  if (state != this->get_quiet_mode_state()) {
52  this->force_send_control_ = true;
53  } else {
55  }
57 #ifdef USE_SWITCH
58  if (this->quiet_mode_switch_ != nullptr) {
59  this->quiet_mode_switch_->publish_state(state);
60  }
61 #endif
62  this->hon_rtc_.save(&this->settings_);
63  }
64 }
65 
68 }
69 
71  return this->current_vertical_swing_;
72 };
73 
76  this->force_send_control_ = true;
77 }
78 
80  return this->current_horizontal_swing_;
81 }
82 
85  this->force_send_control_ = true;
86 }
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 
117 void HonClimate::add_alarm_start_callback(std::function<void(uint8_t, const char *)> &&callback) {
118  this->alarm_start_callback_.add(std::move(callback));
119 }
120 
121 void HonClimate::add_alarm_end_callback(std::function<void(uint8_t, const char *)> &&callback) {
122  this->alarm_end_callback_.add(std::move(callback));
123 }
124 
125 haier_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 
176 haier_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 
191 haier_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,
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_,
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;
235  if (this->current_hvac_settings_.valid)
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 
269 haier_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 
295 haier_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 
359 void 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());
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;
469  case ProtocolPhases::IDLE: {
472  this->forced_request_status_ = false;
473  } else if (std::chrono::duration_cast<std::chrono::milliseconds>(now - this->last_alarm_request_).count() >
474  ALARM_STATUS_REQUEST_INTERVAL_MS) {
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() >
480  SIGNAL_LEVEL_UPDATE_INTERVAL_MS)) {
482  }
483 #endif
484  } break;
485  default:
486  // Shouldn't get here
487  ESP_LOGE(TAG, "Wrong protocol handler state: %s (%d), resetting communication",
488  phase_to_string_(this->protocol_phase_), (int) this->protocol_phase_);
490  break;
491  }
492 }
493 
494 haier_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 
524 haier_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_);
527  hon_protocol::HaierPacketControl *out_data = (hon_protocol::HaierPacketControl *) control_out_buffer;
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;
543  case CLIMATE_MODE_HEAT:
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;
560  case CLIMATE_MODE_COOL:
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;
576  case CLIMATE_FAN_MEDIUM:
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()) {
594  case CLIMATE_SWING_OFF:
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;
600  out_data->vertical_swing_mode = (uint8_t) hon_protocol::VerticalSwingMode::AUTO;
601  break;
603  out_data->horizontal_swing_mode = (uint8_t) hon_protocol::HorizontalSwingMode::AUTO;
604  out_data->vertical_swing_mode = (uint8_t) this->settings_.last_vertiacal_swing;
605  break;
606  case CLIMATE_SWING_BOTH:
607  out_data->horizontal_swing_mode = (uint8_t) hon_protocol::HorizontalSwingMode::AUTO;
608  out_data->vertical_swing_mode = (uint8_t) hon_protocol::VerticalSwingMode::AUTO;
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()) {
623  case CLIMATE_PRESET_NONE:
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;
634  case CLIMATE_PRESET_AWAY:
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 
684 void 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  } else {
717  float alarm_count = 0.0f;
718  static uint8_t nibble_bits_count[] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
719  for (size_t i = 0; i < sizeof(this->active_alarms_); i++) {
720  alarm_count += (float) (nibble_bits_count[packet[2 + i] & 0x0F] + nibble_bits_count[packet[2 + i] >> 4]);
721  }
722  this->active_alarm_count_ = alarm_count;
723  memcpy(this->active_alarms_, packet + 2, sizeof(this->active_alarms_));
724  }
725  }
726 }
727 
728 #ifdef USE_SENSOR
732  if ((this->sub_sensors_[(size_t) type] != nullptr) && (sens == nullptr)) {
733  this->big_data_sensors_--;
734  } else if ((this->sub_sensors_[(size_t) type] == nullptr) && (sens != nullptr)) {
735  this->big_data_sensors_++;
736  }
737  }
738  this->sub_sensors_[(size_t) type] = sens;
739  }
740 }
741 
744  size_t index = (size_t) type;
745  if ((this->sub_sensors_[index] != nullptr) &&
746  ((!this->sub_sensors_[index]->has_state()) || (this->sub_sensors_[index]->raw_state != value)))
747  this->sub_sensors_[index]->publish_state(value);
748  }
749 }
750 #endif // USE_SENSOR
751 
752 #ifdef USE_BINARY_SENSOR
755  if ((this->sub_binary_sensors_[(size_t) type] != nullptr) && (sens == nullptr)) {
756  this->big_data_sensors_--;
757  } else if ((this->sub_binary_sensors_[(size_t) type] == nullptr) && (sens != nullptr)) {
758  this->big_data_sensors_++;
759  }
760  this->sub_binary_sensors_[(size_t) type] = sens;
761  }
762 }
763 
765  if (value < 2) {
766  bool converted_value = value == 1;
767  size_t index = (size_t) type;
768  if ((this->sub_binary_sensors_[index] != nullptr) && ((!this->sub_binary_sensors_[index]->has_state()) ||
769  (this->sub_binary_sensors_[index]->state != converted_value)))
770  this->sub_binary_sensors_[index]->publish_state(converted_value);
771  }
772 }
773 #endif // USE_BINARY_SENSOR
774 
775 #ifdef USE_TEXT_SENSOR
777  this->sub_text_sensors_[(size_t) type] = sens;
778  switch (type) {
780  if (this->hvac_hardware_info_.has_value())
781  sens->publish_state(this->hvac_hardware_info_.value().device_name_);
782  break;
784  if (this->hvac_hardware_info_.has_value())
785  sens->publish_state(this->hvac_hardware_info_.value().protocol_version_);
786  break;
789  break;
790  default:
791  break;
792  }
793 }
794 
796  size_t index = (size_t) type;
797  if (this->sub_text_sensors_[index] != nullptr)
798  this->sub_text_sensors_[index]->publish_state(value);
799 }
800 #endif // USE_TEXT_SENSOR
801 
802 #ifdef USE_SWITCH
804  this->beeper_switch_ = sw;
805  if (this->beeper_switch_ != nullptr) {
807  }
808 }
809 
811  this->quiet_mode_switch_ = sw;
812  if (this->quiet_mode_switch_ != nullptr) {
814  }
815 }
816 #endif // USE_SWITCH
817 
818 haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *packet_buffer, uint8_t size) {
819  size_t expected_size =
821  if (size < expected_size) {
822  ESP_LOGW(TAG, "Unexpected message size %d (expexted >= %d)", size, expected_size);
823  return haier_protocol::HandlerError::WRONG_MESSAGE_STRUCTURE;
824  }
825  uint16_t subtype = (((uint16_t) packet_buffer[0]) << 8) + packet_buffer[1];
826  if ((subtype == 0x7D01) && (size >= expected_size + sizeof(hon_protocol::HaierPacketBigData))) {
827  // Got BigData packet
828  const hon_protocol::HaierPacketBigData *bd_packet =
829  (const hon_protocol::HaierPacketBigData *) (&packet_buffer[expected_size]);
830 #ifdef USE_SENSOR
836  this->update_sub_sensor_(SubSensorType::POWER, encode_uint16(bd_packet->power[0], bd_packet->power[1]));
839  encode_uint16(bd_packet->compressor_current[0], bd_packet->compressor_current[1]) / 10.0);
840  this->update_sub_sensor_(
842  encode_uint16(bd_packet->expansion_valve_open_degree[0], bd_packet->expansion_valve_open_degree[1]) / 4095.0);
843 #endif // USE_SENSOR
844 #ifdef USE_BINARY_SENSOR
851  bd_packet->indoor_electric_heating_status);
852 #endif // USE_BINARY_SENSOR
853  }
854  struct {
857  } packet;
858  memcpy(&packet.control, packet_buffer + 2 + this->status_message_header_size_,
860  memcpy(&packet.sensors, packet_buffer + 2 + this->status_message_header_size_ + this->real_control_packet_size_,
862  if (packet.sensors.error_status != 0) {
863  ESP_LOGW(TAG, "HVAC error, code=0x%02X", packet.sensors.error_status);
864  }
865 #ifdef USE_SENSOR
866  if ((this->sub_sensors_[(size_t) SubSensorType::OUTDOOR_TEMPERATURE] != nullptr) &&
867  (this->got_valid_outdoor_temp_ || (packet.sensors.outdoor_temperature > 0))) {
868  this->got_valid_outdoor_temp_ = true;
870  (float) (packet.sensors.outdoor_temperature + PROTOCOL_OUTDOOR_TEMPERATURE_OFFSET));
871  }
872  if ((this->sub_sensors_[(size_t) SubSensorType::HUMIDITY] != nullptr) && (packet.sensors.room_humidity <= 100)) {
873  this->update_sub_sensor_(SubSensorType::HUMIDITY, (float) packet.sensors.room_humidity);
874  }
875 #endif // USE_SENSOR
876  bool should_publish = false;
877  {
878  // Extra modes/presets
879  optional<ClimatePreset> old_preset = this->preset;
880  if (packet.control.fast_mode != 0) {
882  } else if (packet.control.sleep_mode != 0) {
884  } else if (packet.control.ten_degree != 0) {
885  this->preset = CLIMATE_PRESET_AWAY;
886  } else {
887  this->preset = CLIMATE_PRESET_NONE;
888  }
889  should_publish = should_publish || (!old_preset.has_value()) || (old_preset.value() != this->preset.value());
890  }
891  {
892  // Target temperature
893  float old_target_temperature = this->target_temperature;
894  this->target_temperature = packet.control.set_point + 16.0f + ((packet.control.half_degree == 1) ? 0.5f : 0.0f);
895  should_publish = should_publish || (old_target_temperature != this->target_temperature);
896  }
897  {
898  // Current temperature
899  float old_current_temperature = this->current_temperature;
900  this->current_temperature = packet.sensors.room_temperature / 2.0f;
901  should_publish = should_publish || (old_current_temperature != this->current_temperature);
902  }
903  {
904  // Fan mode
905  optional<ClimateFanMode> old_fan_mode = this->fan_mode;
906  // remember the fan speed we last had for climate vs fan
907  if (packet.control.ac_mode == (uint8_t) hon_protocol::ConditioningMode::FAN) {
908  if (packet.control.fan_mode != (uint8_t) hon_protocol::FanMode::FAN_AUTO)
909  this->fan_mode_speed_ = packet.control.fan_mode;
910  } else {
911  this->other_modes_fan_speed_ = packet.control.fan_mode;
912  }
913  switch (packet.control.fan_mode) {
914  case (uint8_t) hon_protocol::FanMode::FAN_AUTO:
915  if (packet.control.ac_mode != (uint8_t) hon_protocol::ConditioningMode::FAN) {
916  this->fan_mode = CLIMATE_FAN_AUTO;
917  } else {
918  // Shouldn't accept fan speed auto in fan-only mode even if AC reports it
919  ESP_LOGI(TAG, "Fan speed Auto is not supported in Fan only AC mode, ignoring");
920  }
921  break;
922  case (uint8_t) hon_protocol::FanMode::FAN_MID:
924  break;
925  case (uint8_t) hon_protocol::FanMode::FAN_LOW:
926  this->fan_mode = CLIMATE_FAN_LOW;
927  break;
928  case (uint8_t) hon_protocol::FanMode::FAN_HIGH:
929  this->fan_mode = CLIMATE_FAN_HIGH;
930  break;
931  }
932  should_publish = should_publish || (!old_fan_mode.has_value()) || (old_fan_mode.value() != fan_mode.value());
933  }
934  // Display status
935  // should be before "Climate mode" because it is changing this->mode
936  if (packet.control.ac_power != 0) {
937  // if AC is off display status always ON so process it only when AC is on
938  bool disp_status = packet.control.display_status != 0;
939  if (disp_status != this->get_display_state()) {
940  // Do something only if display status changed
941  if (this->mode == CLIMATE_MODE_OFF) {
942  // AC just turned on from remote need to turn off display
943  this->force_send_control_ = true;
944  } else if ((((uint8_t) this->display_status_) & 0b10) == 0) {
945  this->display_status_ = disp_status ? SwitchState::ON : SwitchState::OFF;
946  }
947  }
948  }
949  // Health mode
950  if ((((uint8_t) this->health_mode_) & 0b10) == 0) {
951  bool old_health_mode = this->get_health_mode();
952  this->health_mode_ = packet.control.health_mode == 1 ? SwitchState::ON : SwitchState::OFF;
953  should_publish = should_publish || (old_health_mode != this->get_health_mode());
954  }
955  {
956  CleaningState new_cleaning;
957  if (packet.control.steri_clean == 1) {
958  // Steri-cleaning
959  new_cleaning = CleaningState::STERI_CLEAN;
960  } else if (packet.control.self_cleaning_status == 1) {
961  // Self-cleaning
962  new_cleaning = CleaningState::SELF_CLEAN;
963  } else {
964  // No cleaning
965  new_cleaning = CleaningState::NO_CLEANING;
966  }
967  if (new_cleaning != this->cleaning_status_) {
968  ESP_LOGD(TAG, "Cleaning status change: %d => %d", (uint8_t) this->cleaning_status_, (uint8_t) new_cleaning);
969  if (new_cleaning == CleaningState::NO_CLEANING) {
970  // Turning AC off after cleaning
971  this->action_request_ =
973  }
974  this->cleaning_status_ = new_cleaning;
975 #ifdef USE_TEXT_SENSOR
977 #endif // USE_TEXT_SENSOR
978  }
979  }
980  {
981  // Climate mode
982  ClimateMode old_mode = this->mode;
983  if (packet.control.ac_power == 0) {
984  this->mode = CLIMATE_MODE_OFF;
985  } else {
986  // Check current hvac mode
987  switch (packet.control.ac_mode) {
989  this->mode = CLIMATE_MODE_COOL;
990  break;
992  this->mode = CLIMATE_MODE_HEAT;
993  break;
995  this->mode = CLIMATE_MODE_DRY;
996  break;
998  this->mode = CLIMATE_MODE_FAN_ONLY;
999  break;
1000  case (uint8_t) hon_protocol::ConditioningMode::AUTO:
1001  this->mode = CLIMATE_MODE_HEAT_COOL;
1002  break;
1003  }
1004  }
1005  should_publish = should_publish || (old_mode != this->mode);
1006  }
1007  {
1008  // Quiet mode, should be after climate mode
1009  if ((this->mode != CLIMATE_MODE_FAN_ONLY) && (this->mode != CLIMATE_MODE_OFF) &&
1010  ((((uint8_t) this->quiet_mode_state_) & 0b10) == 0)) {
1011  // In proper mode and not in pending state
1012  bool new_quiet_mode = packet.control.quiet_mode != 0;
1013  if (new_quiet_mode != this->get_quiet_mode_state()) {
1014  this->quiet_mode_state_ = new_quiet_mode ? SwitchState::ON : SwitchState::OFF;
1015  this->settings_.quiet_mode_state = new_quiet_mode;
1016 #ifdef USE_SWITCH
1017  if (this->quiet_mode_switch_ != nullptr) {
1018  this->quiet_mode_switch_->publish_state(new_quiet_mode);
1019  }
1020 #endif // USE_SWITCH
1021  this->hon_rtc_.save(&this->settings_);
1022  }
1023  }
1024  }
1025  {
1026  // Swing mode
1027  ClimateSwingMode old_swing_mode = this->swing_mode;
1028  const std::set<ClimateSwingMode> &swing_modes = traits_.get_supported_swing_modes();
1029  bool vertical_swing_supported = swing_modes.find(CLIMATE_SWING_VERTICAL) != swing_modes.end();
1030  bool horizontal_swing_supported = swing_modes.find(CLIMATE_SWING_HORIZONTAL) != swing_modes.end();
1031  if (horizontal_swing_supported &&
1032  (packet.control.horizontal_swing_mode == (uint8_t) hon_protocol::HorizontalSwingMode::AUTO)) {
1033  if (vertical_swing_supported &&
1034  (packet.control.vertical_swing_mode == (uint8_t) hon_protocol::VerticalSwingMode::AUTO)) {
1036  } else {
1038  }
1039  } else {
1040  if (vertical_swing_supported &&
1041  (packet.control.vertical_swing_mode == (uint8_t) hon_protocol::VerticalSwingMode::AUTO)) {
1043  } else {
1044  this->swing_mode = CLIMATE_SWING_OFF;
1045  }
1046  }
1047  // Saving last known non auto mode for vertical and horizontal swing
1048  this->current_vertical_swing_ = (hon_protocol::VerticalSwingMode) packet.control.vertical_swing_mode;
1049  this->current_horizontal_swing_ = (hon_protocol::HorizontalSwingMode) packet.control.horizontal_swing_mode;
1055  if (save_settings) {
1058  this->hon_rtc_.save(&this->settings_);
1059  }
1060  should_publish = should_publish || (old_swing_mode != this->swing_mode);
1061  }
1062  this->last_valid_status_timestamp_ = std::chrono::steady_clock::now();
1063  if (should_publish) {
1064  this->publish_state();
1065  }
1066  if (should_publish) {
1067  ESP_LOGI(TAG, "HVAC values changed");
1068  }
1069  int log_level = should_publish ? ESPHOME_LOG_LEVEL_INFO : ESPHOME_LOG_LEVEL_DEBUG;
1070  esp_log_printf_(log_level, TAG, __LINE__, "HVAC Mode = 0x%X", packet.control.ac_mode);
1071  esp_log_printf_(log_level, TAG, __LINE__, "Fan speed Status = 0x%X", packet.control.fan_mode);
1072  esp_log_printf_(log_level, TAG, __LINE__, "Horizontal Swing Status = 0x%X", packet.control.horizontal_swing_mode);
1073  esp_log_printf_(log_level, TAG, __LINE__, "Vertical Swing Status = 0x%X", packet.control.vertical_swing_mode);
1074  esp_log_printf_(log_level, TAG, __LINE__, "Set Point Status = 0x%X", packet.control.set_point);
1075  return haier_protocol::HandlerError::HANDLER_OK;
1076 }
1077 
1079  if (!this->current_hvac_settings_.valid && !this->force_send_control_)
1080  return;
1082  HvacSettings climate_control;
1083  climate_control = this->current_hvac_settings_;
1084  // Beeper command
1085  {
1086  this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1089  this->get_beeper_state() ? ZERO_BUF : ONE_BUF, 2);
1090  }
1091  // Health mode
1092  {
1093  this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1096  this->get_health_mode() ? ONE_BUF : ZERO_BUF, 2);
1097  this->health_mode_ = (SwitchState) ((uint8_t) this->health_mode_ & 0b01);
1098  }
1099  // Climate mode
1100  ClimateMode climate_mode = this->mode;
1101  bool new_power = this->mode != CLIMATE_MODE_OFF;
1102  uint8_t fan_mode_buf[] = {0x00, 0xFF};
1103  uint8_t quiet_mode_buf[] = {0x00, 0xFF};
1104  if (climate_control.mode.has_value()) {
1105  climate_mode = climate_control.mode.value();
1106  uint8_t buffer[2] = {0x00, 0x00};
1107  switch (climate_control.mode.value()) {
1108  case CLIMATE_MODE_OFF:
1109  new_power = false;
1110  break;
1112  new_power = true;
1113  buffer[1] = (uint8_t) hon_protocol::ConditioningMode::AUTO;
1114  this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1117  buffer, 2);
1118  fan_mode_buf[1] = this->other_modes_fan_speed_;
1119  break;
1120  case CLIMATE_MODE_HEAT:
1121  new_power = true;
1122  buffer[1] = (uint8_t) hon_protocol::ConditioningMode::HEAT;
1123  this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1125  (uint8_t) hon_protocol::DataParameters::AC_MODE,
1126  buffer, 2);
1127  fan_mode_buf[1] = this->other_modes_fan_speed_;
1128  break;
1129  case CLIMATE_MODE_DRY:
1130  new_power = true;
1131  buffer[1] = (uint8_t) hon_protocol::ConditioningMode::DRY;
1132  this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1134  (uint8_t) hon_protocol::DataParameters::AC_MODE,
1135  buffer, 2);
1136  fan_mode_buf[1] = this->other_modes_fan_speed_;
1137  break;
1138  case CLIMATE_MODE_FAN_ONLY:
1139  new_power = true;
1140  buffer[1] = (uint8_t) hon_protocol::ConditioningMode::FAN;
1141  this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1143  (uint8_t) hon_protocol::DataParameters::AC_MODE,
1144  buffer, 2);
1145  fan_mode_buf[1] = this->other_modes_fan_speed_; // Auto doesn't work in fan only mode
1146  break;
1147  case CLIMATE_MODE_COOL:
1148  new_power = true;
1149  buffer[1] = (uint8_t) hon_protocol::ConditioningMode::COOL;
1150  this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1152  (uint8_t) hon_protocol::DataParameters::AC_MODE,
1153  buffer, 2);
1154  fan_mode_buf[1] = this->other_modes_fan_speed_;
1155  break;
1156  default:
1157  ESP_LOGE("Control", "Unsupported climate mode");
1158  break;
1159  }
1160  }
1161  // Climate power
1162  {
1163  this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1166  new_power ? ONE_BUF : ZERO_BUF, 2);
1167  }
1168  // CLimate preset
1169  {
1170  uint8_t fast_mode_buf[] = {0x00, 0xFF};
1171  uint8_t away_mode_buf[] = {0x00, 0xFF};
1172  if (!new_power) {
1173  // If AC is off - no presets allowed
1174  fast_mode_buf[1] = 0x00;
1175  away_mode_buf[1] = 0x00;
1176  } else if (climate_control.preset.has_value()) {
1177  switch (climate_control.preset.value()) {
1178  case CLIMATE_PRESET_NONE:
1179  fast_mode_buf[1] = 0x00;
1180  away_mode_buf[1] = 0x00;
1181  break;
1182  case CLIMATE_PRESET_BOOST:
1183  // Boost is not supported in Fan only mode
1184  fast_mode_buf[1] = (this->mode != CLIMATE_MODE_FAN_ONLY) ? 0x01 : 0x00;
1185  away_mode_buf[1] = 0x00;
1186  break;
1187  case CLIMATE_PRESET_AWAY:
1188  fast_mode_buf[1] = 0x00;
1189  away_mode_buf[1] = (this->mode == CLIMATE_MODE_HEAT) ? 0x01 : 0x00;
1190  break;
1191  default:
1192  ESP_LOGE("Control", "Unsupported preset");
1193  break;
1194  }
1195  }
1196  {
1197  // Quiet mode
1198  if (new_power && (climate_mode != CLIMATE_MODE_FAN_ONLY) && this->get_quiet_mode_state()) {
1199  quiet_mode_buf[1] = 0x01;
1200  } else {
1201  quiet_mode_buf[1] = 0x00;
1202  }
1203  // Clean quiet mode state pending flag
1204  this->quiet_mode_state_ = (SwitchState) ((uint8_t) this->quiet_mode_state_ & 0b01);
1205  }
1206  auto presets = this->traits_.get_supported_presets();
1207  if (quiet_mode_buf[1] != 0xFF) {
1208  this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1211  quiet_mode_buf, 2);
1212  }
1213  if ((fast_mode_buf[1] != 0xFF) && ((presets.find(climate::ClimatePreset::CLIMATE_PRESET_BOOST) != presets.end()))) {
1214  this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1217  fast_mode_buf, 2);
1218  }
1219  if ((away_mode_buf[1] != 0xFF) && ((presets.find(climate::ClimatePreset::CLIMATE_PRESET_AWAY) != presets.end()))) {
1220  this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1223  away_mode_buf, 2);
1224  }
1225  }
1226  // Target temperature
1227  if (climate_control.target_temperature.has_value() && (this->mode != ClimateMode::CLIMATE_MODE_FAN_ONLY)) {
1228  uint8_t buffer[2] = {0x00, 0x00};
1229  buffer[1] = ((uint8_t) climate_control.target_temperature.value()) - 16;
1230  this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1233  buffer, 2);
1234  }
1235  // Vertical swing mode
1236  if (climate_control.swing_mode.has_value()) {
1237  uint8_t vertical_swing_buf[] = {0x00, (uint8_t) hon_protocol::VerticalSwingMode::AUTO};
1238  uint8_t horizontal_swing_buf[] = {0x00, (uint8_t) hon_protocol::HorizontalSwingMode::AUTO};
1239  switch (climate_control.swing_mode.value()) {
1240  case CLIMATE_SWING_OFF:
1241  horizontal_swing_buf[1] = (uint8_t) this->settings_.last_horizontal_swing;
1242  vertical_swing_buf[1] = (uint8_t) this->settings_.last_vertiacal_swing;
1243  break;
1245  horizontal_swing_buf[1] = (uint8_t) this->settings_.last_horizontal_swing;
1246  break;
1248  vertical_swing_buf[1] = (uint8_t) this->settings_.last_vertiacal_swing;
1249  break;
1250  case CLIMATE_SWING_BOTH:
1251  break;
1252  }
1253  this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1256  horizontal_swing_buf, 2);
1257  this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1260  vertical_swing_buf, 2);
1261  }
1262  // Fan mode
1263  if (climate_control.fan_mode.has_value()) {
1264  switch (climate_control.fan_mode.value()) {
1265  case CLIMATE_FAN_LOW:
1266  fan_mode_buf[1] = (uint8_t) hon_protocol::FanMode::FAN_LOW;
1267  break;
1268  case CLIMATE_FAN_MEDIUM:
1269  fan_mode_buf[1] = (uint8_t) hon_protocol::FanMode::FAN_MID;
1270  break;
1271  case CLIMATE_FAN_HIGH:
1272  fan_mode_buf[1] = (uint8_t) hon_protocol::FanMode::FAN_HIGH;
1273  break;
1274  case CLIMATE_FAN_AUTO:
1275  if (mode != CLIMATE_MODE_FAN_ONLY) // if we are not in fan only mode
1276  fan_mode_buf[1] = (uint8_t) hon_protocol::FanMode::FAN_AUTO;
1277  break;
1278  default:
1279  ESP_LOGE("Control", "Unsupported fan mode");
1280  break;
1281  }
1282  if (fan_mode_buf[1] != 0xFF) {
1283  this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
1286  fan_mode_buf, 2);
1287  }
1288  }
1289 }
1290 
1292  while (!this->control_messages_queue_.empty())
1293  this->control_messages_queue_.pop();
1294 }
1295 
1297  switch (this->action_request_.value().action) {
1300  uint8_t control_out_buffer[haier_protocol::MAX_FRAME_SIZE];
1301  memcpy(control_out_buffer, this->last_status_message_.get(), this->real_control_packet_size_);
1302  hon_protocol::HaierPacketControl *out_data = (hon_protocol::HaierPacketControl *) control_out_buffer;
1303  out_data->self_cleaning_status = 1;
1304  out_data->steri_clean = 0;
1305  out_data->set_point = 0x06;
1306  out_data->vertical_swing_mode = (uint8_t) hon_protocol::VerticalSwingMode::CENTER;
1307  out_data->horizontal_swing_mode = (uint8_t) hon_protocol::HorizontalSwingMode::CENTER;
1308  out_data->ac_power = 1;
1309  out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::DRY;
1310  out_data->light_status = 0;
1311  this->action_request_.value().message = haier_protocol::HaierMessage(
1312  haier_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::SET_GROUP_PARAMETERS,
1313  control_out_buffer, this->real_control_packet_size_);
1314  return true;
1316  this->action_request_.value().message =
1317  haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
1320  ONE_BUF, 2);
1321  return true;
1322  } else {
1323  this->action_request_.reset();
1324  return false;
1325  }
1328  uint8_t control_out_buffer[haier_protocol::MAX_FRAME_SIZE];
1329  memcpy(control_out_buffer, this->last_status_message_.get(), this->real_control_packet_size_);
1330  hon_protocol::HaierPacketControl *out_data = (hon_protocol::HaierPacketControl *) control_out_buffer;
1331  out_data->self_cleaning_status = 0;
1332  out_data->steri_clean = 1;
1333  out_data->set_point = 0x06;
1334  out_data->vertical_swing_mode = (uint8_t) hon_protocol::VerticalSwingMode::CENTER;
1335  out_data->horizontal_swing_mode = (uint8_t) hon_protocol::HorizontalSwingMode::CENTER;
1336  out_data->ac_power = 1;
1337  out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::DRY;
1338  out_data->light_status = 0;
1339  this->action_request_.value().message = haier_protocol::HaierMessage(
1340  haier_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::SET_GROUP_PARAMETERS,
1341  control_out_buffer, this->real_control_packet_size_);
1342  return true;
1343  } else {
1344  // No Steri clean support (yet?) in SET_SINGLE_PARAMETER
1345  this->action_request_.reset();
1346  return false;
1347  }
1348  default:
1350  }
1351 }
1352 
1355 #ifdef USE_SENSOR
1356  for (auto &sub_sensor : this->sub_sensors_) {
1357  if ((sub_sensor != nullptr) && sub_sensor->has_state())
1358  sub_sensor->publish_state(NAN);
1359  }
1360 #endif // USE_SENSOR
1361  this->got_valid_outdoor_temp_ = false;
1362  this->hvac_hardware_info_.reset();
1363  this->last_status_message_.reset(nullptr);
1364 }
1365 
1367  if (this->big_data_sensors_ > 0) {
1368  static uint8_t counter = 0;
1369  counter = (counter + 1) % 3;
1370  return counter == 1;
1371  }
1372  return false;
1373 }
1374 
1375 } // namespace haier
1376 } // namespace esphome
Base class for all switches.
Definition: switch.h:39
value_type const & value() const
Definition: optional.h:89
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition: climate.h:202
esphome::optional< hon_protocol::VerticalSwingMode > current_vertical_swing_
Definition: hon_climate.h:190
haier_protocol::HaierMessage get_control_message() override
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)
void update_sub_sensor_(SubSensorType type, float value)
esphome::optional< hon_protocol::VerticalSwingMode > pending_vertical_direction_
Definition: hon_climate.h:174
esphome::optional< float > target_temperature
Definition: haier_base.h:137
hon_protocol::HorizontalSwingMode last_horizontal_swing
Definition: hon_climate.h:33
CallbackManager< void(uint8_t, const char *)> alarm_start_callback_
Definition: hon_climate.h:185
esphome::optional< esphome::climate::ClimatePreset > preset
Definition: haier_base.h:138
CallbackManager< void(uint8_t, const char *)> alarm_end_callback_
Definition: hon_climate.h:186
haier_protocol::HandlerError process_status_message_(const uint8_t *packet, uint8_t size)
const std::set< ClimateSwingMode > & get_supported_swing_modes() const
std::unique_ptr< uint8_t[]> last_status_message_
Definition: haier_base.h:170
constexpr int PROTOCOL_OUTDOOR_TEMPERATURE_OFFSET
Definition: hon_climate.cpp:17
float target_temperature
The target temperature of the climate device.
Definition: climate.h:186
void add_alarm_end_callback(std::function< void(uint8_t, const char *)> &&callback)
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)
Definition: haier_base.cpp:223
virtual void set_phase(ProtocolPhases phase)
Definition: haier_base.cpp:73
void set_quiet_mode_switch(switch_::Switch *sw)
void control(const esphome::climate::ClimateCall &call) override
Definition: haier_base.cpp:371
void update_sub_text_sensor_(SubTextSensorType type, const std::string &value)
constexpr std::chrono::milliseconds CONTROL_MESSAGE_RETRIES_INTERVAL
Definition: hon_climate.cpp:19
std::chrono::steady_clock::time_point last_status_request_
Definition: haier_base.h:173
std::chrono::steady_clock::time_point last_signal_request_
Definition: haier_base.h:174
const uint8_t ONE_BUF[]
Definition: hon_climate.cpp:21
ClimateMode mode
The active mode of the climate device.
Definition: climate.h:173
void set_handlers() override
void set_vertical_airflow(hon_protocol::VerticalSwingMode direction)
Definition: hon_climate.cpp:74
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition: climate.h:179
void set_sub_binary_sensor(SubBinarySensorType type, binary_sensor::BinarySensor *sens)
CleaningState cleaning_status_
Definition: hon_climate.h:172
void publish_state(const std::string &state)
Definition: text_sensor.cpp:9
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)
Definition: haier_base.cpp:204
bool has_value() const
Definition: optional.h:87
HonControlMethod control_method_
Definition: hon_climate.h:183
esphome::optional< hon_protocol::HorizontalSwingMode > get_horizontal_airflow() const
Definition: hon_climate.cpp:79
esphome::optional< HardwareInfo > hvac_hardware_info_
Definition: hon_climate.h:176
bool get_quiet_mode_state() const
Definition: hon_climate.cpp:66
void set_sub_text_sensor(SubTextSensorType type, text_sensor::TextSensor *sens)
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)
ClimateSwingMode
Enum for all modes a climate swing can be in.
Definition: climate_mode.h:70
void process_phase(std::chrono::steady_clock::time_point now) override
bool save(const T *src)
Definition: preferences.h:21
void initialization() override
void set_quiet_mode_state(bool state)
Definition: hon_climate.cpp:48
esphome::optional< hon_protocol::HorizontalSwingMode > current_horizontal_swing_
Definition: hon_climate.h:191
haier_protocol::ProtocolHandler haier_protocol_
Definition: haier_base.h:155
FanDirection direction
Definition: fan.h:37
bool is_protocol_initialisation_interval_exceeded_(std::chrono::steady_clock::time_point now)
Definition: haier_base.cpp:106
void add_alarm_start_callback(std::function< void(uint8_t, const char *)> &&callback)
constexpr size_t HON_ALARM_COUNT
Definition: hon_packet.h:256
void set_beeper_state(bool state)
Definition: hon_climate.cpp:34
void send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_repeats=0, std::chrono::milliseconds interval=std::chrono::milliseconds::zero())
Definition: haier_base.cpp:420
constexpr size_t SIGNAL_LEVEL_UPDATE_INTERVAL_MS
Definition: hon_climate.cpp:16
haier_protocol::HaierMessage get_wifi_signal_message_()
Definition: haier_base.cpp:111
optional< ClimatePreset > preset
The active preset of the climate device.
Definition: climate.h:208
const char * phase_to_string_(ProtocolPhases phase)
Definition: haier_base.cpp:23
virtual bool has_state() const
Return whether this binary sensor has outputted a state.
const std::set< climate::ClimatePreset > & get_supported_presets() const
ESPPreferences * global_preferences
void set_beeper_switch(switch_::Switch *sw)
esphome::optional< hon_protocol::VerticalSwingMode > get_vertical_airflow() const
Definition: hon_climate.cpp:70
esphome::optional< hon_protocol::HorizontalSwingMode > pending_horizontal_direction_
Definition: hon_climate.h:175
CleaningState get_cleaning_status() const
Definition: hon_climate.cpp:99
void set_sub_sensor(SubSensorType type, sensor::Sensor *sens)
ESPPreferenceObject hon_rtc_
Definition: hon_climate.h:193
esphome::optional< PendingAction > action_request_
Definition: haier_base.h:157
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
sensor::Sensor * sub_sensors_[(size_t) SubSensorType::SUB_SENSOR_TYPE_COUNT]
Definition: hon_climate.h:62
haier_protocol::HandlerError status_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data, size_t data_size)
uint8_t type
esphome::optional< esphome::climate::ClimateFanMode > fan_mode
Definition: haier_base.h:135
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:183
void publish_state(bool state)
Publish a new state to the front-end.
switch_::Switch * beeper_switch_
Definition: hon_climate.h:101
constexpr size_t ALARM_STATUS_REQUEST_INTERVAL_MS
Definition: hon_climate.cpp:20
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition: climate.cpp:395
void HOT esp_log_printf_(int level, const char *tag, int line, const char *format,...)
Definition: log.cpp:11
text_sensor::TextSensor * sub_text_sensors_[(size_t) SubTextSensorType::SUB_TEXT_SENSOR_TYPE_COUNT]
Definition: hon_climate.h:93
ClimateMode
Enum for all modes a climate device can be in.
Definition: climate_mode.h:10
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)
switch_::Switch * quiet_mode_switch_
Definition: hon_climate.h:102
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition: climate.h:199
const uint8_t ZERO_BUF[]
Definition: hon_climate.cpp:22
std::string get_cleaning_status_text() const
Definition: hon_climate.cpp:88
void update_sub_binary_sensor_(SubBinarySensorType type, uint8_t value)
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
bool is_message_interval_exceeded_(std::chrono::steady_clock::time_point now)
Definition: haier_base.cpp:94
std::chrono::steady_clock::time_point last_alarm_request_
Definition: hon_climate.h:188
bool is_status_request_interval_exceeded_(std::chrono::steady_clock::time_point now)
Definition: haier_base.cpp:98
haier_protocol::HandlerError alarm_status_message_handler_(haier_protocol::FrameType type, const uint8_t *buffer, size_t size)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool has_state() const
Return whether this sensor has gotten a full state (that passed through all filters) yet...
Definition: sensor.cpp:97
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
void process_alarm_message_(const uint8_t *packet, uint8_t size, bool check_new)
void process_protocol_reset() override
haier_protocol::HaierMessage get_power_message(bool state) override
Base-class for all sensors.
Definition: sensor.h:57
const std::string HON_ALARM_MESSAGES[]
Definition: hon_packet.h:202
void set_horizontal_airflow(hon_protocol::HorizontalSwingMode direction)
Definition: hon_climate.cpp:83
bool prepare_pending_action() override
constexpr uint8_t CONTROL_MESSAGE_RETRIES
Definition: hon_climate.cpp:18
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition: switch.cpp:47
bool is_control_message_interval_exceeded_(std::chrono::steady_clock::time_point now)
Definition: haier_base.cpp:102
CallbackManager< void(const char *, size_t)> status_message_callback_
Definition: haier_base.h:175
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_
Definition: hon_climate.h:184
binary_sensor::BinarySensor * sub_binary_sensors_[(size_t) SubBinarySensorType::SUB_BINARY_SENSOR_TYPE_COUNT]
Definition: hon_climate.h:79
hon_protocol::VerticalSwingMode last_vertiacal_swing
Definition: hon_climate.h:32
uint32_t get_object_id_hash()
Definition: entity_base.cpp:76
esphome::climate::ClimateTraits traits_
Definition: haier_base.h:167
esphome::optional< esphome::climate::ClimateSwingMode > swing_mode
Definition: haier_base.h:136
bool state
Definition: fan.h:34
std::chrono::steady_clock::time_point last_valid_status_timestamp_
Definition: haier_base.h:172
esphome::optional< esphome::climate::ClimateMode > mode
Definition: haier_base.h:134
void dump_config() override