ESPHome  2024.4.1
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
11 #include "haier_base.h"
12 
13 namespace esphome {
14 namespace haier {
15 
16 enum class AirflowVerticalDirection : uint8_t {
17  HEALTH_UP = 0,
18  MAX_UP = 1,
19  UP = 2,
20  CENTER = 3,
21  DOWN = 4,
22  HEALTH_DOWN = 5,
23 };
24 
25 enum class AirflowHorizontalDirection : uint8_t {
26  MAX_LEFT = 0,
27  LEFT = 1,
28  CENTER = 2,
29  RIGHT = 3,
30  MAX_RIGHT = 4,
31 };
32 
33 enum class CleaningState : uint8_t {
34  NO_CLEANING = 0,
35  SELF_CLEAN = 1,
36  STERI_CLEAN = 2,
37 };
38 
40 
41 class HonClimate : public HaierClimateBase {
42 #ifdef USE_SENSOR
43  public:
44  enum class SubSensorType {
45  // Used data based sensors
46  OUTDOOR_TEMPERATURE = 0,
47  HUMIDITY,
48  // Big data based sensors
49  INDOOR_COIL_TEMPERATURE,
50  OUTDOOR_COIL_TEMPERATURE,
51  OUTDOOR_DEFROST_TEMPERATURE,
52  OUTDOOR_IN_AIR_TEMPERATURE,
53  OUTDOOR_OUT_AIR_TEMPERATURE,
54  POWER,
55  COMPRESSOR_FREQUENCY,
56  COMPRESSOR_CURRENT,
57  EXPANSION_VALVE_OPEN_DEGREE,
58  SUB_SENSOR_TYPE_COUNT,
59  BIG_DATA_FRAME_SUB_SENSORS = INDOOR_COIL_TEMPERATURE,
60  };
61  void set_sub_sensor(SubSensorType type, sensor::Sensor *sens);
62 
63  protected:
64  void update_sub_sensor_(SubSensorType type, float value);
65  sensor::Sensor *sub_sensors_[(size_t) SubSensorType::SUB_SENSOR_TYPE_COUNT]{nullptr};
66 #endif
67 #ifdef USE_BINARY_SENSOR
68  public:
69  enum class SubBinarySensorType {
70  OUTDOOR_FAN_STATUS = 0,
71  DEFROST_STATUS,
72  COMPRESSOR_STATUS,
73  INDOOR_FAN_STATUS,
74  FOUR_WAY_VALVE_STATUS,
75  INDOOR_ELECTRIC_HEATING_STATUS,
76  SUB_BINARY_SENSOR_TYPE_COUNT,
77  };
78  void set_sub_binary_sensor(SubBinarySensorType type, binary_sensor::BinarySensor *sens);
79 
80  protected:
81  void update_sub_binary_sensor_(SubBinarySensorType type, uint8_t value);
82  binary_sensor::BinarySensor *sub_binary_sensors_[(size_t) SubBinarySensorType::SUB_BINARY_SENSOR_TYPE_COUNT]{nullptr};
83 #endif
84  public:
85  HonClimate();
86  HonClimate(const HonClimate &) = delete;
87  HonClimate &operator=(const HonClimate &) = delete;
88  ~HonClimate();
89  void dump_config() override;
90  void set_beeper_state(bool state);
91  bool get_beeper_state() const;
92  AirflowVerticalDirection get_vertical_airflow() const;
93  void set_vertical_airflow(AirflowVerticalDirection direction);
94  AirflowHorizontalDirection get_horizontal_airflow() const;
95  void set_horizontal_airflow(AirflowHorizontalDirection direction);
96  std::string get_cleaning_status_text() const;
97  CleaningState get_cleaning_status() const;
98  void start_self_cleaning();
99  void start_steri_cleaning();
100  void set_extra_control_packet_bytes_size(size_t size) { this->extra_control_packet_bytes_ = size; };
101  void set_control_method(HonControlMethod method) { this->control_method_ = method; };
102  void add_alarm_start_callback(std::function<void(uint8_t, const char *)> &&callback);
103  void add_alarm_end_callback(std::function<void(uint8_t, const char *)> &&callback);
104  float get_active_alarm_count() const { return this->active_alarm_count_; }
105 
106  protected:
107  void set_handlers() override;
108  void process_phase(std::chrono::steady_clock::time_point now) override;
109  haier_protocol::HaierMessage get_control_message() override;
110  haier_protocol::HaierMessage get_power_message(bool state) override;
111  bool prepare_pending_action() override;
112  void process_protocol_reset() override;
113  bool should_get_big_data_();
114 
115  // Answers handlers
116  haier_protocol::HandlerError get_device_version_answer_handler_(haier_protocol::FrameType request_type,
117  haier_protocol::FrameType message_type,
118  const uint8_t *data, size_t data_size);
119  haier_protocol::HandlerError get_device_id_answer_handler_(haier_protocol::FrameType request_type,
120  haier_protocol::FrameType message_type,
121  const uint8_t *data, size_t data_size);
122  haier_protocol::HandlerError status_handler_(haier_protocol::FrameType request_type,
123  haier_protocol::FrameType message_type, const uint8_t *data,
124  size_t data_size);
125  haier_protocol::HandlerError get_management_information_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_alarm_status_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 alarm_status_message_handler_(haier_protocol::FrameType type, const uint8_t *buffer,
132  size_t size);
133  // Helper functions
134  haier_protocol::HandlerError process_status_message_(const uint8_t *packet, uint8_t size);
135  void process_alarm_message_(const uint8_t *packet, uint8_t size, bool check_new);
136  void fill_control_messages_queue_();
137  void clear_control_messages_queue_();
138 
139  struct HardwareInfo {
140  std::string protocol_version_;
141  std::string software_version_;
142  std::string hardware_version_;
143  std::string device_name_;
144  bool functions_[5];
145  };
146 
153  uint8_t active_alarms_[8];
156  std::queue<haier_protocol::HaierMessage> control_messages_queue_;
159  float active_alarm_count_{NAN};
160  std::chrono::steady_clock::time_point last_alarm_request_;
161  int big_data_sensors_{0};
162 };
163 
164 class HaierAlarmStartTrigger : public Trigger<uint8_t, const char *> {
165  public:
166  explicit HaierAlarmStartTrigger(HonClimate *parent) {
167  parent->add_alarm_start_callback(
168  [this](uint8_t alarm_code, const char *alarm_message) { this->trigger(alarm_code, alarm_message); });
169  }
170 };
171 
172 class HaierAlarmEndTrigger : public Trigger<uint8_t, const char *> {
173  public:
174  explicit HaierAlarmEndTrigger(HonClimate *parent) {
175  parent->add_alarm_end_callback(
176  [this](uint8_t alarm_code, const char *alarm_message) { this->trigger(alarm_code, alarm_message); });
177  }
178 };
179 
180 } // namespace haier
181 } // namespace esphome
AirflowVerticalDirection vertical_direction_
Definition: hon_climate.h:150
void add_alarm_end_callback(std::function< void(uint8_t, const char *)> &&callback)
CleaningState cleaning_status_
Definition: hon_climate.h:148
HonControlMethod control_method_
Definition: hon_climate.h:155
esphome::optional< HardwareInfo > hvac_hardware_info_
Definition: hon_climate.h:152
void set_control_method(HonControlMethod method)
Definition: hon_climate.h:101
FanDirection direction
Definition: fan.h:37
void add_alarm_start_callback(std::function< void(uint8_t, const char *)> &&callback)
uint8_t type
void set_extra_control_packet_bytes_size(size_t size)
Definition: hon_climate.h:100
std::chrono::steady_clock::time_point last_alarm_request_
Definition: hon_climate.h:160
float get_active_alarm_count() const
Definition: hon_climate.h:104
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
HaierAlarmStartTrigger(HonClimate *parent)
Definition: hon_climate.h:166
Base-class for all sensors.
Definition: sensor.h:57
std::queue< haier_protocol::HaierMessage > control_messages_queue_
Definition: hon_climate.h:156
AirflowHorizontalDirection horizontal_direction_
Definition: hon_climate.h:151
HaierAlarmEndTrigger(HonClimate *parent)
Definition: hon_climate.h:174
bool state
Definition: fan.h:34