ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
whirlpool.cpp
Go to the documentation of this file.
1#include "whirlpool.h"
3#include "esphome/core/log.h"
4
6
7ESPHOME_LOG_TAG(TAG, "whirlpool.climate");
8
9const uint16_t WHIRLPOOL_HEADER_MARK = 9000;
10const uint16_t WHIRLPOOL_HEADER_SPACE = 4494;
11const uint16_t WHIRLPOOL_BIT_MARK = 572;
12static constexpr remote_base::BitTimings WHIRLPOOL_BIT_TIMINGS{
13 .bit_mark = WHIRLPOOL_BIT_MARK, .one_space = 1659, .zero_space = 553};
15
17
18const uint8_t WHIRLPOOL_STATE_LENGTH = 21;
19static constexpr uint8_t WHIRLPOOL_FIRST_DIVIDER = 6;
20static constexpr uint8_t WHIRLPOOL_SECOND_DIVIDER = 14;
21
22const uint8_t WHIRLPOOL_HEAT = 0;
23const uint8_t WHIRLPOOL_DRY = 3;
24const uint8_t WHIRLPOOL_COOL = 2;
25const uint8_t WHIRLPOOL_FAN = 4;
26const uint8_t WHIRLPOOL_AUTO = 1;
27
28const uint8_t WHIRLPOOL_FAN_AUTO = 0;
29const uint8_t WHIRLPOOL_FAN_HIGH = 1;
30const uint8_t WHIRLPOOL_FAN_MED = 2;
31const uint8_t WHIRLPOOL_FAN_LOW = 3;
32
33const uint8_t WHIRLPOOL_FAN_MASK = 0x03;
34
35const uint8_t WHIRLPOOL_SWING_MASK = 128; // byte 2
36const uint8_t WHIRLPOOL_SWING_BYTE8 = 0x40; // byte 8
37
38const uint8_t WHIRLPOOL_POWER = 0x04;
39
40// Hisense ARG model; unused constexpr values cost nothing in DG11J1-only builds
41static constexpr uint8_t HISENSE_ARG_DISPLAY_ON = 0x80; // byte 11
42static constexpr uint8_t HISENSE_ARG_AMBIENT_TEMP = 0x18; // byte 12, 24 C
43static constexpr uint8_t HISENSE_ARG_POWER_TOGGLE = 0x01; // byte 15
44static constexpr uint8_t HISENSE_ARG_SETTINGS_ONLY = 0x02; // byte 15
45static constexpr uint8_t HISENSE_ARG_FIXED_TEMP_STEP = 7; // auto and fan-only send 23 C
46
53
55 this->last_transmit_time_ = millis(); // setting the time of the last transmission.
56 uint8_t remote_state[WHIRLPOOL_STATE_LENGTH];
57 memset(remote_state, 0, sizeof(remote_state));
58 remote_state[0] = 0x83;
59 remote_state[1] = 0x06;
60 remote_state[6] = 0x80;
61 remote_state[18] = 0x08; // the same on every supported model
62
63 auto powered_on = this->mode != climate::CLIMATE_MODE_OFF;
64 const bool power_toggle = powered_on != this->powered_on_assumed;
65 if (power_toggle) {
66 // Set power toggle command
67 remote_state[2] = 4;
68 remote_state[15] = 1;
69 this->powered_on_assumed = powered_on;
70 }
71 switch (this->mode) {
73 // set fan auto
74 // set temp auto temp
75 // set sleep false
76 remote_state[3] = WHIRLPOOL_AUTO;
77 remote_state[15] = 0x17;
78 break;
80 remote_state[3] = WHIRLPOOL_HEAT;
81 remote_state[15] = 6;
82 break;
84 remote_state[3] = WHIRLPOOL_COOL;
85 remote_state[15] = 6;
86 break;
88 remote_state[3] = WHIRLPOOL_DRY;
89 remote_state[15] = 6;
90 break;
92 remote_state[3] = WHIRLPOOL_FAN;
93 remote_state[15] = 6;
94 break;
96 default:
97 break;
98 }
99
100 const bool hisense_arg = this->is_hisense_arg_();
101
102 // Temperature
103 auto temp = (uint8_t) roundf(clamp(this->target_temperature, this->temperature_min_(), this->temperature_max_()));
104 const auto temp_step = (uint8_t) (temp - this->temperature_min_());
105 if (hisense_arg && this->fixed_temp_mode_()) {
106 remote_state[3] |= HISENSE_ARG_FIXED_TEMP_STEP << 4;
107 } else {
108 remote_state[3] |= temp_step << 4;
109 }
110
111 // Fan speed
112 switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
114 remote_state[2] |= WHIRLPOOL_FAN_HIGH;
115 break;
117 remote_state[2] |= WHIRLPOOL_FAN_MED;
118 break;
120 remote_state[2] |= WHIRLPOOL_FAN_LOW;
121 break;
122 default:
123 break;
124 }
125
126 // Swing
127 ESP_LOGV(TAG, "send swing %s", this->send_swing_cmd_ ? LOG_STR_LITERAL("true") : LOG_STR_LITERAL("false"));
128 if (hisense_arg) {
129 // This model sends swing as a state on every frame; the DG11J1 remotes send a toggle
131 remote_state[2] |= WHIRLPOOL_SWING_MASK;
132 remote_state[8] = WHIRLPOOL_SWING_BYTE8;
133 }
134 this->encode_hisense_arg_(remote_state, power_toggle, temp_step);
135 } else if (this->send_swing_cmd_) {
137 remote_state[2] |= WHIRLPOOL_SWING_MASK;
138 remote_state[8] |= WHIRLPOOL_SWING_BYTE8;
139 }
140 }
141
142 // Checksum
143 remote_state[13] = xor_bytes(&remote_state[2], 11, remote_state[13]);
144 remote_state[20] = xor_bytes(&remote_state[14], 6, remote_state[20]);
145
146 ESP_LOGV_HEX(TAG, remote_state, WHIRLPOOL_STATE_LENGTH, WHIRLPOOL_STATE_LENGTH, "Sending: %s");
147
148 // Send code
149 auto transmit = this->transmitter_->transmit();
150 auto *data = transmit.get_data();
151
152 data->set_carrier_frequency(38000);
153
154 // Header
155 data->mark(WHIRLPOOL_HEADER_MARK);
156 data->space(WHIRLPOOL_HEADER_SPACE);
157 // Data
158 for (uint8_t i = 0; i < WHIRLPOOL_STATE_LENGTH; i++) {
159 if (i == WHIRLPOOL_FIRST_DIVIDER || i == WHIRLPOOL_SECOND_DIVIDER) {
160 // Divider
161 data->mark(WHIRLPOOL_BIT_MARK);
162 data->space(WHIRLPOOL_GAP);
163 }
164 data->item_bytes<WHIRLPOOL_BIT_TIMINGS>(remote_state + i, 1);
165 }
166 // Footer
167 data->mark(WHIRLPOOL_BIT_MARK);
168
169 transmit.perform();
170}
171
175
176void WhirlpoolClimate::encode_hisense_arg_(uint8_t *remote_state, bool power_toggle, uint8_t temp_step) const {
177#ifdef USE_WHIRLPOOL_HISENSE_ARG
178 remote_state[11] = HISENSE_ARG_DISPLAY_ON;
179 remote_state[12] = HISENSE_ARG_AMBIENT_TEMP;
180 remote_state[15] = power_toggle ? HISENSE_ARG_POWER_TOGGLE : HISENSE_ARG_SETTINGS_ONLY;
181
182 switch (this->mode) {
184 // The remote keeps the cool code in the power-off frame
185 remote_state[3] = (remote_state[3] & 0xF0) | WHIRLPOOL_COOL;
186 break;
188 // The unit runs these fan speeds itself; the user's choice is kept for the next mode change
189 remote_state[2] &= ~WHIRLPOOL_FAN_MASK;
190 break;
192 if ((remote_state[2] & WHIRLPOOL_FAN_MASK) == WHIRLPOOL_FAN_AUTO) {
193 remote_state[2] |= WHIRLPOOL_FAN_MED;
194 }
195 break;
197 // Auto sends the target as one of four range codes in byte 2 bits 6:4 instead of the temperature nibble.
198 // Taken from the HPA-4911 firmware: 16 to 21 C, 22 C, 24 C, and everything else (23 C and 25 C up) share a code.
199 if (temp_step < 6) {
200 remote_state[2] |= 0x30;
201 } else if (temp_step == 6) {
202 remote_state[2] |= 0x50;
203 } else if (temp_step == 8) {
204 remote_state[2] |= 0x40;
205 } else {
206 remote_state[2] |= 0x20;
207 }
208 break;
209 default:
210 break;
211 }
212#endif
213}
214
216 // Check if the esp isn't currently transmitting.
217 if (millis() - this->last_transmit_time_ < 500) {
218 ESP_LOGV(TAG, "Blocked receive because of current trasmittion");
219 return false;
220 }
221
222 // Validate header
223 if (!data.expect_item(WHIRLPOOL_HEADER_MARK, WHIRLPOOL_HEADER_SPACE)) {
224 ESP_LOGV(TAG, "Header fail");
225 return false;
226 }
227
228 uint8_t remote_state[WHIRLPOOL_STATE_LENGTH];
229 memset(remote_state, 0, sizeof(remote_state));
230 bool skip_footer = false;
231 // Read all bytes.
232 for (int i = 0; i < WHIRLPOOL_STATE_LENGTH; i++) {
233 if (i == WHIRLPOOL_FIRST_DIVIDER || i == WHIRLPOOL_SECOND_DIVIDER) {
234 if (!data.expect_item(WHIRLPOOL_BIT_MARK, WHIRLPOOL_GAP))
235 return false;
236 }
237 if (i == WHIRLPOOL_SECOND_DIVIDER && !data.is_valid()) {
238 // Remote control only sent 14 bytes, nothing more to read, not even the footer
239 ESP_LOGV(TAG, "Remote control only sent %d bytes", i);
240 skip_footer = true;
241 break;
242 }
243 if (!data.expect_bytes<WHIRLPOOL_BIT_TIMINGS>(remote_state + i, 1))
244 return false;
245 }
246 // Validate footer
247 if (!data.expect_mark(WHIRLPOOL_BIT_MARK) && !skip_footer) {
248 ESP_LOGV(TAG, "Footer fail");
249 return false;
250 }
251
252 // Calculate checksum and compare with signal value.
253 uint8_t checksum13 = xor_bytes(&remote_state[2], 11);
254 uint8_t checksum20 = xor_bytes(&remote_state[14], 6);
255
256 if (checksum13 != remote_state[13] || (!skip_footer && checksum20 != remote_state[20])) {
257 ESP_LOGVV(TAG, "Checksum fail");
258 return false;
259 }
260
261 ESP_LOGV_HEX(TAG, remote_state, WHIRLPOOL_STATE_LENGTH, WHIRLPOOL_STATE_LENGTH, "Received: %s");
262
263 // verify header remote code
264 if (remote_state[0] != 0x83 || remote_state[1] != 0x06)
265 return false;
266
267 // powr on/off button
268 ESP_LOGV(TAG, "Power: %02X", (remote_state[2] & WHIRLPOOL_POWER));
269
270 if ((remote_state[2] & WHIRLPOOL_POWER) == WHIRLPOOL_POWER) {
271 auto powered_on = this->mode != climate::CLIMATE_MODE_OFF;
272
273 if (powered_on) {
275 this->powered_on_assumed = false;
276 } else {
277 this->powered_on_assumed = true;
278 }
279 }
280
281 // Set received mode
282 if (powered_on_assumed) {
283 auto mode = remote_state[3] & 0x7;
284 ESP_LOGV(TAG, "Mode: %02X", mode);
285 switch (mode) {
286 case WHIRLPOOL_HEAT:
288 break;
289 case WHIRLPOOL_COOL:
291 break;
292 case WHIRLPOOL_DRY:
294 break;
295 case WHIRLPOOL_FAN:
297 break;
298 case WHIRLPOOL_AUTO:
300 break;
301 default:
302 break;
303 }
304 }
305
306 // Set received temp; Hisense ARG sends fixed values in auto and fan-only
307 const bool hisense_arg = this->is_hisense_arg_();
308 if (!hisense_arg || !this->fixed_temp_mode_()) {
309 int temp = remote_state[3] & 0xF0;
310 ESP_LOGVV(TAG, "Temperature Raw: %02X", temp);
311 temp = (uint8_t) temp >> 4;
312 temp += static_cast<int>(this->temperature_min_());
313 ESP_LOGVV(TAG, "Temperature Climate: %u", temp);
314 this->target_temperature = temp;
315 }
316
317 // Set received fan speed
318 auto fan = remote_state[2] & WHIRLPOOL_FAN_MASK;
319 ESP_LOGVV(TAG, "Fan: %02X", fan);
320 switch (fan) {
323 break;
326 break;
329 break;
331 default:
333 break;
334 }
335
336 // Set received swing status
337 if (hisense_arg) {
338 this->swing_mode =
340 } else if ((remote_state[2] & WHIRLPOOL_SWING_MASK) == WHIRLPOOL_SWING_MASK &&
341 remote_state[8] == WHIRLPOOL_SWING_BYTE8) {
342 ESP_LOGVV(TAG, "Swing toggle pressed ");
345 } else {
347 }
348 }
349
350 this->publish_state();
351 return true;
352}
353
354} // namespace esphome::whirlpool
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
bool is_hisense_arg_() const
False when the model is compiled out, so the compiler drops the Hisense ARG branches.
Definition whirlpool.h:51
uint32_t last_transmit_time_
Set the time of the last transmission.
Definition whirlpool.h:63
bool fixed_temp_mode_() const
Modes in which the Hisense ARG model sends a fixed temperature.
void transmit_state() override
Transmit via IR the state of this climate controller.
Definition whirlpool.cpp:54
void encode_hisense_arg_(uint8_t *remote_state, bool power_toggle, uint8_t temp_step) const
Add the Hisense ARG bytes on top of the common whirlpool frame.
bool on_receive(remote_base::RemoteReceiveData data) override
Handle received IR Buffer.
@ CLIMATE_SWING_OFF
The swing mode is set to Off.
@ CLIMATE_SWING_VERTICAL
The fan mode is set to Vertical.
@ 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_ON
The fan mode is set to On.
@ 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.
const uint8_t WHIRLPOOL_COOL
Definition whirlpool.cpp:24
ESPHOME_LOG_TAG(TAG, "whirlpool.climate")
const uint16_t WHIRLPOOL_BIT_MARK
Definition whirlpool.cpp:11
const uint8_t WHIRLPOOL_FAN_MASK
Definition whirlpool.cpp:33
const float WHIRLPOOL_DG11J1_3A_TEMP_MAX
Definition whirlpool.h:15
const uint16_t WHIRLPOOL_HEADER_SPACE
Definition whirlpool.cpp:10
const uint8_t WHIRLPOOL_FAN_MED
Definition whirlpool.cpp:30
const uint8_t WHIRLPOOL_FAN_AUTO
Definition whirlpool.cpp:28
const uint8_t WHIRLPOOL_SWING_MASK
Definition whirlpool.cpp:35
const uint8_t WHIRLPOOL_STATE_LENGTH
Definition whirlpool.cpp:18
const uint32_t WHIRLPOOL_CARRIER_FREQUENCY
Definition whirlpool.cpp:16
const uint8_t WHIRLPOOL_FAN_LOW
Definition whirlpool.cpp:31
const uint8_t WHIRLPOOL_SWING_BYTE8
Definition whirlpool.cpp:36
const uint8_t WHIRLPOOL_DRY
Definition whirlpool.cpp:23
const uint16_t WHIRLPOOL_HEADER_MARK
Definition whirlpool.cpp:9
const float WHIRLPOOL_DG11J1_3A_TEMP_MIN
Definition whirlpool.h:16
const uint8_t WHIRLPOOL_HEAT
Definition whirlpool.cpp:22
const uint8_t WHIRLPOOL_AUTO
Definition whirlpool.cpp:26
const uint8_t WHIRLPOOL_POWER
Definition whirlpool.cpp:38
const uint8_t WHIRLPOOL_FAN
Definition whirlpool.cpp:25
const uint8_t WHIRLPOOL_FAN_HIGH
Definition whirlpool.cpp:29
const uint32_t WHIRLPOOL_GAP
Definition whirlpool.cpp:14
constexpr uint8_t xor_bytes(const uint8_t *data, size_t len, uint8_t init=0)
XOR the bytes of data with size len, starting from init.
Definition helpers.h:949
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t
A bit is a mark (one_mark or zero_mark, both bit_mark unless set) and a space (one_space or zero_spac...
Definition remote_base.h:29