Remove hour delay before checking for updates (#31368)

* Check for updates at startup

* Add 100% test coverage for update_coordinator

* Address comments
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2020-02-01 08:14:28 -08:00
1 parent b373c202c9
commit c67f53dc43
5 files changed
+223 -198

No files matched your search

@@ -1,26 +1,24 @@
"""Support for Home Assistant Updater binary sensors."""
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from . import ATTR_NEWEST_VERSION, ATTR_RELEASE_NOTES, DISPATCHER_REMOTE_UPDATE, Updater
from . import ATTR_NEWEST_VERSION, ATTR_RELEASE_NOTES, DOMAIN as UPDATER_DOMAIN
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the updater binary sensors."""
async_add_entities([UpdaterBinary()])
if discovery_info is None:
return
async_add_entities([UpdaterBinary(hass.data[UPDATER_DOMAIN])])
class UpdaterBinary(BinarySensorDevice):
"""Representation of an updater binary sensor."""
def __init__(self):
def __init__(self, coordinator):
"""Initialize the binary sensor."""
self._update_available = None
self._release_notes = None
self._newest_version = None
self._unsub_dispatcher = None
self.coordinator = coordinator
@property
def name(self) -> str:
@@ -35,12 +33,12 @@ class UpdaterBinary(BinarySensorDevice):
@property
def is_on(self) -> bool:
"""Return true if the binary sensor is on."""
return self._update_available
return self.coordinator.data.update_available
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self._update_available is not None
return not self.coordinator.failed_last_update
@property
def should_poll(self) -> bool:
@@ -50,32 +48,24 @@ class UpdaterBinary(BinarySensorDevice):
@property
def device_state_attributes(self) -> dict:
"""Return the optional state attributes."""
data = super().device_state_attributes
if data is None:
data = {}
if self._release_notes:
data[ATTR_RELEASE_NOTES] = self._release_notes
if self._newest_version:
data[ATTR_NEWEST_VERSION] = self._newest_version
data = {}
if self.coordinator.data.release_notes:
data[ATTR_RELEASE_NOTES] = self.coordinator.data.release_notes
if self.coordinator.data.newest_version:
data[ATTR_NEWEST_VERSION] = self.coordinator.data.newest_version
return data
async def async_added_to_hass(self):
"""Register update dispatcher."""
@callback
def async_state_update(updater: Updater):
"""Update callback."""
self._newest_version = updater.newest_version
self._release_notes = updater.release_notes
self._update_available = updater.update_available
self.async_schedule_update_ha_state()
self._unsub_dispatcher = async_dispatcher_connect(
self.hass, DISPATCHER_REMOTE_UPDATE, async_state_update
)
self.coordinator.async_add_listener(self.async_write_ha_state)
async def async_will_remove_from_hass(self):
"""Register update dispatcher."""
if self._unsub_dispatcher is not None:
self._unsub_dispatcher()
self._unsub_dispatcher = None
"""When removed from hass."""
self.coordinator.async_remove_listener(self.async_write_ha_state)
async def async_update(self):
"""Update the entity.
Only used by the generic entity update service.
"""
await self.coordinator.async_request_refresh()