Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Jun 16, 2023
1 parent 3563ef5 commit adfe2e3
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions custom_components/powercalc/sensor.py
Expand Up @@ -279,23 +279,8 @@ async def async_setup_entry(
if CONF_UNIQUE_ID not in sensor_config:
sensor_config[CONF_UNIQUE_ID] = entry.unique_id

@callback
async def _entity_rename_listener(event: Event) -> None:
await handle_entity_id_rename(hass, entry, event)

@callback
def _filter_entity_id(event: Event) -> None:
return (
event.data["action"] == "update" and
"old_entity_id" in event.data
and event.data["old_entity_id"] == sensor_config.get(CONF_ENTITY_ID)
)

hass.bus.async_listen(
EVENT_ENTITY_REGISTRY_UPDATED,
_entity_rename_listener,
event_filter=_filter_entity_id,
)
if CONF_ENTITY_ID in sensor_config:
_register_entity_id_change_listener(hass, entry, sensor_config.get(CONF_ENTITY_ID))

Check failure on line 283 in custom_components/powercalc/sensor.py

View workflow job for this annotation

GitHub Actions / mypy

Argument 3 to "_register_entity_id_change_listener" has incompatible type "Optional[Any]"; expected "str" [arg-type]

Check failure on line 283 in custom_components/powercalc/sensor.py

View workflow job for this annotation

GitHub Actions / mypy

Argument 3 to "_register_entity_id_change_listener" has incompatible type "Optional[Any]"; expected "str" [arg-type]

await _async_setup_entities(
hass,
Expand All @@ -308,14 +293,6 @@ def _filter_entity_id(event: Event) -> None:
await add_to_associated_group(hass, entry)


async def handle_entity_id_rename(hass: HomeAssistant, config_entry: ConfigEntry, event: Event) -> None:
"""Handle entity registry updated."""
old_entity_id = event.data["old_entity_id"]
new_entity_id = event.data[CONF_ENTITY_ID]
_LOGGER.debug(f"Entity id has been changed, updating powercalc config. old_id={old_entity_id}, new_id={new_entity_id}")
hass.config_entries.async_update_entry(config_entry, data={**config_entry.data, CONF_ENTITY_ID: new_entity_id})


async def _async_setup_entities(
hass: HomeAssistant,
config: dict[str, Any],
Expand Down Expand Up @@ -358,6 +335,38 @@ async def _async_setup_entities(
async_add_entities(entities_to_add)


def _register_entity_id_change_listener(hass: HomeAssistant, entry: ConfigEntry, source_entity_id: str) -> None:
"""
When the user changes the entity id of the source entity,
we also need to change the powercalc config entry to reflect these changes
This method adds the necessary listener and handler to facilitate this
"""
@callback
async def _entity_rename_listener(event: Event) -> None:
"""Handle renaming of the entity"""
old_entity_id = event.data["old_entity_id"]
new_entity_id = event.data[CONF_ENTITY_ID]
_LOGGER.debug(
f"Entity id has been changed, updating powercalc config. old_id={old_entity_id}, new_id={new_entity_id}",
)
hass.config_entries.async_update_entry(entry, data={**entry.data, CONF_ENTITY_ID: new_entity_id})

@callback
def _filter_entity_id(event: Event) -> None:
"""Only dispatch the listener for update events concerning the source entity"""
return (

Check failure on line 357 in custom_components/powercalc/sensor.py

View workflow job for this annotation

GitHub Actions / mypy

No return value expected [return-value]

Check failure on line 357 in custom_components/powercalc/sensor.py

View workflow job for this annotation

GitHub Actions / mypy

No return value expected [return-value]
event.data["action"] == "update" and
"old_entity_id" in event.data
and event.data["old_entity_id"] == source_entity_id
)

hass.bus.async_listen(
EVENT_ENTITY_REGISTRY_UPDATED,
_entity_rename_listener,
event_filter=_filter_entity_id,
)


@callback
def save_entity_ids_on_config_entry(
hass: HomeAssistant,
Expand Down

0 comments on commit adfe2e3

Please sign in to comment.