Cleanup tedee callbacks (#135577)

This commit is contained in:
Josef Zweck 2025-01-14 11:34:37 +01:00 committed by GitHub
parent 096c6b8575
commit 6359a75977
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 18 additions and 34 deletions

View File

@ -69,20 +69,15 @@ async def async_setup_entry(
"""Set up the Tedee sensor entity."""
coordinator = entry.runtime_data
async_add_entities(
TedeeBinarySensorEntity(lock, coordinator, entity_description)
for lock in coordinator.data.values()
for entity_description in ENTITIES
)
def _async_add_new_lock(lock_id: int) -> None:
lock = coordinator.data[lock_id]
def _async_add_new_lock(locks: list[TedeeLock]) -> None:
async_add_entities(
TedeeBinarySensorEntity(lock, coordinator, entity_description)
for entity_description in ENTITIES
for lock in locks
)
coordinator.new_lock_callbacks.append(_async_add_new_lock)
_async_add_new_lock(list(coordinator.data.values()))
class TedeeBinarySensorEntity(TedeeDescriptionEntity, BinarySensorEntity):

View File

@ -60,7 +60,7 @@ class TedeeApiCoordinator(DataUpdateCoordinator[dict[int, TedeeLock]]):
self._next_get_locks = time.time()
self._locks_last_update: set[int] = set()
self.new_lock_callbacks: list[Callable[[int], None]] = []
self.new_lock_callbacks: list[Callable[[list[TedeeLock]], None]] = []
self.tedee_webhook_id: int | None = None
async def _async_setup(self) -> None:
@ -158,8 +158,7 @@ class TedeeApiCoordinator(DataUpdateCoordinator[dict[int, TedeeLock]]):
# add new locks
if new_locks := current_locks - self._locks_last_update:
_LOGGER.debug("New locks found: %s", ", ".join(map(str, new_locks)))
for lock_id in new_locks:
for callback in self.new_lock_callbacks:
callback(lock_id)
for callback in self.new_lock_callbacks:
callback([self.data[lock_id] for lock_id in new_locks])
self._locks_last_update = current_locks

View File

@ -24,23 +24,18 @@ async def async_setup_entry(
"""Set up the Tedee lock entity."""
coordinator = entry.runtime_data
entities: list[TedeeLockEntity] = []
for lock in coordinator.data.values():
if lock.is_enabled_pullspring:
entities.append(TedeeLockWithLatchEntity(lock, coordinator))
else:
entities.append(TedeeLockEntity(lock, coordinator))
def _async_add_new_lock(lock_id: int) -> None:
lock = coordinator.data[lock_id]
if lock.is_enabled_pullspring:
async_add_entities([TedeeLockWithLatchEntity(lock, coordinator)])
else:
async_add_entities([TedeeLockEntity(lock, coordinator)])
def _async_add_new_lock(locks: list[TedeeLock]) -> None:
entities: list[TedeeLockEntity] = []
for lock in locks:
if lock.is_enabled_pullspring:
entities.append(TedeeLockWithLatchEntity(lock, coordinator))
else:
entities.append(TedeeLockEntity(lock, coordinator))
async_add_entities(entities)
coordinator.new_lock_callbacks.append(_async_add_new_lock)
async_add_entities(entities)
_async_add_new_lock(list(coordinator.data.values()))
class TedeeLockEntity(TedeeEntity, LockEntity):

View File

@ -58,20 +58,15 @@ async def async_setup_entry(
"""Set up the Tedee sensor entity."""
coordinator = entry.runtime_data
async_add_entities(
TedeeSensorEntity(lock, coordinator, entity_description)
for lock in coordinator.data.values()
for entity_description in ENTITIES
)
def _async_add_new_lock(lock_id: int) -> None:
lock = coordinator.data[lock_id]
def _async_add_new_lock(locks: list[TedeeLock]) -> None:
async_add_entities(
TedeeSensorEntity(lock, coordinator, entity_description)
for entity_description in ENTITIES
for lock in locks
)
coordinator.new_lock_callbacks.append(_async_add_new_lock)
_async_add_new_lock(list(coordinator.data.values()))
class TedeeSensorEntity(TedeeDescriptionEntity, SensorEntity):