ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
ecocomfort2_hub.cpp
Go to the documentation of this file.
2#ifdef USE_ESP32
3#include "ecocomfort2_hub.h"
6#include "esphome/core/log.h"
7
8#include <cmath>
9#include <cstdio>
10#include <cstring>
11
12namespace esphome::ecocomfort2 {
13
14ESPHOME_LOG_TAG(TAG, "ecocomfort2");
15
17#ifdef USE_BINARY_SENSOR
18 if (this->connected_binary_sensor_ != nullptr)
19 this->connected_binary_sensor_->publish_initial_state(false);
20#endif
21#ifdef USE_TIME
22 // The clock is written after each time-source sync
23 if (this->time_ != nullptr)
24 this->time_->add_on_time_sync_callback([this]() { this->sync_clock_(); });
25#endif
26}
27
28void Ecocomfort2Hub::update() {
29 if (!this->is_ready()) {
30 ESP_LOGD(TAG, "[%s] Not ready, skipping update", this->parent_->address_str());
31 return;
32 }
33 // The GATT client runs the requests one after the other
34 this->read_char_(this->char_handle_state_);
35 this->read_char_(this->char_handle_oper_);
36#ifdef USE_ECOCOMFORT2_SETTINGS
37 // The vendor app can change the settings too
38 this->read_settings_();
39#endif
40}
41
42bool Ecocomfort2Hub::write_char_(uint16_t handle, uint8_t *data, uint16_t len) {
43 auto status = esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), handle, len, data,
44 ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
45 if (status != ESP_GATT_OK) {
46 ESP_LOGW(TAG, "[%s] Write to handle 0x%x failed, status=%d", this->parent_->address_str(), handle, status);
47 return false;
48 }
49 return true;
50}
51
53 auto status = esp_ble_gattc_read_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), handle,
54 ESP_GATT_AUTH_REQ_NONE);
55 if (status != ESP_GATT_OK) {
56 ESP_LOGW(TAG, "[%s] Read request for handle 0x%x failed, status=%d", this->parent_->address_str(), handle, status);
57 return false;
58 }
59 return true;
60}
61
62void Ecocomfort2Hub::dump_config() {
63 ESP_LOGCONFIG(TAG,
64 "Ecocomfort2 Hub:\n"
65 " Address: %s",
66 this->parent_->address_str());
67 LOG_UPDATE_INTERVAL(this);
68#ifdef USE_TIME
69 ESP_LOGCONFIG(TAG, " Clock sync: %s", YESNO(this->time_ != nullptr));
70#endif
71#ifdef USE_SENSOR
72 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
73 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
74 LOG_SENSOR(" ", "VOC", this->voc_sensor_);
75 LOG_SENSOR(" ", "Direction", this->direction_sensor_);
76#endif
77#ifdef USE_BINARY_SENSOR
78 LOG_BINARY_SENSOR(" ", "Connected", this->connected_binary_sensor_);
79 LOG_BINARY_SENSOR(" ", "Boost", this->boost_binary_sensor_);
80#endif
81#ifdef USE_TEXT_SENSOR
82 LOG_TEXT_SENSOR(" ", "Firmware", this->firmware_text_sensor_);
83#endif
84#ifdef USE_ECOCOMFORT2_SETTINGS
85#ifdef USE_NUMBER
86 LOG_NUMBER(" ", "Humidity threshold",
88 LOG_NUMBER(" ", "Luminosity threshold",
90 LOG_NUMBER(" ", "VOC threshold", this->numbers_[static_cast<uint8_t>(NumberSetting::NUMBER_SETTING_VOC_THRESHOLD)]);
91 LOG_NUMBER(" ", "Temperature offset",
93 LOG_NUMBER(" ", "Humidity offset",
94 this->numbers_[static_cast<uint8_t>(NumberSetting::NUMBER_SETTING_HUMIDITY_OFFSET)]);
95#endif
96#ifdef USE_SWITCH
97 LOG_SWITCH(" ", "Humidity advanced",
99 LOG_SWITCH(" ", "VOC advanced", this->switches_[static_cast<uint8_t>(SwitchSetting::SWITCH_SETTING_VOC_ADVANCED)]);
100#endif
101#ifdef USE_SELECT
102 LOG_SELECT(" ", "Season", this->selects_[static_cast<uint8_t>(SelectSetting::SELECT_SETTING_SEASON)]);
103 LOG_SELECT(" ", "Free cooling", this->selects_[static_cast<uint8_t>(SelectSetting::SELECT_SETTING_FREE_COOLING)]);
104#endif
105#endif // USE_ECOCOMFORT2_SETTINGS
106}
107
108void Ecocomfort2Hub::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
109 esp_ble_gattc_cb_param_t *param) {
110 switch (event) {
111 case ESP_GATTC_DISCONNECT_EVT: {
112 ESP_LOGD(TAG, "[%s] Disconnected: reason=%d", this->parent_->address_str(), param->disconnect.reason);
115 this->status_set_warning();
116 this->char_handle_oper_ = 0;
117 this->char_handle_state_ = 0;
118 this->char_handle_info_ = 0;
119 this->char_handle_clock_ = 0;
120#ifdef USE_ECOCOMFORT2_SETTINGS
121 for (auto *settings : {&this->config_char_, &this->advanced_char_}) {
122 settings->handle = 0;
123 settings->known = settings->in_flight = settings->dirty = false;
124 }
125#endif
126 this->set_connected_(false);
127 this->publish_unknown_();
128 break;
129 }
130 case ESP_GATTC_SEARCH_CMPL_EVT: {
131 const auto service_uuid = espbt::ESPBTUUID::from_raw("f4b827c3-e660-4bc8-bdf6-3c8e9b845e0d");
132 const auto oper_uuid = espbt::ESPBTUUID::from_raw("b9d6f678-bc0d-4a73-90c8-60b0f07301f1");
133 const auto state_uuid = espbt::ESPBTUUID::from_raw("438d3433-7e5a-459a-a8e4-66343fad2bb0");
134 const auto info_uuid = espbt::ESPBTUUID::from_raw("f5f56229-dd4f-480f-a829-9189269d8b37");
135 const auto clock_uuid = espbt::ESPBTUUID::from_raw("82788997-49e4-4533-b949-7ed433678044");
136 // Keep the handles, not the characteristics: ble_client frees its services once all nodes are established.
137 auto *oper = this->parent_->get_characteristic(service_uuid, oper_uuid);
138 auto *state = this->parent_->get_characteristic(service_uuid, state_uuid);
139 if (oper == nullptr || state == nullptr) {
140 ESP_LOGW(TAG, "[%s] C_OPER or C_STATE characteristic not found, disconnecting", this->parent_->address_str());
141 this->parent_->disconnect();
142 break;
143 }
144 this->char_handle_oper_ = oper->handle;
145 this->char_handle_state_ = state->handle;
146 // The version and clock characteristics only feed optional features, so a unit without them still works
147 auto *info = this->parent_->get_characteristic(service_uuid, info_uuid);
148 this->char_handle_info_ = info != nullptr ? info->handle : 0;
149 auto *clock = this->parent_->get_characteristic(service_uuid, clock_uuid);
150 this->char_handle_clock_ = clock != nullptr ? clock->handle : 0;
151#ifdef USE_ECOCOMFORT2_SETTINGS
152 const auto config_uuid = espbt::ESPBTUUID::from_raw("d3dac48e-b4e1-4f3a-8715-326ddf1da89a");
153 const auto advanced_uuid = espbt::ESPBTUUID::from_raw("f8b2284e-61dd-44e3-a782-a93c9503ab2d");
154 auto *config = this->parent_->get_characteristic(service_uuid, config_uuid);
155 this->config_char_.handle = config != nullptr ? config->handle : 0;
156 auto *advanced = this->parent_->get_characteristic(service_uuid, advanced_uuid);
157 this->advanced_char_.handle = advanced != nullptr ? advanced->handle : 0;
158#endif
159 this->node_state = espbt::ClientState::ESTABLISHED;
160 this->status_set_warning();
161
162 // A bonded device may already have re-encrypted the link before discovery finished.
163 if (this->parent_->is_paired()) {
164 this->on_paired_();
165 break;
166 }
167 ESP_LOGI(TAG, "[%s] Requesting BLE encryption", this->parent_->address_str());
168 if (this->parent_->pair() != ESP_OK) {
169 ESP_LOGW(TAG, "[%s] BLE pair request failed, disconnecting", this->parent_->address_str());
170 this->parent_->disconnect();
171 break;
172 }
174 ESP_LOGW(TAG, "[%s] BLE authentication timed out, disconnecting", this->parent_->address_str());
175 this->parent_->disconnect();
176 });
177 break;
178 }
179 case ESP_GATTC_READ_CHAR_EVT: {
180 if (param->read.conn_id != this->parent_->get_conn_id()) {
181 break;
182 }
183 const uint16_t handle = param->read.handle;
184 if (handle == this->char_handle_oper_) {
185 if (this->read_ok_(param))
186 this->on_oper_(param->read.value, param->read.value_len);
187 } else if (handle == this->char_handle_state_) {
188 if (this->read_ok_(param))
189 this->on_state_(param->read.value, param->read.value_len);
190 } else if (handle == this->char_handle_info_) {
191 if (this->read_ok_(param))
192 this->on_info_(param->read.value, param->read.value_len);
193#ifdef USE_ECOCOMFORT2_SETTINGS
194 } else if (handle == this->config_char_.handle) {
195 if (this->settings_answer_current_(this->config_char_) && this->read_ok_(param))
196 this->on_config_(param->read.value, param->read.value_len);
197 } else if (handle == this->advanced_char_.handle) {
198 if (this->settings_answer_current_(this->advanced_char_) && this->read_ok_(param))
199 this->on_advanced_(param->read.value, param->read.value_len);
200#endif
201 }
202 break;
203 }
204 default:
205 break;
206 }
207}
208
209bool Ecocomfort2Hub::read_ok_(const esp_ble_gattc_cb_param_t *param) {
210 if (param->read.status == ESP_GATT_INSUF_ENCRYPTION || param->read.status == ESP_GATT_INSUF_AUTHENTICATION) {
211 // ble_client reconnects and pairs again
212 ESP_LOGW(TAG, "[%s] Encryption lost, disconnecting", this->parent_->address_str());
213 this->parent_->disconnect();
214 return false;
215 }
216 if (param->read.status != ESP_GATT_OK) {
217 ESP_LOGW(TAG, "[%s] Read of handle 0x%x failed, status=%d", this->parent_->address_str(), param->read.handle,
218 param->read.status);
219 return false;
220 }
221 return true;
222}
223
224void Ecocomfort2Hub::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
225 if (event != ESP_GAP_BLE_AUTH_CMPL_EVT || !this->parent_->check_addr(param->ble_security.auth_cmpl.bd_addr) ||
226 this->node_state != espbt::ClientState::ESTABLISHED) {
227 return;
228 }
229 if (!param->ble_security.auth_cmpl.success) {
231 ESP_LOGW(TAG, "[%s] BLE authentication failed, reason=%d, disconnecting", this->parent_->address_str(),
232 param->ble_security.auth_cmpl.fail_reason);
233 this->parent_->disconnect();
234 return;
235 }
236 this->on_paired_();
237}
238
241 this->status_clear_warning();
242 ESP_LOGI(TAG, "[%s] Encryption confirmed, device ready", this->parent_->address_str());
243 this->set_connected_(true);
244#ifdef USE_TIME
245 if (this->time_ != nullptr)
246 this->sync_clock_();
247#endif
248#ifdef USE_TEXT_SENSOR
249 if (this->firmware_text_sensor_ != nullptr && this->char_handle_info_ != 0)
250 this->read_char_(this->char_handle_info_);
251#endif
252 this->update();
253}
254
255void Ecocomfort2Hub::set_connected_(bool connected) {
256#ifdef USE_BINARY_SENSOR
257 PUBLISH_STATE(this->connected_binary_sensor_, connected);
258#endif
259}
260
261// Readings from a unit that is out of reach would otherwise look current for hours
263#ifdef USE_SENSOR
264 for (auto *sensor : {this->temperature_sensor_, this->humidity_sensor_, this->voc_sensor_, this->direction_sensor_}) {
265 PUBLISH_STATE(sensor, NAN);
266 }
267#endif
268#ifdef USE_BINARY_SENSOR
269 if (this->boost_binary_sensor_ != nullptr)
270 this->boost_binary_sensor_->invalidate_state();
271#endif
272}
273
274// C_OPER: mode byte, speed/flags byte
275void Ecocomfort2Hub::on_oper_(const uint8_t *data, uint16_t len) {
276 if (len < 2) {
277 ESP_LOGW(TAG, "[%s] C_OPER too short: %u bytes", this->parent_->address_str(), len);
278 return;
279 }
280 this->mode_ = data[0];
281 this->speed_ = data[1];
282 ESP_LOGD(TAG, "[%s] C_OPER: mode=0x%02x, speed=0x%02x", this->parent_->address_str(), this->mode_, this->speed_);
283 for (auto *child : this->children_) {
284 child->on_status();
285 }
286}
287
288// C_STATE: bytes 0-1 the mode and speed the unit runs; byte 1 also has the boost flag (0x40) and the advanced
289// control step (0x20). Byte 2 the air flow direction, bytes 3-4 temperature and 5-6 humidity in hundredths,
290// 7-8 VOC in ppb
291void Ecocomfort2Hub::on_state_(const uint8_t *data, uint16_t len) {
292 if (len < 9) {
293 ESP_LOGW(TAG, "[%s] C_STATE too short: %u bytes", this->parent_->address_str(), len);
294 return;
295 }
296 const float temperature = encode_value<int16_t>(&data[3]) / 100.0f;
297 const float humidity = encode_uint16(data[5], data[6]) / 100.0f;
298 const uint16_t voc = encode_uint16(data[7], data[8]);
299 ESP_LOGD(TAG, "[%s] C_STATE: mode=0x%02x, speed=0x%02x, %.2f °C, %.2f %%, %u ppb, direction=%u",
300 this->parent_->address_str(), data[0], data[1], temperature, humidity, voc, data[2]);
301#ifdef USE_BINARY_SENSOR
302 PUBLISH_STATE(this->boost_binary_sensor_, (data[1] & FLAG_BOOST) != 0);
303#endif
304#ifdef USE_SENSOR
305 PUBLISH_STATE(this->temperature_sensor_, temperature);
306 PUBLISH_STATE(this->humidity_sensor_, humidity);
307 PUBLISH_STATE(this->voc_sensor_, voc);
308 PUBLISH_STATE(this->direction_sensor_, data[2]);
309#endif
310}
311
312// C_INFO: firmware version packed as 4-bit major, 6-bit minor and 6-bit patch
313void Ecocomfort2Hub::on_info_(const uint8_t *data, uint16_t len) {
314 if (len < 2) {
315 ESP_LOGW(TAG, "[%s] C_INFO too short: %u bytes", this->parent_->address_str(), len);
316 return;
317 }
318 const uint16_t field = encode_uint16(data[0] & 0x0F, data[1]);
319 char version[12];
320 snprintf(version, sizeof(version), "%u.%u.%u", data[0] >> 4, field >> 6, field & 0x3F);
321 ESP_LOGD(TAG, "[%s] Firmware %s", this->parent_->address_str(), version);
322#ifdef USE_TEXT_SENSOR
323 PUBLISH_STATE(this->firmware_text_sensor_, version);
324#endif
325}
326
327#ifdef USE_TIME
328// C_CLOCK: two-digit year, month, day, weekday with Monday as 0, hour, minute, second, automatic DST flag
330 if (!this->is_ready() || this->char_handle_clock_ == 0)
331 return;
332 const ESPTime now = this->time_->now();
333 if (!now.is_valid())
334 return;
335 // ESPTime counts Sunday=1 .. Saturday=7, the unit Monday=0 .. Sunday=6
336 const uint8_t weekday = (now.day_of_week + 5) % 7;
337 // The time source already applies DST and every sync rewrites the clock, so the unit must not shift it again
338 uint8_t data[8]{
339 static_cast<uint8_t>(now.year % 100), now.month, now.day_of_month, weekday, now.hour, now.minute, now.second, 0};
340 ESP_LOGD(TAG, "[%s] Syncing clock to %04u-%02u-%02u %02u:%02u:%02u", this->parent_->address_str(), now.year,
341 now.month, now.day_of_month, now.hour, now.minute, now.second);
342 this->write_char_(this->char_handle_clock_, data, sizeof(data));
343}
344#endif
345
346void Ecocomfort2Hub::write_oper(uint8_t mode, uint8_t speed) {
347 if (!this->is_ready()) {
348 ESP_LOGW(TAG, "[%s] Not ready, cannot write C_OPER", this->parent_->address_str());
349 return;
350 }
351 uint8_t data[2]{mode, speed};
352 ESP_LOGD(TAG, "[%s] Writing C_OPER: mode=0x%02x, speed=0x%02x", this->parent_->address_str(), mode, speed);
353 if (!this->write_char_(this->char_handle_oper_, data, sizeof(data)))
354 return;
356}
357
358#ifdef USE_ECOCOMFORT2_SETTINGS
359// In a C_CONFIGURATION write, 0x7F in bytes 0-5 keeps the current value, as the vendor app does
360static constexpr uint8_t CONFIG_KEEP = 0x7F;
361// Threshold bytes: level 0-3 in the low bits, the advanced control flag in bit 7
362static constexpr uint8_t THRESHOLD_ADVANCED = 0x80;
363static constexpr uint8_t THRESHOLD_LEVEL_MASK = 0x7F;
364// Byte 4: season in the high nibble (0 winter, 1 summer), free cooling level 0-3 in the low nibble
365static constexpr uint8_t SEASON_BYTE = 4;
366static constexpr uint8_t SEASON_MASK = 0xF0;
367static constexpr uint8_t FREE_COOLING_MASK = 0x0F;
368static constexpr uint8_t SEASON_SUMMER = 0x10;
369// Nibble values that keep the current season or free cooling level
370static constexpr uint8_t KEEP_SEASON = 0x70;
371static constexpr uint8_t KEEP_FREE_COOLING = 0x0F;
372// The thresholds the humidity and VOC advanced flags belong to
373static constexpr NumberSetting ADVANCED_THRESHOLD[] = {NumberSetting::NUMBER_SETTING_HUMIDITY_THRESHOLD,
375
376// The thresholds are C_CONFIGURATION bytes 1-3
377static constexpr uint8_t threshold_byte(NumberSetting setting) { return static_cast<uint8_t>(setting) + 1; }
378
379// The unit answers within a few seconds; after this long an answer was lost (the BLE event pool was full) and
380// the read starts over, which can at worst accept one late answer as current until the next poll
381static constexpr uint32_t SETTINGS_READ_STALL_MS = 30000;
382
384 if (settings.in_flight) {
385 settings.dirty = true;
386 return;
387 }
388 if (this->read_char_(settings.handle)) {
389 settings.in_flight = true;
391 }
392}
393
394// Clears the read in flight. A write sent since makes its answer stale, so the read is sent again
396 settings.in_flight = false;
397 if (!settings.dirty)
398 return true;
399 settings.dirty = false;
400 this->request_settings_(settings);
401 return false;
402}
403
406 for (auto *settings : {&this->config_char_, &this->advanced_char_}) {
407 if (!settings->needed || settings->handle == 0)
408 continue;
409 if (settings->in_flight) {
410 if (now - settings->sent_ms <= SETTINGS_READ_STALL_MS)
411 continue;
412 ESP_LOGW(TAG, "[%s] Settings read not answered, retrying", this->parent_->address_str());
413 settings->in_flight = settings->dirty = false;
414 }
415 this->request_settings_(*settings);
416 }
417}
418
420 if (!settings.known) {
421 ESP_LOGW(TAG, "[%s] Settings not read yet", this->parent_->address_str());
422 }
423 return settings.known;
424}
425
426// C_CONFIGURATION: role, the humidity, luminosity and VOC thresholds, season and free cooling, then the
427// fields for a unit paired to another one, which are left alone
428void Ecocomfort2Hub::on_config_(const uint8_t *data, uint16_t len) {
429 if (len < sizeof(this->config_bytes_)) {
430 ESP_LOGW(TAG, "[%s] C_CONFIGURATION too short: %u bytes", this->parent_->address_str(), len);
431 return;
432 }
433 if (!this->config_char_.known || memcmp(this->config_bytes_, data, sizeof(this->config_bytes_)) != 0) {
434 ESP_LOGD(TAG, "[%s] C_CONFIGURATION: thresholds=0x%02x 0x%02x 0x%02x, season=0x%02x", this->parent_->address_str(),
435 data[1], data[2], data[3], data[SEASON_BYTE]);
436 }
437 memcpy(this->config_bytes_, data, sizeof(this->config_bytes_));
438 this->config_char_.known = true;
439 this->publish_config_();
440}
441
442// C_ADVANCED_SETTINGS: temperature and humidity offsets in hundredths, signed big-endian
443void Ecocomfort2Hub::on_advanced_(const uint8_t *data, uint16_t len) {
444 if (len < 4) {
445 ESP_LOGW(TAG, "[%s] C_ADVANCED_SETTINGS too short: %u bytes", this->parent_->address_str(), len);
446 return;
447 }
448 const int16_t temperature = encode_value<int16_t>(data);
449 const int16_t humidity = encode_value<int16_t>(data + 2);
450 if (!this->advanced_char_.known || this->offsets_[0] != temperature || this->offsets_[1] != humidity) {
451 ESP_LOGD(TAG, "[%s] C_ADVANCED_SETTINGS: offsets %d %d", this->parent_->address_str(), temperature, humidity);
452 }
453 this->offsets_[0] = temperature;
454 this->offsets_[1] = humidity;
455 this->advanced_char_.known = true;
456 this->publish_offsets_();
457}
458
459// Numbers and selects run their callbacks on every publish, so an unchanged value is skipped: an on_value
460// automation that writes another setting must not trigger itself again
461#ifdef USE_NUMBER
462static void publish_if_changed(number::Number *number, float value) {
463 if (number != nullptr && (!number->has_state() || number->state != value))
464 number->publish_state(value);
465}
466#endif
467#ifdef USE_SELECT
468static void publish_if_changed(select::Select *select, size_t index) {
469 if (select == nullptr)
470 return;
471 const auto current = select->active_index();
472 if (!current.has_value() || *current != index)
473 select->publish_state(index);
474}
475#endif
476
478#ifdef USE_NUMBER
479 for (uint8_t i = 0; i < static_cast<uint8_t>(NumberSetting::NUMBER_SETTING_TEMPERATURE_OFFSET); i++)
480 publish_if_changed(this->numbers_[i], this->config_bytes_[i + 1] & THRESHOLD_LEVEL_MASK);
481#endif
482#ifdef USE_SWITCH
483 for (uint8_t i = 0; i < static_cast<uint8_t>(SwitchSetting::SWITCH_SETTING_COUNT); i++) {
484 const uint8_t threshold = this->config_bytes_[threshold_byte(ADVANCED_THRESHOLD[i])];
485 // The flag only counts while the threshold is set
486 PUBLISH_STATE(this->switches_[i], (threshold & THRESHOLD_ADVANCED) && (threshold & THRESHOLD_LEVEL_MASK));
487 }
488#endif
489#ifdef USE_SELECT
490 const uint8_t &season_byte = this->config_bytes_[SEASON_BYTE];
491 auto *season = this->selects_[static_cast<uint8_t>(SelectSetting::SELECT_SETTING_SEASON)];
492 publish_if_changed(season, static_cast<size_t>((season_byte & SEASON_SUMMER) != 0));
493 // The unit keeps the free cooling level through winter but only runs it in summer. The season's on_value
494 // automations may have changed the season, so it is read again.
495 auto *free_cooling = this->selects_[static_cast<uint8_t>(SelectSetting::SELECT_SETTING_FREE_COOLING)];
496 const uint8_t level = (season_byte & SEASON_SUMMER) != 0 ? season_byte & FREE_COOLING_MASK : 0;
497 if (free_cooling != nullptr && free_cooling->has_index(level))
498 publish_if_changed(free_cooling, static_cast<size_t>(level));
499#endif
500}
501
503#ifdef USE_NUMBER
504 for (uint8_t i = 0; i < 2; i++) {
505 publish_if_changed(this->numbers_[static_cast<uint8_t>(NumberSetting::NUMBER_SETTING_TEMPERATURE_OFFSET) + i],
506 this->offsets_[i] / 100.0f);
507 }
508#endif
509}
510
511// The read sent after the write returns what it stored, and a read already in flight is sent again
512bool Ecocomfort2Hub::write_settings_(SettingsChar &settings, uint8_t *data, uint16_t len) {
513 if (!this->is_ready() || settings.handle == 0) {
514 ESP_LOGW(TAG, "[%s] Not ready, cannot change the setting", this->parent_->address_str());
515 return false;
516 }
517 if (!this->write_char_(settings.handle, data, len))
518 return false;
519 this->request_settings_(settings);
520 return true;
521}
522
523// The readback takes seconds, so the copy and entities follow the write now; it corrects a refused write
524void Ecocomfort2Hub::write_config_(uint8_t index, uint8_t value, uint8_t stored) {
525 // Bytes 6-11 hold the address of the main unit for a secondary one; the unit ignores them while the role byte
526 // is keep, and the vendor app sends them as 0x00 too
527 uint8_t data[12]{CONFIG_KEEP, CONFIG_KEEP, CONFIG_KEEP, CONFIG_KEEP, CONFIG_KEEP, CONFIG_KEEP};
528 data[index] = value;
529 ESP_LOGD(TAG, "[%s] Writing C_CONFIGURATION byte %u: 0x%02x", this->parent_->address_str(), index, value);
530 if (!this->write_settings_(this->config_char_, data, sizeof(data)))
531 return;
532 this->config_bytes_[index] = stored;
533 this->publish_config_();
534}
535
536void Ecocomfort2Hub::write_offsets_(int16_t temperature, int16_t humidity) {
537 uint8_t data[4];
538 put_be16(data, temperature);
539 put_be16(data + 2, humidity);
540 ESP_LOGD(TAG, "[%s] Writing C_ADVANCED_SETTINGS: offsets %d %d", this->parent_->address_str(), temperature, humidity);
541 if (!this->write_settings_(this->advanced_char_, data, sizeof(data)))
542 return;
543 this->offsets_[0] = temperature;
544 this->offsets_[1] = humidity;
545 this->publish_offsets_();
546}
547
548#ifdef USE_NUMBER
549void Ecocomfort2Hub::write_number(NumberSetting setting, float value) {
551 // Both offsets go in one write, so the other one must be known
552 if (!this->settings_known_(this->advanced_char_))
553 return;
554 int16_t offsets[2]{this->offsets_[0], this->offsets_[1]};
555 offsets[setting == NumberSetting::NUMBER_SETTING_HUMIDITY_OFFSET] = static_cast<int16_t>(lroundf(value * 100.0f));
556 this->write_offsets_(offsets[0], offsets[1]);
557 return;
558 }
559 // A threshold keeps its advanced flag, which only exists while the threshold is set
560 if (!this->settings_known_(this->config_char_))
561 return;
562 const uint8_t index = threshold_byte(setting);
563 const uint8_t level = static_cast<uint8_t>(lroundf(value));
564 const bool advanced = (this->config_bytes_[index] & THRESHOLD_ADVANCED) != 0 && level > 0;
565 const uint8_t threshold = level | (advanced ? THRESHOLD_ADVANCED : 0);
566 this->write_config_(index, threshold, threshold);
567}
568#endif
569
570#ifdef USE_SWITCH
571void Ecocomfort2Hub::write_switch(SwitchSetting setting, bool state) {
572 if (!this->settings_known_(this->config_char_))
573 return;
574 const uint8_t index = threshold_byte(ADVANCED_THRESHOLD[static_cast<uint8_t>(setting)]);
575 const uint8_t level = this->config_bytes_[index] & THRESHOLD_LEVEL_MASK;
576 if (state && level == 0) {
577 ESP_LOGW(TAG, "[%s] Set the threshold before turning on advanced control", this->parent_->address_str());
578 return;
579 }
580 const uint8_t threshold = level | (state ? THRESHOLD_ADVANCED : 0);
581 this->write_config_(index, threshold, threshold);
582}
583#endif
584
585#ifdef USE_SELECT
586// The written byte keeps the other nibble with its keep value; the local copy takes the new nibble directly
587void Ecocomfort2Hub::write_select(SelectSetting setting, size_t index) {
588 if (!this->settings_known_(this->config_char_))
589 return;
590 const uint8_t season_byte = this->config_bytes_[SEASON_BYTE];
592 const uint8_t season = index == 1 ? SEASON_SUMMER : 0;
593 this->write_config_(SEASON_BYTE, season | KEEP_FREE_COOLING, season | (season_byte & FREE_COOLING_MASK));
594 return;
595 }
596 // The unit only runs free cooling in summer and keeps its level through winter, so even Off is refused then
597 if ((season_byte & SEASON_SUMMER) == 0) {
598 ESP_LOGW(TAG, "[%s] Free cooling is only available in summer", this->parent_->address_str());
599 return;
600 }
601 const uint8_t level = static_cast<uint8_t>(index);
602 this->write_config_(SEASON_BYTE, KEEP_SEASON | level, (season_byte & SEASON_MASK) | level);
603}
604#endif
605#endif // USE_ECOCOMFORT2_SETTINGS
606
607} // namespace esphome::ecocomfort2
608
609#endif // USE_ESP32
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
bool cancel_timeout(const char *name)
Cancel a timeout function.
void status_clear_warning()
Definition component.h:318
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
bool has_state() const
bool settings_known_(const SettingsChar &settings)
static constexpr uint32_t READBACK_DELAY_MS
bool write_char_(uint16_t handle, uint8_t *data, uint16_t len)
void on_advanced_(const uint8_t *data, uint16_t len)
void write_config_(uint8_t index, uint8_t value, uint8_t stored)
Write one C_CONFIGURATION byte, leaving the others as they are, and keep stored as its local copy.
static constexpr uint32_t AUTH_TIMEOUT_ID
void on_state_(const uint8_t *data, uint16_t len)
void on_config_(const uint8_t *data, uint16_t len)
void on_info_(const uint8_t *data, uint16_t len)
void write_offsets_(int16_t temperature, int16_t humidity)
bool settings_answer_current_(SettingsChar &settings)
switch_::Switch * switches_[static_cast< uint8_t >(SwitchSetting::SWITCH_SETTING_COUNT)]
void request_settings_(SettingsChar &settings)
bool write_settings_(SettingsChar &settings, uint8_t *data, uint16_t len)
static constexpr uint32_t READBACK_TIMEOUT_ID
select::Select * selects_[static_cast< uint8_t >(SelectSetting::SELECT_SETTING_COUNT)]
bool read_ok_(const esp_ble_gattc_cb_param_t *param)
void on_oper_(const uint8_t *data, uint16_t len)
number::Number * numbers_[static_cast< uint8_t >(NumberSetting::NUMBER_SETTING_COUNT)]
static constexpr uint32_t AUTH_TIMEOUT_MS
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
Base-class for all numbers.
Definition number.h:29
void publish_state(float state)
Definition number.cpp:20
Base-class for all selects.
Definition select.h:28
optional< size_t > active_index() const
Return the (optional) index offset of the currently active option.
Definition select.cpp:48
void publish_state(const std::string &state)
Definition select.h:35
ESPTime now()
Get the time in the currently defined timezone.
void add_on_time_sync_callback(F &&callback)
int speed
Definition fan.h:3
bool state
Definition fan.h:2
ESPHOME_LOG_TAG(TAG, "ecocomfort2.fan")
uint32_t value
Definition hal.cpp:59
const void size_t len
Definition hal.h:68
constexpr T encode_value(const uint8_t *bytes)
Encode a value from its constituent bytes (from most to least significant) in an array with length si...
Definition helpers.h:1248
constexpr uint8_t * put_be16(uint8_t *dst, uint16_t value)
Write the low 16, 24 or 32 bits of value to dst, most significant byte first (be) or least significan...
Definition helpers.h:1296
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1227
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
A more user-friendly version of struct tm from time.h.
Definition time.h:25
uint8_t minute
minutes after the hour [0-59]
Definition time.h:34
uint8_t second
seconds after the minute [0-60]
Definition time.h:32
uint8_t hour
hours since midnight [0-23]
Definition time.h:36
bool is_valid(bool check_day_of_week=true, bool check_day_of_year=true) const
Check if this ESPTime is valid (year >= internal::MIN_VALID_YEAR and the requested fields are in rang...
Definition time.h:84
uint8_t day_of_month
day of the month [1-31]
Definition time.h:40
uint16_t year
year
Definition time.h:46
uint8_t month
month; january=1 [1-12]
Definition time.h:44
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:38
One settings characteristic. Only one read is in flight at a time; a write sent meanwhile makes its a...
uint16_t temperature
Definition sun_gtil2.cpp:12
spi_device_handle_t handle