From fca96799a0fb26d54c5e3d6b70528daf11d9a38c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 16 May 2020 16:34:34 -0500 Subject: [PATCH] Prevent updater from delaying startup (#35708) * Prevent updater from delaying startup The updater sometimes times out as seen in #33833 and the linked issues. The issue was presenting again today as it appears the service is overloaded again. * s/hass.loop/asyncio/g --- homeassistant/components/updater/__init__.py | 4 +++- homeassistant/components/updater/binary_sensor.py | 12 +++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/updater/__init__.py b/homeassistant/components/updater/__init__.py index 869e3c55271..0b53850733f 100644 --- a/homeassistant/components/updater/__init__.py +++ b/homeassistant/components/updater/__init__.py @@ -1,4 +1,5 @@ """Support to check for available updates.""" +import asyncio from datetime import timedelta from distutils.version import StrictVersion import logging @@ -105,7 +106,8 @@ async def async_setup(hass, config): update_interval=timedelta(days=1), ) - await coordinator.async_refresh() + # This can take up to 15s which can delay startup + asyncio.create_task(coordinator.async_refresh()) hass.async_create_task( discovery.async_load_platform(hass, "binary_sensor", DOMAIN, {}, config) diff --git a/homeassistant/components/updater/binary_sensor.py b/homeassistant/components/updater/binary_sensor.py index 5088856ce6d..5d45b368500 100644 --- a/homeassistant/components/updater/binary_sensor.py +++ b/homeassistant/components/updater/binary_sensor.py @@ -33,6 +33,8 @@ class UpdaterBinary(BinarySensorEntity): @property def is_on(self) -> bool: """Return true if the binary sensor is on.""" + if not self.coordinator.data: + return None return self.coordinator.data.update_available @property @@ -48,6 +50,8 @@ class UpdaterBinary(BinarySensorEntity): @property def device_state_attributes(self) -> dict: """Return the optional state attributes.""" + if not self.coordinator.data: + return None data = {} if self.coordinator.data.release_notes: data[ATTR_RELEASE_NOTES] = self.coordinator.data.release_notes @@ -57,11 +61,9 @@ class UpdaterBinary(BinarySensorEntity): async def async_added_to_hass(self): """Register update dispatcher.""" - self.coordinator.async_add_listener(self.async_write_ha_state) - - async def async_will_remove_from_hass(self): - """When removed from hass.""" - self.coordinator.async_remove_listener(self.async_write_ha_state) + self.async_on_remove( + self.coordinator.async_add_listener(self.async_write_ha_state) + ) async def async_update(self): """Update the entity.