Skip to content

Commit

Permalink
Version sensor update (home-assistant#25162)
Browse files Browse the repository at this point in the history
* component -> integration

* Bump pyhaversion to 3.0.2

* Update requirements

* Formating
  • Loading branch information
ludeeus authored and MartinHjelmare committed Jul 15, 2019
1 parent 99c6c60 commit 50f9117
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/version/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"""The version component."""
"""The version integration."""
2 changes: 1 addition & 1 deletion homeassistant/components/version/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
44 changes: 26 additions & 18 deletions homeassistant/components/version/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,46 @@
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)


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
Expand All @@ -77,11 +97,7 @@ async def async_update(self):
@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):
Expand All @@ -102,19 +118,11 @@ def icon(self):
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()
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 50f9117

Please sign in to comment.