2019-02-13 20:21:14 +00:00
|
|
|
"""Support to check for available updates."""
|
2017-05-02 06:29:01 +00:00
|
|
|
import asyncio
|
2017-11-30 14:58:50 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
from distutils.version import StrictVersion
|
2016-10-20 19:30:44 +00:00
|
|
|
import json
|
2016-11-28 19:49:01 +00:00
|
|
|
import logging
|
2016-10-20 19:30:44 +00:00
|
|
|
import uuid
|
2015-11-15 10:00:24 +00:00
|
|
|
|
2017-05-02 06:29:01 +00:00
|
|
|
import aiohttp
|
|
|
|
import async_timeout
|
2019-12-09 17:54:54 +00:00
|
|
|
from distro import linux_distribution # pylint: disable=import-error
|
2016-09-30 02:02:22 +00:00
|
|
|
import voluptuous as vol
|
2015-11-15 10:00:24 +00:00
|
|
|
|
2019-08-07 22:28:22 +00:00
|
|
|
from homeassistant.const import __version__ as current_version
|
2020-02-01 16:14:28 +00:00
|
|
|
from homeassistant.helpers import discovery, update_coordinator
|
2017-05-02 06:29:01 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2016-11-28 19:49:01 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2015-11-15 10:00:24 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-11-28 19:49:01 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_RELEASE_NOTES = "release_notes"
|
2019-08-07 22:28:22 +00:00
|
|
|
ATTR_NEWEST_VERSION = "newest_version"
|
2016-11-28 19:49:01 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_REPORTING = "reporting"
|
|
|
|
CONF_COMPONENT_REPORTING = "include_used_components"
|
2016-10-20 19:30:44 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN = "updater"
|
2016-11-28 19:49:01 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
UPDATER_URL = "https://updater.home-assistant.io/"
|
|
|
|
UPDATER_UUID_FILE = ".uuid"
|
2016-10-20 19:30:44 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: {
|
|
|
|
vol.Optional(CONF_REPORTING, default=True): cv.boolean,
|
|
|
|
vol.Optional(CONF_COMPONENT_REPORTING, default=False): cv.boolean,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2015-11-15 10:00:24 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
RESPONSE_SCHEMA = vol.Schema(
|
|
|
|
{vol.Required("version"): cv.string, vol.Required("release-notes"): cv.url}
|
|
|
|
)
|
2016-11-30 21:03:09 +00:00
|
|
|
|
2016-09-30 02:02:22 +00:00
|
|
|
|
2019-08-07 22:28:22 +00:00
|
|
|
class Updater:
|
|
|
|
"""Updater class for data exchange."""
|
|
|
|
|
|
|
|
def __init__(self, update_available: bool, newest_version: str, release_notes: str):
|
|
|
|
"""Initialize attributes."""
|
|
|
|
self.update_available = update_available
|
|
|
|
self.release_notes = release_notes
|
|
|
|
self.newest_version = newest_version
|
|
|
|
|
|
|
|
|
2016-10-20 19:30:44 +00:00
|
|
|
def _create_uuid(hass, filename=UPDATER_UUID_FILE):
|
|
|
|
"""Create UUID and save it in a file."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with open(hass.config.path(filename), "w") as fptr:
|
2016-10-20 19:30:44 +00:00
|
|
|
_uuid = uuid.uuid4().hex
|
2019-07-31 19:25:30 +00:00
|
|
|
fptr.write(json.dumps({"uuid": _uuid}))
|
2016-10-20 19:30:44 +00:00
|
|
|
return _uuid
|
|
|
|
|
|
|
|
|
|
|
|
def _load_uuid(hass, filename=UPDATER_UUID_FILE):
|
2016-11-28 19:49:01 +00:00
|
|
|
"""Load UUID from a file or return None."""
|
2016-10-20 19:30:44 +00:00
|
|
|
try:
|
|
|
|
with open(hass.config.path(filename)) as fptr:
|
|
|
|
jsonf = json.loads(fptr.read())
|
2019-07-31 19:25:30 +00:00
|
|
|
return uuid.UUID(jsonf["uuid"], version=4).hex
|
2016-10-20 19:30:44 +00:00
|
|
|
except (ValueError, AttributeError):
|
|
|
|
return None
|
|
|
|
except FileNotFoundError:
|
|
|
|
return _create_uuid(hass, filename)
|
2016-09-30 02:02:22 +00:00
|
|
|
|
2015-11-15 10:00:24 +00:00
|
|
|
|
2018-04-28 23:26:20 +00:00
|
|
|
async def async_setup(hass, config):
|
2016-11-28 19:49:01 +00:00
|
|
|
"""Set up the updater component."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if "dev" in current_version:
|
2016-10-20 19:30:44 +00:00
|
|
|
# This component only makes sense in release versions
|
2018-09-25 05:52:10 +00:00
|
|
|
_LOGGER.info("Running on 'dev', only analytics will be submitted")
|
2016-04-07 01:46:48 +00:00
|
|
|
|
2020-02-03 08:51:27 +00:00
|
|
|
conf = config.get(DOMAIN, {})
|
|
|
|
if conf.get(CONF_REPORTING):
|
2018-04-28 23:26:20 +00:00
|
|
|
huuid = await hass.async_add_job(_load_uuid, hass)
|
2017-05-02 06:29:01 +00:00
|
|
|
else:
|
|
|
|
huuid = None
|
|
|
|
|
2020-02-03 08:51:27 +00:00
|
|
|
include_components = conf.get(CONF_COMPONENT_REPORTING)
|
2017-06-16 05:29:18 +00:00
|
|
|
|
2020-02-01 16:14:28 +00:00
|
|
|
async def check_new_version():
|
2017-05-02 06:29:01 +00:00
|
|
|
"""Check if a new version is available and report if one is."""
|
2020-02-01 16:14:28 +00:00
|
|
|
newest, release_notes = await get_newest_version(
|
|
|
|
hass, huuid, include_components
|
|
|
|
)
|
2017-05-02 06:29:01 +00:00
|
|
|
|
2020-02-01 16:14:28 +00:00
|
|
|
_LOGGER.debug("Fetched version %s: %s", newest, release_notes)
|
2017-05-02 06:29:01 +00:00
|
|
|
|
2018-01-12 14:29:58 +00:00
|
|
|
# Skip on dev
|
2020-02-01 16:14:28 +00:00
|
|
|
if "dev" in current_version:
|
|
|
|
return Updater(False, "", "")
|
2017-05-02 06:29:01 +00:00
|
|
|
|
2020-01-05 12:09:17 +00:00
|
|
|
# Load data from supervisor on Hass.io
|
2018-01-12 14:29:58 +00:00
|
|
|
if hass.components.hassio.is_hassio():
|
|
|
|
newest = hass.components.hassio.get_homeassistant_version()
|
|
|
|
|
|
|
|
# Validate version
|
2019-08-07 22:28:22 +00:00
|
|
|
update_available = False
|
2017-08-10 17:31:28 +00:00
|
|
|
if StrictVersion(newest) > StrictVersion(current_version):
|
2020-02-01 16:14:28 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"The latest available version of Home Assistant is %s", newest
|
|
|
|
)
|
2019-08-07 22:28:22 +00:00
|
|
|
update_available = True
|
2017-08-10 17:31:28 +00:00
|
|
|
elif StrictVersion(newest) == StrictVersion(current_version):
|
2020-02-01 16:14:28 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"You are on the latest version (%s) of Home Assistant", newest
|
|
|
|
)
|
2019-08-07 22:28:22 +00:00
|
|
|
elif StrictVersion(newest) < StrictVersion(current_version):
|
|
|
|
_LOGGER.debug("Local version is newer than the latest version (%s)", newest)
|
|
|
|
|
2020-02-01 16:14:28 +00:00
|
|
|
_LOGGER.debug("Update available: %s", update_available)
|
|
|
|
|
|
|
|
return Updater(update_available, newest, release_notes)
|
2015-11-15 10:00:24 +00:00
|
|
|
|
2020-02-01 16:14:28 +00:00
|
|
|
coordinator = hass.data[DOMAIN] = update_coordinator.DataUpdateCoordinator(
|
2020-02-06 17:29:29 +00:00
|
|
|
hass,
|
|
|
|
_LOGGER,
|
|
|
|
name="Home Assistant update",
|
|
|
|
update_method=check_new_version,
|
|
|
|
update_interval=timedelta(days=1),
|
2020-02-01 16:14:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
await coordinator.async_refresh()
|
|
|
|
|
|
|
|
hass.async_create_task(
|
|
|
|
discovery.async_load_platform(hass, "binary_sensor", DOMAIN, {}, config)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2015-11-15 10:00:24 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2019-01-30 18:57:53 +00:00
|
|
|
async def get_newest_version(hass, huuid, include_components):
|
|
|
|
"""Get the newest Home Assistant version."""
|
|
|
|
if huuid:
|
2019-07-31 19:25:30 +00:00
|
|
|
info_object = await hass.helpers.system_info.async_get_system_info()
|
2019-01-30 18:57:53 +00:00
|
|
|
|
|
|
|
if include_components:
|
2019-07-31 19:25:30 +00:00
|
|
|
info_object["components"] = list(hass.config.components)
|
2019-01-30 18:57:53 +00:00
|
|
|
|
2019-10-12 05:19:53 +00:00
|
|
|
linux_dist = await hass.async_add_executor_job(linux_distribution, False)
|
2019-07-31 19:25:30 +00:00
|
|
|
info_object["distribution"] = linux_dist[0]
|
|
|
|
info_object["os_version"] = linux_dist[1]
|
2016-10-20 19:30:44 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
info_object["huuid"] = huuid
|
2017-05-02 06:29:01 +00:00
|
|
|
else:
|
2016-10-20 19:30:44 +00:00
|
|
|
info_object = {}
|
|
|
|
|
2017-05-02 06:29:01 +00:00
|
|
|
session = async_get_clientsession(hass)
|
2016-10-20 19:30:44 +00:00
|
|
|
try:
|
2019-05-23 04:09:59 +00:00
|
|
|
with async_timeout.timeout(5):
|
2018-04-28 23:26:20 +00:00
|
|
|
req = await session.post(UPDATER_URL, json=info_object)
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.info(
|
|
|
|
(
|
|
|
|
"Submitted analytics to Home Assistant servers. "
|
|
|
|
"Information submitted includes %s"
|
|
|
|
),
|
|
|
|
info_object,
|
|
|
|
)
|
2017-05-02 06:29:01 +00:00
|
|
|
except (asyncio.TimeoutError, aiohttp.ClientError):
|
2019-08-07 22:28:22 +00:00
|
|
|
_LOGGER.error("Could not contact Home Assistant Update to check for updates")
|
2020-02-01 16:14:28 +00:00
|
|
|
raise update_coordinator.UpdateFailed
|
2016-11-30 21:03:09 +00:00
|
|
|
|
2017-05-02 06:29:01 +00:00
|
|
|
try:
|
2018-04-28 23:26:20 +00:00
|
|
|
res = await req.json()
|
2015-11-15 22:30:42 +00:00
|
|
|
except ValueError:
|
2017-05-02 06:29:01 +00:00
|
|
|
_LOGGER.error("Received invalid JSON from Home Assistant Update")
|
2020-02-01 16:14:28 +00:00
|
|
|
raise update_coordinator.UpdateFailed
|
2016-11-30 21:03:09 +00:00
|
|
|
|
2017-05-02 06:29:01 +00:00
|
|
|
try:
|
|
|
|
res = RESPONSE_SCHEMA(res)
|
2019-07-31 19:25:30 +00:00
|
|
|
return res["version"], res["release-notes"]
|
2016-11-30 21:03:09 +00:00
|
|
|
except vol.Invalid:
|
2017-08-10 17:31:28 +00:00
|
|
|
_LOGGER.error("Got unexpected response: %s", res)
|
2020-02-01 16:14:28 +00:00
|
|
|
raise update_coordinator.UpdateFailed
|