Rework how shade updates are processed in powerview (#110928)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
kingy444 2024-02-23 12:58:37 +11:00 committed by GitHub
parent 98a8714705
commit 83859bd342
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -57,13 +57,24 @@ async def async_setup_entry(
pv_entry: PowerviewEntryData = hass.data[DOMAIN][entry.entry_id]
coordinator: PowerviewShadeUpdateCoordinator = pv_entry.coordinator
async def _async_initial_refresh() -> None:
"""Force position refresh shortly after adding.
Legacy shades can become out of sync with hub when moved
using physical remotes. This also allows reducing speed
of calls to older generation hubs in an effort to
prevent hub crashes.
"""
for shade in pv_entry.shade_data.values():
with suppress(TimeoutError):
# hold off to avoid spamming the hub
async with asyncio.timeout(10):
_LOGGER.debug("Initial refresh of shade: %s", shade.name)
await shade.refresh()
entities: list[ShadeEntity] = []
for shade in pv_entry.shade_data.values():
# The shade may be out of sync with the hub
# so we force a refresh when we add it if possible
with suppress(TimeoutError):
async with asyncio.timeout(1):
await shade.refresh()
coordinator.data.update_shade_position(shade.id, shade.current_position)
room_name = getattr(pv_entry.room_data.get(shade.room_id), ATTR_NAME, "")
entities.extend(
@ -74,6 +85,13 @@ async def async_setup_entry(
async_add_entities(entities)
# background the fetching of state for initial launch
entry.async_create_background_task(
hass,
_async_initial_refresh(),
f"powerview {entry.title} initial shade refresh",
)
class PowerViewShadeBase(ShadeEntity, CoverEntity):
"""Representation of a powerview shade."""
@ -305,8 +323,8 @@ class PowerViewShadeBase(ShadeEntity, CoverEntity):
# error if are already have one in flight
return
# suppress timeouts caused by hub nightly reboot
with suppress(asyncio.TimeoutError):
async with asyncio.timeout(5):
with suppress(TimeoutError):
async with asyncio.timeout(10):
await self._shade.refresh()
_LOGGER.debug("Process update %s: %s", self.name, self._shade.current_position)
self._async_update_shade_data(self._shade.current_position)