ESPHome  2024.4.0
haier_base.cpp
Go to the documentation of this file.
1 #include <chrono>
2 #include <string>
5 #ifdef USE_WIFI
7 #endif
8 #include "haier_base.h"
9 
10 using namespace esphome::climate;
11 using namespace esphome::uart;
12 
13 namespace esphome {
14 namespace haier {
15 
16 static const char *const TAG = "haier.climate";
17 constexpr size_t COMMUNICATION_TIMEOUT_MS = 60000;
18 constexpr size_t STATUS_REQUEST_INTERVAL_MS = 5000;
19 constexpr size_t PROTOCOL_INITIALIZATION_INTERVAL = 10000;
20 constexpr size_t DEFAULT_MESSAGES_INTERVAL_MS = 2000;
21 constexpr size_t CONTROL_MESSAGES_INTERVAL_MS = 400;
22 
23 const char *HaierClimateBase::phase_to_string_(ProtocolPhases phase) {
24  static const char *phase_names[] = {
25  "SENDING_INIT_1",
26  "SENDING_INIT_2",
27  "SENDING_FIRST_STATUS_REQUEST",
28  "SENDING_FIRST_ALARM_STATUS_REQUEST",
29  "IDLE",
30  "SENDING_STATUS_REQUEST",
31  "SENDING_UPDATE_SIGNAL_REQUEST",
32  "SENDING_SIGNAL_LEVEL",
33  "SENDING_CONTROL",
34  "SENDING_ACTION_COMMAND",
35  "SENDING_ALARM_STATUS_REQUEST",
36  "UNKNOWN" // Should be the last!
37  };
38  static_assert(
39  (sizeof(phase_names) / sizeof(char *)) == (((int) ProtocolPhases::NUM_PROTOCOL_PHASES) + 1),
40  "Wrong phase_names array size. Please, make sure that this array is aligned with the enum ProtocolPhases");
41  int phase_index = (int) phase;
42  if ((phase_index > (int) ProtocolPhases::NUM_PROTOCOL_PHASES) || (phase_index < 0))
43  phase_index = (int) ProtocolPhases::NUM_PROTOCOL_PHASES;
44  return phase_names[phase_index];
45 }
46 
47 bool check_timeout(std::chrono::steady_clock::time_point now, std::chrono::steady_clock::time_point tpoint,
48  size_t timeout) {
49  return std::chrono::duration_cast<std::chrono::milliseconds>(now - tpoint).count() > timeout;
50 }
51 
52 HaierClimateBase::HaierClimateBase()
53  : haier_protocol_(*this),
54  protocol_phase_(ProtocolPhases::SENDING_INIT_1),
55  display_status_(true),
56  health_mode_(false),
57  force_send_control_(false),
58  forced_request_status_(false),
59  reset_protocol_request_(false),
60  send_wifi_signal_(true),
61  use_crc_(false) {
71 }
72 
74 
76  if (this->protocol_phase_ != phase) {
77  ESP_LOGV(TAG, "Phase transition: %s => %s", phase_to_string_(this->protocol_phase_), phase_to_string_(phase));
78  this->protocol_phase_ = phase;
79  }
80 }
81 
85 }
86 
88  this->force_send_control_ = false;
89  if (this->current_hvac_settings_.valid)
91  this->forced_request_status_ = true;
93  this->action_request_.reset();
94 }
95 
96 bool HaierClimateBase::is_message_interval_exceeded_(std::chrono::steady_clock::time_point now) {
97  return check_timeout(now, this->last_request_timestamp_, DEFAULT_MESSAGES_INTERVAL_MS);
98 }
99 
100 bool HaierClimateBase::is_status_request_interval_exceeded_(std::chrono::steady_clock::time_point now) {
101  return check_timeout(now, this->last_status_request_, STATUS_REQUEST_INTERVAL_MS);
102 }
103 
104 bool HaierClimateBase::is_control_message_interval_exceeded_(std::chrono::steady_clock::time_point now) {
105  return check_timeout(now, this->last_request_timestamp_, CONTROL_MESSAGES_INTERVAL_MS);
106 }
107 
108 bool HaierClimateBase::is_protocol_initialisation_interval_exceeded_(std::chrono::steady_clock::time_point now) {
109  return check_timeout(now, this->last_request_timestamp_, PROTOCOL_INITIALIZATION_INTERVAL);
110 }
111 
112 #ifdef USE_WIFI
113 haier_protocol::HaierMessage HaierClimateBase::get_wifi_signal_message_() {
114  static uint8_t wifi_status_data[4] = {0x00, 0x00, 0x00, 0x00};
116  wifi_status_data[1] = 0;
117  int8_t rssi = wifi::global_wifi_component->wifi_rssi();
118  wifi_status_data[3] = uint8_t((128 + rssi) / 1.28f);
119  ESP_LOGD(TAG, "WiFi signal is: %ddBm => %d%%", rssi, wifi_status_data[3]);
120  } else {
121  ESP_LOGD(TAG, "WiFi is not connected");
122  wifi_status_data[1] = 1;
123  wifi_status_data[3] = 0;
124  }
125  return haier_protocol::HaierMessage(haier_protocol::FrameType::REPORT_NETWORK_STATUS, wifi_status_data,
126  sizeof(wifi_status_data));
127 }
128 #endif
129 
131 
133  if (this->display_status_ != state) {
134  this->display_status_ = state;
135  this->force_send_control_ = true;
136  }
137 }
138 
139 bool HaierClimateBase::get_health_mode() const { return this->health_mode_; }
140 
142  if (this->health_mode_ != state) {
143  this->health_mode_ = state;
144  this->force_send_control_ = true;
145  }
146 }
147 
149  this->action_request_ =
151 }
152 
154  this->action_request_ =
156 }
157 
159  this->action_request_ =
161 }
162 
163 void HaierClimateBase::set_supported_swing_modes(const std::set<climate::ClimateSwingMode> &modes) {
164  this->traits_.set_supported_swing_modes(modes);
165  if (!modes.empty())
167 }
168 
169 void HaierClimateBase::set_answer_timeout(uint32_t timeout) { this->haier_protocol_.set_answer_timeout(timeout); }
170 
171 void HaierClimateBase::set_supported_modes(const std::set<climate::ClimateMode> &modes) {
172  this->traits_.set_supported_modes(modes);
173  this->traits_.add_supported_mode(climate::CLIMATE_MODE_OFF); // Always available
175 }
176 
177 void HaierClimateBase::set_supported_presets(const std::set<climate::ClimatePreset> &presets) {
178  this->traits_.set_supported_presets(presets);
179  if (!presets.empty())
181 }
182 
183 void HaierClimateBase::set_send_wifi(bool send_wifi) { this->send_wifi_signal_ = send_wifi; }
184 
185 void HaierClimateBase::send_custom_command(const haier_protocol::HaierMessage &message) {
187 }
188 
189 haier_protocol::HandlerError HaierClimateBase::answer_preprocess_(
190  haier_protocol::FrameType request_message_type, haier_protocol::FrameType expected_request_message_type,
191  haier_protocol::FrameType answer_message_type, haier_protocol::FrameType expected_answer_message_type,
192  ProtocolPhases expected_phase) {
193  haier_protocol::HandlerError result = haier_protocol::HandlerError::HANDLER_OK;
194  if ((expected_request_message_type != haier_protocol::FrameType::UNKNOWN_FRAME_TYPE) &&
195  (request_message_type != expected_request_message_type))
196  result = haier_protocol::HandlerError::UNSUPPORTED_MESSAGE;
197  if ((expected_answer_message_type != haier_protocol::FrameType::UNKNOWN_FRAME_TYPE) &&
198  (answer_message_type != expected_answer_message_type))
199  result = haier_protocol::HandlerError::UNSUPPORTED_MESSAGE;
200  if (!this->haier_protocol_.is_waiting_for_answer() ||
201  ((expected_phase != ProtocolPhases::UNKNOWN) && (expected_phase != this->protocol_phase_)))
202  result = haier_protocol::HandlerError::UNEXPECTED_MESSAGE;
203  if (answer_message_type == haier_protocol::FrameType::INVALID)
204  result = haier_protocol::HandlerError::INVALID_ANSWER;
205  return result;
206 }
207 
209  haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data,
210  size_t data_size) {
211  haier_protocol::HandlerError result =
212  this->answer_preprocess_(request_type, haier_protocol::FrameType::REPORT_NETWORK_STATUS, message_type,
213  haier_protocol::FrameType::CONFIRM, ProtocolPhases::SENDING_SIGNAL_LEVEL);
215  return result;
216 }
217 
218 haier_protocol::HandlerError HaierClimateBase::timeout_default_handler_(haier_protocol::FrameType request_type) {
219  ESP_LOGW(TAG, "Answer timeout for command %02X, phase %s", (uint8_t) request_type,
223  } else {
225  }
226  return haier_protocol::HandlerError::HANDLER_OK;
227 }
228 
230  ESP_LOGI(TAG, "Haier initialization...");
231  // Set timestamp here to give AC time to boot
232  this->last_request_timestamp_ = std::chrono::steady_clock::now();
234  this->haier_protocol_.set_default_timeout_handler(
235  std::bind(&esphome::haier::HaierClimateBase::timeout_default_handler_, this, std::placeholders::_1));
236  this->set_handlers();
237 }
238 
240  LOG_CLIMATE("", "Haier Climate", this);
241  ESP_LOGCONFIG(TAG, " Device communication status: %s", this->valid_connection() ? "established" : "none");
242 }
243 
245  std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
246  if ((std::chrono::duration_cast<std::chrono::milliseconds>(now - this->last_valid_status_timestamp_).count() >
247  COMMUNICATION_TIMEOUT_MS) ||
248  (this->reset_protocol_request_ && (!this->haier_protocol_.is_waiting_for_answer()))) {
249  this->last_valid_status_timestamp_ = now;
250  if (this->protocol_phase_ >= ProtocolPhases::IDLE) {
251  // No status too long, reseting protocol
252  // No need to reset protocol if we didn't pass initialization phase
253  if (this->reset_protocol_request_) {
254  this->reset_protocol_request_ = false;
255  ESP_LOGW(TAG, "Protocol reset requested");
256  } else {
257  ESP_LOGW(TAG, "Communication timeout, reseting protocol");
258  }
259  this->process_protocol_reset();
260  return;
261  }
262  };
263  if ((!this->haier_protocol_.is_waiting_for_answer()) &&
268  // If control message or action is pending we should send it ASAP unless we are in initialisation
269  // procedure or waiting for an answer
270  if (this->action_request_.has_value() && this->prepare_pending_action()) {
272  } else if (this->next_hvac_settings_.valid || this->force_send_control_) {
273  ESP_LOGV(TAG, "Control packet is pending...");
275  if (this->next_hvac_settings_.valid) {
277  this->next_hvac_settings_.reset();
278  } else {
280  }
281  }
282  }
283  this->process_phase(now);
284  this->haier_protocol_.loop();
285 }
286 
288  this->force_send_control_ = false;
289  if (this->current_hvac_settings_.valid)
291  if (this->next_hvac_settings_.valid)
292  this->next_hvac_settings_.reset();
293  this->mode = CLIMATE_MODE_OFF;
294  this->current_temperature = NAN;
295  this->target_temperature = NAN;
296  this->fan_mode.reset();
297  this->preset.reset();
298  this->publish_state();
300 }
301 
303  if (this->action_request_.has_value()) {
304  switch (this->action_request_.value().action) {
306  return true;
308  this->action_request_.value().message = this->get_power_message(true);
309  return true;
311  this->action_request_.value().message = this->get_power_message(false);
312  return true;
314  this->action_request_.value().message = this->get_power_message(this->mode == ClimateMode::CLIMATE_MODE_OFF);
315  return true;
316  default:
317  ESP_LOGW(TAG, "Unsupported action: %d", (uint8_t) this->action_request_.value().action);
318  this->action_request_.reset();
319  return false;
320  }
321  } else
322  return false;
323 }
324 
326 
328  ESP_LOGD("Control", "Control call");
330  ESP_LOGW(TAG, "Can't send control packet, first poll answer not received");
331  return; // cancel the control, we cant do it without a poll answer.
332  }
333  if (this->current_hvac_settings_.valid) {
334  ESP_LOGW(TAG, "New settings come faster then processed!");
335  }
336  {
337  if (call.get_mode().has_value())
338  this->next_hvac_settings_.mode = call.get_mode();
339  if (call.get_fan_mode().has_value())
341  if (call.get_swing_mode().has_value())
343  if (call.get_target_temperature().has_value())
345  if (call.get_preset().has_value())
346  this->next_hvac_settings_.preset = call.get_preset();
347  this->next_hvac_settings_.valid = true;
348  }
349 }
350 
352  this->valid = false;
353  this->mode.reset();
354  this->fan_mode.reset();
355  this->swing_mode.reset();
356  this->target_temperature.reset();
357  this->preset.reset();
358 }
359 
360 void HaierClimateBase::send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_repeats,
361  std::chrono::milliseconds interval) {
362  this->haier_protocol_.send_message(command, use_crc, num_repeats, interval);
363  this->last_request_timestamp_ = std::chrono::steady_clock::now();
364 }
365 
366 } // namespace haier
367 } // namespace esphome
This class is used to encode all control actions on a climate device.
Definition: climate.h:33
The fan mode is set to Low.
Definition: climate_mode.h:54
constexpr size_t COMMUNICATION_TIMEOUT_MS
Definition: haier_base.cpp:17
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition: climate.h:202
esphome::optional< float > target_temperature
Definition: haier_base.h:115
The fan mode is set to Both.
Definition: climate_mode.h:74
esphome::optional< esphome::climate::ClimatePreset > preset
Definition: haier_base.h:116
constexpr size_t PROTOCOL_INITIALIZATION_INTERVAL
Definition: haier_base.cpp:19
float target_temperature
The target temperature of the climate device.
Definition: climate.h:186
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:208
const optional< ClimateMode > & get_mode() const
Definition: climate.cpp:273
virtual void set_phase(ProtocolPhases phase)
Definition: haier_base.cpp:75
This class contains all static data for climate devices.
void control(const esphome::climate::ClimateCall &call) override
Definition: haier_base.cpp:327
constexpr size_t DEFAULT_MESSAGES_INTERVAL_MS
Definition: haier_base.cpp:20
std::chrono::steady_clock::time_point last_status_request_
Definition: haier_base.h:145
The climate device is set to heat to reach the target temperature.
Definition: climate_mode.h:18
ClimateMode mode
The active mode of the climate device.
Definition: climate.h:173
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition: climate.h:179
void set_send_wifi(bool send_wifi)
Definition: haier_base.cpp:183
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:189
bool has_value() const
Definition: optional.h:87
The climate device is set to dry/humidity mode.
Definition: climate_mode.h:22
void set_supported_presets(std::set< ClimatePreset > presets)
virtual void process_phase(std::chrono::steady_clock::time_point now)=0
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition: util.cpp:15
void add_supported_preset(ClimatePreset preset)
esphome::climate::ClimateTraits traits() override
Definition: haier_base.cpp:325
haier_protocol::ProtocolHandler haier_protocol_
Definition: haier_base.h:127
void add_supported_swing_mode(ClimateSwingMode mode)
bool is_protocol_initialisation_interval_exceeded_(std::chrono::steady_clock::time_point now)
Definition: haier_base.cpp:108
void set_display_state(bool state)
Definition: haier_base.cpp:132
The fan mode is set to Horizontal.
Definition: climate_mode.h:78
The climate device is set to cool to reach the target temperature.
Definition: climate_mode.h:16
const optional< ClimatePreset > & get_preset() const
Definition: climate.cpp:280
void set_supported_fan_modes(std::set< ClimateFanMode > modes)
The fan mode is set to Auto.
Definition: climate_mode.h:52
void set_answer_timeout(uint32_t timeout)
Definition: haier_base.cpp:169
void send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_repeats=0, std::chrono::milliseconds interval=std::chrono::milliseconds::zero())
Definition: haier_base.cpp:360
haier_protocol::HaierMessage get_wifi_signal_message_()
Definition: haier_base.cpp:113
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
constexpr size_t STATUS_REQUEST_INTERVAL_MS
Definition: haier_base.cpp:18
void set_supported_modes(std::set< ClimateMode > modes)
esphome::optional< PendingAction > action_request_
Definition: haier_base.h:129
void set_supported_modes(const std::set< esphome::climate::ClimateMode > &modes)
Definition: haier_base.cpp:171
esphome::optional< esphome::climate::ClimateFanMode > fan_mode
Definition: haier_base.h:113
The climate device is set to heat/cool to reach the target temperature.
Definition: climate_mode.h:14
void set_supported_swing_modes(const std::set< esphome::climate::ClimateSwingMode > &modes)
Definition: haier_base.cpp:163
The fan mode is set to Vertical.
Definition: climate_mode.h:76
constexpr size_t CONTROL_MESSAGES_INTERVAL_MS
Definition: haier_base.cpp:21
WiFiComponent * global_wifi_component
const optional< float > & get_target_temperature() const
Definition: climate.cpp:274
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition: climate.cpp:395
The fan mode is set to High.
Definition: climate_mode.h:58
virtual haier_protocol::HaierMessage get_power_message(bool state)=0
The swing mode is set to Off.
Definition: climate_mode.h:72
The climate device is off.
Definition: climate_mode.h:12
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition: climate.h:199
const optional< ClimateFanMode > & get_fan_mode() const
Definition: climate.cpp:278
bool is_message_interval_exceeded_(std::chrono::steady_clock::time_point now)
Definition: haier_base.cpp:96
const optional< ClimateSwingMode > & get_swing_mode() const
Definition: climate.cpp:282
bool is_status_request_interval_exceeded_(std::chrono::steady_clock::time_point now)
Definition: haier_base.cpp:100
bool check_timeout(std::chrono::steady_clock::time_point now, std::chrono::steady_clock::time_point tpoint, size_t timeout)
Definition: haier_base.cpp:47
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
std::chrono::steady_clock::time_point last_request_timestamp_
Definition: haier_base.h:143
void set_supported_swing_modes(std::set< ClimateSwingMode > modes)
haier_protocol::HandlerError timeout_default_handler_(haier_protocol::FrameType request_type)
Definition: haier_base.cpp:218
void send_custom_command(const haier_protocol::HaierMessage &message)
Definition: haier_base.cpp:185
void set_supported_presets(const std::set< esphome::climate::ClimatePreset > &presets)
Definition: haier_base.cpp:177
void set_supports_current_temperature(bool supports_current_temperature)
The fan mode is set to Medium.
Definition: climate_mode.h:56
The climate device only has the fan enabled, no heating or cooling is taking place.
Definition: climate_mode.h:20
bool is_control_message_interval_exceeded_(std::chrono::steady_clock::time_point now)
Definition: haier_base.cpp:104
void add_supported_mode(ClimateMode mode)
esphome::climate::ClimateTraits traits_
Definition: haier_base.h:139
esphome::optional< esphome::climate::ClimateSwingMode > swing_mode
Definition: haier_base.h:114
bool state
Definition: fan.h:34
std::chrono::steady_clock::time_point last_valid_status_timestamp_
Definition: haier_base.h:144
esphome::optional< esphome::climate::ClimateMode > mode
Definition: haier_base.h:112