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_ENTITY_CATEGORY,
15 CONF_ID,
16 CONF_NAME,
17 ENTITY_CATEGORY_CONFIG,
18 ENTITY_CATEGORY_DIAGNOSTIC,
19)
20from esphome.core import ID
21
22from . import (
23 home_io_control_ns,
24 IOHomeControlComponent,
25 CONF_HOME_IO_CONTROL_ID,
26 inherit_esphome_device,
27)
28
29DEPENDENCIES = ["home_io_control"]
30
31IOHomeDiscoverButton = home_io_control_ns.class_(
32 "IOHomeDiscoverButton", button.Button, cg.Component
33)
34IOHomePairingResultTextSensor = home_io_control_ns.class_(
35 "IOHomePairingResultTextSensor", text_sensor.TextSensor, cg.Component
36)
37
38# Internal config key for the companion pairing-result sensor ID (injected by post-validator).
39CONF_PAIRING_RESULT_SENSOR_ID = "_pairing_result_sensor_id"
40
41
43 """Declare the companion pairing-result sensor ID during schema validation.
44
45 ESPHome 2026.x sizes its runtime component vector from the number of component IDs
46 known at the end of schema validation — before to_code() runs. A companion entity
47 created only in to_code() is not counted and silently drops at runtime. See the
48 identical pattern (and its full rationale) in platform_common.py::companion_id_base().
49 """
50 from esphome.helpers import sanitize
51
52 parent_id = config[CONF_ID]
53 base = parent_id.id if parent_id.id else sanitize(config.get(CONF_NAME, "")).lower()
54 config[CONF_PAIRING_RESULT_SENSOR_ID] = ID(
55 f"{base}_pairing_result_sensor",
56 is_declaration=True,
57 type=IOHomePairingResultTextSensor,
58 )
59 return config
60
61
62CONFIG_SCHEMA = cv.All(
63 button.button_schema(
64 IOHomeDiscoverButton,
65 entity_category=ENTITY_CATEGORY_CONFIG,
66 )
67 .extend(
68 {
69 cv.GenerateID(CONF_HOME_IO_CONTROL_ID): cv.use_id(
70 IOHomeControlComponent
71 ),
72 }
73 )
74 .extend(cv.COMPONENT_SCHEMA),
75 _inject_pairing_result_sensor_id,
76)
77
78
79async def to_code(config):
80 var = cg.new_Pvariable(config[CONF_ID])
81 await cg.register_component(var, config)
82 await button.register_button(var, config)
83
84 parent = await cg.get_variable(config[CONF_HOME_IO_CONTROL_ID])
85 cg.add(var.set_parent(parent))
86
87 result_sensor_config = inherit_esphome_device(
88 {
89 CONF_ID: config[CONF_PAIRING_RESULT_SENSOR_ID],
90 CONF_NAME: "Last Pairing Result",
91 CONF_DISABLED_BY_DEFAULT: False,
92 CONF_ENTITY_CATEGORY: ENTITY_CATEGORY_DIAGNOSTIC,
93 },
94 config,
95 )
96 result_sensor = await text_sensor.new_text_sensor(result_sensor_config)
97 await cg.register_component(result_sensor, result_sensor_config)
98 cg.add(result_sensor.set_parent(parent))
_inject_pairing_result_sensor_id(config)
Definition button.py:42