ESPHome 2025.6.3
Loading...
Searching...
No Matches
gree.cpp
Go to the documentation of this file.
1#include "gree.h"
3
4namespace esphome {
5namespace gree {
6
7static const char *const TAG = "gree.climate";
8
10 if (model == GREE_YX1FF) {
11 this->fan_modes_.insert(climate::CLIMATE_FAN_QUIET); // YX1FF 4 speed
12 this->presets_.insert(climate::CLIMATE_PRESET_NONE); // YX1FF sleep mode
13 this->presets_.insert(climate::CLIMATE_PRESET_SLEEP); // YX1FF sleep mode
14 }
15
16 this->model_ = model;
17}
18
20 uint8_t remote_state[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00};
21
22 remote_state[0] = this->fan_speed_() | this->operation_mode_();
23 remote_state[1] = this->temperature_();
24
25 if (this->model_ == GREE_YAN || this->model_ == GREE_YX1FF || this->model_ == GREE_YAG) {
26 remote_state[2] = 0x60;
27 remote_state[3] = 0x50;
28 remote_state[4] = this->vertical_swing_();
29 }
30
31 if (this->model_ == GREE_YAG) {
32 remote_state[5] = 0x40;
33
35 remote_state[0] |= (1 << 6);
36 }
37 }
38
39 if (this->model_ == GREE_YAC || this->model_ == GREE_YAG) {
40 remote_state[4] |= (this->horizontal_swing_() << 4);
41 }
42
43 if (this->model_ == GREE_YAA || this->model_ == GREE_YAC || this->model_ == GREE_YAC1FB9) {
44 remote_state[2] = 0x20; // bits 0..3 always 0000, bits 4..7 TURBO,LIGHT,HEALTH,X-FAN
45 remote_state[3] = 0x50; // bits 4..7 always 0101
46 remote_state[6] = 0x20; // YAA1FB, FAA1FB1, YB1F2 bits 4..7 always 0010
47
48 if (this->vertical_swing_() == GREE_VDIR_SWING) {
49 remote_state[0] |= (1 << 6); // Enable swing by setting bit 6
50 } else if (this->vertical_swing_() != GREE_VDIR_AUTO) {
51 remote_state[5] = this->vertical_swing_();
52 }
53 }
54
55 if (this->model_ == GREE_YX1FF) {
56 if (this->fan_speed_() == GREE_FAN_TURBO) {
57 remote_state[2] |= GREE_FAN_TURBO_BIT;
58 }
59
60 if (this->preset_() == GREE_PRESET_SLEEP) {
61 remote_state[0] |= GREE_PRESET_SLEEP_BIT;
62 }
63 }
64
65 // Calculate the checksum
66 if (this->model_ == GREE_YAN || this->model_ == GREE_YX1FF) {
67 remote_state[7] = ((remote_state[0] << 4) + (remote_state[1] << 4) + 0xC0);
68 } else if (this->model_ == GREE_YAG) {
69 remote_state[7] =
70 ((((remote_state[0] & 0x0F) + (remote_state[1] & 0x0F) + (remote_state[2] & 0x0F) + (remote_state[3] & 0x0F) +
71 ((remote_state[4] & 0xF0) >> 4) + ((remote_state[5] & 0xF0) >> 4) + ((remote_state[6] & 0xF0) >> 4) + 0x0A) &
72 0x0F)
73 << 4);
74 } else {
75 remote_state[7] =
76 ((((remote_state[0] & 0x0F) + (remote_state[1] & 0x0F) + (remote_state[2] & 0x0F) + (remote_state[3] & 0x0F) +
77 ((remote_state[5] & 0xF0) >> 4) + ((remote_state[6] & 0xF0) >> 4) + ((remote_state[7] & 0xF0) >> 4) + 0x0A) &
78 0x0F)
79 << 4) |
80 (remote_state[7] & 0x0F);
81 }
82
83 auto transmit = this->transmitter_->transmit();
84 auto *data = transmit.get_data();
86
87 data->mark(GREE_HEADER_MARK);
88 if (this->model_ == GREE_YAC1FB9) {
89 data->space(GREE_YAC1FB9_HEADER_SPACE);
90 } else {
91 data->space(GREE_HEADER_SPACE);
92 }
93
94 for (int i = 0; i < 4; i++) {
95 for (uint8_t mask = 1; mask > 0; mask <<= 1) { // iterate through bit mask
96 data->mark(GREE_BIT_MARK);
97 bool bit = remote_state[i] & mask;
98 data->space(bit ? GREE_ONE_SPACE : GREE_ZERO_SPACE);
99 }
100 }
101
102 data->mark(GREE_BIT_MARK);
103 data->space(GREE_ZERO_SPACE);
104 data->mark(GREE_BIT_MARK);
105 data->space(GREE_ONE_SPACE);
106 data->mark(GREE_BIT_MARK);
107 data->space(GREE_ZERO_SPACE);
108
109 data->mark(GREE_BIT_MARK);
110 if (this->model_ == GREE_YAC1FB9) {
111 data->space(GREE_YAC1FB9_MESSAGE_SPACE);
112 } else {
113 data->space(GREE_MESSAGE_SPACE);
114 }
115
116 for (int i = 4; i < 8; i++) {
117 for (uint8_t mask = 1; mask > 0; mask <<= 1) { // iterate through bit mask
118 data->mark(GREE_BIT_MARK);
119 bool bit = remote_state[i] & mask;
120 data->space(bit ? GREE_ONE_SPACE : GREE_ZERO_SPACE);
121 }
122 }
123
124 data->mark(GREE_BIT_MARK);
125 data->space(0);
126
127 transmit.perform();
128}
129
131 uint8_t operating_mode = GREE_MODE_ON;
132
133 switch (this->mode) {
135 operating_mode |= GREE_MODE_COOL;
136 break;
138 operating_mode |= GREE_MODE_DRY;
139 break;
141 operating_mode |= GREE_MODE_HEAT;
142 break;
144 operating_mode |= GREE_MODE_AUTO;
145 break;
147 operating_mode |= GREE_MODE_FAN;
148 break;
150 default:
151 operating_mode = GREE_MODE_OFF;
152 break;
153 }
154
155 return operating_mode;
156}
157
159 // YX1FF has 4 fan speeds -- we treat low as quiet and turbo as high
160 if (this->model_ == GREE_YX1FF) {
161 switch (this->fan_mode.value()) {
163 return GREE_FAN_1;
165 return GREE_FAN_2;
167 return GREE_FAN_3;
169 return GREE_FAN_TURBO;
171 default:
172 return GREE_FAN_AUTO;
173 }
174 }
175
176 switch (this->fan_mode.value()) {
178 return GREE_FAN_1;
180 return GREE_FAN_2;
182 return GREE_FAN_3;
184 default:
185 return GREE_FAN_AUTO;
186 }
187}
188
190 switch (this->swing_mode) {
193 return GREE_HDIR_SWING;
194 default:
195 return GREE_HDIR_MANUAL;
196 }
197}
198
200 switch (this->swing_mode) {
203 return GREE_VDIR_SWING;
204 default:
205 return GREE_VDIR_MANUAL;
206 }
207}
208
210 return (uint8_t) roundf(clamp<float>(this->target_temperature, GREE_TEMP_MIN, GREE_TEMP_MAX));
211}
212
214 // YX1FF has sleep preset
215 if (this->model_ == GREE_YX1FF) {
216 switch (this->preset.value()) {
218 return GREE_PRESET_NONE;
220 return GREE_PRESET_SLEEP;
221 default:
222 return GREE_PRESET_NONE;
223 }
224 }
225
226 return GREE_PRESET_NONE;
227}
228
229} // namespace gree
230} // namespace esphome
ClimateMode mode
The active mode of the climate device.
Definition climate.h:173
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:199
float target_temperature
The target temperature of the climate device.
Definition climate.h:186
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:202
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:208
std::set< climate::ClimateFanMode > fan_modes_
Definition climate_ir.h:63
std::set< climate::ClimatePreset > presets_
Definition climate_ir.h:65
void set_model(Model model)
Definition gree.cpp:9
uint8_t horizontal_swing_()
Definition gree.cpp:189
void transmit_state() override
Definition gree.cpp:19
value_type const & value() const
Definition optional.h:89
void set_carrier_frequency(uint32_t carrier_frequency)
Definition remote_base.h:34
@ CLIMATE_PRESET_NONE
No preset is active.
@ CLIMATE_PRESET_SLEEP
Device is prepared for sleep.
@ 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_QUIET
The fan mode is set to Quiet.
@ CLIMATE_FAN_HIGH
The fan mode is set to High.
const uint32_t GREE_IR_FREQUENCY
Definition gree.h:30
const uint8_t GREE_FAN_3
Definition gree.h:27
const uint8_t GREE_PRESET_SLEEP_BIT
Definition gree.h:78
const uint32_t GREE_BIT_MARK
Definition gree.h:33
const uint8_t GREE_FAN_2
Definition gree.h:26
const uint8_t GREE_FAN_AUTO
Definition gree.h:24
const uint32_t GREE_YAC1FB9_MESSAGE_SPACE
Definition gree.h:45
const uint32_t GREE_HEADER_MARK
Definition gree.h:31
const uint8_t GREE_VDIR_AUTO
Definition gree.h:52
const uint32_t GREE_YAC1FB9_HEADER_SPACE
Definition gree.h:44
const uint8_t GREE_FAN_TURBO_BIT
Definition gree.h:75
const uint8_t GREE_MODE_FAN
Definition gree.h:18
const uint8_t GREE_MODE_OFF
Definition gree.h:20
const uint8_t GREE_TEMP_MIN
Definition gree.h:10
const uint8_t GREE_MODE_AUTO
Definition gree.h:14
const uint8_t GREE_VDIR_SWING
Definition gree.h:54
const uint8_t GREE_MODE_ON
Definition gree.h:21
const uint8_t GREE_HDIR_MANUAL
Definition gree.h:64
const uint8_t GREE_MODE_DRY
Definition gree.h:17
const uint32_t GREE_ZERO_SPACE
Definition gree.h:35
const uint8_t GREE_HDIR_SWING
Definition gree.h:65
const uint8_t GREE_PRESET_SLEEP
Definition gree.h:77
const uint8_t GREE_VDIR_MANUAL
Definition gree.h:53
const uint32_t GREE_MESSAGE_SPACE
Definition gree.h:36
@ GREE_YAC1FB9
Definition gree.h:81
@ GREE_YX1FF
Definition gree.h:81
const uint8_t GREE_MODE_COOL
Definition gree.h:15
const uint8_t GREE_TEMP_MAX
Definition gree.h:11
const uint32_t GREE_HEADER_SPACE
Definition gree.h:32
const uint8_t GREE_MODE_HEAT
Definition gree.h:16
const uint8_t GREE_FAN_TURBO
Definition gree.h:74
const uint8_t GREE_PRESET_NONE
Definition gree.h:76
const uint8_t GREE_FAN_1
Definition gree.h:25
const uint32_t GREE_ONE_SPACE
Definition gree.h:34
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7