ESPHome  2022.6.3
coolix.cpp
Go to the documentation of this file.
1 #include "coolix.h"
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace coolix {
7 
8 static const char *const TAG = "coolix.climate";
9 
10 static const uint32_t COOLIX_OFF = 0xB27BE0;
11 static const uint32_t COOLIX_SWING = 0xB26BE0;
12 static const uint32_t COOLIX_LED = 0xB5F5A5;
13 static const uint32_t COOLIX_SILENCE_FP = 0xB5F5B6;
14 
15 // On, 25C, Mode: Auto, Fan: Auto, Zone Follow: Off, Sensor Temp: Ignore.
16 static const uint8_t COOLIX_COOL = 0b0000;
17 static const uint8_t COOLIX_DRY_FAN = 0b0100;
18 static const uint8_t COOLIX_AUTO = 0b1000;
19 static const uint8_t COOLIX_HEAT = 0b1100;
20 static const uint32_t COOLIX_MODE_MASK = 0b1100;
21 static const uint32_t COOLIX_FAN_MASK = 0xF000;
22 static const uint32_t COOLIX_FAN_MODE_AUTO_DRY = 0x1000;
23 static const uint32_t COOLIX_FAN_AUTO = 0xB000;
24 static const uint32_t COOLIX_FAN_MIN = 0x9000;
25 static const uint32_t COOLIX_FAN_MED = 0x5000;
26 static const uint32_t COOLIX_FAN_MAX = 0x3000;
27 
28 // Temperature
29 static const uint8_t COOLIX_TEMP_RANGE = COOLIX_TEMP_MAX - COOLIX_TEMP_MIN + 1;
30 static const uint8_t COOLIX_FAN_TEMP_CODE = 0b11100000; // Part of Fan Mode.
31 static const uint32_t COOLIX_TEMP_MASK = 0b11110000;
32 static const uint8_t COOLIX_TEMP_MAP[COOLIX_TEMP_RANGE] = {
33  0b00000000, // 17C
34  0b00010000, // 18c
35  0b00110000, // 19C
36  0b00100000, // 20C
37  0b01100000, // 21C
38  0b01110000, // 22C
39  0b01010000, // 23C
40  0b01000000, // 24C
41  0b11000000, // 25C
42  0b11010000, // 26C
43  0b10010000, // 27C
44  0b10000000, // 28C
45  0b10100000, // 29C
46  0b10110000 // 30C
47 };
48 
50  uint32_t remote_state = 0xB20F00;
51 
52  if (send_swing_cmd_) {
53  send_swing_cmd_ = false;
54  remote_state = COOLIX_SWING;
55  } else {
56  switch (this->mode) {
58  remote_state |= COOLIX_COOL;
59  break;
61  remote_state |= COOLIX_HEAT;
62  break;
64  remote_state |= COOLIX_AUTO;
65  break;
68  remote_state |= COOLIX_DRY_FAN;
69  break;
71  default:
72  remote_state = COOLIX_OFF;
73  break;
74  }
75  if (this->mode != climate::CLIMATE_MODE_OFF) {
76  if (this->mode != climate::CLIMATE_MODE_FAN_ONLY) {
77  auto temp = (uint8_t) roundf(clamp<float>(this->target_temperature, COOLIX_TEMP_MIN, COOLIX_TEMP_MAX));
78  remote_state |= COOLIX_TEMP_MAP[temp - COOLIX_TEMP_MIN];
79  } else {
80  remote_state |= COOLIX_FAN_TEMP_CODE;
81  }
84  remote_state |= COOLIX_FAN_MODE_AUTO_DRY;
85  } else {
86  switch (this->fan_mode.value()) {
88  remote_state |= COOLIX_FAN_MAX;
89  break;
91  remote_state |= COOLIX_FAN_MED;
92  break;
94  remote_state |= COOLIX_FAN_MIN;
95  break;
97  default:
98  remote_state |= COOLIX_FAN_AUTO;
99  break;
100  }
101  }
102  }
103  }
104  ESP_LOGV(TAG, "Sending coolix code: 0x%06X", remote_state);
105 
106  auto transmit = this->transmitter_->transmit();
107  auto *data = transmit.get_data();
108  remote_base::CoolixProtocol().encode(data, remote_state);
109  transmit.perform();
110 }
111 
113  auto decoded = remote_base::CoolixProtocol().decode(data);
114  if (!decoded.has_value())
115  return false;
116  // Decoded remote state y 3 bytes long code.
117  uint32_t remote_state = *decoded;
118  ESP_LOGV(TAG, "Decoded 0x%06X", remote_state);
119  if ((remote_state & 0xFF0000) != 0xB20000)
120  return false;
121 
122  if (remote_state == COOLIX_OFF) {
124  } else if (remote_state == COOLIX_SWING) {
125  parent->swing_mode =
127  } else {
128  if ((remote_state & COOLIX_MODE_MASK) == COOLIX_HEAT) {
130  } else if ((remote_state & COOLIX_MODE_MASK) == COOLIX_AUTO) {
132  } else if ((remote_state & COOLIX_MODE_MASK) == COOLIX_DRY_FAN) {
133  if ((remote_state & COOLIX_FAN_MASK) == COOLIX_FAN_MODE_AUTO_DRY) {
135  } else {
137  }
138  } else
140 
141  // Fan Speed
142  if ((remote_state & COOLIX_FAN_AUTO) == COOLIX_FAN_AUTO || parent->mode == climate::CLIMATE_MODE_HEAT_COOL ||
143  parent->mode == climate::CLIMATE_MODE_DRY) {
145  } else if ((remote_state & COOLIX_FAN_MIN) == COOLIX_FAN_MIN) {
147  } else if ((remote_state & COOLIX_FAN_MED) == COOLIX_FAN_MED) {
149  } else if ((remote_state & COOLIX_FAN_MAX) == COOLIX_FAN_MAX) {
151  }
152 
153  // Temperature
154  uint8_t temperature_code = remote_state & COOLIX_TEMP_MASK;
155  for (uint8_t i = 0; i < COOLIX_TEMP_RANGE; i++) {
156  if (COOLIX_TEMP_MAP[i] == temperature_code)
157  parent->target_temperature = i + COOLIX_TEMP_MIN;
158  }
159  }
160  parent->publish_state();
161 
162  return true;
163 }
164 
165 } // namespace coolix
166 } // 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:204
optional< CoolixData > decode(RemoteReceiveData data) override
float target_temperature
The target temperature of the climate device.
Definition: climate.h:183
The climate device is set to heat to reach the target temperature.
Definition: climate_mode.h:18
ClimateMode mode
The active mode of the climate device.
Definition: climate.h:175
const uint8_t COOLIX_TEMP_MAX
Definition: coolix.h:10
The climate device is set to dry/humidity mode.
Definition: climate_mode.h:22
void encode(RemoteTransmitData *dst, const CoolixData &data) override
const uint8_t COOLIX_TEMP_MIN
Definition: coolix.h:9
The climate device is set to cool to reach the target temperature.
Definition: climate_mode.h:16
The fan mode is set to Auto.
Definition: climate_mode.h:52
static bool on_coolix(climate::Climate *parent, remote_base::RemoteReceiveData data)
This static method can be used in other climate components that accept the Coolix protocol...
Definition: coolix.cpp:112
void transmit_state() override
Transmit via IR the state of this climate controller.
Definition: coolix.cpp:49
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:74
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition: climate.cpp:384
The fan mode is set to High.
Definition: climate_mode.h:58
The swing mode is set to Off.
Definition: climate_mode.h:70
The climate device is off.
Definition: climate_mode.h:12
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition: climate.h:198
Definition: a4988.cpp:4
The fan mode is set to Medium.
Definition: climate_mode.h:56
The climate device only has the fan enabled, no heating or cooling is taking place.
Definition: climate_mode.h:20
remote_transmitter::RemoteTransmitterComponent * transmitter_
Definition: climate_ir.h:67
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:167