7import esphome.codegen
as cg
8import esphome.config_validation
as cv
9from esphome.components
import switch, text_sensor
10from esphome.const
import (
11 CONF_DISABLED_BY_DEFAULT,
14 ENTITY_CATEGORY_DIAGNOSTIC,
16from esphome.core
import CORE, ID
20 IOHomeControlComponent,
21 CONF_HOME_IO_CONTROL_ID,
23 validate_status_poll_interval,
25 device_type_expression,
28DEPENDENCIES = [
"home_io_control"]
30CONF_DEVICE_ID =
"io_device_id"
31CONF_LINKED_REMOTES =
"linked_remotes"
32CONF_DEVICE_TYPE =
"io_device_type"
33CONF_SUBTYPE =
"io_subtype"
34CONF_STATUS_POLL_INTERVAL =
"status_poll_interval"
36IOHomeSwitch = home_io_control_ns.class_(
"IOHomeSwitch", switch.Switch, cg.Component)
37IOHomeDeviceNameTextSensor = home_io_control_ns.class_(
38 "IOHomeDeviceNameTextSensor", text_sensor.TextSensor, cg.Component
43 """Generate a unique ID for the diagnostic device-name text sensor."""
45 f
"{parent_id.id}_device_name_sensor",
47 type=IOHomeDeviceNameTextSensor,
52 """Derive the device-name sensor name from the parent entity name."""
53 base_name = config.get(CONF_NAME,
"")
55 return f
"{base_name} Device Name"
61 switch.switch_schema(IOHomeSwitch)
64 cv.GenerateID(CONF_HOME_IO_CONTROL_ID): cv.use_id(
65 IOHomeControlComponent
67 cv.Required(CONF_DEVICE_ID): validate_device_id,
68 cv.Optional(CONF_DEVICE_TYPE): validate_device_type,
69 cv.Optional(CONF_SUBTYPE): cv.int_range(min=0, max=63),
70 cv.Optional(CONF_LINKED_REMOTES): cv.ensure_list(validate_device_id),
71 cv.Optional(CONF_STATUS_POLL_INTERVAL): validate_status_poll_interval,
74 .extend(cv.COMPONENT_SCHEMA)
79 var = cg.new_Pvariable(config[CONF_ID])
80 await cg.register_component(var, config)
81 await switch.register_switch(var, config)
83 parent = await cg.get_variable(config[CONF_HOME_IO_CONTROL_ID])
86 cg.add(var.set_parent(parent))
87 cg.add(var.set_device_id(config[CONF_DEVICE_ID]))
89 if CONF_DEVICE_TYPE
in config:
91 if CONF_SUBTYPE
in config:
92 cg.add(var.set_subtype(config[CONF_SUBTYPE]))
93 if CONF_STATUS_POLL_INTERVAL
in config:
94 cg.add(var.set_status_poll_interval(config[CONF_STATUS_POLL_INTERVAL].total_milliseconds))
96 if CONF_LINKED_REMOTES
in config:
97 for remote_id
in config[CONF_LINKED_REMOTES]:
98 cg.add(parent.add_linked_remote(remote_id, config[CONF_DEVICE_ID]))
100 device_name_config = {
103 CONF_DISABLED_BY_DEFAULT:
True,
104 "entity_category": ENTITY_CATEGORY_DIAGNOSTIC,
106 device_name = await text_sensor.new_text_sensor(device_name_config)
107 CORE.component_ids.add(str(device_name.base))
108 await cg.register_component(device_name, device_name_config)
109 cg.add(device_name.set_parent(parent))
110 cg.add(device_name.set_device_id(config[CONF_DEVICE_ID]))
device_name_sensor_id(parent_id)
device_name_sensor_name(config)
device_type_expression(value)