Skip to content

Commit 52ddc2e

Browse files
Handle battery entity_id renaming (#930)
* Detect original battery sensor rename * WIP
1 parent a5943fa commit 52ddc2e

File tree

2 files changed

+68
-10
lines changed

2 files changed

+68
-10
lines changed

custom_components/battery_notes/binary_sensor.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,6 @@ async def async_added_to_hass(self) -> None:
201201

202202
await super().async_added_to_hass()
203203

204-
# Update entity options
205-
registry = er.async_get(self.hass)
206-
if registry.async_get(self.entity_id) is not None and self.coordinator.wrapped_battery.entity_id:
207-
registry.async_update_entity_options(
208-
self.entity_id,
209-
DOMAIN,
210-
{"entity_id": self.coordinator.wrapped_battery.entity_id},
211-
)
212-
213204
await self.coordinator.async_config_entry_first_refresh()
214205

215206
@callback

custom_components/battery_notes/sensor.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
from homeassistant.helpers.reload import async_setup_reload_service
3838
from homeassistant.helpers.typing import EventType
3939

40+
from homeassistant.helpers.entity_registry import (
41+
EVENT_ENTITY_REGISTRY_UPDATED,
42+
)
43+
4044
from homeassistant.const import (
4145
CONF_NAME,
4246
CONF_DEVICE_ID,
@@ -236,7 +240,6 @@ async def async_setup_platform(
236240

237241
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
238242

239-
240243
class BatteryNotesBatteryPlusSensor(
241244
SensorEntity, CoordinatorEntity[BatteryNotesCoordinator]
242245
):
@@ -331,6 +334,65 @@ async def async_state_changed_listener(
331334

332335
self.async_write_ha_state()
333336

337+
async def _register_entity_id_change_listener(
338+
self,
339+
entity_id: str,
340+
source_entity_id: str,
341+
) -> None:
342+
"""
343+
When the user changes the entity id of the source entity,
344+
we also need to change the battery notes plus config entry to reflect these changes
345+
This method adds the necessary listener and handler to facilitate this
346+
"""
347+
348+
@callback
349+
async def _entity_rename_listener(event: Event) -> None:
350+
"""Handle renaming of the entity"""
351+
old_entity_id = event.data["old_entity_id"]
352+
new_entity_id = event.data[CONF_ENTITY_ID]
353+
_LOGGER.debug(
354+
"Entity id has been changed, updating battery notes plus entity registry. old_id=%s, new_id=%s",
355+
old_entity_id,
356+
new_entity_id,
357+
)
358+
359+
entity_registry = er.async_get(self.hass)
360+
if (
361+
entity_registry.async_get(entity_id) is not None
362+
):
363+
entity_registry.async_update_entity_options(
364+
entity_id,
365+
DOMAIN,
366+
{"entity_id": new_entity_id},
367+
)
368+
369+
new_wrapped_battery = entity_registry.async_get(new_entity_id)
370+
self.coordinator.wrapped_battery = new_wrapped_battery
371+
372+
# Create a listener for the newly named battery entity
373+
self.async_on_remove(
374+
async_track_state_change_event(
375+
self.hass,
376+
[self.coordinator.wrapped_battery.entity_id],
377+
self.async_state_changed_listener,
378+
)
379+
)
380+
381+
@callback
382+
def _filter_entity_id(event: Event) -> bool:
383+
"""Only dispatch the listener for update events concerning the source entity"""
384+
return (
385+
event.data["action"] == "update"
386+
and "old_entity_id" in event.data
387+
and event.data["old_entity_id"] == source_entity_id
388+
)
389+
390+
self.hass.bus.async_listen(
391+
EVENT_ENTITY_REGISTRY_UPDATED,
392+
_entity_rename_listener,
393+
event_filter=_filter_entity_id,
394+
)
395+
334396
async def async_added_to_hass(self) -> None:
335397
"""Handle added to Hass."""
336398

@@ -350,6 +412,11 @@ async def _async_state_changed_listener(
350412
)
351413
)
352414

415+
await self._register_entity_id_change_listener(
416+
self.entity_id,
417+
self.coordinator.wrapped_battery.entity_id,
418+
)
419+
353420
# Call once on adding
354421
await _async_state_changed_listener()
355422

0 commit comments

Comments
 (0)