Home IO Control
ESPHome add-on for IO-Homecontrol devices
Loading...
Searching...
No Matches
light.py
Go to the documentation of this file.
1## @file
2## @brief ESPHome binary light platform schema and code generation.
3## @ingroup hioc_codegen
4##
5## Defines the experimental binary light integration and wires it to the shared hub.
6
7import esphome.codegen as cg
8import esphome.config_validation as cv
9from esphome.components import light, text_sensor
10from esphome.const import (
11 CONF_DISABLED_BY_DEFAULT,
12 CONF_ID,
13 CONF_NAME,
14 CONF_OUTPUT_ID,
15 ENTITY_CATEGORY_DIAGNOSTIC,
16)
17from esphome.core import CORE, ID
18
19from . import (
20 home_io_control_ns,
21 IOHomeControlComponent,
22 CONF_HOME_IO_CONTROL_ID,
23 validate_device_id,
24 validate_status_poll_interval,
25 validate_device_type,
26 device_type_expression,
27)
28
29DEPENDENCIES = ["home_io_control"]
30
31CONF_DEVICE_ID = "io_device_id"
32CONF_LINKED_REMOTES = "linked_remotes"
33CONF_DEVICE_TYPE = "io_device_type"
34CONF_SUBTYPE = "io_subtype"
35CONF_STATUS_POLL_INTERVAL = "status_poll_interval"
36
37IOHomeLight = home_io_control_ns.class_("IOHomeLight", light.LightOutput, cg.Component)
38IOHomeDeviceNameTextSensor = home_io_control_ns.class_(
39 "IOHomeDeviceNameTextSensor", text_sensor.TextSensor, cg.Component
40)
41
42
43def device_name_sensor_id(parent_id):
44 """Generate a unique ID for the diagnostic device-name text sensor."""
45 return ID(
46 f"{parent_id.id}_device_name_sensor",
47 is_declaration=True,
48 type=IOHomeDeviceNameTextSensor,
49 )
50
51
53 """Derive the device-name sensor name from the parent entity name."""
54 base_name = config.get(CONF_NAME, "")
55 if base_name:
56 return f"{base_name} Device Name"
57 return "Device Name"
58
59CONFIG_SCHEMA = (
60 # Expose this as a binary light on purpose. The transport may carry 0-100 values, but only
61 # on/off semantics are backed by current protocol evidence, needs real device to verify
62 light.light_schema(IOHomeLight, light.LightType.BINARY)
63 .extend(
64 {
65 cv.GenerateID(CONF_HOME_IO_CONTROL_ID): cv.use_id(
66 IOHomeControlComponent
67 ),
68 cv.Required(CONF_DEVICE_ID): validate_device_id,
69 cv.Optional(CONF_DEVICE_TYPE): validate_device_type,
70 cv.Optional(CONF_SUBTYPE): cv.int_range(min=0, max=63),
71 cv.Optional(CONF_LINKED_REMOTES): cv.ensure_list(validate_device_id),
72 cv.Optional(CONF_STATUS_POLL_INTERVAL): validate_status_poll_interval,
73 }
74 )
75 .extend(cv.COMPONENT_SCHEMA)
76)
77
78
79async def to_code(config):
80 var = cg.new_Pvariable(config[CONF_OUTPUT_ID])
81 await cg.register_component(var, config)
82 await light.register_light(var, config)
83
84 parent = await cg.get_variable(config[CONF_HOME_IO_CONTROL_ID])
85 cg.add(var.set_parent(parent))
86 cg.add(var.set_device_id(config[CONF_DEVICE_ID]))
87
88 if CONF_DEVICE_TYPE in config:
89 cg.add(var.set_device_type(device_type_expression(config[CONF_DEVICE_TYPE])))
90 if CONF_SUBTYPE in config:
91 cg.add(var.set_subtype(config[CONF_SUBTYPE]))
92 if CONF_STATUS_POLL_INTERVAL in config:
93 cg.add(var.set_status_poll_interval(config[CONF_STATUS_POLL_INTERVAL].total_milliseconds))
94
95 if CONF_LINKED_REMOTES in config:
96 for remote_id in config[CONF_LINKED_REMOTES]:
97 cg.add(parent.add_linked_remote(remote_id, config[CONF_DEVICE_ID]))
98
99 device_name_config = {
100 CONF_ID: device_name_sensor_id(config[CONF_OUTPUT_ID]),
101 CONF_NAME: device_name_sensor_name(config),
102 CONF_DISABLED_BY_DEFAULT: True,
103 "entity_category": ENTITY_CATEGORY_DIAGNOSTIC,
104 }
105 device_name = await text_sensor.new_text_sensor(device_name_config)
106 CORE.component_ids.add(str(device_name.base))
107 await cg.register_component(device_name, device_name_config)
108 cg.add(device_name.set_parent(parent))
109 cg.add(device_name.set_device_id(config[CONF_DEVICE_ID]))
device_name_sensor_id(parent_id)
Definition light.py:43
device_name_sensor_name(config)
Definition light.py:52
device_type_expression(value)
Definition __init__.py:107