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/dimmable light platform schema and code generation.
3## @ingroup hioc_codegen
4##
5## Defines the binary (default) or dimmable light integration and wires it to the shared hub.
6## Shared device-binding logic lives in platform_common.py; only light-specific entity
7## construction/registration stays here.
8
9import esphome.codegen as cg
10import esphome.config_validation as cv
11from esphome.components import light
12from esphome.const import CONF_OUTPUT_ID
13
14from . import home_io_control_ns
15from .hub_validators import FEATURE_MAIN_PARAMETER_ONLY
16from .platform_common import (
17 INSTANT_LIGHT_TRANSITION,
18 create_companion_sensors,
19 inject_companion_sensor_ids,
20 platform_schema_extension,
21 require_feature,
22 validate_device_model,
23 warn_if_experimental_kind,
24 wire_device_binding,
25 CONF_HOME_IO_CONTROL_ID,
26)
27
28DEPENDENCIES = ["home_io_control"]
29
30IOHomeLight = home_io_control_ns.class_("IOHomeLight", light.LightOutput, cg.Component)
31
32# Opt-in: the protocol gives no machine-readable "is this dimmable" signal (DISCOVER_RESP's
33# subtype byte is manufacturer-opaque), so this defaults to false/binary rather than guessing.
34CONF_DIMMABLE = "dimmable"
35
36
38 # Light reads its entity ID from CONF_OUTPUT_ID rather than CONF_ID.
39 return inject_companion_sensor_ids(config, CONF_OUTPUT_ID)
40
41
43 """Reject `dimmable: true` on a declared light kind that is on/off only (subtype 58)."""
44 require_feature(
45 config,
46 CONF_DIMMABLE,
47 config[CONF_DIMMABLE],
48 forbidden=FEATURE_MAIN_PARAMETER_ONLY,
49 reason="this light is on/off only (KLF 200 Appendix 2); remove dimmable:",
50 )
51 return config
52
53
54def _validate(config):
55 # The LightType passed to light.light_schema() determines which YAML keys are even accepted
56 # (e.g. gamma_correct only makes sense for a brightness-capable light), so it must be chosen
57 # from the raw dimmable: value before the rest of the schema runs.
58 light_type = (
59 light.LightType.BRIGHTNESS_ONLY
60 if cv.boolean(config.get(CONF_DIMMABLE, False))
61 else light.LightType.BINARY
62 )
63 schema = (
64 light.light_schema(IOHomeLight, light_type)
65 .extend(platform_schema_extension())
66 .extend({cv.Optional(CONF_DIMMABLE, default=False): cv.boolean})
67 .extend(cv.COMPONENT_SCHEMA)
68 )
69 if light_type == light.LightType.BRIGHTNESS_ONLY:
70 schema = schema.extend(INSTANT_LIGHT_TRANSITION)
71 return schema(config)
72
73
74CONFIG_SCHEMA = cv.All(
75 _validate,
76 validate_device_model,
77 _require_dimmable_kind,
78 warn_if_experimental_kind,
79 _inject_companion_ids,
80)
81
82
83async def to_code(config):
84 var = cg.new_Pvariable(config[CONF_OUTPUT_ID])
85 await cg.register_component(var, config)
86 await light.register_light(var, config)
87
88 cg.add(var.set_dimmable(config[CONF_DIMMABLE]))
89
90 parent = await cg.get_variable(config[CONF_HOME_IO_CONTROL_ID])
91 await wire_device_binding(var, parent, config)
92 await create_companion_sensors(config, parent)
_validate(config)
Definition light.py:54
_require_dimmable_kind(config)
Definition light.py:42
_inject_companion_ids(config)
Definition light.py:37