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