Skip to content

Commit

Permalink
Don't add fritz entities with update_before_add (#110667)
Browse files Browse the repository at this point in the history
Co-authored-by: mib1185 <[email protected]>
  • Loading branch information
emontnemery and mib1185 authored Feb 15, 2024
1 parent ae39945 commit 5f00e15
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/fritz/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async def async_setup_entry(
if description.is_suitable(connection_info)
]

async_add_entities(entities, True)
async_add_entities(entities)


class FritzBoxBinarySensor(FritzBoxBaseCoordinatorEntity, BinarySensorEntity):
Expand Down
24 changes: 17 additions & 7 deletions homeassistant/components/fritz/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def setup(self) -> None:

self.has_call_deflections = "X_AVM-DE_OnTel1" in self.connection.services

def register_entity_updates(
async def async_register_entity_updates(
self, key: str, update_fn: Callable[[FritzStatus, StateType], Any]
) -> Callable[[], None]:
"""Register an entity to be updated by coordinator."""
Expand All @@ -305,6 +305,12 @@ def unregister_entity_updates() -> None:
if key not in self._entity_update_functions:
_LOGGER.debug("register entity %s for updates", key)
self._entity_update_functions[key] = update_fn
if self.fritz_status:
self.data["entity_states"][
key
] = await self.hass.async_add_executor_job(
update_fn, self.fritz_status, self.data["entity_states"].get(key)
)
return unregister_entity_updates

async def _async_update_data(self) -> UpdateCoordinatorDataType:
Expand Down Expand Up @@ -1121,16 +1127,20 @@ def __init__(
) -> None:
"""Init device info class."""
super().__init__(avm_wrapper)
if description.value_fn is not None:
self.async_on_remove(
avm_wrapper.register_entity_updates(
description.key, description.value_fn
)
)
self.entity_description = description
self._device_name = device_name
self._attr_unique_id = f"{avm_wrapper.unique_id}-{description.key}"

async def async_added_to_hass(self) -> None:
"""When entity is added to hass."""
await super().async_added_to_hass()
if self.entity_description.value_fn is not None:
self.async_on_remove(
await self.coordinator.async_register_entity_updates(
self.entity_description.key, self.entity_description.value_fn
)
)

@property
def device_info(self) -> DeviceInfo:
"""Return the device information."""
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/fritz/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ async def async_setup_entry(
if description.is_suitable(connection_info)
]

async_add_entities(entities, True)
async_add_entities(entities)


class FritzBoxSensor(FritzBoxBaseCoordinatorEntity, SensorEntity):
Expand Down

0 comments on commit 5f00e15

Please sign in to comment.