ESPHome  2024.11.0
haier_base.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <chrono>
4 #include <set>
8 // HaierProtocol
9 #include <protocol/haier_protocol.h>
10 
11 #ifdef USE_SWITCH
13 #endif
14 
15 namespace esphome {
16 namespace haier {
17 
18 enum class ActionRequest : uint8_t {
20  TURN_POWER_ON = 1,
21  TURN_POWER_OFF = 2,
22  TOGGLE_POWER = 3,
23  START_SELF_CLEAN = 4, // only hOn
24  START_STERI_CLEAN = 5, // only hOn
25 };
26 
30 };
31 
35  public haier_protocol::ProtocolStream {
36 #ifdef USE_SWITCH
37  public:
38  void set_display_switch(switch_::Switch *sw);
39  void set_health_mode_switch(switch_::Switch *sw);
40 
41  protected:
42  switch_::Switch *display_switch_{nullptr};
43  switch_::Switch *health_mode_switch_{nullptr};
44 #endif
45  public:
47  HaierClimateBase(const HaierClimateBase &) = delete;
48  HaierClimateBase &operator=(const HaierClimateBase &) = delete;
50  void setup() override;
51  void loop() override;
52  void control(const esphome::climate::ClimateCall &call) override;
53  void dump_config() override;
54  float get_setup_priority() const override { return esphome::setup_priority::HARDWARE; }
55  void set_display_state(bool state);
56  bool get_display_state() const;
57  void set_health_mode(bool state);
58  bool get_health_mode() const;
59  void send_power_on_command();
60  void send_power_off_command();
61  void toggle_power();
62  void reset_protocol() { this->reset_protocol_request_ = true; };
63  void set_supported_modes(const std::set<esphome::climate::ClimateMode> &modes);
64  void set_supported_swing_modes(const std::set<esphome::climate::ClimateSwingMode> &modes);
65  void set_supported_presets(const std::set<esphome::climate::ClimatePreset> &presets);
66  bool valid_connection() const { return this->protocol_phase_ >= ProtocolPhases::IDLE; };
67  size_t available() noexcept override { return esphome::uart::UARTDevice::available(); };
68  size_t read_array(uint8_t *data, size_t len) noexcept override {
69  return esphome::uart::UARTDevice::read_array(data, len) ? len : 0;
70  };
71  void write_array(const uint8_t *data, size_t len) noexcept override {
73  };
74  bool can_send_message() const { return haier_protocol_.get_outgoing_queue_size() == 0; };
75  void set_answer_timeout(uint32_t timeout);
76  void set_send_wifi(bool send_wifi);
77  void send_custom_command(const haier_protocol::HaierMessage &message);
78  void add_status_message_callback(std::function<void(const char *, size_t)> &&callback);
79 
80  protected:
81  enum class ProtocolPhases {
82  UNKNOWN = -1,
83  // INITIALIZATION
84  SENDING_INIT_1 = 0,
85  SENDING_INIT_2,
86  SENDING_FIRST_STATUS_REQUEST,
87  SENDING_FIRST_ALARM_STATUS_REQUEST,
88  // FUNCTIONAL STATE
89  IDLE,
90  SENDING_STATUS_REQUEST,
91  SENDING_UPDATE_SIGNAL_REQUEST,
92  SENDING_SIGNAL_LEVEL,
93  SENDING_CONTROL,
94  SENDING_ACTION_COMMAND,
95  SENDING_ALARM_STATUS_REQUEST,
96  NUM_PROTOCOL_PHASES
97  };
98  const char *phase_to_string_(ProtocolPhases phase);
99  virtual void set_handlers() = 0;
100  virtual void process_phase(std::chrono::steady_clock::time_point now) = 0;
101  virtual haier_protocol::HaierMessage get_control_message() = 0; // NOLINT(readability-identifier-naming)
102  virtual haier_protocol::HaierMessage get_power_message(bool state) = 0; // NOLINT(readability-identifier-naming)
103  virtual void save_settings();
104  virtual void initialization();
105  virtual bool prepare_pending_action();
106  virtual void process_protocol_reset();
107  esphome::climate::ClimateTraits traits() override;
108  // Answer handlers
109  haier_protocol::HandlerError answer_preprocess_(haier_protocol::FrameType request_message_type,
110  haier_protocol::FrameType expected_request_message_type,
111  haier_protocol::FrameType answer_message_type,
112  haier_protocol::FrameType expected_answer_message_type,
113  ProtocolPhases expected_phase);
114  haier_protocol::HandlerError report_network_status_answer_handler_(haier_protocol::FrameType request_type,
115  haier_protocol::FrameType message_type,
116  const uint8_t *data, size_t data_size);
117  // Timeout handler
118  haier_protocol::HandlerError timeout_default_handler_(haier_protocol::FrameType request_type);
119  // Helper functions
120  void send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_repeats = 0,
121  std::chrono::milliseconds interval = std::chrono::milliseconds::zero());
122  virtual void set_phase(ProtocolPhases phase);
123  void reset_phase_();
124  void reset_to_idle_();
125  bool is_message_interval_exceeded_(std::chrono::steady_clock::time_point now);
126  bool is_status_request_interval_exceeded_(std::chrono::steady_clock::time_point now);
127  bool is_control_message_interval_exceeded_(std::chrono::steady_clock::time_point now);
128  bool is_protocol_initialisation_interval_exceeded_(std::chrono::steady_clock::time_point now);
129 #ifdef USE_WIFI
130  haier_protocol::HaierMessage get_wifi_signal_message_();
131 #endif
132 
133  struct HvacSettings {
139  bool valid;
140  HvacSettings() : valid(false){};
141  HvacSettings(const HvacSettings &) = default;
142  HvacSettings &operator=(const HvacSettings &) = default;
143  void reset();
144  };
145  struct PendingAction {
148  };
149  enum class SwitchState {
150  OFF = 0b00,
151  ON = 0b01,
152  PENDING_OFF = 0b10,
153  PENDING_ON = 0b11,
154  };
155  haier_protocol::ProtocolHandler haier_protocol_;
160  SwitchState display_status_{SwitchState::ON};
161  SwitchState health_mode_{SwitchState::OFF};
166  bool use_crc_;
170  std::unique_ptr<uint8_t[]> last_status_message_{nullptr};
171  std::chrono::steady_clock::time_point last_request_timestamp_; // For interval between messages
172  std::chrono::steady_clock::time_point last_valid_status_timestamp_; // For protocol timeout
173  std::chrono::steady_clock::time_point last_status_request_; // To request AC status
174  std::chrono::steady_clock::time_point last_signal_request_; // To send WiFI signal level
177 };
178 
179 class StatusMessageTrigger : public Trigger<const char *, size_t> {
180  public:
182  parent->add_status_message_callback([this](const char *data, size_t data_size) { this->trigger(data, data_size); });
183  }
184 };
185 
186 } // namespace haier
187 } // namespace esphome
void setup()
This class is used to encode all control actions on a climate device.
Definition: climate.h:33
Base class for all switches.
Definition: switch.h:39
void loop()
esphome::optional< float > target_temperature
Definition: haier_base.h:137
optional< std::array< uint8_t, N > > read_array()
Definition: uart.h:33
void write_array(const uint8_t *data, size_t len)
Definition: uart.h:21
esphome::optional< esphome::climate::ClimatePreset > preset
Definition: haier_base.h:138
This class contains all static data for climate devices.
std::chrono::steady_clock::time_point last_status_request_
Definition: haier_base.h:173
std::chrono::steady_clock::time_point last_signal_request_
Definition: haier_base.h:174
esphome::optional< haier_protocol::HaierMessage > message
Definition: haier_base.h:147
ESPPreferenceObject base_rtc_
Definition: haier_base.h:176
void write_array(const uint8_t *data, size_t len) noexcept override
Definition: haier_base.h:71
haier_protocol::ProtocolHandler haier_protocol_
Definition: haier_base.h:155
size_t available() noexcept override
Definition: haier_base.h:67
esphome::optional< PendingAction > action_request_
Definition: haier_base.h:157
esphome::optional< esphome::climate::ClimateFanMode > fan_mode
Definition: haier_base.h:135
uint16_t reset
Definition: ina226.h:39
void add_status_message_callback(std::function< void(const char *, size_t)> &&callback)
Definition: haier_base.cpp:200
StatusMessageTrigger(HaierClimateBase *parent)
Definition: haier_base.h:181
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
std::string size_t len
Definition: helpers.h:293
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::chrono::steady_clock::time_point last_request_timestamp_
Definition: haier_base.h:171
size_t read_array(uint8_t *data, size_t len) noexcept override
Definition: haier_base.h:68
float get_setup_priority() const override
Definition: haier_base.h:54
esphome::climate::ClimateTraits traits_
Definition: haier_base.h:167
esphome::optional< esphome::climate::ClimateSwingMode > swing_mode
Definition: haier_base.h:136
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:168
bool state
Definition: fan.h:34
std::chrono::steady_clock::time_point last_valid_status_timestamp_
Definition: haier_base.h:172
esphome::optional< esphome::climate::ClimateMode > mode
Definition: haier_base.h:134