ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
climate_ir_lg.cpp
Go to the documentation of this file.
1#include "climate_ir_lg.h"
2#include "esphome/core/log.h"
3
5
6static const char *const TAG = "climate.climate_ir_lg";
7
8// Commands
9const uint32_t COMMAND_MASK = 0xFF000;
10const uint32_t COMMAND_OFF = 0xC0000;
11const uint32_t COMMAND_SWING = 0x10000;
12
13const uint32_t COMMAND_ON_COOL = 0x00000;
14const uint32_t COMMAND_ON_DRY = 0x01000;
16const uint32_t COMMAND_ON_AI = 0x03000;
17const uint32_t COMMAND_ON_HEAT = 0x04000;
18
19const uint32_t COMMAND_COOL = 0x08000;
20const uint32_t COMMAND_DRY = 0x09000;
21const uint32_t COMMAND_FAN_ONLY = 0x0A000;
22const uint32_t COMMAND_AI = 0x0B000;
23const uint32_t COMMAND_HEAT = 0x0C000;
24
25// Fan speed
26const uint32_t FAN_MASK = 0xF0;
27const uint32_t FAN_AUTO = 0x50;
28const uint32_t FAN_MIN = 0x00;
29const uint32_t FAN_MED = 0x20;
30const uint32_t FAN_MAX = 0x40;
31
32// Temperature
33const uint8_t TEMP_RANGE = TEMP_MAX - TEMP_MIN + 1;
34const uint32_t TEMP_MASK = 0xF00;
36
37const uint16_t BITS = 28;
38
40 uint32_t remote_state = 0x8800000;
41
42 // ESP_LOGD(TAG, "climate_lg_ir mode_before_ code: 0x%02X", modeBefore_);
43
44 // Set command
45 if (this->send_swing_cmd_) {
46 this->send_swing_cmd_ = false;
47 remote_state |= COMMAND_SWING;
48 } else {
49 bool climate_is_off = (this->mode_before_ == climate::CLIMATE_MODE_OFF);
50 switch (this->mode) {
52 remote_state |= climate_is_off ? COMMAND_ON_COOL : COMMAND_COOL;
53 break;
55 remote_state |= climate_is_off ? COMMAND_ON_DRY : COMMAND_DRY;
56 break;
58 remote_state |= climate_is_off ? COMMAND_ON_FAN_ONLY : COMMAND_FAN_ONLY;
59 break;
61 remote_state |= climate_is_off ? COMMAND_ON_AI : COMMAND_AI;
62 break;
64 remote_state |= climate_is_off ? COMMAND_ON_HEAT : COMMAND_HEAT;
65 break;
67 default:
68 remote_state |= COMMAND_OFF;
69 break;
70 }
71 }
72
73 this->mode_before_ = this->mode;
74
75 ESP_LOGD(TAG, "climate_lg_ir mode code: 0x%02X", this->mode);
76
77 // Set fan speed
78 if (this->mode == climate::CLIMATE_MODE_OFF) {
79 remote_state |= FAN_AUTO;
80 } else {
81 switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
83 remote_state |= FAN_MAX;
84 break;
86 remote_state |= FAN_MED;
87 break;
89 remote_state |= FAN_MIN;
90 break;
92 default:
93 remote_state |= FAN_AUTO;
94 break;
95 }
96 }
97
98 // Set temperature
100 auto temp = (uint8_t) roundf(clamp<float>(this->target_temperature, TEMP_MIN, TEMP_MAX));
101 remote_state |= ((temp - 15) << TEMP_SHIFT);
102 }
103
104 this->transmit_(remote_state);
105 this->publish_state();
106}
107
109 uint8_t nbits = 0;
110 uint32_t remote_state = 0;
111
112 if (!data.expect_item(this->header_high_, this->header_low_))
113 return false;
114
115 for (nbits = 0; nbits < 32; nbits++) {
116 if (data.expect_item(this->bit_high_, this->bit_one_low_)) {
117 remote_state = (remote_state << 1) | 1;
118 } else if (data.expect_item(this->bit_high_, this->bit_zero_low_)) {
119 remote_state = (remote_state << 1) | 0;
120 } else if (nbits == BITS) {
121 break;
122 } else {
123 return false;
124 }
125 }
126
127 ESP_LOGD(TAG, "Decoded 0x%02" PRIX32, remote_state);
128 if ((remote_state & 0xFF00000) != 0x8800000)
129 return false;
130
131 // Get command
132 if ((remote_state & COMMAND_MASK) == COMMAND_OFF) {
134 } else if ((remote_state & COMMAND_MASK) == COMMAND_SWING) {
135 this->swing_mode =
137 } else {
138 switch (remote_state & COMMAND_MASK) {
139 case COMMAND_DRY:
140 case COMMAND_ON_DRY:
142 break;
143 case COMMAND_FAN_ONLY:
146 break;
147 case COMMAND_AI:
148 case COMMAND_ON_AI:
150 break;
151 case COMMAND_HEAT:
152 case COMMAND_ON_HEAT:
154 break;
155 case COMMAND_COOL:
156 case COMMAND_ON_COOL:
157 default:
159 break;
160 }
161
162 // Get fan speed
165 } else if (this->mode == climate::CLIMATE_MODE_COOL || this->mode == climate::CLIMATE_MODE_DRY ||
167 if ((remote_state & FAN_MASK) == FAN_AUTO) {
169 } else if ((remote_state & FAN_MASK) == FAN_MIN) {
171 } else if ((remote_state & FAN_MASK) == FAN_MED) {
173 } else if ((remote_state & FAN_MASK) == FAN_MAX) {
175 }
176 }
177
178 // Get temperature
180 this->target_temperature = ((remote_state & TEMP_MASK) >> TEMP_SHIFT) + 15;
181 }
182 }
183 this->publish_state();
184
185 return true;
186}
187
189 this->calc_checksum_(value);
190 ESP_LOGD(TAG, "Sending climate_lg_ir code: 0x%02" PRIX32, value);
191
192 auto transmit = this->transmitter_->transmit();
193 auto *data = transmit.get_data();
194
195 data->set_carrier_frequency(38000);
196 data->reserve(2 + BITS * 2u);
197
198 data->item(this->header_high_, this->header_low_);
199
200 for (uint32_t mask = 1UL << (BITS - 1); mask != 0; mask >>= 1) {
201 if (value & mask) {
202 data->item(this->bit_high_, this->bit_one_low_);
203 } else {
204 data->item(this->bit_high_, this->bit_zero_low_);
205 }
206 }
207 data->mark(this->bit_high_);
208 transmit.perform();
209}
211 uint32_t mask = 0xF;
212 uint32_t sum = 0;
213 for (uint8_t i = 1; i < 8; i++) {
214 sum += (value & (mask << (i * 4))) >> (i * 4);
215 }
216
217 value |= (sum & mask);
218}
219
220} // namespace esphome::climate_ir_lg
ClimateMode mode
The active mode of the climate device.
Definition climate.h:293
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:287
float target_temperature
The target temperature of the climate device.
Definition climate.h:274
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:299
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:437
bool on_receive(remote_base::RemoteReceiveData data) override
Handle received IR Buffer.
void transmit_state() override
Transmit via IR the state of this climate controller.
const uint32_t COMMAND_OFF
const uint32_t COMMAND_COOL
const uint32_t COMMAND_ON_FAN_ONLY
const uint32_t TEMP_SHIFT
const uint32_t COMMAND_ON_DRY
const uint32_t COMMAND_MASK
const uint32_t COMMAND_FAN_ONLY
const uint32_t COMMAND_ON_COOL
const uint32_t COMMAND_AI
const uint32_t COMMAND_DRY
const uint32_t COMMAND_HEAT
const uint32_t COMMAND_SWING
const uint32_t COMMAND_ON_AI
const uint32_t COMMAND_ON_HEAT
@ CLIMATE_SWING_OFF
The swing mode is set to Off.
@ CLIMATE_SWING_VERTICAL
The fan mode is set to Vertical.
@ 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_ON
The fan mode is set to On.
@ CLIMATE_FAN_AUTO
The fan mode is set to Auto.
@ CLIMATE_FAN_LOW
The fan mode is set to Low.
@ CLIMATE_FAN_HIGH
The fan mode is set to High.
static void uint32_t