2015-11-15 10:00:24 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.sensor.updater
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Sensor that checks for available updates.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
at https://home-assistant.io/components/sensor.updater/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2015-11-15 22:30:42 +00:00
|
|
|
from homeassistant.const import __version__ as CURRENT_VERSION
|
|
|
|
from homeassistant.const import ATTR_FRIENDLY_NAME
|
2015-11-15 10:00:24 +00:00
|
|
|
from homeassistant.helpers import event
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
PYPI_URL = 'https://pypi.python.org/pypi/homeassistant/json'
|
|
|
|
DEPENDENCIES = []
|
|
|
|
DOMAIN = 'updater'
|
2015-11-15 22:23:56 +00:00
|
|
|
ENTITY_ID = 'updater.updater'
|
2015-11-15 10:00:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
|
|
|
''' setup the updater component '''
|
|
|
|
|
|
|
|
def check_newest_version(_=None):
|
|
|
|
''' check if a new version is available and report if one is '''
|
2015-11-15 22:30:42 +00:00
|
|
|
newest = get_newest_version()
|
2015-11-15 22:23:56 +00:00
|
|
|
|
2015-11-15 10:00:24 +00:00
|
|
|
if newest != CURRENT_VERSION and newest is not None:
|
|
|
|
hass.states.set(
|
2015-11-15 22:32:05 +00:00
|
|
|
ENTITY_ID, newest, {'ATTR_FRIENDLY_NAME': 'Update Available'})
|
2015-11-15 10:00:24 +00:00
|
|
|
|
|
|
|
event.track_time_change(hass, check_newest_version,
|
2015-11-15 22:23:56 +00:00
|
|
|
hour=[0, 12], minute=0, second=0)
|
2015-11-15 10:00:24 +00:00
|
|
|
|
|
|
|
check_newest_version()
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def get_newest_version():
|
|
|
|
''' Get the newest HA version form PyPI '''
|
2015-11-15 22:30:42 +00:00
|
|
|
try:
|
|
|
|
req = requests.get(PYPI_URL)
|
|
|
|
|
|
|
|
return req.json()['info']['version']
|
|
|
|
except requests.RequestException:
|
|
|
|
_LOGGER.exception('Could not contact PyPI to check for updates')
|
|
|
|
return
|
|
|
|
except ValueError:
|
|
|
|
_LOGGER.exception('Received invalid response from PyPI')
|
|
|
|
return
|
|
|
|
except KeyError:
|
|
|
|
_LOGGER.exception('Response from PyPI did not include version')
|
|
|
|
return
|