ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
tcl112.cpp
Go to the documentation of this file.
1#include "tcl112.h"
3#include "esphome/core/log.h"
4
5namespace esphome::tcl112 {
6
7ESPHOME_LOG_TAG(TAG, "tcl112.climate");
8
9const uint16_t TCL112_STATE_LENGTH = 14;
10const uint16_t TCL112_BITS = TCL112_STATE_LENGTH * 8;
11
12const uint8_t TCL112_HEAT = 1;
13const uint8_t TCL112_DRY = 2;
14const uint8_t TCL112_COOL = 3;
15const uint8_t TCL112_FAN = 7;
16const uint8_t TCL112_AUTO = 8;
17
18const uint8_t TCL112_FAN_AUTO = 0;
19const uint8_t TCL112_FAN_LOW = 2;
20const uint8_t TCL112_FAN_MED = 3;
21const uint8_t TCL112_FAN_HIGH = 5;
22
23const uint8_t TCL112_VSWING_MASK = 0x38;
24const uint8_t TCL112_POWER_MASK = 0x04;
25
26const uint8_t TCL112_HALF_DEGREE = 0b00100000;
27
28const uint16_t TCL112_HEADER_MARK = 3100;
29const uint16_t TCL112_HEADER_SPACE = 1650;
30const uint16_t TCL112_BIT_MARK = 500;
31static constexpr remote_base::BitTimings TCL112_BIT_TIMINGS{
32 .bit_mark = TCL112_BIT_MARK, .one_space = 1100, .zero_space = 350};
34
36 uint8_t remote_state[TCL112_STATE_LENGTH] = {0};
37
38 // A known good state. (On, Cool, 24C)
39 remote_state[0] = 0x23;
40 remote_state[1] = 0xCB;
41 remote_state[2] = 0x26;
42 remote_state[3] = 0x01;
43 remote_state[5] = 0x24;
44 remote_state[6] = 0x03;
45 remote_state[7] = 0x07;
46 remote_state[8] = 0x40;
47
48 // Set mode
49 switch (this->mode) {
51 remote_state[6] &= 0xF0;
52 remote_state[6] |= TCL112_AUTO;
53 break;
55 remote_state[6] &= 0xF0;
56 remote_state[6] |= TCL112_COOL;
57 break;
59 remote_state[6] &= 0xF0;
60 remote_state[6] |= TCL112_HEAT;
61 break;
63 remote_state[6] &= 0xF0;
64 remote_state[6] |= TCL112_DRY;
65 break;
67 remote_state[6] &= 0xF0;
68 remote_state[6] |= TCL112_FAN;
69 break;
71 default:
72 remote_state[5] &= ~TCL112_POWER_MASK;
73 break;
74 }
75
76 // Set temperature
77 // Make sure we have desired temp in the correct range.
78 float safecelsius = std::max(this->target_temperature, TCL112_TEMP_MIN);
79 safecelsius = std::min(safecelsius, TCL112_TEMP_MAX);
80 // Convert to integer nr. of half degrees.
81 auto half_degrees = static_cast<uint8_t>(safecelsius * 2);
82 if (half_degrees & 1) { // Do we have a half degree celsius?
83 remote_state[12] |= TCL112_HALF_DEGREE; // Add 0.5 degrees
84 } else {
85 remote_state[12] &= ~TCL112_HALF_DEGREE; // Clear the half degree.
86 }
87 remote_state[7] &= 0xF0; // Clear temp bits.
88 remote_state[7] |= ((uint8_t) TCL112_TEMP_MAX - half_degrees / 2);
89
90 // Set fan
91 uint8_t selected_fan;
92 switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
94 selected_fan = TCL112_FAN_HIGH;
95 break;
97 selected_fan = TCL112_FAN_MED;
98 break;
100 selected_fan = TCL112_FAN_LOW;
101 break;
103 default:
104 selected_fan = TCL112_FAN_AUTO;
105 }
106 remote_state[8] |= selected_fan;
107
108 // Set swing
110 remote_state[8] |= TCL112_VSWING_MASK;
111 }
112
113 // Calculate & set the checksum for the current internal state of the remote.
114 // Stored the checksum value in the last byte.
115 for (uint8_t checksum_byte = 0; checksum_byte < TCL112_STATE_LENGTH - 1; checksum_byte++)
116 remote_state[TCL112_STATE_LENGTH - 1] += remote_state[checksum_byte];
117
118 ESP_LOGV_HEX(TAG, remote_state, TCL112_STATE_LENGTH, TCL112_STATE_LENGTH, "Sending: %s");
119
120 auto transmit = this->transmitter_->transmit();
121 auto *data = transmit.get_data();
122
123 data->set_carrier_frequency(38000);
124
125 // Header
126 data->mark(TCL112_HEADER_MARK);
127 data->space(TCL112_HEADER_SPACE);
128 // Data
129 data->item_bytes<TCL112_BIT_TIMINGS>(remote_state, TCL112_STATE_LENGTH);
130 // Footer
131 data->mark(TCL112_BIT_MARK);
132 data->space(TCL112_GAP);
133
134 transmit.perform();
135}
136
138 // Validate header
139 if (!data.expect_item(TCL112_HEADER_MARK, TCL112_HEADER_SPACE)) {
140 ESP_LOGVV(TAG, "Header fail");
141 return false;
142 }
143
144 uint8_t remote_state[TCL112_STATE_LENGTH];
145 if (!data.expect_bytes<TCL112_BIT_TIMINGS>(remote_state, TCL112_STATE_LENGTH)) {
146 ESP_LOGVV(TAG, "Data fail");
147 return false;
148 }
149 // Validate footer
150 if (!data.expect_mark(TCL112_BIT_MARK)) {
151 ESP_LOGVV(TAG, "Footer fail");
152 return false;
153 }
154
155 // Calculate & set the checksum for the current internal state of the remote.
156 // Stored the checksum value in the last byte.
157 const uint8_t checksum = sum_bytes(remote_state, TCL112_STATE_LENGTH - 1);
158 if (checksum != remote_state[TCL112_STATE_LENGTH - 1]) {
159 ESP_LOGVV(TAG, "Checksum fail");
160 return false;
161 }
162
163 ESP_LOGV_HEX(TAG, remote_state, TCL112_STATE_LENGTH, TCL112_STATE_LENGTH, "Received: %s");
164
165 // two first bytes are constant
166 if (remote_state[0] != 0x23 || remote_state[1] != 0xCB)
167 return false;
168
169 if ((remote_state[5] & TCL112_POWER_MASK) == 0) {
171 } else {
172 auto mode = remote_state[6] & 0x0F;
173 switch (mode) {
174 case TCL112_HEAT:
176 break;
177 case TCL112_COOL:
179 break;
180 case TCL112_DRY:
182 break;
183 case TCL112_FAN:
185 break;
186 case TCL112_AUTO:
188 break;
189 default:
190 break;
191 }
192 }
193 auto temp = TCL112_TEMP_MAX - remote_state[7];
194 if (remote_state[12] & TCL112_HALF_DEGREE)
195 temp += .5f;
196 this->target_temperature = temp;
197 auto fan = remote_state[8] & 0x7;
198 switch (fan) {
199 case TCL112_FAN_HIGH:
201 break;
202 case TCL112_FAN_MED:
204 break;
205 case TCL112_FAN_LOW:
207 break;
208 case TCL112_FAN_AUTO:
209 default:
211 break;
212 }
213 if ((remote_state[8] & TCL112_VSWING_MASK) == TCL112_VSWING_MASK) {
215 } else {
217 }
218
219 this->publish_state();
220 return true;
221}
222
223} // namespace esphome::tcl112
uint8_t checksum
Definition bl0906.h:3
ClimateMode mode
The active mode of the climate device.
Definition climate.h:317
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:311
float target_temperature
The target temperature of the climate device.
Definition climate.h:298
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:323
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:432
void transmit_state() override
Transmit via IR the state of this climate controller.
Definition tcl112.cpp:35
bool on_receive(remote_base::RemoteReceiveData data) override
Handle received IR Buffer.
Definition tcl112.cpp:137
@ 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.
const uint8_t TCL112_DRY
Definition tcl112.cpp:13
const uint8_t TCL112_FAN
Definition tcl112.cpp:15
const uint8_t TCL112_AUTO
Definition tcl112.cpp:16
const float TCL112_TEMP_MAX
Definition tcl112.h:8
const uint16_t TCL112_BIT_MARK
Definition tcl112.cpp:30
const uint8_t TCL112_HALF_DEGREE
Definition tcl112.cpp:26
const uint8_t TCL112_VSWING_MASK
Definition tcl112.cpp:23
const uint16_t TCL112_HEADER_MARK
Definition tcl112.cpp:28
ESPHOME_LOG_TAG(TAG, "tcl112.climate")
const uint16_t TCL112_HEADER_SPACE
Definition tcl112.cpp:29
const uint32_t TCL112_GAP
Definition tcl112.cpp:33
const uint8_t TCL112_FAN_MED
Definition tcl112.cpp:20
const uint8_t TCL112_FAN_HIGH
Definition tcl112.cpp:21
const float TCL112_TEMP_MIN
Definition tcl112.h:9
const uint8_t TCL112_FAN_LOW
Definition tcl112.cpp:19
const uint8_t TCL112_FAN_AUTO
Definition tcl112.cpp:18
const uint8_t TCL112_COOL
Definition tcl112.cpp:14
const uint16_t TCL112_BITS
Definition tcl112.cpp:10
const uint8_t TCL112_POWER_MASK
Definition tcl112.cpp:24
const uint16_t TCL112_STATE_LENGTH
Definition tcl112.cpp:9
const uint8_t TCL112_HEAT
Definition tcl112.cpp:12
constexpr uint8_t sum_bytes(const uint8_t *data, size_t len, uint8_t sum=0)
Add up the bytes of data with size len, truncated to 8 bits: the checksum many serial and IR protocol...
Definition helpers.h:939
static void uint32_t
A bit is a mark (one_mark or zero_mark, both bit_mark unless set) and a space (one_space or zero_spac...
Definition remote_base.h:29