ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
haier_tundra.cpp
Go to the documentation of this file.
1#include "haier_tundra.h"
3
5
6// Number of bytes in decoded packet sans checksum
7static constexpr uint8_t HAIER_TUNDRA_FRAME_SIZE = 13;
8
12
13static constexpr IRCode MODES[] PROGMEM = {
14 {climate::CLIMATE_MODE_HEAT_COOL, HAIER_MODE_AUTO}, {climate::CLIMATE_MODE_COOL, HAIER_MODE_COOL},
15 {climate::CLIMATE_MODE_DRY, HAIER_MODE_DRY}, {climate::CLIMATE_MODE_HEAT, HAIER_MODE_HEAT},
16 {climate::CLIMATE_MODE_FAN_ONLY, HAIER_MODE_FAN},
17};
18static constexpr IRCode FANS[] PROGMEM = {
19 {climate::CLIMATE_FAN_AUTO, HAIER_FAN_AUTO},
20 {climate::CLIMATE_FAN_LOW, HAIER_FAN_LOW},
21 {climate::CLIMATE_FAN_MEDIUM, HAIER_FAN_MED},
22 {climate::CLIMATE_FAN_HIGH, HAIER_FAN_HIGH},
23};
24
26 // Button value does not seem to matter too much except
27 // when toggling light, as it has no bit flag of its own
28 // So in the default case we can simply set it to POWER
29 this->transmit_(HAIER_BUTTON_POWER);
30}
31
32void HaierTundra::transmit_(uint8_t button) {
33 // Force Quiet and Turbo off if we're not in Cool or Heat mode
35#ifdef USE_SWITCH
36 PUBLISH_STATE(this->quiet_mode_switch_, false);
37 PUBLISH_STATE(this->turbo_mode_switch_, false);
38#endif
39
40 this->quiet_mode_ = false;
41 this->turbo_mode_ = false;
42 }
43
44 const uint8_t frame[HAIER_TUNDRA_FRAME_SIZE] = {
45 this->unit_,
46 static_cast<uint8_t>(this->temperature_() << 4 | this->vertical_swing_()),
47 static_cast<uint8_t>(this->horizontal_swing_() << 5),
48 static_cast<uint8_t>(this->health_() << 1),
49 static_cast<uint8_t>(this->power_() << 6),
50 static_cast<uint8_t>(to_ir_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO), HAIER_FAN_AUTO) << 5),
51 static_cast<uint8_t>(this->quiet_() << 7 | this->turbo_() << 6),
52 static_cast<uint8_t>(to_ir_code(MODES, this->mode, HAIER_MODE_AUTO) << 5),
53 0x00,
54 0x00,
55 static_cast<uint8_t>((button == HAIER_BUTTON_SELF_CLEAN ? 1 : 0) << 4),
56 0x00,
57 button,
58 };
59
60 auto transmit = this->transmitter_->transmit();
61 remote_base::HaierProtocol().encode(transmit.get_data(), frame, sizeof(frame));
62 transmit.perform();
63}
64
66 return (uint8_t) roundf(clamp<float>(this->target_temperature, HAIER_TEMP_MIN, HAIER_TEMP_MAX)) - HAIER_TEMP_MIN;
67}
68
70 if (this->mode == climate::CLIMATE_MODE_OFF) {
71 return HAIER_SWING_V_OFF;
72 }
73 switch (this->swing_mode) {
76 return HAIER_SWING_V_AUTO;
77 default:
78 if (this->mode == climate::CLIMATE_MODE_HEAT) {
79 return HAIER_SWING_V_1BOT;
80 }
81 return HAIER_SWING_V_1TOP; // Cool/Dry/Auto
82 }
83}
84
86 switch (this->swing_mode) {
89 return HAIER_SWING_H_AUTO;
90 default:
91 return HAIER_SWING_H_MIDDLE;
92 }
93}
94
95uint8_t HaierTundra::health_() { return this->health_mode_ ? 1 : 0; }
96
97uint8_t HaierTundra::power_() { return this->mode == climate::CLIMATE_MODE_OFF ? 0 : 1; }
98
99uint8_t HaierTundra::quiet_() { return this->quiet_mode_ ? 1 : 0; }
100
101uint8_t HaierTundra::turbo_() { return this->turbo_mode_ ? 1 : 0; }
102
104 auto haier = remote_base::HaierProtocol().decode(data);
105 if (haier.has_value()) {
106 return this->on_haier_(*haier);
107 }
108 return false;
109}
110
112 // Verify frame length and header
113 if (haier.data.size() != HAIER_TUNDRA_FRAME_SIZE || haier.data[0] != this->unit_) {
114 return false;
115 }
116
117 this->target_temperature = this->get_temperature_(haier.data[1] >> 4);
118 this->swing_mode = this->get_swing_mode_(haier.data[1] & 0xF, haier.data[2] >> 5);
119 this->health_mode_ = haier.data[3] & 0x2;
120 this->mode = (haier.data[4] & 0x40) ? from_ir_code(MODES, haier.data[7] >> 5, climate::CLIMATE_MODE_HEAT_COOL)
122 this->fan_mode = from_ir_code(FANS, haier.data[5] >> 5, climate::CLIMATE_FAN_AUTO);
123 this->quiet_mode_ = haier.data[6] & 0x80;
124 this->turbo_mode_ = haier.data[6] & 0x40;
125
126#ifdef USE_SWITCH
127 PUBLISH_STATE(this->health_mode_switch_, this->health_mode_);
128 PUBLISH_STATE(this->quiet_mode_switch_, this->quiet_mode_);
129 PUBLISH_STATE(this->turbo_mode_switch_, this->turbo_mode_);
130#endif
131
132 this->publish_state();
133
134 return true;
135}
136
137float HaierTundra::get_temperature_(uint8_t temp) { return temp + HAIER_TEMP_MIN; }
138
140 if (swing_v == HAIER_SWING_V_AUTO) {
141 if (swing_h == HAIER_SWING_H_AUTO) {
143 }
144
146 } else if (swing_h == HAIER_SWING_H_AUTO) {
148 }
149
151}
152
153void HaierTundra::set_health_mode(bool health, bool transmit) {
154 this->health_mode_ = health;
155
156 if (transmit && this->mode != climate::CLIMATE_MODE_OFF) {
157 this->transmit_(HAIER_BUTTON_HEALTH);
158 }
159}
160
161void HaierTundra::set_quiet_mode(bool quiet, bool transmit) {
162 // Quiet & Turbo cannot be enabled at the same time
163 if (quiet) {
164#ifdef USE_SWITCH
165 PUBLISH_STATE(this->turbo_mode_switch_, false);
166#endif
167 this->turbo_mode_ = false;
168 }
169
170 this->quiet_mode_ = quiet;
171 if (transmit)
172 this->transmit_(HAIER_BUTTON_TURBO_QUIET);
173}
174
175void HaierTundra::set_turbo_mode(bool turbo, bool transmit) {
176 // Quiet & Turbo cannot be enabled at the same time
177 if (turbo) {
178#ifdef USE_SWITCH
179 PUBLISH_STATE(this->quiet_mode_switch_, false);
180#endif
181 this->quiet_mode_ = false;
182 }
183
184 this->turbo_mode_ = turbo;
185 if (transmit)
186 this->transmit_(HAIER_BUTTON_TURBO_QUIET);
187}
188
190 if (this->mode != climate::CLIMATE_MODE_OFF) {
191 this->transmit_(HAIER_BUTTON_LIGHT);
192 }
193}
194
196 // Can be started when off
197 this->transmit_(HAIER_BUTTON_SELF_CLEAN);
198}
199
200} // namespace esphome::haier_tundra
size_t size() const
Definition helpers.h:404
ClimateMode mode
The active mode of the climate device.
Definition climate.h:317
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:311
float target_temperature
The target temperature of the climate device.
Definition climate.h:298
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:323
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:432
void set_quiet_mode(bool quiet, bool transmit=true)
climate::ClimateSwingMode get_swing_mode_(uint8_t swing_v, uint8_t swing_h)
bool on_haier_(const remote_base::HaierData &haier)
void set_turbo_mode(bool turbo, bool transmit=true)
void set_health_mode(bool health, bool transmit=true)
bool on_receive(remote_base::RemoteReceiveData data) override
void encode(RemoteTransmitData *dst, const HaierData &data)
optional< HaierData > decode(RemoteReceiveData src)
T from_ir_code(const IRCode(&table)[N], uint8_t code, T fallback)
The value for code, or fallback when the table has no such code; pass the current state to keep it.
Definition climate_ir.h:40
uint8_t to_ir_code(const IRCode(&table)[N], uint8_t value, uint8_t fallback)
The code for value, or fallback when the table has no such value.
Definition climate_ir.h:36
ClimateSwingMode
Enum for all modes a climate swing can be in NOTE: If adding values, update ClimateSwingModeMask in c...
@ CLIMATE_SWING_OFF
The swing mode is set to Off.
@ CLIMATE_SWING_HORIZONTAL
The fan mode is set to Horizontal.
@ CLIMATE_SWING_VERTICAL
The fan mode is set to Vertical.
@ CLIMATE_SWING_BOTH
The fan mode is set to Both.
@ CLIMATE_MODE_DRY
The climate device is set to dry/humidity mode.
@ CLIMATE_MODE_FAN_ONLY
The climate device only has the fan enabled, no heating or cooling is taking place.
@ CLIMATE_MODE_HEAT
The climate device is set to heat to reach the target temperature.
@ CLIMATE_MODE_COOL
The climate device is set to cool to reach the target temperature.
@ CLIMATE_MODE_HEAT_COOL
The climate device is set to heat/cool to reach the target temperature.
@ CLIMATE_MODE_OFF
The climate device is off.
@ CLIMATE_FAN_MEDIUM
The fan mode is set to Medium.
@ CLIMATE_FAN_AUTO
The fan mode is set to Auto.
@ CLIMATE_FAN_LOW
The fan mode is set to Low.
@ CLIMATE_FAN_HIGH
The fan mode is set to High.
One entry of a PROGMEM table that maps a climate enum value to the unit's code.
Definition climate_ir.h:15
RemoteBytes< HAIER_MAX_DATA_BYTES > data