17#ifdef USE_BINARY_SENSOR
18 if (this->connected_binary_sensor_ !=
nullptr)
19 this->connected_binary_sensor_->publish_initial_state(
false);
23 if (this->
time_ !=
nullptr)
28void Ecocomfort2Hub::update() {
29 if (!this->is_ready()) {
36#ifdef USE_ECOCOMFORT2_SETTINGS
44 ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
45 if (
status != ESP_GATT_OK) {
54 ESP_GATT_AUTH_REQ_NONE);
55 if (
status != ESP_GATT_OK) {
62void Ecocomfort2Hub::dump_config() {
67 LOG_UPDATE_INTERVAL(
this);
69 ESP_LOGCONFIG(TAG,
" Clock sync: %s", YESNO(this->
time_ !=
nullptr));
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_);
77#ifdef USE_BINARY_SENSOR
78 LOG_BINARY_SENSOR(
" ",
"Connected", this->connected_binary_sensor_);
79 LOG_BINARY_SENSOR(
" ",
"Boost", this->boost_binary_sensor_);
82 LOG_TEXT_SENSOR(
" ",
"Firmware", this->firmware_text_sensor_);
84#ifdef USE_ECOCOMFORT2_SETTINGS
86 LOG_NUMBER(
" ",
"Humidity threshold",
88 LOG_NUMBER(
" ",
"Luminosity threshold",
91 LOG_NUMBER(
" ",
"Temperature offset",
93 LOG_NUMBER(
" ",
"Humidity offset",
97 LOG_SWITCH(
" ",
"Humidity advanced",
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) {
111 case ESP_GATTC_DISCONNECT_EVT: {
112 ESP_LOGD(TAG,
"[%s] Disconnected: reason=%d", this->
parent_->
address_str(), param->disconnect.reason);
120#ifdef USE_ECOCOMFORT2_SETTINGS
122 settings->handle = 0;
123 settings->known = settings->in_flight = settings->dirty =
false;
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");
139 if (oper ==
nullptr ||
state ==
nullptr) {
140 ESP_LOGW(TAG,
"[%s] C_OPER or C_STATE characteristic not found, disconnecting", this->
parent_->
address_str());
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");
159 this->
node_state = espbt::ClientState::ESTABLISHED;
169 ESP_LOGW(TAG,
"[%s] BLE pair request failed, disconnecting", this->
parent_->
address_str());
174 ESP_LOGW(TAG,
"[%s] BLE authentication timed out, disconnecting", this->
parent_->
address_str());
179 case ESP_GATTC_READ_CHAR_EVT: {
180 if (param->read.conn_id != this->parent_->get_conn_id()) {
183 const uint16_t
handle = param->read.handle;
186 this->
on_oper_(param->read.value, param->read.value_len);
189 this->
on_state_(param->read.value, param->read.value_len);
192 this->
on_info_(param->read.value, param->read.value_len);
193#ifdef USE_ECOCOMFORT2_SETTINGS
196 this->
on_config_(param->read.value, param->read.value_len);
199 this->
on_advanced_(param->read.value, param->read.value_len);
210 if (param->read.status == ESP_GATT_INSUF_ENCRYPTION || param->read.status == ESP_GATT_INSUF_AUTHENTICATION) {
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,
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) {
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);
242 ESP_LOGI(TAG,
"[%s] Encryption confirmed, device ready", this->
parent_->
address_str());
245 if (this->
time_ !=
nullptr)
248#ifdef USE_TEXT_SENSOR
256#ifdef USE_BINARY_SENSOR
257 PUBLISH_STATE(this->connected_binary_sensor_, connected);
264 for (
auto *sensor : {this->temperature_sensor_, this->humidity_sensor_, this->voc_sensor_, this->direction_sensor_}) {
265 PUBLISH_STATE(sensor, NAN);
268#ifdef USE_BINARY_SENSOR
269 if (this->boost_binary_sensor_ !=
nullptr)
270 this->boost_binary_sensor_->invalidate_state();
280 this->
mode_ = data[0];
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_) {
297 const float humidity =
encode_uint16(data[5], data[6]) / 100.0f;
299 ESP_LOGD(TAG,
"[%s] C_STATE: mode=0x%02x, speed=0x%02x, %.2f °C, %.2f %%, %u ppb, direction=%u",
301#ifdef USE_BINARY_SENSOR
302 PUBLISH_STATE(this->boost_binary_sensor_, (data[1] & FLAG_BOOST) != 0);
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]);
318 const uint16_t field =
encode_uint16(data[0] & 0x0F, data[1]);
320 snprintf(version,
sizeof(version),
"%u.%u.%u", data[0] >> 4, field >> 6, field & 0x3F);
322#ifdef USE_TEXT_SENSOR
323 PUBLISH_STATE(this->firmware_text_sensor_, version);
340 ESP_LOGD(TAG,
"[%s] Syncing clock to %04u-%02u-%02u %02u:%02u:%02u", this->
parent_->
address_str(), now.
year,
346void Ecocomfort2Hub::write_oper(uint8_t
mode, uint8_t speed) {
347 if (!this->is_ready()) {
352 ESP_LOGD(TAG,
"[%s] Writing C_OPER: mode=0x%02x, speed=0x%02x", this->
parent_->
address_str(), mode, speed);
358#ifdef USE_ECOCOMFORT2_SETTINGS
360static constexpr uint8_t CONFIG_KEEP = 0x7F;
362static constexpr uint8_t THRESHOLD_ADVANCED = 0x80;
363static constexpr uint8_t THRESHOLD_LEVEL_MASK = 0x7F;
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;
370static constexpr uint8_t KEEP_SEASON = 0x70;
371static constexpr uint8_t KEEP_FREE_COOLING = 0x0F;
377static constexpr uint8_t threshold_byte(
NumberSetting setting) {
return static_cast<uint8_t
>(setting) + 1; }
381static constexpr uint32_t SETTINGS_READ_STALL_MS = 30000;
385 settings.
dirty =
true;
399 settings.
dirty =
false;
407 if (!settings->needed || settings->handle == 0)
409 if (settings->in_flight) {
410 if (now - settings->sent_ms <= SETTINGS_READ_STALL_MS)
412 ESP_LOGW(TAG,
"[%s] Settings read not answered, retrying", this->
parent_->
address_str());
413 settings->in_flight = settings->dirty =
false;
420 if (!settings.
known) {
423 return settings.
known;
430 ESP_LOGW(TAG,
"[%s] C_CONFIGURATION too short: %u bytes", this->
parent_->
address_str(), len);
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]);
445 ESP_LOGW(TAG,
"[%s] C_ADVANCED_SETTINGS too short: %u bytes", this->
parent_->
address_str(), len);
451 ESP_LOGD(TAG,
"[%s] C_ADVANCED_SETTINGS: offsets %d %d", this->
parent_->
address_str(), temperature, humidity);
468static void publish_if_changed(
select::Select *select,
size_t index) {
469 if (select ==
nullptr)
472 if (!current.has_value() || *current != index)
484 const uint8_t threshold = this->
config_bytes_[threshold_byte(ADVANCED_THRESHOLD[i])];
486 PUBLISH_STATE(this->
switches_[i], (threshold & THRESHOLD_ADVANCED) && (threshold & THRESHOLD_LEVEL_MASK));
490 const uint8_t &season_byte = this->
config_bytes_[SEASON_BYTE];
492 publish_if_changed(season,
static_cast<size_t>((season_byte & SEASON_SUMMER) != 0));
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));
504 for (uint8_t i = 0; i < 2; i++) {
513 if (!this->is_ready() || settings.
handle == 0) {
514 ESP_LOGW(TAG,
"[%s] Not ready, cannot change the setting", this->
parent_->
address_str());
527 uint8_t data[12]{CONFIG_KEEP, CONFIG_KEEP, CONFIG_KEEP, CONFIG_KEEP, CONFIG_KEEP, CONFIG_KEEP};
540 ESP_LOGD(TAG,
"[%s] Writing C_ADVANCED_SETTINGS: offsets %d %d", this->
parent_->
address_str(), temperature, humidity);
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);
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());
580 const uint8_t threshold = level | (
state ? THRESHOLD_ADVANCED : 0);
587void Ecocomfort2Hub::write_select(
SelectSetting setting,
size_t index) {
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));
597 if ((season_byte & SEASON_SUMMER) == 0) {
598 ESP_LOGW(TAG,
"[%s] Free cooling is only available in summer", this->
parent_->
address_str());
601 const uint8_t level =
static_cast<uint8_t
>(index);
602 this->
write_config_(SEASON_BYTE, KEEP_SEASON | level, (season_byte & SEASON_MASK) | level);
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 status_clear_warning()
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
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)
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 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...
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
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 >= internal::MIN_VALID_YEAR and the requested fields are in rang...
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