Home IO Control
ESPHome add-on for IO-Homecontrol devices
Loading...
Searching...
No Matches
button.py
Go to the documentation of this file.
1## @file
2## @brief ESPHome pairing-button schema and code generation.
3## @ingroup hioc_codegen
4##
5## Exposes the Home Assistant button entity that starts device discovery and pairing, plus
6## its companion "Last Pairing Result" diagnostic text sensor (machine-readable telemetry
7## summary, published after every attempt).
8
9import esphome.codegen as cg
10from esphome.components import button, text_sensor
11import esphome.config_validation as cv
12from esphome.const import (
13 CONF_DISABLED_BY_DEFAULT,
14 CONF_ID,
15 CONF_NAME,
16 ENTITY_CATEGORY_CONFIG,
17 ENTITY_CATEGORY_DIAGNOSTIC,
18)
19from esphome.core import ID
20
21from . import home_io_control_ns, IOHomeControlComponent, CONF_HOME_IO_CONTROL_ID
22
23DEPENDENCIES = ["home_io_control"]
24
25IOHomeDiscoverButton = home_io_control_ns.class_(
26 "IOHomeDiscoverButton", button.Button, cg.Component
27)
28IOHomePairingResultTextSensor = home_io_control_ns.class_(
29 "IOHomePairingResultTextSensor", text_sensor.TextSensor, cg.Component
30)
31
32# Internal config key for the companion pairing-result sensor ID (injected by post-validator).
33CONF_PAIRING_RESULT_SENSOR_ID = "_pairing_result_sensor_id"
34
35
37 """Declare the companion pairing-result sensor ID during schema validation.
38
39 ESPHome 2026.x sizes its runtime component vector from the number of component IDs
40 known at the end of schema validation — before to_code() runs. A companion entity
41 created only in to_code() is not counted and silently drops at runtime. See the
42 identical pattern (and its full rationale) in platform_common.py::companion_id_base().
43 """
44 from esphome.helpers import sanitize
45
46 parent_id = config[CONF_ID]
47 base = parent_id.id if parent_id.id else sanitize(config.get(CONF_NAME, "")).lower()
48 config[CONF_PAIRING_RESULT_SENSOR_ID] = ID(
49 f"{base}_pairing_result_sensor",
50 is_declaration=True,
51 type=IOHomePairingResultTextSensor,
52 )
53 return config
54
55
56CONFIG_SCHEMA = cv.All(
57 button.button_schema(
58 IOHomeDiscoverButton,
59 entity_category=ENTITY_CATEGORY_CONFIG,
60 )
61 .extend(
62 {
63 cv.GenerateID(CONF_HOME_IO_CONTROL_ID): cv.use_id(
64 IOHomeControlComponent
65 ),
66 }
67 )
68 .extend(cv.COMPONENT_SCHEMA),
69 _inject_pairing_result_sensor_id,
70)
71
72
73async def to_code(config):
74 var = cg.new_Pvariable(config[CONF_ID])
75 await cg.register_component(var, config)
76 await button.register_button(var, config)
77
78 parent = await cg.get_variable(config[CONF_HOME_IO_CONTROL_ID])
79 cg.add(var.set_parent(parent))
80
81 result_sensor_config = {
82 CONF_ID: config[CONF_PAIRING_RESULT_SENSOR_ID],
83 CONF_NAME: "Last Pairing Result",
84 CONF_DISABLED_BY_DEFAULT: False,
85 "entity_category": ENTITY_CATEGORY_DIAGNOSTIC,
86 }
87 result_sensor = await text_sensor.new_text_sensor(result_sensor_config)
88 await cg.register_component(result_sensor, result_sensor_config)
89 cg.add(result_sensor.set_parent(parent))
_inject_pairing_result_sensor_id(config)
Definition button.py:36