mirror of
https://github.com/esphome/esphome.git
synced 2025-02-15 17:38:15 +00:00
51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
from esphome.components import select
|
|
import esphome.config_validation as cv
|
|
import esphome.codegen as cg
|
|
from esphome.const import CONF_OPTIONS, CONF_OPTIMISTIC, CONF_ENUM_DATAPOINT
|
|
from .. import tuya_ns, CONF_TUYA_ID, Tuya
|
|
|
|
DEPENDENCIES = ["tuya"]
|
|
CODEOWNERS = ["@bearpawmaxim"]
|
|
|
|
TuyaSelect = tuya_ns.class_("TuyaSelect", select.Select, cg.Component)
|
|
|
|
|
|
def ensure_option_map(value):
|
|
cv.check_not_templatable(value)
|
|
option = cv.All(cv.int_range(0, 2**8 - 1))
|
|
mapping = cv.All(cv.string_strict)
|
|
options_map_schema = cv.Schema({option: mapping})
|
|
value = options_map_schema(value)
|
|
|
|
all_values = list(value.keys())
|
|
unique_values = set(value.keys())
|
|
if len(all_values) != len(unique_values):
|
|
raise cv.Invalid("Mapping values must be unique.")
|
|
|
|
return value
|
|
|
|
|
|
CONFIG_SCHEMA = (
|
|
select.select_schema(TuyaSelect)
|
|
.extend(
|
|
{
|
|
cv.GenerateID(CONF_TUYA_ID): cv.use_id(Tuya),
|
|
cv.Required(CONF_ENUM_DATAPOINT): cv.uint8_t,
|
|
cv.Required(CONF_OPTIONS): ensure_option_map,
|
|
cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
|
|
}
|
|
)
|
|
.extend(cv.COMPONENT_SCHEMA)
|
|
)
|
|
|
|
|
|
async def to_code(config):
|
|
options_map = config[CONF_OPTIONS]
|
|
var = await select.new_select(config, options=list(options_map.values()))
|
|
await cg.register_component(var, 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]))
|
|
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
|