From 83859bd342b2f1780a452f81e03d7ad18384b33d Mon Sep 17 00:00:00 2001 From: kingy444 Date: Fri, 23 Feb 2024 12:58:37 +1100 Subject: [PATCH] Rework how shade updates are processed in powerview (#110928) Co-authored-by: J. Nick Koston --- .../hunterdouglas_powerview/cover.py | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/hunterdouglas_powerview/cover.py b/homeassistant/components/hunterdouglas_powerview/cover.py index 5637af57b72..5b998b697a4 100644 --- a/homeassistant/components/hunterdouglas_powerview/cover.py +++ b/homeassistant/components/hunterdouglas_powerview/cover.py @@ -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)