16#ifdef USE_BINARY_SENSOR
17 if (this->connected_binary_sensor_ !=
nullptr)
18 this->connected_binary_sensor_->publish_initial_state(
false);
22 if (this->
time_ !=
nullptr)
27void Ecocomfort2Hub::update() {
28 if (!this->is_ready()) {
35#ifdef USE_ECOCOMFORT2_SETTINGS
43 ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
44 if (
status != ESP_GATT_OK) {
53 ESP_GATT_AUTH_REQ_NONE);
54 if (
status != ESP_GATT_OK) {
61void Ecocomfort2Hub::dump_config() {
66 LOG_UPDATE_INTERVAL(
this);
68 ESP_LOGCONFIG(TAG,
" Clock sync: %s", YESNO(this->
time_ !=
nullptr));
71 LOG_SENSOR(
" ",
"Temperature", this->temperature_sensor_);
72 LOG_SENSOR(
" ",
"Humidity", this->humidity_sensor_);
73 LOG_SENSOR(
" ",
"VOC", this->voc_sensor_);
74 LOG_SENSOR(
" ",
"Direction", this->direction_sensor_);
76#ifdef USE_BINARY_SENSOR
77 LOG_BINARY_SENSOR(
" ",
"Connected", this->connected_binary_sensor_);
78 LOG_BINARY_SENSOR(
" ",
"Boost", this->boost_binary_sensor_);
81 LOG_TEXT_SENSOR(
" ",
"Firmware", this->firmware_text_sensor_);
83#ifdef USE_ECOCOMFORT2_SETTINGS
85 LOG_NUMBER(
" ",
"Humidity threshold",
87 LOG_NUMBER(
" ",
"Luminosity threshold",
90 LOG_NUMBER(
" ",
"Temperature offset",
92 LOG_NUMBER(
" ",
"Humidity offset",
96 LOG_SWITCH(
" ",
"Humidity advanced",
107void Ecocomfort2Hub::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
108 esp_ble_gattc_cb_param_t *param) {
110 case ESP_GATTC_DISCONNECT_EVT: {
111 ESP_LOGD(TAG,
"[%s] Disconnected: reason=%d", this->
parent_->
address_str(), param->disconnect.reason);
119#ifdef USE_ECOCOMFORT2_SETTINGS
121 settings->handle = 0;
122 settings->known = settings->in_flight = settings->dirty =
false;
129 case ESP_GATTC_SEARCH_CMPL_EVT: {
130 const auto service_uuid = espbt::ESPBTUUID::from_raw(
"f4b827c3-e660-4bc8-bdf6-3c8e9b845e0d");
131 const auto oper_uuid = espbt::ESPBTUUID::from_raw(
"b9d6f678-bc0d-4a73-90c8-60b0f07301f1");
132 const auto state_uuid = espbt::ESPBTUUID::from_raw(
"438d3433-7e5a-459a-a8e4-66343fad2bb0");
133 const auto info_uuid = espbt::ESPBTUUID::from_raw(
"f5f56229-dd4f-480f-a829-9189269d8b37");
134 const auto clock_uuid = espbt::ESPBTUUID::from_raw(
"82788997-49e4-4533-b949-7ed433678044");
138 if (oper ==
nullptr ||
state ==
nullptr) {
139 ESP_LOGW(TAG,
"[%s] C_OPER or C_STATE characteristic not found, disconnecting", this->
parent_->
address_str());
150#ifdef USE_ECOCOMFORT2_SETTINGS
151 const auto config_uuid = espbt::ESPBTUUID::from_raw(
"d3dac48e-b4e1-4f3a-8715-326ddf1da89a");
152 const auto advanced_uuid = espbt::ESPBTUUID::from_raw(
"f8b2284e-61dd-44e3-a782-a93c9503ab2d");
158 this->
node_state = espbt::ClientState::ESTABLISHED;
168 ESP_LOGW(TAG,
"[%s] BLE pair request failed, disconnecting", this->
parent_->
address_str());
173 ESP_LOGW(TAG,
"[%s] BLE authentication timed out, disconnecting", this->
parent_->
address_str());
178 case ESP_GATTC_READ_CHAR_EVT: {
179 if (param->read.conn_id != this->parent_->get_conn_id()) {
182 const uint16_t
handle = param->read.handle;
185 this->
on_oper_(param->read.value, param->read.value_len);
188 this->
on_state_(param->read.value, param->read.value_len);
191 this->
on_info_(param->read.value, param->read.value_len);
192#ifdef USE_ECOCOMFORT2_SETTINGS
195 this->
on_config_(param->read.value, param->read.value_len);
198 this->
on_advanced_(param->read.value, param->read.value_len);
209 if (param->read.status == ESP_GATT_INSUF_ENCRYPTION || param->read.status == ESP_GATT_INSUF_AUTHENTICATION) {
215 if (param->read.status != ESP_GATT_OK) {
216 ESP_LOGW(TAG,
"[%s] Read of handle 0x%x failed, status=%d", this->
parent_->
address_str(), param->read.handle,
223void Ecocomfort2Hub::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
224 if (event != ESP_GAP_BLE_AUTH_CMPL_EVT || !this->
parent_->
check_addr(param->ble_security.auth_cmpl.bd_addr) ||
225 this->node_state != espbt::ClientState::ESTABLISHED) {
228 if (!param->ble_security.auth_cmpl.success) {
230 ESP_LOGW(TAG,
"[%s] BLE authentication failed, reason=%d, disconnecting", this->
parent_->
address_str(),
231 param->ble_security.auth_cmpl.fail_reason);
241 ESP_LOGI(TAG,
"[%s] Encryption confirmed, device ready", this->
parent_->
address_str());
244 if (this->
time_ !=
nullptr)
247#ifdef USE_TEXT_SENSOR
255#ifdef USE_BINARY_SENSOR
256 PUBLISH_STATE(this->connected_binary_sensor_, connected);
263 for (
auto *sensor : {this->temperature_sensor_, this->humidity_sensor_, this->voc_sensor_, this->direction_sensor_}) {
264 PUBLISH_STATE(sensor, NAN);
267#ifdef USE_BINARY_SENSOR
268 if (this->boost_binary_sensor_ !=
nullptr)
269 this->boost_binary_sensor_->invalidate_state();
279 this->
mode_ = data[0];
281 ESP_LOGD(TAG,
"[%s] C_OPER: mode=0x%02x, speed=0x%02x", this->
parent_->
address_str(), this->mode_, this->speed_);
282#ifdef ECOCOMFORT2_CHILD_COUNT
298 const float humidity =
encode_uint16(data[5], data[6]) / 100.0f;
300 ESP_LOGD(TAG,
"[%s] C_STATE: mode=0x%02x, speed=0x%02x, %.2f °C, %.2f %%, %u ppb, direction=%u",
302#ifdef USE_BINARY_SENSOR
303 PUBLISH_STATE(this->boost_binary_sensor_, (data[1] & FLAG_BOOST) != 0);
306 PUBLISH_STATE(this->temperature_sensor_,
temperature);
307 PUBLISH_STATE(this->humidity_sensor_, humidity);
308 PUBLISH_STATE(this->voc_sensor_, voc);
309 PUBLISH_STATE(this->direction_sensor_, data[2]);
319 const uint16_t field =
encode_uint16(data[0] & 0x0F, data[1]);
321 snprintf(version,
sizeof(version),
"%u.%u.%u", data[0] >> 4, field >> 6, field & 0x3F);
323#ifdef USE_TEXT_SENSOR
324 PUBLISH_STATE(this->firmware_text_sensor_, version);
341 ESP_LOGD(TAG,
"[%s] Syncing clock to %04u-%02u-%02u %02u:%02u:%02u", this->
parent_->
address_str(), now.
year,
347void Ecocomfort2Hub::write_oper(uint8_t
mode, uint8_t speed) {
348 if (!this->is_ready()) {
353 ESP_LOGD(TAG,
"[%s] Writing C_OPER: mode=0x%02x, speed=0x%02x", this->
parent_->
address_str(), mode, speed);
359#ifdef USE_ECOCOMFORT2_SETTINGS
361static constexpr uint8_t CONFIG_KEEP = 0x7F;
363static constexpr uint8_t THRESHOLD_ADVANCED = 0x80;
364static constexpr uint8_t THRESHOLD_LEVEL_MASK = 0x7F;
366static constexpr uint8_t SEASON_BYTE = 4;
367static constexpr uint8_t SEASON_MASK = 0xF0;
368static constexpr uint8_t FREE_COOLING_MASK = 0x0F;
369static constexpr uint8_t SEASON_SUMMER = 0x10;
371static constexpr uint8_t KEEP_SEASON = 0x70;
372static constexpr uint8_t KEEP_FREE_COOLING = 0x0F;
378static constexpr uint8_t threshold_byte(
NumberSetting setting) {
return static_cast<uint8_t
>(setting) + 1; }
382static constexpr uint32_t SETTINGS_READ_STALL_MS = 30000;
386 settings.
dirty =
true;
400 settings.
dirty =
false;
408 if (!settings->needed || settings->handle == 0)
410 if (settings->in_flight) {
411 if (now - settings->sent_ms <= SETTINGS_READ_STALL_MS)
413 ESP_LOGW(TAG,
"[%s] Settings read not answered, retrying", this->
parent_->
address_str());
414 settings->in_flight = settings->dirty =
false;
421 if (!settings.
known) {
424 return settings.
known;
431 ESP_LOGW(TAG,
"[%s] C_CONFIGURATION too short: %u bytes", this->
parent_->
address_str(), len);
435 ESP_LOGD(TAG,
"[%s] C_CONFIGURATION: thresholds=0x%02x 0x%02x 0x%02x, season=0x%02x", this->
parent_->
address_str(),
436 data[1], data[2], data[3], data[SEASON_BYTE]);
446 ESP_LOGW(TAG,
"[%s] C_ADVANCED_SETTINGS too short: %u bytes", this->
parent_->
address_str(), len);
452 ESP_LOGD(TAG,
"[%s] C_ADVANCED_SETTINGS: offsets %d %d", this->
parent_->
address_str(), temperature, humidity);
469static void publish_if_changed(
select::Select *select,
size_t index) {
470 if (select ==
nullptr)
473 if (!current.has_value() || *current != index)
485 const uint8_t threshold = this->
config_bytes_[threshold_byte(ADVANCED_THRESHOLD[i])];
487 PUBLISH_STATE(this->
switches_[i], (threshold & THRESHOLD_ADVANCED) && (threshold & THRESHOLD_LEVEL_MASK));
491 const uint8_t &season_byte = this->
config_bytes_[SEASON_BYTE];
493 publish_if_changed(season,
static_cast<size_t>((season_byte & SEASON_SUMMER) != 0));
497 const uint8_t level = (season_byte & SEASON_SUMMER) != 0 ? season_byte & FREE_COOLING_MASK : 0;
498 if (free_cooling !=
nullptr && free_cooling->has_index(level))
499 publish_if_changed(free_cooling,
static_cast<size_t>(level));
505 for (uint8_t i = 0; i < 2; i++) {
514 if (!this->is_ready() || settings.
handle == 0) {
515 ESP_LOGW(TAG,
"[%s] Not ready, cannot change the setting", this->
parent_->
address_str());
528 uint8_t data[12]{CONFIG_KEEP, CONFIG_KEEP, CONFIG_KEEP, CONFIG_KEEP, CONFIG_KEEP, CONFIG_KEEP};
539 const auto humidity_bytes =
decode_value(
static_cast<uint16_t
>(humidity));
540 uint8_t data[4]{temperature_bytes[0], temperature_bytes[1], humidity_bytes[0], humidity_bytes[1]};
541 ESP_LOGD(TAG,
"[%s] Writing C_ADVANCED_SETTINGS: offsets %d %d", this->
parent_->
address_str(), temperature, humidity);
563 const uint8_t index = threshold_byte(setting);
564 const uint8_t level =
static_cast<uint8_t
>(lroundf(
value));
565 const bool advanced = (this->
config_bytes_[index] & THRESHOLD_ADVANCED) != 0 && level > 0;
566 const uint8_t threshold = level | (advanced ? THRESHOLD_ADVANCED : 0);
575 const uint8_t index = threshold_byte(ADVANCED_THRESHOLD[
static_cast<uint8_t
>(setting)]);
576 const uint8_t level = this->
config_bytes_[index] & THRESHOLD_LEVEL_MASK;
577 if (
state && level == 0) {
578 ESP_LOGW(TAG,
"[%s] Set the threshold before turning on advanced control", this->
parent_->
address_str());
581 const uint8_t threshold = level | (
state ? THRESHOLD_ADVANCED : 0);
588void Ecocomfort2Hub::write_select(
SelectSetting setting,
size_t index) {
591 const uint8_t season_byte = this->
config_bytes_[SEASON_BYTE];
593 const uint8_t season = index == 1 ? SEASON_SUMMER : 0;
594 this->
write_config_(SEASON_BYTE, season | KEEP_FREE_COOLING, season | (season_byte & FREE_COOLING_MASK));
598 if ((season_byte & SEASON_SUMMER) == 0) {
599 ESP_LOGW(TAG,
"[%s] Free cooling is only available in summer", this->
parent_->
address_str());
602 const uint8_t level =
static_cast<uint8_t
>(index);
603 this->
write_config_(SEASON_BYTE, KEEP_SEASON | level, (season_byte & SEASON_MASK) | level);
608#ifdef ECOCOMFORT2_CHILD_COUNT
609void Ecocomfort2Hub::register_child(Ecocomfort2Client *obj) {
611 obj->set_parent(
this);
BedjetMode mode
BedJet operating mode.
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.
void status_set_warning()
bool cancel_timeout(const char *name)
Cancel a timeout function.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
void status_clear_warning()
espbt::ClientState node_state
bool settings_known_(const SettingsChar &settings)
uint16_t char_handle_state_
SettingsChar config_char_
static constexpr uint32_t READBACK_DELAY_MS
bool write_char_(uint16_t handle, uint8_t *data, uint16_t len)
bool read_char_(uint16_t handle)
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)
uint16_t char_handle_oper_
void write_offsets_(int16_t temperature, int16_t humidity)
StaticVector< Ecocomfort2Client *, ECOCOMFORT2_CHILD_COUNT > children_
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)
time::RealTimeClock * time_
static constexpr uint32_t READBACK_TIMEOUT_ID
uint16_t char_handle_info_
select::Select * selects_[static_cast< uint8_t >(SelectSetting::SELECT_SETTING_COUNT)]
bool read_ok_(const esp_ble_gattc_cb_param_t *param)
uint16_t char_handle_clock_
SettingsChar advanced_char_
void set_connected_(bool connected)
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
const char * address_str() const
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
void disconnect() override
bool check_addr(esp_bd_addr_t &addr)
Base-class for all numbers.
void publish_state(float state)
Base-class for all selects.
optional< size_t > active_index() const
Return the (optional) index offset of the currently active option.
void publish_state(const std::string &state)
ESPTime now()
Get the time in the currently defined timezone.
void add_on_time_sync_callback(F &&callback)
@ NUMBER_SETTING_HUMIDITY_THRESHOLD
@ NUMBER_SETTING_LUMINOSITY_THRESHOLD
@ NUMBER_SETTING_HUMIDITY_OFFSET
@ NUMBER_SETTING_VOC_THRESHOLD
@ NUMBER_SETTING_TEMPERATURE_OFFSET
ESPHOME_LOG_TAG(TAG, "ecocomfort2.fan")
@ SWITCH_SETTING_HUMIDITY_ADVANCED
@ SWITCH_SETTING_VOC_ADVANCED
@ SELECT_SETTING_FREE_COOLING
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...
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
constexpr std::array< uint8_t, sizeof(T)> decode_value(T val)
Decode a value into its constituent bytes (from most to least significant).
Application App
Global storage of Application pointer - only one Application can exist.
A more user-friendly version of struct tm from time.h.
uint8_t minute
minutes after the hour [0-59]
uint8_t second
seconds after the minute [0-60]
uint8_t hour
hours since midnight [0-23]
bool is_valid(bool check_day_of_week=true, bool check_day_of_year=true) const
Check if this ESPTime is valid (year >= 2019 and the requested fields are in range).
uint8_t day_of_month
day of the month [1-31]
uint8_t month
month; january=1 [1-12]
uint8_t day_of_week
day of the week; sunday=1 [1-7]
One settings characteristic. Only one read is in flight at a time; a write sent meanwhile makes its a...
spi_device_handle_t handle