ESPHome 2026.11.0b6
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"
10
11using namespace esphome::climate;
12using namespace esphome::uart;
13
14namespace esphome::haier {
15
16ESPHOME_LOG_TAG(TAG, "haier.climate");
17constexpr size_t COMMUNICATION_TIMEOUT_MS = 60000;
18constexpr size_t STATUS_REQUEST_INTERVAL_MS = 5000;
19constexpr size_t PROTOCOL_INITIALIZATION_INTERVAL = 10000;
20constexpr size_t DEFAULT_MESSAGES_INTERVAL_MS = 2000;
21constexpr size_t CONTROL_MESSAGES_INTERVAL_MS = 400;
22
23PROGMEM_STRING_TABLE(ProtocolPhaseStrings, "SENDING_INIT_1", "SENDING_INIT_2", "SENDING_FIRST_STATUS_REQUEST",
24 "SENDING_FIRST_ALARM_STATUS_REQUEST", "IDLE", "SENDING_STATUS_REQUEST",
25 "SENDING_UPDATE_SIGNAL_REQUEST", "SENDING_SIGNAL_LEVEL", "SENDING_CONTROL",
26 "SENDING_ACTION_COMMAND", "SENDING_ALARM_STATUS_REQUEST", "UNKNOWN");
27
29 static_assert(ProtocolPhaseStrings::COUNT == ((int) ProtocolPhases::NUM_PROTOCOL_PHASES) + 1,
30 "ProtocolPhaseStrings must match the enum ProtocolPhases, with UNKNOWN last");
31 // UNKNOWN (-1) wraps past the table and reads as the last entry, "UNKNOWN"
32 return ProtocolPhaseStrings::get_log_str(static_cast<uint8_t>(phase), ProtocolPhaseStrings::LAST_INDEX);
33}
34
35bool check_timeout(std::chrono::steady_clock::time_point now, std::chrono::steady_clock::time_point tpoint,
36 size_t timeout) {
37 return std::chrono::duration_cast<std::chrono::milliseconds>(now - tpoint).count() > timeout;
38}
39
58
60
62 if (this->protocol_phase_ != phase) {
63 ESP_LOGV(TAG, "Phase transition: %s => %s", LOG_STR_ARG(phase_to_string_(this->protocol_phase_)),
64 LOG_STR_ARG(phase_to_string_(phase)));
65 this->protocol_phase_ = phase;
66 }
67}
68
73
82
83bool HaierClimateBase::is_message_interval_exceeded_(std::chrono::steady_clock::time_point now) {
84 return check_timeout(now, this->last_request_timestamp_, DEFAULT_MESSAGES_INTERVAL_MS);
85}
86
87bool HaierClimateBase::is_status_request_interval_exceeded_(std::chrono::steady_clock::time_point now) {
88 return check_timeout(now, this->last_status_request_, STATUS_REQUEST_INTERVAL_MS);
89}
90
91bool HaierClimateBase::is_control_message_interval_exceeded_(std::chrono::steady_clock::time_point now) {
92 return check_timeout(now, this->last_request_timestamp_, CONTROL_MESSAGES_INTERVAL_MS);
93}
94
95bool HaierClimateBase::is_protocol_initialisation_interval_exceeded_(std::chrono::steady_clock::time_point now) {
96 return check_timeout(now, this->last_request_timestamp_, PROTOCOL_INITIALIZATION_INTERVAL);
97}
98
99#ifdef USE_WIFI
100haier_protocol::HaierMessage HaierClimateBase::get_wifi_signal_message_() {
101 static uint8_t wifi_status_data[4] = {0x00, 0x00, 0x00, 0x00};
102 if (wifi::global_wifi_component->is_connected()) {
103 wifi_status_data[1] = 0;
104 int8_t rssi = wifi::global_wifi_component->wifi_rssi();
105 wifi_status_data[3] = uint8_t((128 + rssi) / 1.28f);
106 ESP_LOGD(TAG, "WiFi signal is: %ddBm => %d%%", rssi, wifi_status_data[3]);
107 } else {
108 ESP_LOGD(TAG, "WiFi is not connected");
109 wifi_status_data[1] = 1;
110 wifi_status_data[3] = 0;
111 }
112 return haier_protocol::HaierMessage(haier_protocol::FrameType::REPORT_NETWORK_STATUS, wifi_status_data,
113 sizeof(wifi_status_data));
114}
115#endif
116
118 HaierBaseSettings settings{this->get_health_mode(), this->get_display_state()};
119 if (!this->base_rtc_.save(&settings)) {
120 ESP_LOGW(TAG, "Failed to save settings");
121 }
122}
123
127
129 if (state != this->get_display_state()) {
131 this->force_send_control_ = true;
132 this->save_settings();
133 }
134}
135
139
141 if (state != this->get_health_mode()) {
143 this->force_send_control_ = true;
144 this->save_settings();
145 }
146}
147
149 this->action_request_ =
150 PendingAction({ActionRequest::TURN_POWER_ON, esphome::optional<haier_protocol::HaierMessage>()});
151}
152
154 this->action_request_ =
155 PendingAction({ActionRequest::TURN_POWER_OFF, esphome::optional<haier_protocol::HaierMessage>()});
156}
157
159 this->action_request_ =
160 PendingAction({ActionRequest::TOGGLE_POWER, esphome::optional<haier_protocol::HaierMessage>()});
161}
162
168
169void HaierClimateBase::set_answer_timeout(uint32_t timeout) { this->haier_protocol_.set_answer_timeout(timeout); }
170
176
182
186
187haier_protocol::HandlerError HaierClimateBase::answer_preprocess_(
188 haier_protocol::FrameType request_message_type, haier_protocol::FrameType expected_request_message_type,
189 haier_protocol::FrameType answer_message_type, haier_protocol::FrameType expected_answer_message_type,
190 ProtocolPhases expected_phase) {
191 haier_protocol::HandlerError result = haier_protocol::HandlerError::HANDLER_OK;
192 if ((expected_request_message_type != haier_protocol::FrameType::UNKNOWN_FRAME_TYPE) &&
193 (request_message_type != expected_request_message_type))
194 result = haier_protocol::HandlerError::UNSUPPORTED_MESSAGE;
195 if ((expected_answer_message_type != haier_protocol::FrameType::UNKNOWN_FRAME_TYPE) &&
196 (answer_message_type != expected_answer_message_type))
197 result = haier_protocol::HandlerError::UNSUPPORTED_MESSAGE;
198 if (!this->haier_protocol_.is_waiting_for_answer() ||
199 ((expected_phase != ProtocolPhases::UNKNOWN) && (expected_phase != this->protocol_phase_)))
200 result = haier_protocol::HandlerError::UNEXPECTED_MESSAGE;
201 if (answer_message_type == haier_protocol::FrameType::INVALID)
202 result = haier_protocol::HandlerError::INVALID_ANSWER;
203 return result;
204}
205
207 haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data,
208 size_t data_size) {
209 haier_protocol::HandlerError result =
210 this->answer_preprocess_(request_type, haier_protocol::FrameType::REPORT_NETWORK_STATUS, message_type,
211 haier_protocol::FrameType::CONFIRM, ProtocolPhases::SENDING_SIGNAL_LEVEL);
213 return result;
214}
215
216haier_protocol::HandlerError HaierClimateBase::timeout_default_handler_(haier_protocol::FrameType request_type) {
217 ESP_LOGW(TAG, "Answer timeout for command %02X, phase %s", (uint8_t) request_type,
218 LOG_STR_ARG(phase_to_string_(this->protocol_phase_)));
221 } else {
223 }
224 return haier_protocol::HandlerError::HANDLER_OK;
225}
226
228 // Set timestamp here to give AC time to boot
229 this->last_request_timestamp_ = std::chrono::steady_clock::now();
231 this->haier_protocol_.set_default_timeout_handler(
232 [this](haier_protocol::FrameType type) { return this->timeout_default_handler_(type); });
233 this->set_handlers();
234 this->initialization();
235}
236
238 LOG_CLIMATE("", "Haier Climate", this);
239 ESP_LOGCONFIG(TAG, " Device communication status: %s",
240 this->valid_connection() ? LOG_STR_LITERAL("established") : LOG_STR_LITERAL("none"));
241}
242
244 std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
245 if ((std::chrono::duration_cast<std::chrono::milliseconds>(now - this->last_valid_status_timestamp_).count() >
246 COMMUNICATION_TIMEOUT_MS) ||
247 (this->reset_protocol_request_ && (!this->haier_protocol_.is_waiting_for_answer()))) {
250 // No status too long, reseting protocol
251 // No need to reset protocol if we didn't pass initialization phase
252 if (this->reset_protocol_request_) {
253 this->reset_protocol_request_ = false;
254 ESP_LOGW(TAG, "Protocol reset requested");
255 } else {
256 ESP_LOGW(TAG, "Communication timeout, reseting protocol");
257 }
259 return;
260 }
261 };
262 if ((!this->haier_protocol_.is_waiting_for_answer()) &&
263 ((this->protocol_phase_ == ProtocolPhases::IDLE) ||
264 (this->protocol_phase_ == ProtocolPhases::SENDING_STATUS_REQUEST) ||
265 (this->protocol_phase_ == ProtocolPhases::SENDING_UPDATE_SIGNAL_REQUEST) ||
266 (this->protocol_phase_ == ProtocolPhases::SENDING_SIGNAL_LEVEL))) {
267 // If control message or action is pending we should send it ASAP unless we are in initialisation
268 // procedure or waiting for an answer
269 if (this->action_request_.has_value() && this->prepare_pending_action()) {
271 } else if (this->next_hvac_settings_.valid || this->force_send_control_) {
272 ESP_LOGV(TAG, "Control packet is pending");
274 if (this->next_hvac_settings_.valid) {
277 } else {
279 }
280 }
281 }
282 this->process_phase(now);
283 this->haier_protocol_.loop();
284#ifdef USE_SWITCH
285 if ((this->display_switch_ != nullptr) && (this->display_switch_->state != this->get_display_state())) {
287 }
288 if ((this->health_mode_switch_ != nullptr) && (this->health_mode_switch_->state != this->get_health_mode())) {
290 }
291#endif // USE_SWITCH
292}
293
295 this->force_send_control_ = false;
298 if (this->next_hvac_settings_.valid)
300 this->mode = CLIMATE_MODE_OFF;
301 this->current_temperature = NAN;
302 this->target_temperature = NAN;
303 this->fan_mode.reset();
304 this->preset.reset();
305 this->publish_state();
307}
308
310 if (this->action_request_.has_value()) {
311 PendingAction &request = this->action_request_.value();
312 switch (request.action) {
314 return true;
316 request.message = this->get_power_message(true);
317 return true;
319 request.message = this->get_power_message(false);
320 return true;
322 request.message = this->get_power_message(this->mode == ClimateMode::CLIMATE_MODE_OFF);
323 return true;
324 default:
325 ESP_LOGW(TAG, "Unsupported action: %d", (uint8_t) request.action);
326 this->action_request_.reset();
327 return false;
328 }
329 } else {
330 return false;
331 }
332}
333
335
337 constexpr uint32_t restore_settings_version = 0xA77D21EF;
338 this->base_rtc_ = this->make_entity_preference<HaierBaseSettings>(restore_settings_version);
339 HaierBaseSettings recovered;
340 if (!this->base_rtc_.load(&recovered)) {
341 recovered = {false, true};
342 }
345#ifdef USE_SWITCH
346 PUBLISH_STATE(this->display_switch_, this->get_display_state());
347 PUBLISH_STATE(this->health_mode_switch_, this->get_health_mode());
348#endif
349}
350
352 ESP_LOGD("Control", "Control call");
353 if (!this->valid_connection()) {
354 ESP_LOGW(TAG, "Can't send control packet, first poll answer not received");
355 return; // cancel the control, we cant do it without a poll answer.
356 }
357 if (this->current_hvac_settings_.valid) {
358 ESP_LOGW(TAG, "New settings come faster then processed!");
359 }
360 {
361 if (call.get_mode().has_value())
362 this->next_hvac_settings_.mode = call.get_mode();
363 if (call.get_fan_mode().has_value())
365 if (call.get_swing_mode().has_value())
367 if (call.get_target_temperature().has_value())
369 if (call.get_preset().has_value())
371 this->next_hvac_settings_.valid = true;
372 }
373}
374
375#ifdef USE_SWITCH
377 this->display_switch_ = sw;
378 if ((this->display_switch_ != nullptr) && (this->valid_connection())) {
380 }
381}
382
384 this->health_mode_switch_ = sw;
385 if ((this->health_mode_switch_ != nullptr) && (this->valid_connection())) {
387 }
388}
389#endif
390
392 this->valid = false;
393 this->mode.reset();
394 this->fan_mode.reset();
395 this->swing_mode.reset();
396 this->target_temperature.reset();
397 this->preset.reset();
398}
399
400void HaierClimateBase::send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_repeats,
401 std::chrono::milliseconds interval) {
402 this->haier_protocol_.send_message(command, use_crc, num_repeats, interval);
403 this->last_request_timestamp_ = std::chrono::steady_clock::now();
404}
405
406} // 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)
const LogString * phase_to_string_(ProtocolPhases phase)
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
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:75
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
PROGMEM_STRING_TABLE(ProtocolPhaseStrings, "SENDING_INIT_1", "SENDING_INIT_2", "SENDING_FIRST_STATUS_REQUEST", "SENDING_FIRST_ALARM_STATUS_REQUEST", "IDLE", "SENDING_STATUS_REQUEST", "SENDING_UPDATE_SIGNAL_REQUEST", "SENDING_SIGNAL_LEVEL", "SENDING_CONTROL", "SENDING_ACTION_COMMAND", "SENDING_ALARM_STATUS_REQUEST", "UNKNOWN")
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