mirror of
https://github.com/esphome/esphome.git
synced 2026-09-18 10:38:38 +00:00
139 lines
4.7 KiB
Python
139 lines
4.7 KiB
Python
from collections.abc import Callable
|
|
from typing import Any
|
|
|
|
import esphome.codegen as cg
|
|
from esphome.components import modbus, select
|
|
from esphome.components.modbus.helpers import SENSOR_VALUE_TYPE, RegisterValues
|
|
import esphome.config_validation as cv
|
|
from esphome.const import CONF_ADDRESS, CONF_ID, CONF_LAMBDA, CONF_OPTIMISTIC
|
|
from esphome.types import ConfigType
|
|
|
|
from .. import (
|
|
RANGE_REUSE,
|
|
ModbusController,
|
|
SensorItem,
|
|
modbus_controller_ns,
|
|
validate_range_reuse_migration,
|
|
validate_skip_updates_deprecated,
|
|
validate_writer_item,
|
|
)
|
|
from ..const import (
|
|
CONF_FORCE_NEW_RANGE,
|
|
CONF_MODBUS_CONTROLLER_ID,
|
|
CONF_REGISTER_COUNT,
|
|
CONF_REUSE_PREVIOUS_RANGE,
|
|
CONF_SKIP_UPDATES,
|
|
CONF_USE_WRITE_MULTIPLE,
|
|
CONF_VALUE_TYPE,
|
|
CONF_WRITE_LAMBDA,
|
|
)
|
|
|
|
DEPENDENCIES = ["modbus_controller"]
|
|
CODEOWNERS = ["@martgras", "@stegm"]
|
|
CONF_OPTIONSMAP = "optionsmap"
|
|
|
|
ModbusSelect = modbus_controller_ns.class_(
|
|
"ModbusSelect", cg.Component, select.Select, SensorItem
|
|
)
|
|
|
|
|
|
def ensure_option_map() -> Callable[[Any], dict[str, int]]:
|
|
def validator(value: Any) -> dict[str, int]:
|
|
cv.check_not_templatable(value)
|
|
option = cv.All(cv.string_strict)
|
|
mapping = cv.All(cv.int_range(-(2**63), 2**63 - 1))
|
|
options_map_schema = cv.Schema({option: mapping})
|
|
value = options_map_schema(value)
|
|
|
|
all_values = list(value.values())
|
|
unique_values = set(value.values())
|
|
if len(all_values) != len(unique_values):
|
|
raise cv.Invalid("Mapping values must be unique.")
|
|
|
|
return value
|
|
|
|
return validator
|
|
|
|
|
|
INTEGER_SENSOR_VALUE_TYPE = {
|
|
key: value for key, value in SENSOR_VALUE_TYPE.items() if not key.startswith("FP")
|
|
}
|
|
|
|
CONFIG_SCHEMA = cv.All(
|
|
select.select_schema(ModbusSelect)
|
|
.extend(cv.COMPONENT_SCHEMA)
|
|
.extend(
|
|
{
|
|
cv.GenerateID(CONF_MODBUS_CONTROLLER_ID): cv.use_id(ModbusController),
|
|
cv.Required(CONF_ADDRESS): cv.positive_int,
|
|
cv.Optional(CONF_VALUE_TYPE, default="U_WORD"): cv.enum(
|
|
INTEGER_SENSOR_VALUE_TYPE
|
|
),
|
|
cv.Optional(CONF_SKIP_UPDATES): validate_skip_updates_deprecated,
|
|
cv.Optional(CONF_REUSE_PREVIOUS_RANGE, default="auto"): cv.Any(
|
|
cv.boolean, cv.one_of("auto", lower=True)
|
|
),
|
|
# Deprecated options, migrated by validate_range_reuse_migration(). Remove before 2027.3.0
|
|
cv.Optional(CONF_FORCE_NEW_RANGE): cv.boolean,
|
|
cv.Optional(CONF_REGISTER_COUNT): cv.positive_int,
|
|
cv.Required(CONF_OPTIONSMAP): ensure_option_map(),
|
|
cv.Optional(CONF_USE_WRITE_MULTIPLE, default=False): cv.boolean,
|
|
**modbus.command_options_schema(direction="write"),
|
|
cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
|
|
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
|
cv.Optional(CONF_WRITE_LAMBDA): cv.returning_lambda,
|
|
},
|
|
),
|
|
validate_range_reuse_migration,
|
|
)
|
|
|
|
|
|
FINAL_VALIDATE_SCHEMA = validate_writer_item
|
|
|
|
|
|
async def to_code(config: ConfigType) -> None:
|
|
options_map = config[CONF_OPTIONSMAP]
|
|
|
|
var = cg.new_Pvariable(
|
|
config[CONF_ID],
|
|
config[CONF_VALUE_TYPE],
|
|
config[CONF_ADDRESS],
|
|
RANGE_REUSE[config[CONF_REUSE_PREVIOUS_RANGE]],
|
|
list(options_map.values()),
|
|
)
|
|
|
|
await cg.register_component(var, config)
|
|
await select.register_select(var, config, options=list(options_map.keys()))
|
|
|
|
parent = await cg.get_variable(config[CONF_MODBUS_CONTROLLER_ID])
|
|
cg.add(parent.add_sensor_item(var))
|
|
cg.add(var.set_parent(parent))
|
|
cg.add(var.set_use_write_mutiple(config[CONF_USE_WRITE_MULTIPLE]))
|
|
modbus.add_command_options(var, "set_write_options", config, direction="write")
|
|
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
|
|
|
|
if CONF_LAMBDA in config:
|
|
template_ = await cg.process_lambda(
|
|
config[CONF_LAMBDA],
|
|
[
|
|
(ModbusSelect.operator("const_ptr"), "item"),
|
|
(cg.int64, "x"),
|
|
(cg.std_span.template(cg.uint8.operator("const")), "data"),
|
|
],
|
|
return_type=cg.optional.template(cg.std_string),
|
|
)
|
|
cg.add(var.set_template(template_))
|
|
|
|
if CONF_WRITE_LAMBDA in config:
|
|
template_ = await cg.process_lambda(
|
|
config[CONF_WRITE_LAMBDA],
|
|
[
|
|
(ModbusSelect.operator("const_ptr"), "item"),
|
|
(cg.std_string.operator("const").operator("ref"), "x"),
|
|
(cg.int64, "value"),
|
|
(RegisterValues.operator("ref"), "payload"),
|
|
],
|
|
return_type=cg.optional.template(cg.int64),
|
|
)
|
|
cg.add(var.set_write_template(template_))
|