diff --git a/esphome/components/tuya/select/__init__.py b/esphome/components/tuya/select/__init__.py index a34e279746..e5b2e36ce7 100644 --- a/esphome/components/tuya/select/__init__.py +++ b/esphome/components/tuya/select/__init__.py @@ -1,7 +1,12 @@ import esphome.codegen as cg from esphome.components import select import esphome.config_validation as cv -from esphome.const import CONF_ENUM_DATAPOINT, CONF_OPTIMISTIC, CONF_OPTIONS +from esphome.const import ( + CONF_ENUM_DATAPOINT, + CONF_INT_DATAPOINT, + CONF_OPTIMISTIC, + CONF_OPTIONS, +) from .. import CONF_TUYA_ID, Tuya, tuya_ns @@ -26,17 +31,19 @@ def ensure_option_map(value): return value -CONFIG_SCHEMA = ( +CONFIG_SCHEMA = cv.All( select.select_schema(TuyaSelect) .extend( { cv.GenerateID(CONF_TUYA_ID): cv.use_id(Tuya), - cv.Required(CONF_ENUM_DATAPOINT): cv.uint8_t, + cv.Optional(CONF_ENUM_DATAPOINT): cv.uint8_t, + cv.Optional(CONF_INT_DATAPOINT): cv.uint8_t, cv.Required(CONF_OPTIONS): ensure_option_map, cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean, } ) - .extend(cv.COMPONENT_SCHEMA) + .extend(cv.COMPONENT_SCHEMA), + cv.has_exactly_one_key(CONF_ENUM_DATAPOINT, CONF_INT_DATAPOINT), ) @@ -47,5 +54,8 @@ async def to_code(config): cg.add(var.set_select_mappings(list(options_map.keys()))) parent = await cg.get_variable(config[CONF_TUYA_ID]) cg.add(var.set_tuya_parent(parent)) - cg.add(var.set_select_id(config[CONF_ENUM_DATAPOINT])) + if enum_datapoint := config.get(CONF_ENUM_DATAPOINT, None) is not None: + cg.add(var.set_select_id(enum_datapoint, False)) + if int_datapoint := config.get(CONF_INT_DATAPOINT, None) is not None: + cg.add(var.set_select_id(int_datapoint, True)) cg.add(var.set_optimistic(config[CONF_OPTIMISTIC])) diff --git a/esphome/components/tuya/select/tuya_select.cpp b/esphome/components/tuya/select/tuya_select.cpp index a4df0873b0..02643e97f4 100644 --- a/esphome/components/tuya/select/tuya_select.cpp +++ b/esphome/components/tuya/select/tuya_select.cpp @@ -31,7 +31,11 @@ void TuyaSelect::control(const std::string &value) { if (idx.has_value()) { uint8_t mapping = this->mappings_.at(idx.value()); ESP_LOGV(TAG, "Setting %u datapoint value to %u:%s", this->select_id_, mapping, value.c_str()); - this->parent_->set_enum_datapoint_value(this->select_id_, mapping); + if (this->is_int_) { + this->parent_->set_integer_datapoint_value(this->select_id_, mapping); + } else { + this->parent_->set_enum_datapoint_value(this->select_id_, mapping); + } return; } @@ -41,6 +45,7 @@ void TuyaSelect::control(const std::string &value) { void TuyaSelect::dump_config() { LOG_SELECT("", "Tuya Select", this); ESP_LOGCONFIG(TAG, " Select has datapoint ID %u", this->select_id_); + ESP_LOGCONFIG(TAG, " Data type: %s", this->is_int_ ? "int" : "enum"); ESP_LOGCONFIG(TAG, " Options are:"); auto options = this->traits.get_options(); for (auto i = 0; i < this->mappings_.size(); i++) { diff --git a/esphome/components/tuya/select/tuya_select.h b/esphome/components/tuya/select/tuya_select.h index 6a7e5c7ed0..12d7b507d4 100644 --- a/esphome/components/tuya/select/tuya_select.h +++ b/esphome/components/tuya/select/tuya_select.h @@ -16,7 +16,10 @@ class TuyaSelect : public select::Select, public Component { void set_tuya_parent(Tuya *parent) { this->parent_ = parent; } void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; } - void set_select_id(uint8_t select_id) { this->select_id_ = select_id; } + void set_select_id(uint8_t select_id, bool is_int) { + this->select_id_ = select_id; + this->is_int_ = is_int; + } void set_select_mappings(std::vector mappings) { this->mappings_ = std::move(mappings); } protected: @@ -26,6 +29,7 @@ class TuyaSelect : public select::Select, public Component { bool optimistic_ = false; uint8_t select_id_; std::vector mappings_; + bool is_int_ = false; }; } // namespace tuya diff --git a/esphome/const.py b/esphome/const.py index d656b15519..0f811aa870 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -408,6 +408,7 @@ CONF_INITIAL_OPTION = "initial_option" CONF_INITIAL_STATE = "initial_state" CONF_INITIAL_VALUE = "initial_value" CONF_INPUT = "input" +CONF_INT_DATAPOINT = "int_datapoint" CONF_INTEGRATION_TIME = "integration_time" CONF_INTENSITY = "intensity" CONF_INTERLOCK = "interlock" diff --git a/tests/components/tuya/common.yaml b/tests/components/tuya/common.yaml index fcf8a2d96b..2c40628139 100644 --- a/tests/components/tuya/common.yaml +++ b/tests/components/tuya/common.yaml @@ -60,12 +60,19 @@ number: select: - platform: tuya - id: tuya_select + id: tuya_select_enum enum_datapoint: 42 options: 0: Internal 1: Floor 2: Both + - platform: tuya + id: tuya_select_int + int_datapoint: 43 + options: + 0: Internal + 1: Floor + 2: Both sensor: - platform: tuya