ESPHome  2024.4.0
haier_base.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <chrono>
4 #include <set>
7 // HaierProtocol
8 #include <protocol/haier_protocol.h>
9 
10 namespace esphome {
11 namespace haier {
12 
13 enum class ActionRequest : uint8_t {
15  TURN_POWER_ON = 1,
16  TURN_POWER_OFF = 2,
17  TOGGLE_POWER = 3,
18  START_SELF_CLEAN = 4, // only hOn
19  START_STERI_CLEAN = 5, // only hOn
20 };
21 
25  public haier_protocol::ProtocolStream {
26  public:
28  HaierClimateBase(const HaierClimateBase &) = delete;
29  HaierClimateBase &operator=(const HaierClimateBase &) = delete;
31  void setup() override;
32  void loop() override;
33  void control(const esphome::climate::ClimateCall &call) override;
34  void dump_config() override;
35  float get_setup_priority() const override { return esphome::setup_priority::HARDWARE; }
36  void set_display_state(bool state);
37  bool get_display_state() const;
38  void set_health_mode(bool state);
39  bool get_health_mode() const;
40  void send_power_on_command();
41  void send_power_off_command();
42  void toggle_power();
43  void reset_protocol() { this->reset_protocol_request_ = true; };
44  void set_supported_modes(const std::set<esphome::climate::ClimateMode> &modes);
45  void set_supported_swing_modes(const std::set<esphome::climate::ClimateSwingMode> &modes);
46  void set_supported_presets(const std::set<esphome::climate::ClimatePreset> &presets);
47  bool valid_connection() { return this->protocol_phase_ >= ProtocolPhases::IDLE; };
48  size_t available() noexcept override { return esphome::uart::UARTDevice::available(); };
49  size_t read_array(uint8_t *data, size_t len) noexcept override {
50  return esphome::uart::UARTDevice::read_array(data, len) ? len : 0;
51  };
52  void write_array(const uint8_t *data, size_t len) noexcept override {
54  };
55  bool can_send_message() const { return haier_protocol_.get_outgoing_queue_size() == 0; };
56  void set_answer_timeout(uint32_t timeout);
57  void set_send_wifi(bool send_wifi);
58  void send_custom_command(const haier_protocol::HaierMessage &message);
59 
60  protected:
61  enum class ProtocolPhases {
62  UNKNOWN = -1,
63  // INITIALIZATION
64  SENDING_INIT_1 = 0,
65  SENDING_INIT_2,
66  SENDING_FIRST_STATUS_REQUEST,
67  SENDING_FIRST_ALARM_STATUS_REQUEST,
68  // FUNCTIONAL STATE
69  IDLE,
70  SENDING_STATUS_REQUEST,
71  SENDING_UPDATE_SIGNAL_REQUEST,
72  SENDING_SIGNAL_LEVEL,
73  SENDING_CONTROL,
74  SENDING_ACTION_COMMAND,
75  SENDING_ALARM_STATUS_REQUEST,
76  NUM_PROTOCOL_PHASES
77  };
78  const char *phase_to_string_(ProtocolPhases phase);
79  virtual void set_handlers() = 0;
80  virtual void process_phase(std::chrono::steady_clock::time_point now) = 0;
81  virtual haier_protocol::HaierMessage get_control_message() = 0;
82  virtual haier_protocol::HaierMessage get_power_message(bool state) = 0;
83  virtual bool prepare_pending_action();
84  virtual void process_protocol_reset();
85  esphome::climate::ClimateTraits traits() override;
86  // Answer handlers
87  haier_protocol::HandlerError answer_preprocess_(haier_protocol::FrameType request_message_type,
88  haier_protocol::FrameType expected_request_message_type,
89  haier_protocol::FrameType answer_message_type,
90  haier_protocol::FrameType expected_answer_message_type,
91  ProtocolPhases expected_phase);
92  haier_protocol::HandlerError report_network_status_answer_handler_(haier_protocol::FrameType request_type,
93  haier_protocol::FrameType message_type,
94  const uint8_t *data, size_t data_size);
95  // Timeout handler
96  haier_protocol::HandlerError timeout_default_handler_(haier_protocol::FrameType request_type);
97  // Helper functions
98  void send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_repeats = 0,
99  std::chrono::milliseconds interval = std::chrono::milliseconds::zero());
100  virtual void set_phase(ProtocolPhases phase);
101  void reset_phase_();
102  void reset_to_idle_();
103  bool is_message_interval_exceeded_(std::chrono::steady_clock::time_point now);
104  bool is_status_request_interval_exceeded_(std::chrono::steady_clock::time_point now);
105  bool is_control_message_interval_exceeded_(std::chrono::steady_clock::time_point now);
106  bool is_protocol_initialisation_interval_exceeded_(std::chrono::steady_clock::time_point now);
107 #ifdef USE_WIFI
108  haier_protocol::HaierMessage get_wifi_signal_message_();
109 #endif
110 
111  struct HvacSettings {
117  bool valid;
118  HvacSettings() : valid(false){};
119  HvacSettings(const HvacSettings &) = default;
120  HvacSettings &operator=(const HvacSettings &) = default;
121  void reset();
122  };
123  struct PendingAction {
126  };
127  haier_protocol::ProtocolHandler haier_protocol_;
138  bool use_crc_;
142  std::unique_ptr<uint8_t[]> last_status_message_;
143  std::chrono::steady_clock::time_point last_request_timestamp_; // For interval between messages
144  std::chrono::steady_clock::time_point last_valid_status_timestamp_; // For protocol timeout
145  std::chrono::steady_clock::time_point last_status_request_; // To request AC status
146  std::chrono::steady_clock::time_point last_signal_request_; // To send WiFI signal level
147 };
148 
149 } // namespace haier
150 } // namespace esphome
void setup()
This class is used to encode all control actions on a climate device.
Definition: climate.h:33
void loop()
esphome::optional< float > target_temperature
Definition: haier_base.h:115
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:116
std::unique_ptr< uint8_t[]> last_status_message_
Definition: haier_base.h:142
This class contains all static data for climate devices.
std::chrono::steady_clock::time_point last_status_request_
Definition: haier_base.h:145
std::chrono::steady_clock::time_point last_signal_request_
Definition: haier_base.h:146
esphome::optional< haier_protocol::HaierMessage > message
Definition: haier_base.h:125
void write_array(const uint8_t *data, size_t len) noexcept override
Definition: haier_base.h:52
haier_protocol::ProtocolHandler haier_protocol_
Definition: haier_base.h:127
size_t available() noexcept override
Definition: haier_base.h:48
esphome::optional< PendingAction > action_request_
Definition: haier_base.h:129
esphome::optional< esphome::climate::ClimateFanMode > fan_mode
Definition: haier_base.h:113
uint16_t reset
Definition: ina226.h:39
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:292
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
std::chrono::steady_clock::time_point last_request_timestamp_
Definition: haier_base.h:143
size_t read_array(uint8_t *data, size_t len) noexcept override
Definition: haier_base.h:49
float get_setup_priority() const override
Definition: haier_base.h:35
esphome::climate::ClimateTraits traits_
Definition: haier_base.h:139
esphome::optional< esphome::climate::ClimateSwingMode > swing_mode
Definition: haier_base.h:114
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:144
esphome::optional< esphome::climate::ClimateMode > mode
Definition: haier_base.h:112