16import esphome.codegen
as cg
17import esphome.config_validation
as cv
18from esphome.components
import sensor, text_sensor
19from esphome.const
import (
20 CONF_ACCURACY_DECIMALS,
22 CONF_DISABLED_BY_DEFAULT,
27 CONF_UNIT_OF_MEASUREMENT,
28 ENTITY_CATEGORY_DIAGNOSTIC,
29 STATE_CLASS_MEASUREMENT,
30 STATE_CLASS_TOTAL_INCREASING,
32from esphome.components.sensor
import DEVICE_CLASS_SIGNAL_STRENGTH
33from esphome.core
import ID
37 IOHomeControlComponent,
38 CONF_HOME_IO_CONTROL_ID,
39 device_type_expression,
42 validate_linked_remote_entry,
43 validate_status_poll_interval,
47CONF_DEVICE_ID =
"io_device_id"
48CONF_LINKED_REMOTES =
"linked_remotes"
49CONF_DEVICE_TYPE =
"io_device_type"
50CONF_SUBTYPE =
"io_subtype"
51CONF_STATUS_POLL_INTERVAL =
"status_poll_interval"
54CONF_DEVICE_NAME_SENSOR_ID =
"_device_name_sensor_id"
56CONF_ACTIVE_ISSUE_SENSOR_ID =
"_active_issue_sensor_id"
58CONF_RSSI_SENSOR_ID =
"_rssi_sensor_id"
59CONF_LAST_CONTACT_SENSOR_ID =
"_last_contact_sensor_id"
60CONF_EXCHANGE_FAILURES_SENSOR_ID =
"_exchange_failures_sensor_id"
62IOHomeDeviceNameTextSensor = home_io_control_ns.class_(
63 "IOHomeDeviceNameTextSensor", text_sensor.TextSensor, cg.Component
65IOHomeActiveIssueTextSensor = home_io_control_ns.class_(
66 "IOHomeActiveIssueTextSensor", text_sensor.TextSensor, cg.Component
68IOHomeRssiSensor = home_io_control_ns.class_(
"IOHomeRssiSensor", sensor.Sensor, cg.Component)
69IOHomeLastContactSensor = home_io_control_ns.class_(
70 "IOHomeLastContactSensor", sensor.Sensor, cg.Component
72IOHomeExchangeFailuresSensor = home_io_control_ns.class_(
73 "IOHomeExchangeFailuresSensor", sensor.Sensor, cg.Component
78_COMPANION_SENSOR_IDS = (
79 (CONF_DEVICE_NAME_SENSOR_ID,
"device_name_sensor", IOHomeDeviceNameTextSensor),
80 (CONF_ACTIVE_ISSUE_SENSOR_ID,
"active_issue_sensor", IOHomeActiveIssueTextSensor),
81 (CONF_RSSI_SENSOR_ID,
"rssi_sensor", IOHomeRssiSensor),
82 (CONF_LAST_CONTACT_SENSOR_ID,
"last_contact_sensor", IOHomeLastContactSensor),
83 (CONF_EXCHANGE_FAILURES_SENSOR_ID,
"exchange_failures_sensor", IOHomeExchangeFailuresSensor),
88 """Derive a companion sensor's entity name from the parent entity name."""
89 base_name = config.get(CONF_NAME,
"")
91 return f
"{base_name} {suffix}"
96 """Return the shared ID prefix for a platform's companion entity IDs.
98 ESPHome 2026.x sizes its runtime component vector (StaticVector) from the number of
99 component IDs known at the end of schema validation — before to_code() runs. If
100 companion entities are only created inside to_code(), their IDs are not counted and
101 the StaticVector overflows at runtime, silently dropping later components whose
102 setup() then never executes. Companion IDs must therefore be declared during
103 validation, and they all share the prefix returned here.
105 ``parent_id_key`` differs per platform: light reads the entity ID from
106 CONF_OUTPUT_ID, while cover, switch and lock read it from CONF_ID.
108 from esphome.helpers
import sanitize
110 parent_id = config[parent_id_key]
113 return parent_id.id
if parent_id.id
else sanitize(config[CONF_NAME]).lower()
117 """Declare every auto-generated companion sensor ID during schema validation.
119 Shared post-validator body for every device-bound platform, covering all entries in
120 _COMPANION_SENSOR_IDS. See companion_id_base() for why the IDs must be declared at
121 validation time rather than in to_code().
124 for conf_key, id_suffix, sensor_class
in _COMPANION_SENSOR_IDS:
125 config[conf_key] = ID(
126 f
"{base}_{id_suffix}", is_declaration=
True, type=sensor_class
132 """Return the shared schema keys every device-bound platform extends with."""
134 cv.Required(CONF_NAME): cv.string,
135 cv.GenerateID(CONF_HOME_IO_CONTROL_ID): cv.use_id(IOHomeControlComponent),
136 cv.Required(CONF_DEVICE_ID): validate_device_id,
137 cv.Optional(CONF_DEVICE_TYPE): validate_device_type,
138 cv.Optional(CONF_SUBTYPE): cv.int_range(min=0, max=63),
139 cv.Optional(CONF_LINKED_REMOTES): cv.ensure_list(validate_linked_remote_entry),
140 cv.Optional(CONF_STATUS_POLL_INTERVAL): validate_status_poll_interval,
145 """Emit the shared to_code() wiring that binds an entity to its hub device.
147 Covers set_parent / set_device_id, the optional device type / subtype / status
148 poll interval, and the linked-remotes registration loop — identical across all
149 four device-bound platforms.
151 cg.add(var.set_parent(parent))
152 cg.add(var.set_device_id(config[CONF_DEVICE_ID]))
154 if CONF_DEVICE_TYPE
in config:
155 cg.add(var.set_device_type(device_type_expression(config[CONF_DEVICE_TYPE])))
156 if CONF_SUBTYPE
in config:
157 cg.add(var.set_subtype(config[CONF_SUBTYPE]))
158 if CONF_STATUS_POLL_INTERVAL
in config:
160 var.set_status_poll_interval(
161 config[CONF_STATUS_POLL_INTERVAL].total_milliseconds
165 if CONF_LINKED_REMOTES
in config:
166 for remote_id
in config[CONF_LINKED_REMOTES]:
167 if remote_id.startswith(
"class:"):
169 type_value = int(remote_id.split(
":", 1)[1], 16)
171 parent.add_linked_remote_class(
172 device_type_expression(type_value),
173 config[CONF_DEVICE_ID],
177 cg.add(parent.add_linked_remote(remote_id, config[CONF_DEVICE_ID]))
181 """Shared body for the auto-generated companion `text_sensor:` entities."""
185 CONF_DISABLED_BY_DEFAULT: disabled_by_default,
186 "entity_category": ENTITY_CATEGORY_DIAGNOSTIC,
188 companion = await text_sensor.new_text_sensor(companion_config)
189 await cg.register_component(companion, companion_config)
190 cg.add(companion.set_parent(parent))
191 cg.add(companion.set_device_id(config[CONF_DEVICE_ID]))
195 """Shared body for the three auto-generated link-health `sensor:` companions.
197 All three (RSSI, Last Contact, Exchange Failures) are numeric, diagnostic, and disabled by
198 default (noise control); only the name and sensor-specific schema keys
199 (unit/device_class/state_class/accuracy_decimals) differ between them, so those are the
200 only things each call in create_companion_sensors() supplies.
202 if CONF_STATE_CLASS
in sensor_kwargs:
207 sensor_kwargs[CONF_STATE_CLASS] = sensor.validate_state_class(sensor_kwargs[CONF_STATE_CLASS])
209 link_health_config = {
212 CONF_DISABLED_BY_DEFAULT:
True,
213 "entity_category": ENTITY_CATEGORY_DIAGNOSTIC,
214 CONF_FORCE_UPDATE:
False,
217 var = await sensor.new_sensor(link_health_config)
218 await cg.register_component(var, link_health_config)
219 cg.add(var.set_parent(parent))
220 cg.add(var.set_device_id(config[CONF_DEVICE_ID]))
224 """Create and register every auto-generated companion diagnostic sensor.
226 Single to_code() entry point for the device-bound platforms (the counterpart of
227 inject_companion_sensor_ids()), so adding a companion touches this module only:
229 - Device Name: disabled by default (clutter control).
230 - Active Issue: the one enabled-by-default companion — it is the headline diagnostic
231 value that turns a silently-ignored command into a self-explained one (e.g. a
232 wind/rain lockout), so users should see it without an opt-in step. Empty except while
233 a CMD_ERROR_RESP reason is outstanding; see IOHomeActiveIssueTextSensor.
234 - RSSI / Last Contact / Exchange Failures: numeric link-health diagnostics, disabled by
235 default (noise control). Last Contact publishes seconds since the last frame from the
236 device (an age, not a Home Assistant timestamp) and keeps counting up between frames via
237 its own heartbeat; see IOHomeLastContactSensor.
242 config[CONF_DEVICE_NAME_SENSOR_ID],
244 disabled_by_default=
True,
249 config[CONF_ACTIVE_ISSUE_SENSOR_ID],
251 disabled_by_default=
False,
256 config[CONF_RSSI_SENSOR_ID],
259 CONF_UNIT_OF_MEASUREMENT:
"dBm",
260 CONF_DEVICE_CLASS: DEVICE_CLASS_SIGNAL_STRENGTH,
261 CONF_STATE_CLASS: STATE_CLASS_MEASUREMENT,
262 CONF_ACCURACY_DECIMALS: 0,
268 config[CONF_LAST_CONTACT_SENSOR_ID],
271 CONF_UNIT_OF_MEASUREMENT:
"s",
272 CONF_STATE_CLASS: STATE_CLASS_MEASUREMENT,
273 CONF_ACCURACY_DECIMALS: 0,
279 config[CONF_EXCHANGE_FAILURES_SENSOR_ID],
282 CONF_STATE_CLASS: STATE_CLASS_TOTAL_INCREASING,
283 CONF_ACCURACY_DECIMALS: 0,