Retry creating esphome update entities later if dashboard is unavailable (#92042)

This commit is contained in:
J. Nick Koston 2023-04-26 18:41:00 +02:00 committed by GitHub
parent f33e8c518f
commit ec5f50913a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 19 deletions

View file

@ -13,7 +13,7 @@ from homeassistant.components.update import (
UpdateEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import DeviceInfo
@ -33,35 +33,36 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up ESPHome update based on a config entry."""
dashboard = async_get_dashboard(hass)
if dashboard is None:
if (dashboard := async_get_dashboard(hass)) is None:
return
entry_data = DomainData.get(hass).get_entry_data(entry)
unsub = None
unsubs: list[CALLBACK_TYPE] = []
async def setup_update_entity() -> None:
@callback
def _async_setup_update_entity() -> None:
"""Set up the update entity."""
nonlocal unsub
nonlocal unsubs
assert dashboard is not None
# Keep listening until device is available
if not entry_data.available:
if not entry_data.available or not dashboard.last_update_success:
return
if unsub is not None:
unsub() # type: ignore[unreachable]
for unsub in unsubs:
unsub()
unsubs.clear()
assert dashboard is not None
async_add_entities([ESPHomeUpdateEntity(entry_data, dashboard)])
if entry_data.available:
await setup_update_entity()
if entry_data.available and dashboard.last_update_success:
_async_setup_update_entity()
return
unsub = async_dispatcher_connect(
hass, entry_data.signal_device_updated, setup_update_entity
)
unsubs = [
async_dispatcher_connect(
hass, entry_data.signal_device_updated, _async_setup_update_entity
),
dashboard.async_add_listener(_async_setup_update_entity),
]
class ESPHomeUpdateEntity(CoordinatorEntity[ESPHomeDashboard], UpdateEntity):
@ -88,7 +89,11 @@ class ESPHomeUpdateEntity(CoordinatorEntity[ESPHomeDashboard], UpdateEntity):
# If the device has deep sleep, we can't assume we can install updates
# as the ESP will not be connectable (by design).
if coordinator.supports_update and not self._device_info.has_deep_sleep:
if (
coordinator.last_update_success
and coordinator.supports_update
and not self._device_info.has_deep_sleep
):
self._attr_supported_features = UpdateEntityFeature.INSTALL
@property