ESPHome  2024.5.0
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) {
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  data->space(GREE_HEADER_SPACE);
57 
58  for (int i = 0; i < 4; i++) {
59  for (uint8_t mask = 1; mask > 0; mask <<= 1) { // iterate through bit mask
60  data->mark(GREE_BIT_MARK);
61  bool bit = remote_state[i] & mask;
62  data->space(bit ? GREE_ONE_SPACE : GREE_ZERO_SPACE);
63  }
64  }
65 
66  data->mark(GREE_BIT_MARK);
67  data->space(GREE_ZERO_SPACE);
68  data->mark(GREE_BIT_MARK);
69  data->space(GREE_ONE_SPACE);
70  data->mark(GREE_BIT_MARK);
71  data->space(GREE_ZERO_SPACE);
72 
73  data->mark(GREE_BIT_MARK);
74  data->space(GREE_MESSAGE_SPACE);
75 
76  for (int i = 4; i < 8; i++) {
77  for (uint8_t mask = 1; mask > 0; mask <<= 1) { // iterate through bit mask
78  data->mark(GREE_BIT_MARK);
79  bool bit = remote_state[i] & mask;
80  data->space(bit ? GREE_ONE_SPACE : GREE_ZERO_SPACE);
81  }
82  }
83 
84  data->mark(GREE_BIT_MARK);
85  data->space(0);
86 
87  transmit.perform();
88 }
89 
91  uint8_t operating_mode = GREE_MODE_ON;
92 
93  switch (this->mode) {
95  operating_mode |= GREE_MODE_COOL;
96  break;
98  operating_mode |= GREE_MODE_DRY;
99  break;
101  operating_mode |= GREE_MODE_HEAT;
102  break;
104  operating_mode |= GREE_MODE_AUTO;
105  break;
107  operating_mode |= GREE_MODE_FAN;
108  break;
110  default:
111  operating_mode = GREE_MODE_OFF;
112  break;
113  }
114 
115  return operating_mode;
116 }
117 
119  switch (this->fan_mode.value()) {
121  return GREE_FAN_1;
123  return GREE_FAN_2;
125  return GREE_FAN_3;
127  default:
128  return GREE_FAN_AUTO;
129  }
130 }
131 
133  switch (this->swing_mode) {
136  return GREE_HDIR_SWING;
137  default:
138  return GREE_HDIR_MANUAL;
139  }
140 }
141 
143  switch (this->swing_mode) {
146  return GREE_VDIR_SWING;
147  default:
148  return GREE_VDIR_MANUAL;
149  }
150 }
151 
153  return (uint8_t) roundf(clamp<float>(this->target_temperature, GREE_TEMP_MIN, GREE_TEMP_MAX));
154 }
155 
156 } // namespace gree
157 } // 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:90
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:51
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:62
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:50
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:61
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:142
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
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
const uint8_t GREE_VDIR_AUTO
Definition: gree.h:49
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 uint8_t GREE_TEMP_MIN
Definition: gree.h:10
uint8_t horizontal_swing_()
Definition: gree.cpp:132