Tuya Select - Add int_datapoint option (#8393)

This commit is contained in:
Cossid 2025-05-12 23:44:51 -05:00 committed by GitHub
parent 5e164b107a
commit 8b65d1673a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 35 additions and 8 deletions

View File

@ -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]))

View File

@ -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++) {

View File

@ -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<uint8_t> 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<uint8_t> mappings_;
bool is_int_ = false;
};
} // namespace tuya

View File

@ -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"

View File

@ -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