Use Debouncer helper in HEOS Coordinator (#141133)

Use Debouncer
This commit is contained in:
Andrew Sayre 2025-03-22 14:12:51 -05:00 committed by GitHub
parent f245bbd8dd
commit 7f640252a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,7 +6,6 @@ entities to update. Entities subscribe to entity-specific updates within the ent
""" """
from collections.abc import Callable, Sequence from collections.abc import Callable, Sequence
from datetime import datetime, timedelta
import logging import logging
from typing import Any from typing import Any
@ -25,10 +24,10 @@ from pyheos import (
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.event import async_call_later from homeassistant.helpers.debounce import Debouncer
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import DOMAIN from .const import DOMAIN
@ -60,7 +59,13 @@ class HeosCoordinator(DataUpdateCoordinator[None]):
) )
) )
self._platform_callbacks: list[Callable[[Sequence[HeosPlayer]], None]] = [] self._platform_callbacks: list[Callable[[Sequence[HeosPlayer]], None]] = []
self._update_sources_pending: bool = False self._update_sources_debouncer = Debouncer(
hass,
_LOGGER,
immediate=True,
cooldown=2.0,
function=self._async_update_sources,
)
self._source_list: list[str] = [] self._source_list: list[str] = []
self._favorites: dict[int, MediaItem] = {} self._favorites: dict[int, MediaItem] = {}
self._inputs: Sequence[MediaItem] = [] self._inputs: Sequence[MediaItem] = []
@ -182,31 +187,9 @@ class HeosCoordinator(DataUpdateCoordinator[None]):
if event == const.EVENT_PLAYERS_CHANGED: if event == const.EVENT_PLAYERS_CHANGED:
assert data is not None assert data is not None
self._async_handle_player_update_result(data) self._async_handle_player_update_result(data)
elif ( elif event in (const.EVENT_SOURCES_CHANGED, const.EVENT_USER_CHANGED):
event in (const.EVENT_SOURCES_CHANGED, const.EVENT_USER_CHANGED) # Debounce because we may have received multiple qualifying events in rapid succession.
and not self._update_sources_pending await self._update_sources_debouncer.async_call()
):
# Update the sources after a brief delay as we may have received multiple qualifying
# events at once and devices cannot handle immediately attempting to refresh sources.
self._update_sources_pending = True
async def update_sources_job(_: datetime | None = None) -> None:
await self._async_update_sources()
self._update_sources_pending = False
self.async_update_listeners()
assert self.config_entry is not None
self.config_entry.async_on_unload(
async_call_later(
self.hass,
timedelta(seconds=1),
HassJob(
update_sources_job,
"heos_update_sources",
cancel_on_shutdown=True,
),
)
)
self.async_update_listeners() self.async_update_listeners()
def _async_update_player_ids(self, updated_player_ids: dict[int, int]) -> None: def _async_update_player_ids(self, updated_player_ids: dict[int, int]) -> None: