ESPHome  2024.10.2
gree.cpp
Go to the documentation of this file.
1 #include "gree.h"
3 
4 namespace esphome {
5 namespace gree {
6 
7 static 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) {
26  remote_state[2] = 0x60;
27  remote_state[3] = 0x50;
28  remote_state[4] = this->vertical_swing_();
29  }
30 
31  if (this->model_ == GREE_YAC) {
32  remote_state[4] |= (this->horizontal_swing_() << 4);
33  }
34 
35  if (this->model_ == GREE_YAA || this->model_ == GREE_YAC || this->model_ == GREE_YAC1FB9) {
36  remote_state[2] = 0x20; // bits 0..3 always 0000, bits 4..7 TURBO,LIGHT,HEALTH,X-FAN
37  remote_state[3] = 0x50; // bits 4..7 always 0101
38  remote_state[6] = 0x20; // YAA1FB, FAA1FB1, YB1F2 bits 4..7 always 0010
39 
40  if (this->vertical_swing_() == GREE_VDIR_SWING) {
41  remote_state[0] |= (1 << 6); // Enable swing by setting bit 6
42  } else if (this->vertical_swing_() != GREE_VDIR_AUTO) {
43  remote_state[5] = this->vertical_swing_();
44  }
45  }
46 
47  if (this->model_ == GREE_YX1FF) {
48  if (this->fan_speed_() == GREE_FAN_TURBO) {
49  remote_state[2] |= GREE_FAN_TURBO_BIT;
50  }
51 
52  if (this->preset_() == GREE_PRESET_SLEEP) {
53  remote_state[0] |= GREE_PRESET_SLEEP_BIT;
54  }
55  }
56 
57  // Calculate the checksum
58  if (this->model_ == GREE_YAN || this->model_ == GREE_YX1FF) {
59  remote_state[7] = ((remote_state[0] << 4) + (remote_state[1] << 4) + 0xC0);
60  } else {
61  remote_state[7] =
62  ((((remote_state[0] & 0x0F) + (remote_state[1] & 0x0F) + (remote_state[2] & 0x0F) + (remote_state[3] & 0x0F) +
63  ((remote_state[5] & 0xF0) >> 4) + ((remote_state[6] & 0xF0) >> 4) + ((remote_state[7] & 0xF0) >> 4) + 0x0A) &
64  0x0F)
65  << 4) |
66  (remote_state[7] & 0x0F);
67  }
68 
69  auto transmit = this->transmitter_->transmit();
70  auto *data = transmit.get_data();
72 
73  data->mark(GREE_HEADER_MARK);
74  if (this->model_ == GREE_YAC1FB9) {
75  data->space(GREE_YAC1FB9_HEADER_SPACE);
76  } else {
77  data->space(GREE_HEADER_SPACE);
78  }
79 
80  for (int i = 0; i < 4; i++) {
81  for (uint8_t mask = 1; mask > 0; mask <<= 1) { // iterate through bit mask
82  data->mark(GREE_BIT_MARK);
83  bool bit = remote_state[i] & mask;
84  data->space(bit ? GREE_ONE_SPACE : GREE_ZERO_SPACE);
85  }
86  }
87 
88  data->mark(GREE_BIT_MARK);
89  data->space(GREE_ZERO_SPACE);
90  data->mark(GREE_BIT_MARK);
91  data->space(GREE_ONE_SPACE);
92  data->mark(GREE_BIT_MARK);
93  data->space(GREE_ZERO_SPACE);
94 
95  data->mark(GREE_BIT_MARK);
96  if (this->model_ == GREE_YAC1FB9) {
97  data->space(GREE_YAC1FB9_MESSAGE_SPACE);
98  } else {
99  data->space(GREE_MESSAGE_SPACE);
100  }
101 
102  for (int i = 4; i < 8; i++) {
103  for (uint8_t mask = 1; mask > 0; mask <<= 1) { // iterate through bit mask
104  data->mark(GREE_BIT_MARK);
105  bool bit = remote_state[i] & mask;
106  data->space(bit ? GREE_ONE_SPACE : GREE_ZERO_SPACE);
107  }
108  }
109 
110  data->mark(GREE_BIT_MARK);
111  data->space(0);
112 
113  transmit.perform();
114 }
115 
117  uint8_t operating_mode = GREE_MODE_ON;
118 
119  switch (this->mode) {
121  operating_mode |= GREE_MODE_COOL;
122  break;
124  operating_mode |= GREE_MODE_DRY;
125  break;
127  operating_mode |= GREE_MODE_HEAT;
128  break;
130  operating_mode |= GREE_MODE_AUTO;
131  break;
133  operating_mode |= GREE_MODE_FAN;
134  break;
136  default:
137  operating_mode = GREE_MODE_OFF;
138  break;
139  }
140 
141  return operating_mode;
142 }
143 
145  // YX1FF has 4 fan speeds -- we treat low as quiet and turbo as high
146  if (this->model_ == GREE_YX1FF) {
147  switch (this->fan_mode.value()) {
149  return GREE_FAN_1;
151  return GREE_FAN_2;
153  return GREE_FAN_3;
155  return GREE_FAN_TURBO;
157  default:
158  return GREE_FAN_AUTO;
159  }
160  }
161 
162  switch (this->fan_mode.value()) {
164  return GREE_FAN_1;
166  return GREE_FAN_2;
168  return GREE_FAN_3;
170  default:
171  return GREE_FAN_AUTO;
172  }
173 }
174 
176  switch (this->swing_mode) {
179  return GREE_HDIR_SWING;
180  default:
181  return GREE_HDIR_MANUAL;
182  }
183 }
184 
186  switch (this->swing_mode) {
189  return GREE_VDIR_SWING;
190  default:
191  return GREE_VDIR_MANUAL;
192  }
193 }
194 
196  return (uint8_t) roundf(clamp<float>(this->target_temperature, GREE_TEMP_MIN, GREE_TEMP_MAX));
197 }
198 
200  // YX1FF has sleep preset
201  if (this->model_ == GREE_YX1FF) {
202  switch (this->preset.value()) {
204  return GREE_PRESET_NONE;
206  return GREE_PRESET_SLEEP;
207  default:
208  return GREE_PRESET_NONE;
209  }
210  }
211 
212  return GREE_PRESET_NONE;
213 }
214 
215 } // namespace gree
216 } // namespace esphome
The fan mode is set to Low.
Definition: climate_mode.h:54
value_type const & value() const
Definition: optional.h:89
The fan mode is set to Quiet.
Definition: climate_mode.h:66
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition: climate.h:202
uint8_t operation_mode_()
Definition: gree.cpp:116
const uint32_t GREE_HEADER_SPACE
Definition: gree.h:32
The fan mode is set to Both.
Definition: climate_mode.h:74
const uint8_t GREE_PRESET_SLEEP_BIT
Definition: gree.h:78
float target_temperature
The target temperature of the climate device.
Definition: climate.h:186
void set_carrier_frequency(uint32_t carrier_frequency)
Definition: remote_base.h:34
const uint32_t GREE_MESSAGE_SPACE
Definition: gree.h:36
const uint8_t GREE_FAN_TURBO_BIT
Definition: gree.h:75
const uint32_t GREE_BIT_MARK
Definition: gree.h:33
The climate device is set to heat to reach the target temperature.
Definition: climate_mode.h:18
const uint8_t GREE_FAN_AUTO
Definition: gree.h:24
const uint8_t GREE_FAN_3
Definition: gree.h:27
ClimateMode mode
The active mode of the climate device.
Definition: climate.h:173
const uint8_t GREE_VDIR_SWING
Definition: gree.h:54
The climate device is set to dry/humidity mode.
Definition: climate_mode.h:22
const uint32_t GREE_ONE_SPACE
Definition: gree.h:34
const uint8_t GREE_MODE_FAN
Definition: gree.h:18
Device is prepared for sleep.
Definition: climate_mode.h:96
const uint8_t GREE_HDIR_SWING
Definition: gree.h:65
const uint8_t GREE_MODE_OFF
Definition: gree.h:20
The fan mode is set to Horizontal.
Definition: climate_mode.h:78
The climate device is set to cool to reach the target temperature.
Definition: climate_mode.h:16
const uint8_t GREE_VDIR_MANUAL
Definition: gree.h:53
The fan mode is set to Auto.
Definition: climate_mode.h:52
optional< ClimatePreset > preset
The active preset of the climate device.
Definition: climate.h:208
const uint8_t GREE_PRESET_NONE
Definition: gree.h:76
std::set< climate::ClimatePreset > presets_
Definition: climate_ir.h:65
RemoteTransmitterBase * transmitter_
Definition: remote_base.h:276
const uint8_t GREE_HDIR_MANUAL
Definition: gree.h:64
const uint8_t GREE_MODE_AUTO
Definition: gree.h:14
const uint8_t GREE_MODE_COOL
Definition: gree.h:15
const uint32_t GREE_HEADER_MARK
Definition: gree.h:31
const uint8_t GREE_FAN_1
Definition: gree.h:25
The climate device is set to heat/cool to reach the target temperature.
Definition: climate_mode.h:14
The fan mode is set to Vertical.
Definition: climate_mode.h:76
void set_model(Model model)
Definition: gree.cpp:9
uint8_t vertical_swing_()
Definition: gree.cpp:185
const uint8_t GREE_MODE_ON
Definition: gree.h:21
const uint8_t GREE_MODE_DRY
Definition: gree.h:17
const uint8_t GREE_PRESET_SLEEP
Definition: gree.h:77
const uint8_t GREE_FAN_TURBO
Definition: gree.h:74
The fan mode is set to High.
Definition: climate_mode.h:58
The climate device is off.
Definition: climate_mode.h:12
const uint8_t GREE_MODE_HEAT
Definition: gree.h:16
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition: climate.h:199
const uint32_t GREE_IR_FREQUENCY
Definition: gree.h:30
void transmit_state() override
Definition: gree.cpp:19
const uint32_t GREE_ZERO_SPACE
Definition: gree.h:35
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const uint8_t GREE_VDIR_AUTO
Definition: gree.h:52
std::set< climate::ClimateFanMode > fan_modes_
Definition: climate_ir.h:63
const uint8_t GREE_TEMP_MAX
Definition: gree.h:11
The fan mode is set to Medium.
Definition: climate_mode.h:56
const uint8_t GREE_FAN_2
Definition: gree.h:26
The climate device only has the fan enabled, no heating or cooling is taking place.
Definition: climate_mode.h:20
const uint32_t GREE_YAC1FB9_MESSAGE_SPACE
Definition: gree.h:45
const uint32_t GREE_YAC1FB9_HEADER_SPACE
Definition: gree.h:44
const uint8_t GREE_TEMP_MIN
Definition: gree.h:10
uint8_t horizontal_swing_()
Definition: gree.cpp:175