Use eventing for some of the upnp sensors, instead of polling (#120262)
This commit is contained in:
parent
b5f1076bb2
commit
559caf4179
7 changed files with 170 additions and 17 deletions
|
@ -1,5 +1,7 @@
|
|||
"""UPnP/IGD coordinator."""
|
||||
|
||||
from collections import defaultdict
|
||||
from collections.abc import Callable
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from async_upnp_client.exceptions import UpnpCommunicationError
|
||||
|
@ -27,6 +29,7 @@ class UpnpDataUpdateCoordinator(
|
|||
"""Initialize."""
|
||||
self.device = device
|
||||
self.device_entry = device_entry
|
||||
self._features_by_entity_id: defaultdict[str, set[str]] = defaultdict(set)
|
||||
|
||||
super().__init__(
|
||||
hass,
|
||||
|
@ -35,12 +38,35 @@ class UpnpDataUpdateCoordinator(
|
|||
update_interval=update_interval,
|
||||
)
|
||||
|
||||
def register_entity(self, key: str, entity_id: str) -> Callable[[], None]:
|
||||
"""Register an entity."""
|
||||
# self._entities.append(entity)
|
||||
self._features_by_entity_id[key].add(entity_id)
|
||||
|
||||
def unregister_entity() -> None:
|
||||
"""Unregister entity."""
|
||||
self._features_by_entity_id[key].remove(entity_id)
|
||||
|
||||
if not self._features_by_entity_id[key]:
|
||||
del self._features_by_entity_id[key]
|
||||
|
||||
return unregister_entity
|
||||
|
||||
@property
|
||||
def _entity_description_keys(self) -> list[str] | None:
|
||||
"""Return a list of entity description keys for which data is required."""
|
||||
if not self._features_by_entity_id:
|
||||
# Must be the first update, no entities attached/enabled yet.
|
||||
return None
|
||||
|
||||
return list(self._features_by_entity_id.keys())
|
||||
|
||||
async def _async_update_data(
|
||||
self,
|
||||
) -> dict[str, str | datetime | int | float | None]:
|
||||
"""Update data."""
|
||||
try:
|
||||
return await self.device.async_get_data()
|
||||
return await self.device.async_get_data(self._entity_description_keys)
|
||||
except UpnpCommunicationError as exception:
|
||||
LOGGER.debug(
|
||||
"Caught exception when updating device: %s, exception: %s",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue