ESPHome  2024.11.0
hon_climate.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <chrono>
4 #ifdef USE_SENSOR
6 #endif
7 #ifdef USE_BINARY_SENSOR
9 #endif
10 #ifdef USE_TEXT_SENSOR
12 #endif
13 #ifdef USE_SWITCH
15 #endif
17 #include "haier_base.h"
18 #include "hon_packet.h"
19 
20 namespace esphome {
21 namespace haier {
22 
23 enum class CleaningState : uint8_t {
24  NO_CLEANING = 0,
25  SELF_CLEAN = 1,
26  STERI_CLEAN = 2,
27 };
28 
30 
31 struct HonSettings {
36 };
37 
38 class HonClimate : public HaierClimateBase {
39 #ifdef USE_SENSOR
40  public:
41  enum class SubSensorType {
42  // Used data based sensors
43  OUTDOOR_TEMPERATURE = 0,
44  HUMIDITY,
45  // Big data based sensors
46  INDOOR_COIL_TEMPERATURE,
47  OUTDOOR_COIL_TEMPERATURE,
48  OUTDOOR_DEFROST_TEMPERATURE,
49  OUTDOOR_IN_AIR_TEMPERATURE,
50  OUTDOOR_OUT_AIR_TEMPERATURE,
51  POWER,
52  COMPRESSOR_FREQUENCY,
53  COMPRESSOR_CURRENT,
54  EXPANSION_VALVE_OPEN_DEGREE,
55  SUB_SENSOR_TYPE_COUNT,
56  BIG_DATA_FRAME_SUB_SENSORS = INDOOR_COIL_TEMPERATURE,
57  };
58  void set_sub_sensor(SubSensorType type, sensor::Sensor *sens);
59 
60  protected:
61  void update_sub_sensor_(SubSensorType type, float value);
62  sensor::Sensor *sub_sensors_[(size_t) SubSensorType::SUB_SENSOR_TYPE_COUNT]{nullptr};
63 #endif
64 #ifdef USE_BINARY_SENSOR
65  public:
66  enum class SubBinarySensorType {
67  OUTDOOR_FAN_STATUS = 0,
68  DEFROST_STATUS,
69  COMPRESSOR_STATUS,
70  INDOOR_FAN_STATUS,
71  FOUR_WAY_VALVE_STATUS,
72  INDOOR_ELECTRIC_HEATING_STATUS,
73  SUB_BINARY_SENSOR_TYPE_COUNT,
74  };
75  void set_sub_binary_sensor(SubBinarySensorType type, binary_sensor::BinarySensor *sens);
76 
77  protected:
78  void update_sub_binary_sensor_(SubBinarySensorType type, uint8_t value);
79  binary_sensor::BinarySensor *sub_binary_sensors_[(size_t) SubBinarySensorType::SUB_BINARY_SENSOR_TYPE_COUNT]{nullptr};
80 #endif
81 #ifdef USE_TEXT_SENSOR
82  public:
83  enum class SubTextSensorType {
84  CLEANING_STATUS = 0,
85  PROTOCOL_VERSION,
86  APPLIANCE_NAME,
87  SUB_TEXT_SENSOR_TYPE_COUNT,
88  };
89  void set_sub_text_sensor(SubTextSensorType type, text_sensor::TextSensor *sens);
90 
91  protected:
92  void update_sub_text_sensor_(SubTextSensorType type, const std::string &value);
93  text_sensor::TextSensor *sub_text_sensors_[(size_t) SubTextSensorType::SUB_TEXT_SENSOR_TYPE_COUNT]{nullptr};
94 #endif
95 #ifdef USE_SWITCH
96  public:
97  void set_beeper_switch(switch_::Switch *sw);
98  void set_quiet_mode_switch(switch_::Switch *sw);
99 
100  protected:
101  switch_::Switch *beeper_switch_{nullptr};
102  switch_::Switch *quiet_mode_switch_{nullptr};
103 #endif
104  public:
105  HonClimate();
106  HonClimate(const HonClimate &) = delete;
107  HonClimate &operator=(const HonClimate &) = delete;
108  ~HonClimate();
109  void dump_config() override;
110  void set_beeper_state(bool state);
111  bool get_beeper_state() const;
112  void set_quiet_mode_state(bool state);
113  bool get_quiet_mode_state() const;
114  esphome::optional<hon_protocol::VerticalSwingMode> get_vertical_airflow() const;
115  void set_vertical_airflow(hon_protocol::VerticalSwingMode direction);
116  esphome::optional<hon_protocol::HorizontalSwingMode> get_horizontal_airflow() const;
117  void set_horizontal_airflow(hon_protocol::HorizontalSwingMode direction);
118  std::string get_cleaning_status_text() const;
119  CleaningState get_cleaning_status() const;
120  void start_self_cleaning();
121  void start_steri_cleaning();
122  void set_extra_control_packet_bytes_size(size_t size) { this->extra_control_packet_bytes_ = size; };
123  void set_extra_sensors_packet_bytes_size(size_t size) { this->extra_sensors_packet_bytes_ = size; };
124  void set_status_message_header_size(size_t size) { this->status_message_header_size_ = size; };
125  void set_control_method(HonControlMethod method) { this->control_method_ = method; };
126  void add_alarm_start_callback(std::function<void(uint8_t, const char *)> &&callback);
127  void add_alarm_end_callback(std::function<void(uint8_t, const char *)> &&callback);
128  float get_active_alarm_count() const { return this->active_alarm_count_; }
129 
130  protected:
131  void set_handlers() override;
132  void process_phase(std::chrono::steady_clock::time_point now) override;
133  haier_protocol::HaierMessage get_control_message() override;
134  haier_protocol::HaierMessage get_power_message(bool state) override;
135  void initialization() override;
136  bool prepare_pending_action() override;
137  void process_protocol_reset() override;
138  bool should_get_big_data_();
139 
140  // Answers handlers
141  haier_protocol::HandlerError get_device_version_answer_handler_(haier_protocol::FrameType request_type,
142  haier_protocol::FrameType message_type,
143  const uint8_t *data, size_t data_size);
144  haier_protocol::HandlerError get_device_id_answer_handler_(haier_protocol::FrameType request_type,
145  haier_protocol::FrameType message_type,
146  const uint8_t *data, size_t data_size);
147  haier_protocol::HandlerError status_handler_(haier_protocol::FrameType request_type,
148  haier_protocol::FrameType message_type, const uint8_t *data,
149  size_t data_size);
150  haier_protocol::HandlerError get_management_information_answer_handler_(haier_protocol::FrameType request_type,
151  haier_protocol::FrameType message_type,
152  const uint8_t *data, size_t data_size);
153  haier_protocol::HandlerError get_alarm_status_answer_handler_(haier_protocol::FrameType request_type,
154  haier_protocol::FrameType message_type,
155  const uint8_t *data, size_t data_size);
156  haier_protocol::HandlerError alarm_status_message_handler_(haier_protocol::FrameType type, const uint8_t *buffer,
157  size_t size);
158  // Helper functions
159  haier_protocol::HandlerError process_status_message_(const uint8_t *packet, uint8_t size);
160  void process_alarm_message_(const uint8_t *packet, uint8_t size, bool check_new);
161  void fill_control_messages_queue_();
162  void clear_control_messages_queue_();
163 
164  struct HardwareInfo {
165  std::string protocol_version_;
166  std::string software_version_;
167  std::string hardware_version_;
168  std::string device_name_;
169  bool functions_[5];
170  };
171 
176  esphome::optional<HardwareInfo> hvac_hardware_info_{};
177  uint8_t active_alarms_[8];
178  int extra_control_packet_bytes_{0};
179  int extra_sensors_packet_bytes_{4};
180  int status_message_header_size_{0};
181  int real_control_packet_size_{sizeof(hon_protocol::HaierPacketControl)};
182  int real_sensors_packet_size_{sizeof(hon_protocol::HaierPacketSensors) + 4};
184  std::queue<haier_protocol::HaierMessage> control_messages_queue_;
187  float active_alarm_count_{NAN};
188  std::chrono::steady_clock::time_point last_alarm_request_;
189  int big_data_sensors_{0};
194  SwitchState quiet_mode_state_{SwitchState::OFF};
195 };
196 
197 class HaierAlarmStartTrigger : public Trigger<uint8_t, const char *> {
198  public:
199  explicit HaierAlarmStartTrigger(HonClimate *parent) {
200  parent->add_alarm_start_callback(
201  [this](uint8_t alarm_code, const char *alarm_message) { this->trigger(alarm_code, alarm_message); });
202  }
203 };
204 
205 class HaierAlarmEndTrigger : public Trigger<uint8_t, const char *> {
206  public:
207  explicit HaierAlarmEndTrigger(HonClimate *parent) {
208  parent->add_alarm_end_callback(
209  [this](uint8_t alarm_code, const char *alarm_message) { this->trigger(alarm_code, alarm_message); });
210  }
211 };
212 
213 } // namespace haier
214 } // namespace esphome
Base class for all switches.
Definition: switch.h:39
hon_protocol::HorizontalSwingMode last_horizontal_swing
Definition: hon_climate.h:33
void add_alarm_end_callback(std::function< void(uint8_t, const char *)> &&callback)
CleaningState cleaning_status_
Definition: hon_climate.h:172
HonControlMethod control_method_
Definition: hon_climate.h:183
void set_control_method(HonControlMethod method)
Definition: hon_climate.h:125
void set_status_message_header_size(size_t size)
Definition: hon_climate.h:124
FanDirection direction
Definition: fan.h:37
void add_alarm_start_callback(std::function< void(uint8_t, const char *)> &&callback)
void set_extra_sensors_packet_bytes_size(size_t size)
Definition: hon_climate.h:123
ESPPreferenceObject hon_rtc_
Definition: hon_climate.h:193
uint8_t type
void set_extra_control_packet_bytes_size(size_t size)
Definition: hon_climate.h:122
std::chrono::steady_clock::time_point last_alarm_request_
Definition: hon_climate.h:188
float get_active_alarm_count() const
Definition: hon_climate.h:128
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
HaierAlarmStartTrigger(HonClimate *parent)
Definition: hon_climate.h:199
Base-class for all sensors.
Definition: sensor.h:57
std::queue< haier_protocol::HaierMessage > control_messages_queue_
Definition: hon_climate.h:184
hon_protocol::VerticalSwingMode last_vertiacal_swing
Definition: hon_climate.h:32
HaierAlarmEndTrigger(HonClimate *parent)
Definition: hon_climate.h:207
bool state
Definition: fan.h:34