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,
16 ENTITY_CATEGORY_CONFIG,
17 ENTITY_CATEGORY_DIAGNOSTIC,
19from esphome.core
import ID
21from .
import home_io_control_ns, IOHomeControlComponent, CONF_HOME_IO_CONTROL_ID
23DEPENDENCIES = [
"home_io_control"]
25IOHomeDiscoverButton = home_io_control_ns.class_(
26 "IOHomeDiscoverButton", button.Button, cg.Component
28IOHomePairingResultTextSensor = home_io_control_ns.class_(
29 "IOHomePairingResultTextSensor", text_sensor.TextSensor, cg.Component
33CONF_PAIRING_RESULT_SENSOR_ID =
"_pairing_result_sensor_id"
37 """Declare the companion pairing-result sensor ID during schema validation.
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().
44 from esphome.helpers
import sanitize
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",
51 type=IOHomePairingResultTextSensor,
56CONFIG_SCHEMA = cv.All(
59 entity_category=ENTITY_CATEGORY_CONFIG,
63 cv.GenerateID(CONF_HOME_IO_CONTROL_ID): cv.use_id(
64 IOHomeControlComponent
68 .extend(cv.COMPONENT_SCHEMA),
69 _inject_pairing_result_sensor_id,
74 var = cg.new_Pvariable(config[CONF_ID])
75 await cg.register_component(var, config)
76 await button.register_button(var, config)
78 parent = await cg.get_variable(config[CONF_HOME_IO_CONTROL_ID])
79 cg.add(var.set_parent(parent))
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,
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))