Home IO Control
ESPHome add-on for IO-Homecontrol devices
Toggle main menu visibility
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
9
import
esphome.codegen
as
cg
10
import
esphome.config_validation
as
cv
11
from
esphome.components
import
light
12
from
esphome.const
import
CONF_OUTPUT_ID
13
14
from
.
import
home_io_control_ns
15
from
.hub_validators
import
FEATURE_MAIN_PARAMETER_ONLY
16
from
.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
28
DEPENDENCIES = [
"home_io_control"
]
29
30
IOHomeLight = 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.
34
CONF_DIMMABLE =
"dimmable"
35
36
37
def
_inject_companion_ids
(config):
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
42
def
_require_dimmable_kind
(config):
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
54
def
_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
74
CONFIG_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
83
async 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)
home_io_control.light._validate
_validate(config)
Definition
light.py:54
home_io_control.light._require_dimmable_kind
_require_dimmable_kind(config)
Definition
light.py:42
home_io_control.light.to_code
to_code(config)
Definition
light.py:83
home_io_control.light._inject_companion_ids
_inject_companion_ids(config)
Definition
light.py:37
components
home_io_control
light.py
Generated by
1.18.0