From 0ea41e2f7172cde92f873d0b30634d7ca775fbf8 Mon Sep 17 00:00:00 2001 From: stubs12 <43162406+stubs12@users.noreply.github.com> Date: Wed, 2 Dec 2020 22:38:17 -0600 Subject: [PATCH] Add option to suppress embedded MCU updates on certain datapoints (#1396) --- esphome/components/tuya/__init__.py | 6 ++++++ esphome/components/tuya/tuya.cpp | 8 ++++++++ esphome/components/tuya/tuya.h | 4 ++++ 3 files changed, 18 insertions(+) diff --git a/esphome/components/tuya/__init__.py b/esphome/components/tuya/__init__.py index 114099cc60..83a4f733ca 100644 --- a/esphome/components/tuya/__init__.py +++ b/esphome/components/tuya/__init__.py @@ -6,6 +6,8 @@ from esphome.const import CONF_ID, CONF_TIME_ID DEPENDENCIES = ['uart'] +CONF_IGNORE_MCU_UPDATE_ON_DATAPOINTS = "ignore_mcu_update_on_datapoints" + tuya_ns = cg.esphome_ns.namespace('tuya') Tuya = tuya_ns.class_('Tuya', cg.Component, uart.UARTDevice) @@ -13,6 +15,7 @@ CONF_TUYA_ID = 'tuya_id' CONFIG_SCHEMA = cv.Schema({ cv.GenerateID(): cv.declare_id(Tuya), cv.Optional(CONF_TIME_ID): cv.use_id(time.RealTimeClock), + cv.Optional(CONF_IGNORE_MCU_UPDATE_ON_DATAPOINTS): cv.ensure_list(cv.uint8_t), }).extend(cv.COMPONENT_SCHEMA).extend(uart.UART_DEVICE_SCHEMA) @@ -23,3 +26,6 @@ def to_code(config): if CONF_TIME_ID in config: time_ = yield cg.get_variable(config[CONF_TIME_ID]) cg.add(var.set_time_id(time_)) + if CONF_IGNORE_MCU_UPDATE_ON_DATAPOINTS in config: + for dp in config[CONF_IGNORE_MCU_UPDATE_ON_DATAPOINTS]: + cg.add(var.add_ignore_mcu_update_on_datapoints(dp)) diff --git a/esphome/components/tuya/tuya.cpp b/esphome/components/tuya/tuya.cpp index 380129a61e..cadb45fbe3 100644 --- a/esphome/components/tuya/tuya.cpp +++ b/esphome/components/tuya/tuya.cpp @@ -256,6 +256,14 @@ void Tuya::handle_datapoint_(const uint8_t *buffer, size_t len) { datapoint.type = (TuyaDatapointType) buffer[1]; datapoint.value_uint = 0; + // drop update if datapoint is in ignore_mcu_datapoint_update list + for (auto i : this->ignore_mcu_update_on_datapoints_) { + if (datapoint.id == i) { + ESP_LOGV(TAG, "Datapoint %u found in ignore_mcu_update_on_datapoints list, dropping MCU update", datapoint.id); + return; + } + } + size_t data_size = (buffer[2] << 8) + buffer[3]; const uint8_t *data = buffer + 4; size_t data_len = len - 4; diff --git a/esphome/components/tuya/tuya.h b/esphome/components/tuya/tuya.h index 3a66aecb1a..ba20cfd314 100644 --- a/esphome/components/tuya/tuya.h +++ b/esphome/components/tuya/tuya.h @@ -71,6 +71,9 @@ class Tuya : public Component, public uart::UARTDevice { #ifdef USE_TIME void set_time_id(time::RealTimeClock *time_id) { this->time_id_ = time_id; } #endif + void add_ignore_mcu_update_on_datapoints(uint8_t ignore_mcu_update_on_datapoints) { + this->ignore_mcu_update_on_datapoints_.push_back(ignore_mcu_update_on_datapoints); + } protected: void handle_char_(uint8_t c); @@ -93,6 +96,7 @@ class Tuya : public Component, public uart::UARTDevice { std::vector listeners_; std::vector datapoints_; std::vector rx_message_; + std::vector ignore_mcu_update_on_datapoints_{}; }; } // namespace tuya