ESPHome  2024.7.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 
9 void GreeClimate::set_model(Model model) { this->model_ = model; }
10 
12  uint8_t remote_state[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00};
13 
14  remote_state[0] = this->fan_speed_() | this->operation_mode_();
15  remote_state[1] = this->temperature_();
16 
17  if (this->model_ == GREE_YAN) {
18  remote_state[2] = 0x60;
19  remote_state[3] = 0x50;
20  remote_state[4] = this->vertical_swing_();
21  }
22 
23  if (this->model_ == GREE_YAC) {
24  remote_state[4] |= (this->horizontal_swing_() << 4);
25  }
26 
27  if (this->model_ == GREE_YAA || this->model_ == GREE_YAC || this->model_ == GREE_YAC1FB9) {
28  remote_state[2] = 0x20; // bits 0..3 always 0000, bits 4..7 TURBO,LIGHT,HEALTH,X-FAN
29  remote_state[3] = 0x50; // bits 4..7 always 0101
30  remote_state[6] = 0x20; // YAA1FB, FAA1FB1, YB1F2 bits 4..7 always 0010
31 
32  if (this->vertical_swing_() == GREE_VDIR_SWING) {
33  remote_state[0] |= (1 << 6); // Enable swing by setting bit 6
34  } else if (this->vertical_swing_() != GREE_VDIR_AUTO) {
35  remote_state[5] = this->vertical_swing_();
36  }
37  }
38 
39  // Calculate the checksum
40  if (this->model_ == GREE_YAN) {
41  remote_state[7] = ((remote_state[0] << 4) + (remote_state[1] << 4) + 0xC0);
42  } else {
43  remote_state[7] =
44  ((((remote_state[0] & 0x0F) + (remote_state[1] & 0x0F) + (remote_state[2] & 0x0F) + (remote_state[3] & 0x0F) +
45  ((remote_state[5] & 0xF0) >> 4) + ((remote_state[6] & 0xF0) >> 4) + ((remote_state[7] & 0xF0) >> 4) + 0x0A) &
46  0x0F)
47  << 4) |
48  (remote_state[7] & 0x0F);
49  }
50 
51  auto transmit = this->transmitter_->transmit();
52  auto *data = transmit.get_data();
54 
55  data->mark(GREE_HEADER_MARK);
56  if (this->model_ == GREE_YAC1FB9) {
57  data->space(GREE_YAC1FB9_HEADER_SPACE);
58  } else {
59  data->space(GREE_HEADER_SPACE);
60  }
61 
62  for (int i = 0; i < 4; i++) {
63  for (uint8_t mask = 1; mask > 0; mask <<= 1) { // iterate through bit mask
64  data->mark(GREE_BIT_MARK);
65  bool bit = remote_state[i] & mask;
66  data->space(bit ? GREE_ONE_SPACE : GREE_ZERO_SPACE);
67  }
68  }
69 
70  data->mark(GREE_BIT_MARK);
71  data->space(GREE_ZERO_SPACE);
72  data->mark(GREE_BIT_MARK);
73  data->space(GREE_ONE_SPACE);
74  data->mark(GREE_BIT_MARK);
75  data->space(GREE_ZERO_SPACE);
76 
77  data->mark(GREE_BIT_MARK);
78  if (this->model_ == GREE_YAC1FB9) {
79  data->space(GREE_YAC1FB9_MESSAGE_SPACE);
80  } else {
81  data->space(GREE_MESSAGE_SPACE);
82  }
83 
84  for (int i = 4; i < 8; i++) {
85  for (uint8_t mask = 1; mask > 0; mask <<= 1) { // iterate through bit mask
86  data->mark(GREE_BIT_MARK);
87  bool bit = remote_state[i] & mask;
88  data->space(bit ? GREE_ONE_SPACE : GREE_ZERO_SPACE);
89  }
90  }
91 
92  data->mark(GREE_BIT_MARK);
93  data->space(0);
94 
95  transmit.perform();
96 }
97 
99  uint8_t operating_mode = GREE_MODE_ON;
100 
101  switch (this->mode) {
103  operating_mode |= GREE_MODE_COOL;
104  break;
106  operating_mode |= GREE_MODE_DRY;
107  break;
109  operating_mode |= GREE_MODE_HEAT;
110  break;
112  operating_mode |= GREE_MODE_AUTO;
113  break;
115  operating_mode |= GREE_MODE_FAN;
116  break;
118  default:
119  operating_mode = GREE_MODE_OFF;
120  break;
121  }
122 
123  return operating_mode;
124 }
125 
127  switch (this->fan_mode.value()) {
129  return GREE_FAN_1;
131  return GREE_FAN_2;
133  return GREE_FAN_3;
135  default:
136  return GREE_FAN_AUTO;
137  }
138 }
139 
141  switch (this->swing_mode) {
144  return GREE_HDIR_SWING;
145  default:
146  return GREE_HDIR_MANUAL;
147  }
148 }
149 
151  switch (this->swing_mode) {
154  return GREE_VDIR_SWING;
155  default:
156  return GREE_VDIR_MANUAL;
157  }
158 }
159 
161  return (uint8_t) roundf(clamp<float>(this->target_temperature, GREE_TEMP_MIN, GREE_TEMP_MAX));
162 }
163 
164 } // namespace gree
165 } // namespace esphome
The fan mode is set to Low.
Definition: climate_mode.h:54
value_type const & value() const
Definition: optional.h:89
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition: climate.h:202
uint8_t operation_mode_()
Definition: gree.cpp:98
const uint32_t GREE_HEADER_SPACE
Definition: gree.h:33
The fan mode is set to Both.
Definition: climate_mode.h:74
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:37
const uint32_t GREE_BIT_MARK
Definition: gree.h:34
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:55
The climate device is set to dry/humidity mode.
Definition: climate_mode.h:22
const uint32_t GREE_ONE_SPACE
Definition: gree.h:35
const uint8_t GREE_MODE_FAN
Definition: gree.h:18
const uint8_t GREE_HDIR_SWING
Definition: gree.h:66
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:54
The fan mode is set to Auto.
Definition: climate_mode.h:52
RemoteTransmitterBase * transmitter_
Definition: remote_base.h:276
const uint8_t GREE_HDIR_MANUAL
Definition: gree.h:65
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:32
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:150
const uint8_t GREE_MODE_ON
Definition: gree.h:21
const uint8_t GREE_MODE_DRY
Definition: gree.h:17
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:31
void transmit_state() override
Definition: gree.cpp:11
const uint32_t GREE_ZERO_SPACE
Definition: gree.h:36
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const uint8_t GREE_VDIR_AUTO
Definition: gree.h:53
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:46
const uint32_t GREE_YAC1FB9_HEADER_SPACE
Definition: gree.h:45
const uint8_t GREE_TEMP_MIN
Definition: gree.h:10
uint8_t horizontal_swing_()
Definition: gree.cpp:140