ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ballu.cpp
Go to the documentation of this file.
1#include "ballu.h"
3#include "esphome/core/log.h"
4
5namespace esphome::ballu {
6
7ESPHOME_LOG_TAG(TAG, "ballu.climate");
8
9const uint16_t BALLU_HEADER_MARK = 9000;
10const uint16_t BALLU_HEADER_SPACE = 4500;
11const uint16_t BALLU_BIT_MARK = 575;
12static constexpr remote_base::BitTimings BALLU_BIT_TIMINGS{
13 .bit_mark = BALLU_BIT_MARK, .one_space = 1675, .zero_space = 550};
14
16
17const uint8_t BALLU_STATE_LENGTH = 13;
18
19const uint8_t BALLU_AUTO = 0;
20const uint8_t BALLU_COOL = 0x20;
21const uint8_t BALLU_DRY = 0x40;
22const uint8_t BALLU_HEAT = 0x80;
23const uint8_t BALLU_FAN = 0xc0;
24
25const uint8_t BALLU_FAN_AUTO = 0xa0;
26const uint8_t BALLU_FAN_HIGH = 0x20;
27const uint8_t BALLU_FAN_MED = 0x40;
28const uint8_t BALLU_FAN_LOW = 0x60;
29
30const uint8_t BALLU_SWING_VER = 0x07;
31const uint8_t BALLU_SWING_HOR = 0xe0;
32const uint8_t BALLU_POWER = 0x20;
33
35 uint8_t remote_state[BALLU_STATE_LENGTH] = {0};
36
37 auto temp = (uint8_t) roundf(clamp(this->target_temperature, YKR_K_002E_TEMP_MIN, YKR_K_002E_TEMP_MAX));
38 auto swing_ver =
40 auto swing_hor =
42
43 remote_state[0] = 0xc3;
44 remote_state[1] = ((temp - 8) << 3) | (swing_ver ? 0 : BALLU_SWING_VER);
45 remote_state[2] = swing_hor ? 0 : BALLU_SWING_HOR;
46 remote_state[9] = (this->mode == climate::CLIMATE_MODE_OFF) ? 0 : BALLU_POWER;
47 remote_state[11] = 0x1e;
48
49 // Fan speed
50 switch (this->fan_mode.value_or(climate::CLIMATE_FAN_ON)) {
52 remote_state[4] |= BALLU_FAN_HIGH;
53 break;
55 remote_state[4] |= BALLU_FAN_MED;
56 break;
58 remote_state[4] |= BALLU_FAN_LOW;
59 break;
61 remote_state[4] |= BALLU_FAN_AUTO;
62 break;
63 default:
64 break;
65 }
66
67 // Mode
68 switch (this->mode) {
70 remote_state[6] |= BALLU_AUTO;
71 break;
73 remote_state[6] |= BALLU_HEAT;
74 break;
76 remote_state[6] |= BALLU_COOL;
77 break;
79 remote_state[6] |= BALLU_DRY;
80 break;
82 remote_state[6] |= BALLU_FAN;
83 break;
85 remote_state[6] |= BALLU_AUTO;
86 default:
87 break;
88 }
89
90 // Checksum
91 for (uint8_t i = 0; i < BALLU_STATE_LENGTH - 1; i++)
92 remote_state[12] += remote_state[i];
93
94 ESP_LOGV_HEX(TAG, remote_state, BALLU_STATE_LENGTH, BALLU_STATE_LENGTH, "Sending: %s");
95
96 // Send code
97 auto transmit = this->transmitter_->transmit();
98 auto *data = transmit.get_data();
99
100 data->set_carrier_frequency(38000);
101
102 // Header
103 data->mark(BALLU_HEADER_MARK);
104 data->space(BALLU_HEADER_SPACE);
105 // Data
106 data->item_bytes<BALLU_BIT_TIMINGS>(remote_state, BALLU_STATE_LENGTH);
107 // Footer
108 data->mark(BALLU_BIT_MARK);
109
110 transmit.perform();
111}
112
114 // Validate header
115 if (!data.expect_item(BALLU_HEADER_MARK, BALLU_HEADER_SPACE)) {
116 ESP_LOGV(TAG, "Header fail");
117 return false;
118 }
119
120 uint8_t remote_state[BALLU_STATE_LENGTH];
121 if (!data.expect_bytes<BALLU_BIT_TIMINGS>(remote_state, BALLU_STATE_LENGTH)) {
122 ESP_LOGV(TAG, "Data fail");
123 return false;
124 }
125 // Validate footer
126 if (!data.expect_mark(BALLU_BIT_MARK)) {
127 ESP_LOGV(TAG, "Footer fail");
128 return false;
129 }
130
131 const uint8_t checksum = sum_bytes(remote_state, BALLU_STATE_LENGTH - 1);
132
133 if (checksum != remote_state[BALLU_STATE_LENGTH - 1]) {
134 ESP_LOGVV(TAG, "Checksum fail");
135 return false;
136 }
137
138 ESP_LOGV_HEX(TAG, remote_state, BALLU_STATE_LENGTH, BALLU_STATE_LENGTH, "Received: %s");
139
140 // verify header remote code
141 if (remote_state[0] != 0xc3)
142 return false;
143
144 // powr on/off button
145 ESP_LOGV(TAG, "Power: %02X", (remote_state[9] & BALLU_POWER));
146
147 if ((remote_state[9] & BALLU_POWER) != BALLU_POWER) {
149 } else {
150 auto mode = remote_state[6] & 0xe0;
151 ESP_LOGV(TAG, "Mode: %02X", mode);
152 switch (mode) {
153 case BALLU_HEAT:
155 break;
156 case BALLU_COOL:
158 break;
159 case BALLU_DRY:
161 break;
162 case BALLU_FAN:
164 break;
165 case BALLU_AUTO:
167 break;
168 default:
169 break;
170 }
171 }
172
173 // Set received temp
174 int temp = remote_state[1] & 0xf8;
175 ESP_LOGVV(TAG, "Temperature Raw: %02X", temp);
176 temp = ((uint8_t) temp >> 3) + 8;
177 ESP_LOGVV(TAG, "Temperature Climate: %u", temp);
178 this->target_temperature = temp;
179
180 // Set received fan speed
181 auto fan = remote_state[4] & 0xe0;
182 ESP_LOGVV(TAG, "Fan: %02X", fan);
183 switch (fan) {
184 case BALLU_FAN_HIGH:
186 break;
187 case BALLU_FAN_MED:
189 break;
190 case BALLU_FAN_LOW:
192 break;
193 case BALLU_FAN_AUTO:
194 default:
196 break;
197 }
198
199 // Set received swing status
200 ESP_LOGVV(TAG, "Swing status: %02X %02X", remote_state[1] & BALLU_SWING_VER, remote_state[2] & BALLU_SWING_HOR);
201 if (((remote_state[1] & BALLU_SWING_VER) != BALLU_SWING_VER) &&
202 ((remote_state[2] & BALLU_SWING_HOR) != BALLU_SWING_HOR)) {
204 } else if ((remote_state[1] & BALLU_SWING_VER) != BALLU_SWING_VER) {
206 } else if ((remote_state[2] & BALLU_SWING_HOR) != BALLU_SWING_HOR) {
208 } else {
210 }
211
212 this->publish_state();
213 return true;
214}
215
216} // namespace esphome::ballu
uint8_t checksum
Definition bl0906.h:3
void transmit_state() override
Transmit via IR the state of this climate controller.
Definition ballu.cpp:34
bool on_receive(remote_base::RemoteReceiveData data) override
Handle received IR Buffer.
Definition ballu.cpp:113
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
const uint16_t BALLU_HEADER_SPACE
Definition ballu.cpp:10
ESPHOME_LOG_TAG(TAG, "ballu.climate")
const uint8_t BALLU_COOL
Definition ballu.cpp:20
const uint8_t BALLU_AUTO
Definition ballu.cpp:19
const uint16_t BALLU_BIT_MARK
Definition ballu.cpp:11
const float YKR_K_002E_TEMP_MIN
Definition ballu.h:10
const uint8_t BALLU_DRY
Definition ballu.cpp:21
const uint8_t BALLU_FAN
Definition ballu.cpp:23
const uint8_t BALLU_STATE_LENGTH
Definition ballu.cpp:17
const uint32_t BALLU_CARRIER_FREQUENCY
Definition ballu.cpp:15
const float YKR_K_002E_TEMP_MAX
Definition ballu.h:11
const uint8_t BALLU_SWING_VER
Definition ballu.cpp:30
const uint8_t BALLU_FAN_LOW
Definition ballu.cpp:28
const uint8_t BALLU_HEAT
Definition ballu.cpp:22
const uint8_t BALLU_FAN_AUTO
Definition ballu.cpp:25
const uint16_t BALLU_HEADER_MARK
Definition ballu.cpp:9
const uint8_t BALLU_FAN_HIGH
Definition ballu.cpp:26
const uint8_t BALLU_POWER
Definition ballu.cpp:32
const uint8_t BALLU_SWING_HOR
Definition ballu.cpp:31
const uint8_t BALLU_FAN_MED
Definition ballu.cpp:27
@ CLIMATE_SWING_OFF
The swing mode is set to Off.
@ CLIMATE_SWING_HORIZONTAL
The fan mode is set to Horizontal.
@ CLIMATE_SWING_VERTICAL
The fan mode is set to Vertical.
@ CLIMATE_SWING_BOTH
The fan mode is set to Both.
@ 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_OFF
The climate device is off.
@ CLIMATE_MODE_AUTO
The climate device is adjusting the temperature dynamically.
@ 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.
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