From 50f911798263aea3a18c492879809b55e905a162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Mon, 15 Jul 2019 19:38:21 +0200 Subject: [PATCH] Version sensor update (#25162) * component -> integration * Bump pyhaversion to 3.0.2 * Update requirements * Formating --- homeassistant/components/version/__init__.py | 2 +- .../components/version/manifest.json | 2 +- homeassistant/components/version/sensor.py | 44 +++++++++++-------- requirements_all.txt | 2 +- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/version/__init__.py b/homeassistant/components/version/__init__.py index eb257007f7c..64b04fd7d71 100644 --- a/homeassistant/components/version/__init__.py +++ b/homeassistant/components/version/__init__.py @@ -1 +1 @@ -"""The version component.""" +"""The version integration.""" diff --git a/homeassistant/components/version/manifest.json b/homeassistant/components/version/manifest.json index 16d11e913f7..2a48f91a6f8 100644 --- a/homeassistant/components/version/manifest.json +++ b/homeassistant/components/version/manifest.json @@ -3,7 +3,7 @@ "name": "Version", "documentation": "https://www.home-assistant.io/components/version", "requirements": [ - "pyhaversion==2.2.1" + "pyhaversion==3.0.2" ], "dependencies": [], "codeowners": [ diff --git a/homeassistant/components/version/sensor.py b/homeassistant/components/version/sensor.py index 6aed6da17f7..1cdf67a480e 100644 --- a/homeassistant/components/version/sensor.py +++ b/homeassistant/components/version/sensor.py @@ -45,18 +45,38 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ async def async_setup_platform( hass, config, async_add_entities, discovery_info=None): """Set up the Version sensor platform.""" - from pyhaversion import Version + from pyhaversion import ( + LocalVersion, DockerVersion, HassioVersion, PyPiVersion) beta = config.get(CONF_BETA) image = config.get(CONF_IMAGE) name = config.get(CONF_NAME) source = config.get(CONF_SOURCE) session = async_get_clientsession(hass) + if beta: branch = 'beta' else: branch = 'stable' - haversion = VersionData(Version(hass.loop, session, branch, image), source) + + if source == 'pypi': + haversion = VersionData( + PyPiVersion(hass.loop, session, branch)) + elif source == 'hassio': + haversion = VersionData( + HassioVersion(hass.loop, session, branch, image)) + elif source == 'docker': + haversion = VersionData( + DockerVersion(hass.loop, session, branch, image)) + else: + haversion = VersionData( + LocalVersion(hass.loop, session)) + + if not name: + if source == DEFAULT_SOURCE: + name = DEFAULT_NAME_LOCAL + else: + name = DEFAULT_NAME_LATEST async_add_entities([VersionSensor(haversion, name)], True) @@ -64,7 +84,7 @@ async def async_setup_platform( class VersionSensor(Entity): """Representation of a Home Assistant version sensor.""" - def __init__(self, haversion, name=''): + def __init__(self, haversion, name): """Initialize the Version sensor.""" self.haversion = haversion self._name = name @@ -77,11 +97,7 @@ class VersionSensor(Entity): @property def name(self): """Return the name of the sensor.""" - if self._name: - return self._name - if self.haversion.source == DEFAULT_SOURCE: - return DEFAULT_NAME_LOCAL - return DEFAULT_NAME_LATEST + return self._name @property def state(self): @@ -102,19 +118,11 @@ class VersionSensor(Entity): class VersionData: """Get the latest data and update the states.""" - def __init__(self, api, source): + def __init__(self, api): """Initialize the data object.""" self.api = api - self.source = source @Throttle(TIME_BETWEEN_UPDATES) async def async_update(self): """Get the latest version information.""" - if self.source == 'pypi': - await self.api.get_pypi_version() - elif self.source == 'hassio': - await self.api.get_hassio_version() - elif self.source == 'docker': - await self.api.get_docker_version() - else: - await self.api.get_local_version() + await self.api.get_version() diff --git a/requirements_all.txt b/requirements_all.txt index 9bd4974a6ab..73441f50149 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1160,7 +1160,7 @@ pygtfs==0.1.5 pygtt==1.1.2 # homeassistant.components.version -pyhaversion==2.2.1 +pyhaversion==3.0.2 # homeassistant.components.heos pyheos==0.5.2