ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
gree.cpp
Go to the documentation of this file.
1#include "gree.h"
2
3#include <cmath>
6
7namespace esphome::gree {
8
9ESPHOME_LOG_TAG(TAG, "gree.climate");
10
11static constexpr remote_base::BitTimings GREE_BIT_TIMINGS{
12 .bit_mark = GREE_BIT_MARK, .one_space = GREE_ONE_SPACE, .zero_space = GREE_ZERO_SPACE};
13
14static constexpr uint8_t GREE_SWING_AUTO_MASK = 0x40;
15#ifdef USE_GREE_YAW1F
16// The YAW1F second message is 00 00 00 A0 | 00 00 00 A0.
17static constexpr uint8_t GREE_YAW1F_FOLLOWUP_BYTE = 0xA0;
18#endif
19
20// The mode, fan and feature mappings use if-else: a switch becomes a lookup table in RAM on ESP8266.
21static constexpr uint8_t supported_feature_mask(Model model) {
22 if (model == GREE_YB1FA)
23 return GREE_MODE_BIT_TURBO | GREE_MODE_BIT_LIGHT | GREE_MODE_BIT_XFAN;
24 if (model == GREE_YX1FF)
25 return GREE_MODE_BIT_LIGHT;
26 if (model == GREE_GENERIC || model == GREE_YAG || model == GREE_YAW1F)
27 return 0;
28 return GREE_MODE_BIT_TURBO | GREE_MODE_BIT_LIGHT | GREE_MODE_BIT_HEALTH | GREE_MODE_BIT_XFAN;
29}
30
31static constexpr uint8_t default_feature_bits(Model model) {
32 return supported_feature_mask(model) & GREE_MODE_BIT_LIGHT;
33}
34
35#ifdef USE_GREE_FAHRENHEIT
36uint8_t fahrenheit_half_steps(float target_celsius) {
37 if (!std::isfinite(target_celsius)) {
38 return 0;
39 }
40 // Whole degrees shown at k = 0 and at the top of the range
41 static constexpr int MIN_F = static_cast<int>(celsius_to_fahrenheit(GREE_TEMP_MIN));
42 static constexpr int MAX_F = static_cast<int>(celsius_to_fahrenheit(GREE_TEMP_MAX));
43 const int desired_f = static_cast<int>(roundf(clamp<float>(celsius_to_fahrenheit(target_celsius), MIN_F, MAX_F)));
44 // The display at k is (10 * 32 + 18 * GREE_TEMP_MIN + 9 * k) / 10; pick the smallest k that reaches desired_f
45 const int tenths_above_min = 10 * desired_f - (10 * 32 + 18 * GREE_TEMP_MIN);
46 return static_cast<uint8_t>((tenths_above_min + 8) / 9);
47}
48#endif
49
50struct GreeFrameSpaces {
51 uint32_t header;
52 uint32_t message;
53};
54
55static GreeFrameSpaces frame_spaces(Model model) {
56 if (model == GREE_YAC1FB9 || model == GREE_YAW1F)
57 return {GREE_YAC1FB9_HEADER_SPACE, GREE_YAC1FB9_MESSAGE_SPACE};
58 return {GREE_HEADER_SPACE, GREE_MESSAGE_SPACE};
59}
60
61static void write_frame(remote_base::RemoteTransmitData *data, const GreeState &frame, const GreeFrameSpaces &spaces) {
62 data->item(GREE_HEADER_MARK, spaces.header);
63 data->item_bytes<GREE_BIT_TIMINGS>(frame.data(), 4);
64 // Fixed 010 between the two halves, then the message gap
65 data->item(GREE_BIT_MARK, GREE_ZERO_SPACE);
66 data->item(GREE_BIT_MARK, GREE_ONE_SPACE);
67 data->item(GREE_BIT_MARK, GREE_ZERO_SPACE);
68 data->item(GREE_BIT_MARK, spaces.message);
69 data->item_bytes<GREE_BIT_TIMINGS>(frame.data() + 4, 4);
70 data->mark(GREE_BIT_MARK);
71}
72
73#ifdef USE_GREE_RECEIVER
74static constexpr uint8_t GREE_MODE_MASK = 0x07;
75static constexpr uint8_t GREE_POWER_MASK = 0x08;
76static constexpr uint8_t GREE_FAN_MASK = 0x30;
77static constexpr uint8_t GREE_SLEEP_MASK = 0x80;
78static constexpr uint8_t GREE_TEMP_MASK = 0x0F;
79static constexpr uint8_t GREE_TIMER_HALF_HOUR_MASK = 0x10;
80static constexpr uint8_t GREE_TIMER_TENS_HOUR_MASK = 0x60;
81static constexpr uint8_t GREE_TIMER_HOURS_MASK = 0x0F;
82static constexpr uint8_t GREE_BYTE5_FIXED_MASK = 0xB8;
83static constexpr uint8_t GREE_BYTE5_FIXED_VALUE = 0x20;
84
85static bool read_frame(remote_base::RemoteReceiveData &data, GreeState &frame, const GreeFrameSpaces &spaces) {
86 return data.expect_item(GREE_HEADER_MARK, spaces.header) && data.expect_bytes<GREE_BIT_TIMINGS>(frame.data(), 4) &&
87 data.expect_item(GREE_BIT_MARK, GREE_ZERO_SPACE) && data.expect_item(GREE_BIT_MARK, GREE_ONE_SPACE) &&
88 data.expect_item(GREE_BIT_MARK, GREE_ZERO_SPACE) && data.expect_item(GREE_BIT_MARK, spaces.message) &&
89 data.expect_bytes<GREE_BIT_TIMINGS>(frame.data() + 4, 4) && data.expect_mark(GREE_BIT_MARK);
90}
91
92#ifdef USE_GREE_YAW1F
93static bool is_yaw1f_followup(const GreeState &state) {
94 return state[3] == GREE_YAW1F_FOLLOWUP_BYTE && state[7] == GREE_YAW1F_FOLLOWUP_BYTE &&
95 (state[0] | state[1] | state[2] | state[4] | state[5] | state[6]) == 0;
96}
97#endif
98
99// YAC sends a 0x70 follow-up frame and YAW1F a fixed second message; neither carries climate state.
100static bool is_auxiliary_frame(Model model, const GreeState &state) {
101#ifdef USE_GREE_YAW1F
102 if (model == GREE_YAW1F && is_yaw1f_followup(state))
103 return true;
104#endif
105 return model == GREE_YAC && (state[3] & 0xF0) == 0x70;
106}
107
108static bool is_valid_timer(const GreeState &state) {
109 const uint8_t timer_hours = state[2] & GREE_TIMER_HOURS_MASK;
110 const uint8_t timer_tens_hours = (state[1] & GREE_TIMER_TENS_HOUR_MASK) >> 5;
111 const bool timer_half_hour = state[1] & GREE_TIMER_HALF_HOUR_MASK;
112
113 if (timer_hours > 9 || timer_tens_hours > 2)
114 return false;
115 if (timer_tens_hours < 2)
116 return true;
117 return timer_hours < 4 || (timer_hours == 4 && !timer_half_hour);
118}
119
120static bool is_valid_vertical_swing(bool automatic, uint8_t position) {
121 if (automatic) {
122 return position == GREE_VDIR_SWING || position == GREE_VDIR_SWING_DOWN || position == GREE_VDIR_SWING_MIDDLE ||
123 position == GREE_VDIR_SWING_UP;
124 }
125 return position == GREE_VDIR_MANUAL || (position >= GREE_VDIR_UP && position <= GREE_VDIR_DOWN);
126}
127
128static bool is_valid_swing(Model model, const GreeState &state) {
129 const bool automatic = state[0] & GREE_SWING_AUTO_MASK;
130 const uint8_t vertical = state[4] & 0x0F;
131 const uint8_t horizontal = state[4] >> 4;
132
133 if (!is_valid_vertical_swing(automatic, vertical))
134 return false;
135 if (model == GREE_YX1FF)
136 return horizontal == (automatic ? GREE_HDIR_SWING : GREE_HDIR_MANUAL);
137 return horizontal == GREE_HDIR_MANUAL;
138}
139
140static climate::ClimateMode decode_operation_mode(uint8_t mode) {
141 if (mode == GREE_MODE_COOL)
143 if (mode == GREE_MODE_DRY)
145 if (mode == GREE_MODE_FAN)
147 if (mode == GREE_MODE_HEAT)
150}
151
152// Use if-else rather than lookup tables to avoid RAM usage on ESP8266.
153static climate::ClimateFanMode decode_fan_mode(Model model, uint8_t fan) {
154 if (fan == GREE_FAN_AUTO)
156 if (model == GREE_YX1FF) {
157 if (fan == GREE_FAN_1)
159 if (fan == GREE_FAN_2)
162 }
163 if (fan == GREE_FAN_1)
165 if (fan == GREE_FAN_2)
168}
169#endif // USE_GREE_RECEIVER
170
172 const GreeFrameSpaces spaces = frame_spaces(this->model_);
173 data->reserve(this->model_ == GREE_YAW1F ? 280 : 140);
174 data->set_carrier_frequency(GREE_IR_FREQUENCY);
175
176 write_frame(data, state, spaces);
177#ifdef USE_GREE_YAW1F
178 if (this->model_ == GREE_YAW1F) {
179 // The YAW1F remote follows every command with a fixed second message.
180 GreeState followup{};
181 followup[3] = followup[7] = GREE_YAW1F_FOLLOWUP_BYTE;
182 data->space(GREE_YAW1F_FOLLOWUP_SPACE);
183 write_frame(data, followup, spaces);
184 }
185#endif
186 data->space(0);
187}
188
189#ifdef USE_GREE_RECEIVER
191 const GreeFrameSpaces spaces = frame_spaces(this->model_);
193 if (!read_frame(data, state, spaces) || !GreeProtocol::valid_checksum(state))
194 return {};
195
196#ifdef USE_GREE_YAW1F
197 // Depending on receiver idle settings, YAW1F's ~40 ms second message may arrive in
198 // the same capture. Validate it completely; never treat it as climate state.
199 if (this->model_ == GREE_YAW1F && data.is_valid(1)) {
200 GreeState followup{};
201 if (!data.expect_space(GREE_YAW1F_FOLLOWUP_SPACE) || !read_frame(data, followup, spaces) ||
202 !is_yaw1f_followup(followup)) {
203 ESP_LOGV(TAG, "YAW1F frame dropped: second message invalid");
204 return {};
205 }
206 }
207#endif
208
209 // A receiver normally appends one final idle space. Reject any extra pulses.
210 if (data.is_valid(1) || (data.is_valid() && data.peek() > 0)) {
211 ESP_LOGV(TAG, "Frame dropped: extra pulses follow it (receiver idle may be too long)");
212 return {};
213 }
214
215 return state;
216}
217#endif // USE_GREE_RECEIVER
218
220 uint8_t sum = 0x0A;
221 for (uint8_t pos = 0; pos < 4; pos++)
222 sum += state[pos] & 0x0F;
223 for (uint8_t pos = 4; pos < GREE_STATE_FRAME_SIZE - 1; pos++)
224 sum += state[pos] >> 4;
225 return (sum & 0x0F) << 4;
226}
227
228#ifdef USE_GREE_RECEIVER
230 return (state[GREE_STATE_FRAME_SIZE - 1] & 0xF0) == GreeProtocol::calculate_checksum(state);
231}
232#endif
233
235 GreeState state{0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00};
236 const uint8_t vertical_swing = GreeClimateCodec::encode_vertical_swing(data.swing_mode);
237 const uint8_t horizontal_swing = GreeClimateCodec::encode_horizontal_swing(data.swing_mode);
238
239 state[0] =
241 const uint8_t target_temperature = clamp<uint8_t>(data.target_temperature, GREE_TEMP_MIN, GREE_TEMP_MAX);
242 state[1] = target_temperature - GREE_TEMP_MIN;
243 state[2] = data.feature_bits & supported_feature_mask(model);
244 if (model != GREE_GENERIC)
245 state[3] = 0x50;
246
247 if (model == GREE_YAN)
248 state[4] = vertical_swing;
249
250 if (model == GREE_YB1FA || model == GREE_YX1FF) {
251 if (data.mode != climate::CLIMATE_MODE_OFF)
252 state[2] |= GREE_MODEL_A_BIT;
253 if (model == GREE_YX1FF && data.fan_mode == climate::CLIMATE_FAN_HIGH)
254 state[2] |= GREE_FAN_TURBO_BIT;
255 if (vertical_swing == GREE_VDIR_SWING) {
256 state[0] |= GREE_SWING_AUTO_MASK;
257 // YX1FF repeats the automatic swing value in both nibbles, while YB1FA only uses the vertical field.
258 state[4] = model == GREE_YX1FF ? GREE_YX1FF_VANE_SWING : GREE_VDIR_SWING;
259 }
260 }
261
262 if (model == GREE_YAG || model == GREE_YAW1F) {
263 state[2] |= 0x60;
264 state[5] = model == GREE_YAW1F ? 0x80 : 0x40;
265 if (vertical_swing == GREE_VDIR_SWING || horizontal_swing == GREE_HDIR_SWING)
266 state[0] |= GREE_SWING_AUTO_MASK;
267 }
268
269 // YAC, like YAG, keeps vertical swing in the low nibble of byte 4; the byte 0 swing flag alone is not enough.
270 if (model == GREE_YAC || model == GREE_YAG || model == GREE_YAW1F)
271 state[4] = vertical_swing | (horizontal_swing << 4);
272
273 if (model == GREE_YAA || model == GREE_YAC || model == GREE_YAC1FB9) {
274 state[6] = 0x20;
275 if (vertical_swing == GREE_VDIR_SWING)
276 state[0] |= GREE_SWING_AUTO_MASK;
277 }
278
279 if ((model == GREE_YX1FF || model == GREE_YAC1FB9) && data.preset == climate::CLIMATE_PRESET_SLEEP)
280 state[0] |= GREE_PRESET_SLEEP_BIT;
281
282 if (data.fahrenheit) {
283 state[3] |= GREE_FAHRENHEIT_BIT;
284 if (data.half_degree)
285 state[3] |= GREE_TEMP_HALF_DEGREE_BIT;
286 }
287
288 state[GREE_STATE_FRAME_SIZE - 1] = GreeProtocol::calculate_checksum(state);
289 return state;
290}
291
292#ifdef USE_GREE_RECEIVER
293optional<GreeClimateData> GreeClimateCodec::decode(Model model, const GreeState &state) {
294 if (model == GREE_YB1FA || model == GREE_YX1FF)
297}
298#endif // USE_GREE_RECEIVER
299
302 return GREE_MODE_ON | GREE_MODE_COOL;
304 return GREE_MODE_ON | GREE_MODE_DRY;
306 return GREE_MODE_ON | GREE_MODE_HEAT;
308 return GREE_MODE_ON | GREE_MODE_AUTO;
310 return GREE_MODE_ON | GREE_MODE_FAN;
311 return GREE_MODE_OFF;
312}
313
315 if (model == GREE_YX1FF) {
317 return GREE_FAN_1;
319 return GREE_FAN_2;
321 return GREE_FAN_3;
322 return GREE_FAN_AUTO;
323 }
324
326 return GREE_FAN_1;
328 return GREE_FAN_2;
330 return GREE_FAN_3;
331 return GREE_FAN_AUTO;
332}
333
335 switch (swing_mode) {
338 return GREE_HDIR_SWING;
339 default:
340 return GREE_HDIR_MANUAL;
341 }
342}
343
345 switch (swing_mode) {
348 return GREE_VDIR_SWING;
349 default:
350 return GREE_VDIR_MANUAL;
351 }
352}
353
354#ifdef USE_GREE_RECEIVER
355optional<GreeClimateData> GreeClimateCodec::decode_legacy(Model model, const GreeState &state) {
356 if (is_auxiliary_frame(model, state))
357 return {};
358
359 const uint8_t mode = state[0] & GREE_MODE_MASK;
360 const bool power = state[0] & GREE_POWER_MASK;
361 const uint8_t fan = state[0] & GREE_FAN_MASK;
362 const uint8_t temperature = state[1] & GREE_TEMP_MASK;
363
364 // Legacy GREE-branded remotes have several model and OEM variants. Decode the fields that are stable across the
365 // protocol, but do not require the transmitter-side fixed bytes: physical Tadiran/Tosot/GREE remotes may populate
366 // timer, display-temperature, I-Feel, WiFi, vane-position, or otherwise unused fields differently.
367 if (mode > GREE_MODE_HEAT || temperature > GREE_TEMP_MAX - GREE_TEMP_MIN)
368 return {};
369
370 const uint8_t vertical = state[4] & 0x0F;
371 const uint8_t horizontal = (state[4] >> 4) & 0x07;
372 bool swing_vertical = false;
373 bool swing_horizontal = false;
374
375 switch (model) {
376 case GREE_YAG:
377 case GREE_YAW1F:
378 case GREE_YAC:
379 // On YAC1FB bit 6 does not reliably identify vertical swing; use vane fields for both axes.
380 swing_vertical = is_valid_vertical_swing(true, vertical);
381 swing_horizontal = horizontal == GREE_HDIR_SWING;
382 break;
383 case GREE_GENERIC:
384 case GREE_YAN:
385 case GREE_YAA:
386 case GREE_YAC1FB9:
387 default:
388 // ESPHome's YAN transmitter stores automatic vertical swing in byte 4, while YAA/YAC1FB9 use bit 6.
389 // Accept either representation so branded remotes using the standard GREE layout are handled as well.
390 swing_vertical = (state[0] & GREE_SWING_AUTO_MASK) || is_valid_vertical_swing(true, vertical);
391 break;
392 }
393
394 GreeClimateData data{
396 .target_temperature = static_cast<uint8_t>(GREE_TEMP_MIN + temperature),
399 .preset = model == GREE_YAC1FB9 && (state[0] & GREE_SLEEP_MASK) ? climate::CLIMATE_PRESET_SLEEP
401 .feature_bits = static_cast<uint8_t>(state[2] & supported_feature_mask(model)),
402 .fahrenheit = static_cast<bool>(state[3] & GREE_FAHRENHEIT_BIT),
403 .half_degree = static_cast<bool>(state[3] & GREE_TEMP_HALF_DEGREE_BIT),
404 };
405
406 if (swing_vertical && swing_horizontal) {
407 data.swing_mode = climate::CLIMATE_SWING_BOTH;
408 } else if (swing_vertical) {
409 data.swing_mode = climate::CLIMATE_SWING_VERTICAL;
410 } else if (swing_horizontal) {
411 data.swing_mode = climate::CLIMATE_SWING_HORIZONTAL;
412 }
413
414 if (power)
415 data.mode = decode_operation_mode(mode);
416
417 data.fan_mode = decode_fan_mode(model, fan);
418
419 return data;
420}
421
422optional<GreeClimateData> GreeClimateCodec::decode_model_a(Model model, const GreeState &state) {
423 const uint8_t mode = state[0] & GREE_MODE_MASK;
424 const bool power = state[0] & GREE_POWER_MASK;
425 const uint8_t fan = state[0] & GREE_FAN_MASK;
426 const bool swing = state[0] & GREE_SWING_AUTO_MASK;
427 const bool turbo = state[2] & GREE_FAN_TURBO_BIT;
428 const uint8_t temperature = state[1] & GREE_TEMP_MASK;
429
430 // Validate fields that are not represented by Climate where possible. Supported independent feature state is
431 // returned separately so the parent can synchronize its feature switches.
432 if (mode > GREE_MODE_HEAT || temperature > GREE_TEMP_MAX - GREE_TEMP_MIN || !is_valid_timer(state) ||
433 static_cast<bool>(state[2] & GREE_MODEL_A_BIT) != power ||
434 (state[3] & ~(GREE_FAHRENHEIT_BIT | GREE_TEMP_HALF_DEGREE_BIT)) != 0x50 || !is_valid_swing(model, state) ||
435 (state[5] & GREE_BYTE5_FIXED_MASK) != GREE_BYTE5_FIXED_VALUE || state[6] != 0x00 || (state[7] & 0x0F) != 0x00 ||
436 (model == GREE_YX1FF && turbo && fan != GREE_FAN_3)) {
437 return {};
438 }
439
440 GreeClimateData data{
442 .target_temperature = static_cast<uint8_t>(GREE_TEMP_MIN + temperature),
445 .preset = model == GREE_YX1FF && state[0] & GREE_SLEEP_MASK ? climate::CLIMATE_PRESET_SLEEP
447 .feature_bits = static_cast<uint8_t>(state[2] & supported_feature_mask(model)),
448 .fahrenheit = static_cast<bool>(state[3] & GREE_FAHRENHEIT_BIT),
449 .half_degree = static_cast<bool>(state[3] & GREE_TEMP_HALF_DEGREE_BIT),
450 };
451
452 if (power)
453 data.mode = decode_operation_mode(mode);
454
455 data.fan_mode = turbo ? climate::CLIMATE_FAN_HIGH : decode_fan_mode(model, fan);
456
457 return data;
458}
459#endif // USE_GREE_RECEIVER
460
462 if (model == GREE_YAN || model == GREE_YB1FA || model == GREE_YX1FF) {
463 // These remotes only expose a vertical swing control.
466 }
467 if (model == GREE_YX1FF)
469 if (model == GREE_YX1FF || model == GREE_YAC1FB9) {
472 }
473
474 this->model_ = model;
475 this->feature_bits_ = default_feature_bits(model);
476}
477
478void GreeClimate::set_feature_state(GreeFeature feature, bool enabled) {
479 const uint8_t bit = gree_feature_bit(feature);
480 if (!(supported_feature_mask(this->model_) & bit)) {
481 ESP_LOGW(TAG, "GREE model %u does not support feature %u", static_cast<unsigned>(this->model_),
482 static_cast<unsigned>(feature));
483#ifdef USE_GREE_FEATURE_SWITCH
484 this->publish_feature_state_(feature);
485#endif
486 return;
487 }
488
489 // YB1FA reports Turbo as effective HIGH fan. Keep the Climate and feature entities consistent when Turbo is enabled.
490 const bool fan_changed = this->model_ == GREE_YB1FA && feature == GREE_FEATURE_TURBO && enabled &&
492 if (fan_changed)
494
495 assign_bits(this->feature_bits_, bit, enabled);
496 this->transmit_state();
497#ifdef USE_GREE_FEATURE_SWITCH
498 this->publish_feature_state_(feature);
499#endif
500 if (fan_changed)
501 this->publish_state();
502}
503
505 return this->feature_bits_ & gree_feature_bit(feature);
506}
507
508#ifdef USE_GREE_FEATURE_SWITCH
510 climate_ir::ClimateIR::setup();
511 // Apply every restored switch, then send one frame. Models that receive feature state have restoring disabled,
512 // so their switches show the current feature bits instead.
513 bool restored = false;
514 for (size_t feature = 0; feature < this->feature_switches_.size(); feature++) {
515 auto *feature_switch = this->feature_switches_[feature];
516 if (feature_switch == nullptr)
517 continue;
518 if (auto initial = feature_switch->get_initial_state_with_restore_mode()) {
519 // What control() would hand the switch's apply body
520 assign_bits(this->feature_bits_, gree_feature_bit(static_cast<GreeFeature>(feature)),
521 *initial != feature_switch->is_inverted());
522 restored = true;
523 }
524 this->publish_feature_state_(static_cast<GreeFeature>(feature));
525 }
526 if (restored)
527 this->transmit_state();
528}
529
531 this->feature_switches_[static_cast<uint8_t>(feature)] = feature_switch;
532}
533
535 PUBLISH_STATE(this->feature_switches_[static_cast<uint8_t>(feature)], this->get_feature_state(feature));
536}
537
538#ifdef USE_GREE_RECEIVER
540 for (size_t feature = 0; feature < this->feature_switches_.size(); feature++)
541 this->publish_feature_state_(static_cast<GreeFeature>(feature));
542}
543#endif
544#endif // USE_GREE_FEATURE_SWITCH
545
547 // An explicit fan selection exits YB1FA Turbo. Disabling Turbo itself leaves the effective HIGH fan selected.
548 const bool clear_turbo =
549 this->model_ == GREE_YB1FA && call.get_fan_mode().has_value() && this->get_feature_state(GREE_FEATURE_TURBO);
550 if (clear_turbo) {
551 this->feature_bits_ &= ~GREE_MODE_BIT_TURBO;
552#ifdef USE_GREE_FEATURE_SWITCH
554#endif
555 }
557}
558
560 GreeClimateData climate_data{
561 .mode = this->mode,
562 .target_temperature =
563 static_cast<uint8_t>(roundf(clamp<float>(this->target_temperature, GREE_TEMP_MIN, GREE_TEMP_MAX))),
565 .swing_mode = this->swing_mode,
566 .preset = this->preset.value_or(climate::CLIMATE_PRESET_NONE),
567 .feature_bits = this->feature_bits_,
568 };
569#ifdef USE_GREE_FAHRENHEIT
570 if (this->use_fahrenheit_) {
571 const uint8_t half_steps = fahrenheit_half_steps(this->target_temperature);
572 climate_data.target_temperature = GREE_TEMP_MIN + (half_steps >> 1);
573 climate_data.fahrenheit = true;
574 climate_data.half_degree = half_steps & 1;
575 }
576#endif
577 const GreeState state = GreeClimateCodec::encode(this->model_, climate_data);
578
579 auto transmit = this->transmitter_->transmit();
580 GreeProtocol(this->model_).encode(transmit.get_data(), state);
581 transmit.perform();
582}
583
584#ifdef USE_GREE_RECEIVER
586 auto state = GreeProtocol(this->model_).decode(data);
587 if (!state.has_value())
588 return false;
589
590 auto decoded = GreeClimateCodec::decode(this->model_, *state);
591 if (!decoded.has_value()) {
592 ESP_LOGD_HEX(TAG, state->data(), state->size(), GREE_STATE_FRAME_SIZE, "GREE frame rejected for model %u: %s",
593 static_cast<unsigned>(this->model_));
594 return false;
595 }
596
597 this->mode = decoded->mode;
598 const int half_steps = 2 * (decoded->target_temperature - GREE_TEMP_MIN) + decoded->half_degree;
599 float target = GREE_TEMP_MIN + 0.5f * half_steps;
600 // The panel shows (10 * 32 + 18 * GREE_TEMP_MIN + 9 * k) / 10 F; store that degree so it is sent back unchanged
601 if (decoded->fahrenheit) {
602 // The remote truncates the displayed Fahrenheit degree; retain that exact integer
603 // through subsequent feature-only and fan-only transmissions.
604 const int fahrenheit_degree = (10 * 32 + 18 * GREE_TEMP_MIN + 9 * half_steps) / 10;
605 target = fahrenheit_to_celsius(static_cast<float>(fahrenheit_degree));
606 }
607 this->target_temperature = target;
608 this->fan_mode = decoded->fan_mode;
609 this->swing_mode = decoded->swing_mode;
610 this->preset = decoded->preset;
611 this->feature_bits_ = decoded->feature_bits;
612#ifdef USE_GREE_FEATURE_SWITCH
614#endif
615 this->publish_state();
616 return true;
617}
618#endif // USE_GREE_RECEIVER
619
620} // namespace esphome::gree
BedjetMode mode
BedJet operating mode.
constexpr void insert(ValueType value)
Add a single value to the set (std::set compatibility)
constexpr void erase(ValueType value)
Remove a value from the set (std::set compatibility)
This class is used to encode all control actions on a climate device.
Definition climate.h:31
const optional< ClimateFanMode > & get_fan_mode() const
Definition climate.cpp:316
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
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:314
climate::ClimatePresetMask presets_
Definition climate_ir.h:85
climate::ClimateFanModeMask fan_modes_
Definition climate_ir.h:83
climate::ClimateSwingModeMask swing_modes_
Definition climate_ir.h:84
void control(const climate::ClimateCall &call) override
Override control to change settings of the climate device.
static optional< GreeClimateData > decode_legacy(Model model, const GreeState &state)
Definition gree.cpp:355
static GreeState encode(Model model, const GreeClimateData &data)
Definition gree.cpp:234
static uint8_t encode_fan_mode(Model model, climate::ClimateFanMode fan_mode)
Definition gree.cpp:314
static uint8_t encode_operation_mode(climate::ClimateMode mode)
Definition gree.cpp:300
static uint8_t encode_vertical_swing(climate::ClimateSwingMode swing_mode)
Definition gree.cpp:344
static uint8_t encode_horizontal_swing(climate::ClimateSwingMode swing_mode)
Definition gree.cpp:334
static optional< GreeClimateData > decode_model_a(Model model, const GreeState &state)
Definition gree.cpp:422
static optional< GreeClimateData > decode(Model model, const GreeState &state)
Decode a frame whose checksum the protocol layer already checked.
Definition gree.cpp:293
void publish_feature_state_(GreeFeature feature)
Definition gree.cpp:534
std::array< switch_::Switch *, 4 > feature_switches_
Definition gree.h:218
void control(const climate::ClimateCall &call) override
Definition gree.cpp:546
void setup() override
Definition gree.cpp:509
bool on_receive(remote_base::RemoteReceiveData data) override
Definition gree.cpp:585
void register_feature_switch(GreeFeature feature, switch_::Switch *feature_switch)
Definition gree.cpp:530
void set_feature_state(GreeFeature feature, bool enabled)
Definition gree.cpp:478
void set_model(Model model)
Definition gree.cpp:461
bool get_feature_state(GreeFeature feature) const
Definition gree.cpp:504
void transmit_state() override
Definition gree.cpp:559
static bool valid_checksum(const GreeState &state)
Definition gree.cpp:229
static uint8_t calculate_checksum(const GreeState &state)
Definition gree.cpp:219
void encode(remote_base::RemoteTransmitData *data, const GreeState &state) const
Definition gree.cpp:171
optional< GreeState > decode(remote_base::RemoteReceiveData data) const
Definition gree.cpp:190
bool is_valid(uint32_t offset=0) const
Definition remote_base.h:97
void set_carrier_frequency(uint32_t carrier_frequency)
Definition remote_base.h:64
Base class for all switches.
Definition switch.h:43
ClimateSwingMode swing_mode
Definition climate.h:11
float target_temperature
Definition climate.h:0
ClimateFanMode fan_mode
Definition climate.h:3
ClimatePreset preset
Definition climate.h:8
float position
Definition cover.h:0
bool state
Definition fan.h:2
@ CLIMATE_PRESET_NONE
No preset is active.
@ CLIMATE_PRESET_SLEEP
Device is prepared for sleep.
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.
ClimateMode
Enum for all modes a climate device can be in.
@ 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.
ClimateFanMode
NOTE: If adding values, update ClimateFanModeMask in climate_traits.h to use the new last value.
@ 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_QUIET
The fan mode is set to Quiet.
@ CLIMATE_FAN_HIGH
The fan mode is set to High.
optional< climate::ClimateFanMode > decode_fan_mode(uint8_t fan_field, optional< climate::ClimateFanMode > current_mode)
Unassigned values keep the current fan mode.
@ GREE_FEATURE_TURBO
Definition gree.h:123
std::array< uint8_t, GREE_STATE_FRAME_SIZE > GreeState
Definition gree.h:131
@ GREE_YAC1FB9
Definition gree.h:115
@ GREE_GENERIC
Definition gree.h:111
ESPHOME_LOG_TAG(TAG, "gree.climate")
uint8_t fahrenheit_half_steps(float target_celsius)
Return k = 2 * temp_field + half_degree_bit (0..28) for a Celsius target.
Definition gree.cpp:36
std::span< const uint8_t > data
ESPHOME_ALWAYS_INLINE constexpr void assign_bits(T &value, std::type_identity_t< T > mask, bool on)
Set the bits of mask in value when on is true, clear them otherwise.
Definition helpers.h:1150
size_t size_t pos
Definition helpers.h:1333
constexpr float celsius_to_fahrenheit(float value)
Convert degrees Celsius to degrees Fahrenheit.
Definition helpers.h:1863
constexpr float fahrenheit_to_celsius(float value)
Convert degrees Fahrenheit to degrees Celsius.
Definition helpers.h:1865
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
uint16_t temperature
Definition sun_gtil2.cpp:12