ESPHome 2026.5.1
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
15static const char *const 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
193void HaierClimateBase::set_send_wifi(bool send_wifi) { this->send_wifi_signal_ = send_wifi; }
194
198
199haier_protocol::HandlerError HaierClimateBase::answer_preprocess_(
200 haier_protocol::FrameType request_message_type, haier_protocol::FrameType expected_request_message_type,
201 haier_protocol::FrameType answer_message_type, haier_protocol::FrameType expected_answer_message_type,
202 ProtocolPhases expected_phase) {
203 haier_protocol::HandlerError result = haier_protocol::HandlerError::HANDLER_OK;
204 if ((expected_request_message_type != haier_protocol::FrameType::UNKNOWN_FRAME_TYPE) &&
205 (request_message_type != expected_request_message_type))
206 result = haier_protocol::HandlerError::UNSUPPORTED_MESSAGE;
207 if ((expected_answer_message_type != haier_protocol::FrameType::UNKNOWN_FRAME_TYPE) &&
208 (answer_message_type != expected_answer_message_type))
209 result = haier_protocol::HandlerError::UNSUPPORTED_MESSAGE;
210 if (!this->haier_protocol_.is_waiting_for_answer() ||
211 ((expected_phase != ProtocolPhases::UNKNOWN) && (expected_phase != this->protocol_phase_)))
212 result = haier_protocol::HandlerError::UNEXPECTED_MESSAGE;
213 if (answer_message_type == haier_protocol::FrameType::INVALID)
214 result = haier_protocol::HandlerError::INVALID_ANSWER;
215 return result;
216}
217
219 haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data,
220 size_t data_size) {
221 haier_protocol::HandlerError result =
222 this->answer_preprocess_(request_type, haier_protocol::FrameType::REPORT_NETWORK_STATUS, message_type,
223 haier_protocol::FrameType::CONFIRM, ProtocolPhases::SENDING_SIGNAL_LEVEL);
225 return result;
226}
227
228haier_protocol::HandlerError HaierClimateBase::timeout_default_handler_(haier_protocol::FrameType request_type) {
229 ESP_LOGW(TAG, "Answer timeout for command %02X, phase %s", (uint8_t) request_type,
233 } else {
235 }
236 return haier_protocol::HandlerError::HANDLER_OK;
237}
238
240 // Set timestamp here to give AC time to boot
241 this->last_request_timestamp_ = std::chrono::steady_clock::now();
243 this->haier_protocol_.set_default_timeout_handler(
244 [this](haier_protocol::FrameType type) { return this->timeout_default_handler_(type); });
245 this->set_handlers();
246 this->initialization();
247}
248
250 LOG_CLIMATE("", "Haier Climate", this);
251 ESP_LOGCONFIG(TAG, " Device communication status: %s", this->valid_connection() ? "established" : "none");
252}
253
255 std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
256 if ((std::chrono::duration_cast<std::chrono::milliseconds>(now - this->last_valid_status_timestamp_).count() >
258 (this->reset_protocol_request_ && (!this->haier_protocol_.is_waiting_for_answer()))) {
261 // No status too long, reseting protocol
262 // No need to reset protocol if we didn't pass initialization phase
263 if (this->reset_protocol_request_) {
264 this->reset_protocol_request_ = false;
265 ESP_LOGW(TAG, "Protocol reset requested");
266 } else {
267 ESP_LOGW(TAG, "Communication timeout, reseting protocol");
268 }
270 return;
271 }
272 };
273 if ((!this->haier_protocol_.is_waiting_for_answer()) &&
274 ((this->protocol_phase_ == ProtocolPhases::IDLE) ||
275 (this->protocol_phase_ == ProtocolPhases::SENDING_STATUS_REQUEST) ||
276 (this->protocol_phase_ == ProtocolPhases::SENDING_UPDATE_SIGNAL_REQUEST) ||
277 (this->protocol_phase_ == ProtocolPhases::SENDING_SIGNAL_LEVEL))) {
278 // If control message or action is pending we should send it ASAP unless we are in initialisation
279 // procedure or waiting for an answer
280 if (this->action_request_.has_value() && this->prepare_pending_action()) {
282 } else if (this->next_hvac_settings_.valid || this->force_send_control_) {
283 ESP_LOGV(TAG, "Control packet is pending");
285 if (this->next_hvac_settings_.valid) {
288 } else {
290 }
291 }
292 }
293 this->process_phase(now);
294 this->haier_protocol_.loop();
295#ifdef USE_SWITCH
296 if ((this->display_switch_ != nullptr) && (this->display_switch_->state != this->get_display_state())) {
298 }
299 if ((this->health_mode_switch_ != nullptr) && (this->health_mode_switch_->state != this->get_health_mode())) {
301 }
302#endif // USE_SWITCH
303}
304
306 this->force_send_control_ = false;
309 if (this->next_hvac_settings_.valid)
311 this->mode = CLIMATE_MODE_OFF;
312 this->current_temperature = NAN;
313 this->target_temperature = NAN;
314 this->fan_mode.reset();
315 this->preset.reset();
316 this->publish_state();
318}
319
321 if (this->action_request_.has_value()) {
322 switch (this->action_request_.value().action) {
324 return true;
326 this->action_request_.value().message = this->get_power_message(true);
327 return true;
329 this->action_request_.value().message = this->get_power_message(false);
330 return true;
332 this->action_request_.value().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) this->action_request_.value().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:34
const optional< ClimateSwingMode > & get_swing_mode() const
Definition climate.cpp:314
const optional< float > & get_target_temperature() const
Definition climate.cpp:307
const optional< ClimatePreset > & get_preset() const
Definition climate.cpp:315
const optional< ClimateFanMode > & get_fan_mode() const
Definition climate.cpp:313
const optional< ClimateMode > & get_mode() const
Definition climate.cpp:312
ClimateMode mode
The active mode of the climate device.
Definition climate.h:293
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:287
float target_temperature
The target temperature of the climate device.
Definition climate.h:274
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:267
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:437
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:290
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)
void set_send_wifi(bool send_wifi)
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:38
bool state
The current reported state of the binary sensor.
Definition switch.h:55
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:56
const char * message
Definition component.cpp:35
uint16_t type
bool state
Definition fan.h:2
@ 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
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