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

+28 -28
View File
@@ -12,11 +12,9 @@ from distro import linux_distribution # pylint: disable=import-error
import voluptuous as vol
from homeassistant.const import __version__ as current_version
from homeassistant.helpers import discovery, event
from homeassistant.helpers import discovery, update_coordinator
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_send
import homeassistant.util.dt as dt_util
_LOGGER = logging.getLogger(__name__)
@@ -28,8 +26,6 @@ CONF_COMPONENT_REPORTING = "include_used_components"
DOMAIN = "updater"
DISPATCHER_REMOTE_UPDATE = "updater_remote_update"
UPDATER_URL = "https://updater.home-assistant.io/"
UPDATER_UUID_FILE = ".uuid"
@@ -84,10 +80,6 @@ async def async_setup(hass, config):
# This component only makes sense in release versions
_LOGGER.info("Running on 'dev', only analytics will be submitted")
hass.async_create_task(
discovery.async_load_platform(hass, "binary_sensor", DOMAIN, {}, config)
)
config = config.get(DOMAIN, {})
if config.get(CONF_REPORTING):
huuid = await hass.async_add_job(_load_uuid, hass)
@@ -96,18 +88,17 @@ async def async_setup(hass, config):
include_components = config.get(CONF_COMPONENT_REPORTING)
async def check_new_version(now):
async def check_new_version():
"""Check if a new version is available and report if one is."""
result = await get_newest_version(hass, huuid, include_components)
newest, release_notes = await get_newest_version(
hass, huuid, include_components
)
if result is None:
return
newest, release_notes = result
_LOGGER.debug("Fetched version %s: %s", newest, release_notes)
# Skip on dev
if newest is None or "dev" in current_version:
return
if "dev" in current_version:
return Updater(False, "", "")
# Load data from supervisor on Hass.io
if hass.components.hassio.is_hassio():
@@ -116,20 +107,29 @@ async def async_setup(hass, config):
# Validate version
update_available = False
if StrictVersion(newest) > StrictVersion(current_version):
_LOGGER.info("The latest available version of Home Assistant is %s", newest)
_LOGGER.debug(
"The latest available version of Home Assistant is %s", newest
)
update_available = True
elif StrictVersion(newest) == StrictVersion(current_version):
_LOGGER.info("You are on the latest version (%s) of Home Assistant", newest)
_LOGGER.debug(
"You are on the latest version (%s) of Home Assistant", newest
)
elif StrictVersion(newest) < StrictVersion(current_version):
_LOGGER.debug("Local version is newer than the latest version (%s)", newest)
updater = Updater(update_available, newest, release_notes)
async_dispatcher_send(hass, DISPATCHER_REMOTE_UPDATE, updater)
_LOGGER.debug("Update available: %s", update_available)
# Update daily, start 1 hour after startup
_dt = dt_util.utcnow() + timedelta(hours=1)
event.async_track_utc_time_change(
hass, check_new_version, hour=_dt.hour, minute=_dt.minute, second=_dt.second
return Updater(update_available, newest, release_notes)
coordinator = hass.data[DOMAIN] = update_coordinator.DataUpdateCoordinator(
hass, _LOGGER, "Home Assistant update", check_new_version, timedelta(days=1)
)
await coordinator.async_refresh()
hass.async_create_task(
discovery.async_load_platform(hass, "binary_sensor", DOMAIN, {}, config)
)
return True
@@ -164,17 +164,17 @@ async def get_newest_version(hass, huuid, include_components):
)
except (asyncio.TimeoutError, aiohttp.ClientError):
_LOGGER.error("Could not contact Home Assistant Update to check for updates")
return None
raise update_coordinator.UpdateFailed
try:
res = await req.json()
except ValueError:
_LOGGER.error("Received invalid JSON from Home Assistant Update")
return None
raise update_coordinator.UpdateFailed
try:
res = RESPONSE_SCHEMA(res)
return res["version"], res["release-notes"]
except vol.Invalid:
_LOGGER.error("Got unexpected response: %s", res)
return None
raise update_coordinator.UpdateFailed