Skip to content

Handle entity_id changes in place (async_entity_id_changed, Home Assistant 2027.11) #5250

Open

Description

@emontnemery

From Home Assistant Core 2026.11, an entity whose entity_id is changed in the entity registry (for example renamed by a user in the UI) is no longer removed and added again. It is updated in place: the state under the old entity_id is removed, self.entity_id is set to the new value, and the state is written under the new entity_id. async_will_remove_from_hass, async_added_to_hass and callbacks registered with async_on_remove are not called. Entities can implement a new @callback method, async_entity_id_changed(self, old_entity_id), to update anything that is keyed on their own entity_id.

Custom integrations get a transition period. An entity is still removed and added again if one of the integration's own classes in its class hierarchy defines async_prepare_to_add_to_hass, async_added_to_hass or async_will_remove_from_hass, and neither that class nor a subclass of it defines async_entity_id_changed. Base classes from Home Assistant Core, such as CoordinatorEntity or SensorEntity, don't count. This backwards compatibility will be removed in Home Assistant Core 2027.11; from then on, every entity is changed in place.

What in battery_notes depends on the entity_id

  1. Template sensors bind this to the old entity_id. BatteryNotesBatteryLowBinaryTemplateSensor (custom_components/battery_notes/binary_sensor.py:302) and BatteryNotesBatteryPlusTemplateSensor (sensor.py:808) render their template with {"this": TemplateStateFromEntityId(self.hass, self.entity_id)} (binary_sensor.py:391, sensor.py:902). On the default branch this code has moved to TemplateTrackingMixin._async_template_startup in template_helpers.py, unchanged. After an in-place entity_id change, this keeps pointing at the old entity_id, which no longer has a state.

  2. The wrapped-battery rename listener captures the own entity_id. BatteryNotesBatteryBinaryLowSensor and BatteryNotesBatteryPlusSensor call _register_entity_id_change_listener(self.entity_id, ...) (binary_sensor.py:740-743, sensor.py:728-731). The closure looks up its own registry entry with that captured id (binary_sensor.py:685, sensor.py:654). After an in-place change of the Battery Notes entity's own entity_id that lookup returns None, so a later rename of the wrapped battery entity is ignored.

Suggested fix

1. Template sensors: re-track in the hook, after writing the state. Rendering this needs the entity's state under the new entity_id, so call super(), write the state, then restart template tracking. This is what core's template entities do. With the default branch's TemplateTrackingMixin:

# template_helpers.py, TemplateTrackingMixin
@callback
def async_entity_id_changed(self, old_entity_id: str) -> None:
    """Re-track the template, `this` was bound to the old entity_id."""
    super().async_entity_id_changed(old_entity_id)
    self.async_write_ha_state()
    if self._template_result_info is None:
        return  # not started yet, the startup will use the new entity_id
    self._template_result_info.async_remove()
    self._async_template_startup(None)

_async_template_startup registers self.async_on_remove(result_info.async_remove) on every call, so each entity_id change adds another remover. Consider registering one callback once that removes whatever self._template_result_info currently is.

Both template sensor classes define their own async_added_to_hass, so a hook on the mixin doesn't opt them in before 2027.11. Give each of them a hook that only calls super() (see below); that runs the mixin's hook.

2. Rename listener: read the live entity_id (stable key). Use self.entity_id inside the closure instead of the captured entity_id argument (and drop the argument):

entity_registry = er.async_get(self.hass)
if entity_registry.async_get(self.entity_id) is not None:
    entity_registry.async_update_entity_options(
        self.entity_id, DOMAIN, {"entity_id": new_entity_id}
    )

Nothing else in these two classes depends on their own entity_id, so they then only need an empty hook to opt in early.

Unrelated, but in the same listener: _filter_entity_id compares against the wrapped battery's entity_id captured when the listener was registered, so only the first rename of the wrapped battery is followed.

Opting in before 2027.11 (optional)

Until 2027.11, entities of the classes below are still removed and added again on an entity_id change, unless the class (or a subclass) defines the hook. Add this to each to get in-place changes now:

@callback
def async_entity_id_changed(self, old_entity_id: str) -> None:
    super().async_entity_id_changed(old_entity_id)
  • BatteryNotesBatteryLowBinaryTemplateSensor (binary_sensor.py:302) and BatteryNotesBatteryPlusTemplateSensor (sensor.py:808), on top of the mixin hook above
  • BatteryNotesBatteryBinaryLowSensor (binary_sensor.py:595) and BatteryNotesBatteryPlusSensor (sensor.py:490), after the change in 2.
  • BatteryNotesBatteryPercentageTemplateLowSensor (binary_sensor.py:482)
  • BatteryNotesBatteryWrappedLowSensor (binary_sensor.py:533)
  • BatteryNotesButton (button.py:91)
  • BatteryNotesTypeSensor (sensor.py:235)
  • BatteryNotesLastReplacedSensor (sensor.py:307)

A hook on each of the three binary sensor leaf classes also covers their common base BatteryNotesNonTemplateBatteryLowSensor, which defines async_added_to_hass too. These async_added_to_hass bodies only use self.entity_id at add time, to set registry options or hidden_by on the entity's own registry entry, which stays with the entry when the entity_id changes.


This is based on reading the code as of 2026-09-30 (line numbers refer to that version). The default branch has changed since (the template code moved to TemplateTrackingMixin in template_helpers.py), but both dependencies are still there as of 2026-10-12. It may be outdated, so please double-check against the current code.

Activity

  1. andrew-codechimp commented on Oct 13, 2026

    @andrew-codechimp
    Owner

    Thanks @emontnemery for raising this, and ones in my other repos.
    I do regularly follow the dev blog and had noted I'll need to take action on this but great to have the additional info.

  2. added
    HA deprecation
    no-stale
    on Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

HA deprecationno-stale

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions