ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
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
10using namespace esphome::climate;
11using namespace esphome::uart;
12
13namespace esphome::haier {
14
15ESPHOME_LOG_TAG(TAG, "haier.climate");
16constexpr size_t COMMUNICATION_TIMEOUT_MS = 60000;
17constexpr size_t STATUS_REQUEST_INTERVAL_MS = 5000;
18constexpr size_t PROTOCOL_INITIALIZATION_INTERVAL = 10000;
19constexpr size_t DEFAULT_MESSAGES_INTERVAL_MS = 2000;
20constexpr size_t CONTROL_MESSAGES_INTERVAL_MS = 400;
21
23 static const char *phase_names[] = {
24 "SENDING_INIT_1",
25 "SENDING_INIT_2",
26 "SENDING_FIRST_STATUS_REQUEST",
27 "SENDING_FIRST_ALARM_STATUS_REQUEST",
28 "IDLE",
29 "SENDING_STATUS_REQUEST",
30 "SENDING_UPDATE_SIGNAL_REQUEST",
31 "SENDING_SIGNAL_LEVEL",
32 "SENDING_CONTROL",
33 "SENDING_ACTION_COMMAND",
34 "SENDING_ALARM_STATUS_REQUEST",
35 "UNKNOWN" // Should be the last!
36 };
37 static_assert(
38 (sizeof(phase_names) / sizeof(char *)) == (((int) ProtocolPhases::NUM_PROTOCOL_PHASES) + 1),
39 "Wrong phase_names array size. Please, make sure that this array is aligned with the enum ProtocolPhases");
40 int phase_index = (int) phase;
41 if ((phase_index > (int) ProtocolPhases::NUM_PROTOCOL_PHASES) || (phase_index < 0))
42 phase_index = (int) ProtocolPhases::NUM_PROTOCOL_PHASES;
43 return phase_names[phase_index];
44}
45
46bool check_timeout(std::chrono::steady_clock::time_point now, std::chrono::steady_clock::time_point tpoint,
47 size_t timeout) {
48 return std::chrono::duration_cast<std::chrono::milliseconds>(now - tpoint).count() > timeout;
49}
50
69
71
73 if (this->protocol_phase_ != phase) {
74 ESP_LOGV(TAG, "Phase transition: %s => %s", phase_to_string_(this->protocol_phase_), phase_to_string_(phase));
75 this->protocol_phase_ = phase;
76 }
77}
78
83
92
93bool HaierClimateBase::is_message_interval_exceeded_(std::chrono::steady_clock::time_point now) {
94 return check_timeout(now, this->last_request_timestamp_, DEFAULT_MESSAGES_INTERVAL_MS);
95}
96
97bool HaierClimateBase::is_status_request_interval_exceeded_(std::chrono::steady_clock::time_point now) {
98 return check_timeout(now, this->last_status_request_, STATUS_REQUEST_INTERVAL_MS);
99}
100
101bool HaierClimateBase::is_control_message_interval_exceeded_(std::chrono::steady_clock::time_point now) {
102 return check_timeout(now, this->last_request_timestamp_, CONTROL_MESSAGES_INTERVAL_MS);
103}
104
105bool HaierClimateBase::is_protocol_initialisation_interval_exceeded_(std::chrono::steady_clock::time_point now) {
106 return check_timeout(now, this->last_request_timestamp_, PROTOCOL_INITIALIZATION_INTERVAL);
107}
108
109#ifdef USE_WIFI
110haier_protocol::HaierMessage HaierClimateBase::get_wifi_signal_message_() {
111 static uint8_t wifi_status_data[4] = {0x00, 0x00, 0x00, 0x00};
112 if (wifi::global_wifi_component->is_connected()) {
113 wifi_status_data[1] = 0;
114 int8_t rssi = wifi::global_wifi_component->wifi_rssi();
115 wifi_status_data[3] = uint8_t((128 + rssi) / 1.28f);
116 ESP_LOGD(TAG, "WiFi signal is: %ddBm => %d%%", rssi, wifi_status_data[3]);
117 } else {
118 ESP_LOGD(TAG, "WiFi is not connected");
119 wifi_status_data[1] = 1;
120 wifi_status_data[3] = 0;
121 }
122 return haier_protocol::HaierMessage(haier_protocol::FrameType::REPORT_NETWORK_STATUS, wifi_status_data,
123 sizeof(wifi_status_data));
124}
125#endif
126
128 HaierBaseSettings settings{this->get_health_mode(), this->get_display_state()};
129 if (!this->base_rtc_.save(&settings)) {
130 ESP_LOGW(TAG, "Failed to save settings");
131 }
132}
133
137
139 if (state != this->get_display_state()) {
141 this->force_send_control_ = true;
142 this->save_settings();
143 }
144}
145
149
151 if (state != this->get_health_mode()) {
153 this->force_send_control_ = true;
154 this->save_settings();
155 }
156}
157
159 this->action_request_ =
160 PendingAction({ActionRequest::TURN_POWER_ON, esphome::optional<haier_protocol::HaierMessage>()});
161}
162
164 this->action_request_ =
165 PendingAction({ActionRequest::TURN_POWER_OFF, esphome::optional<haier_protocol::HaierMessage>()});
166}
167
169 this->action_request_ =
170 PendingAction({ActionRequest::TOGGLE_POWER, esphome::optional<haier_protocol::HaierMessage>()});
171}
172
178
179void HaierClimateBase::set_answer_timeout(uint32_t timeout) { this->haier_protocol_.set_answer_timeout(timeout); }
180
186
192
196
197haier_protocol::HandlerError HaierClimateBase::answer_preprocess_(
198 haier_protocol::FrameType request_message_type, haier_protocol::FrameType expected_request_message_type,
199 haier_protocol::FrameType answer_message_type, haier_protocol::FrameType expected_answer_message_type,
200 ProtocolPhases expected_phase) {
201 haier_protocol::HandlerError result = haier_protocol::HandlerError::HANDLER_OK;
202 if ((expected_request_message_type != haier_protocol::FrameType::UNKNOWN_FRAME_TYPE) &&
203 (request_message_type != expected_request_message_type))
204 result = haier_protocol::HandlerError::UNSUPPORTED_MESSAGE;
205 if ((expected_answer_message_type != haier_protocol::FrameType::UNKNOWN_FRAME_TYPE) &&
206 (answer_message_type != expected_answer_message_type))
207 result = haier_protocol::HandlerError::UNSUPPORTED_MESSAGE;
208 if (!this->haier_protocol_.is_waiting_for_answer() ||
209 ((expected_phase != ProtocolPhases::UNKNOWN) && (expected_phase != this->protocol_phase_)))
210 result = haier_protocol::HandlerError::UNEXPECTED_MESSAGE;
211 if (answer_message_type == haier_protocol::FrameType::INVALID)
212 result = haier_protocol::HandlerError::INVALID_ANSWER;
213 return result;
214}
215
217 haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data,
218 size_t data_size) {
219 haier_protocol::HandlerError result =
220 this->answer_preprocess_(request_type, haier_protocol::FrameType::REPORT_NETWORK_STATUS, message_type,
221 haier_protocol::FrameType::CONFIRM, ProtocolPhases::SENDING_SIGNAL_LEVEL);
223 return result;
224}
225
226haier_protocol::HandlerError HaierClimateBase::timeout_default_handler_(haier_protocol::FrameType request_type) {
227 ESP_LOGW(TAG, "Answer timeout for command %02X, phase %s", (uint8_t) request_type,
231 } else {
233 }
234 return haier_protocol::HandlerError::HANDLER_OK;
235}
236
238 // Set timestamp here to give AC time to boot
239 this->last_request_timestamp_ = std::chrono::steady_clock::now();
241 this->haier_protocol_.set_default_timeout_handler(
242 [this](haier_protocol::FrameType type) { return this->timeout_default_handler_(type); });
243 this->set_handlers();
244 this->initialization();
245}
246
248 LOG_CLIMATE("", "Haier Climate", this);
249 ESP_LOGCONFIG(TAG, " Device communication status: %s",
250 this->valid_connection() ? LOG_STR_LITERAL("established") : LOG_STR_LITERAL("none"));
251}
252
254 std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
255 if ((std::chrono::duration_cast<std::chrono::milliseconds>(now - this->last_valid_status_timestamp_).count() >
256 COMMUNICATION_TIMEOUT_MS) ||
257 (this->reset_protocol_request_ && (!this->haier_protocol_.is_waiting_for_answer()))) {
260 // No status too long, reseting protocol
261 // No need to reset protocol if we didn't pass initialization phase
262 if (this->reset_protocol_request_) {
263 this->reset_protocol_request_ = false;
264 ESP_LOGW(TAG, "Protocol reset requested");
265 } else {
266 ESP_LOGW(TAG, "Communication timeout, reseting protocol");
267 }
269 return;
270 }
271 };
272 if ((!this->haier_protocol_.is_waiting_for_answer()) &&
273 ((this->protocol_phase_ == ProtocolPhases::IDLE) ||
274 (this->protocol_phase_ == ProtocolPhases::SENDING_STATUS_REQUEST) ||
275 (this->protocol_phase_ == ProtocolPhases::SENDING_UPDATE_SIGNAL_REQUEST) ||
276 (this->protocol_phase_ == ProtocolPhases::SENDING_SIGNAL_LEVEL))) {
277 // If control message or action is pending we should send it ASAP unless we are in initialisation
278 // procedure or waiting for an answer
279 if (this->action_request_.has_value() && this->prepare_pending_action()) {
281 } else if (this->next_hvac_settings_.valid || this->force_send_control_) {
282 ESP_LOGV(TAG, "Control packet is pending");
284 if (this->next_hvac_settings_.valid) {
287 } else {
289 }
290 }
291 }
292 this->process_phase(now);
293 this->haier_protocol_.loop();
294#ifdef USE_SWITCH
295 if ((this->display_switch_ != nullptr) && (this->display_switch_->state != this->get_display_state())) {
297 }
298 if ((this->health_mode_switch_ != nullptr) && (this->health_mode_switch_->state != this->get_health_mode())) {
300 }
301#endif // USE_SWITCH
302}
303
305 this->force_send_control_ = false;
308 if (this->next_hvac_settings_.valid)
310 this->mode = CLIMATE_MODE_OFF;
311 this->current_temperature = NAN;
312 this->target_temperature = NAN;
313 this->fan_mode.reset();
314 this->preset.reset();
315 this->publish_state();
317}
318
320 if (this->action_request_.has_value()) {
321 PendingAction &request = this->action_request_.value();
322 switch (request.action) {
324 return true;
326 request.message = this->get_power_message(true);
327 return true;
329 request.message = this->get_power_message(false);
330 return true;
332 request.message = this->get_power_message(this->mode == ClimateMode::CLIMATE_MODE_OFF);
333 return true;
334 default:
335 ESP_LOGW(TAG, "Unsupported action: %d", (uint8_t) request.action);
336 this->action_request_.reset();
337 return false;
338 }
339 } else {
340 return false;
341 }
342}
343
345
347 constexpr uint32_t restore_settings_version = 0xA77D21EF;
348 this->base_rtc_ = this->make_entity_preference<HaierBaseSettings>(restore_settings_version);
349 HaierBaseSettings recovered;
350 if (!this->base_rtc_.load(&recovered)) {
351 recovered = {false, true};
352 }
355#ifdef USE_SWITCH
356 if (this->display_switch_ != nullptr) {
358 }
359 if (this->health_mode_switch_ != nullptr) {
361 }
362#endif
363}
364
366 ESP_LOGD("Control", "Control call");
367 if (!this->valid_connection()) {
368 ESP_LOGW(TAG, "Can't send control packet, first poll answer not received");
369 return; // cancel the control, we cant do it without a poll answer.
370 }
371 if (this->current_hvac_settings_.valid) {
372 ESP_LOGW(TAG, "New settings come faster then processed!");
373 }
374 {
375 if (call.get_mode().has_value())
376 this->next_hvac_settings_.mode = call.get_mode();
377 if (call.get_fan_mode().has_value())
379 if (call.get_swing_mode().has_value())
381 if (call.get_target_temperature().has_value())
383 if (call.get_preset().has_value())
385 this->next_hvac_settings_.valid = true;
386 }
387}
388
389#ifdef USE_SWITCH
391 this->display_switch_ = sw;
392 if ((this->display_switch_ != nullptr) && (this->valid_connection())) {
394 }
395}
396
398 this->health_mode_switch_ = sw;
399 if ((this->health_mode_switch_ != nullptr) && (this->valid_connection())) {
401 }
402}
403#endif
404
406 this->valid = false;
407 this->mode.reset();
408 this->fan_mode.reset();
409 this->swing_mode.reset();
410 this->target_temperature.reset();
411 this->preset.reset();
412}
413
414void HaierClimateBase::send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_repeats,
415 std::chrono::milliseconds interval) {
416 this->haier_protocol_.send_message(command, use_crc, num_repeats, interval);
417 this->last_request_timestamp_ = std::chrono::steady_clock::now();
418}
419
420} // namespace esphome::haier
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
constexpr bool empty() const
Check if the set is empty.
This class is used to encode all control actions on a climate device.
Definition climate.h:31
const optional< ClimateSwingMode > & get_swing_mode() const
Definition climate.cpp:317
const optional< float > & get_target_temperature() const
Definition climate.cpp:310
const optional< ClimatePreset > & get_preset() const
Definition climate.cpp:318
const optional< ClimateFanMode > & get_fan_mode() const
Definition climate.cpp:316
const optional< ClimateMode > & get_mode() const
Definition climate.cpp:315
ClimateMode mode
The active mode of the climate device.
Definition climate.h:317
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:311
float target_temperature
The target temperature of the climate device.
Definition climate.h:298
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:291
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:432
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:314
void add_feature_flags(uint32_t feature_flags)
void add_supported_preset(ClimatePreset preset)
void set_supported_presets(ClimatePresetMask presets)
void set_supported_swing_modes(ClimateSwingModeMask modes)
void set_supported_modes(ClimateModeMask modes)
void add_supported_mode(ClimateMode mode)
void set_supported_fan_modes(ClimateFanModeMask modes)
void add_supported_swing_mode(ClimateSwingMode mode)
virtual haier_protocol::HaierMessage get_power_message(bool state)=0
void set_supported_modes(esphome::climate::ClimateModeMask modes)
esphome::climate::ClimateTraits traits_
Definition haier_base.h:167
ESPPreferenceObject base_rtc_
Definition haier_base.h:176
void set_supported_presets(esphome::climate::ClimatePresetMask presets)
bool is_message_interval_exceeded_(std::chrono::steady_clock::time_point now)
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)
esphome::climate::ClimateTraits traits() override
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
std::chrono::steady_clock::time_point last_request_timestamp_
Definition haier_base.h:171
const char * phase_to_string_(ProtocolPhases phase)
void send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_repeats=0, std::chrono::milliseconds interval=std::chrono::milliseconds::zero())
haier_protocol::HandlerError report_network_status_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data, size_t data_size)
void control(const esphome::climate::ClimateCall &call) override
virtual void process_phase(std::chrono::steady_clock::time_point now)=0
void set_supported_swing_modes(esphome::climate::ClimateSwingModeMask modes)
void set_health_mode_switch(switch_::Switch *sw)
std::chrono::steady_clock::time_point last_valid_status_timestamp_
Definition haier_base.h:172
haier_protocol::HaierMessage get_wifi_signal_message_()
haier_protocol::HandlerError timeout_default_handler_(haier_protocol::FrameType request_type)
bool is_control_message_interval_exceeded_(std::chrono::steady_clock::time_point now)
switch_::Switch * health_mode_switch_
Definition haier_base.h:41
esphome::optional< PendingAction > action_request_
Definition haier_base.h:157
void set_answer_timeout(uint32_t timeout)
std::chrono::steady_clock::time_point last_status_request_
Definition haier_base.h:173
void set_display_switch(switch_::Switch *sw)
virtual void set_phase(ProtocolPhases phase)
void send_custom_command(const haier_protocol::HaierMessage &message)
switch_::Switch * display_switch_
Definition haier_base.h:40
Base class for all switches.
Definition switch.h:43
bool state
The current reported state of the binary sensor.
Definition switch.h:60
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:70
const LogString * message
Definition component.cpp:35
uint8_t type
bool state
Definition fan.h:2
unsigned count
Definition gif.cpp:51
@ CLIMATE_SUPPORTS_CURRENT_TEMPERATURE
@ CLIMATE_PRESET_NONE
No preset is active.
@ 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.
@ CLIMATE_MODE_DRY
The climate device is set to dry/humidity mode.
@ CLIMATE_MODE_FAN_ONLY
The climate device only has the fan enabled, no heating or cooling is taking place.
@ CLIMATE_MODE_HEAT
The climate device is set to heat to reach the target temperature.
@ CLIMATE_MODE_COOL
The climate device is set to cool to reach the target temperature.
@ CLIMATE_MODE_HEAT_COOL
The climate device is set to heat/cool to reach the target temperature.
@ CLIMATE_MODE_OFF
The climate device is off.
@ CLIMATE_FAN_MEDIUM
The fan mode is set to Medium.
@ CLIMATE_FAN_AUTO
The fan mode is set to Auto.
@ CLIMATE_FAN_LOW
The fan mode is set to Low.
@ CLIMATE_FAN_HIGH
The fan mode is set to High.
constexpr size_t STATUS_REQUEST_INTERVAL_MS
bool check_timeout(std::chrono::steady_clock::time_point now, std::chrono::steady_clock::time_point tpoint, size_t timeout)
constexpr size_t PROTOCOL_INITIALIZATION_INTERVAL
constexpr size_t DEFAULT_MESSAGES_INTERVAL_MS
constexpr size_t COMMUNICATION_TIMEOUT_MS
ESPHOME_LOG_TAG(TAG, "haier.climate")
constexpr size_t CONTROL_MESSAGES_INTERVAL_MS
WiFiComponent * global_wifi_component
static void uint32_t
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
esphome::optional< haier_protocol::HaierMessage > message
Definition haier_base.h:147