core/homeassistant/components/updater.py

54 lines
1.5 KiB
Python
Raw Normal View History

2015-11-15 10:00:24 +00:00
"""
2016-03-07 17:49:31 +00:00
Support to check for available updates.
2015-11-15 10:00:24 +00:00
For more details about this platform, please refer to the documentation at
2015-11-16 06:53:31 +00:00
at https://home-assistant.io/components/updater/
2015-11-15 10:00:24 +00:00
"""
import logging
import requests
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'
DOMAIN = 'updater'
ENTITY_ID = 'updater.updater'
2015-11-15 10:00:24 +00:00
def setup(hass, config):
2016-03-07 17:49:31 +00:00
"""Setup the updater component."""
2015-11-15 10:00:24 +00:00
def check_newest_version(_=None):
2016-03-07 17:49:31 +00:00
"""Check if a new version is available and report if one is."""
newest = get_newest_version()
2015-11-15 10:00:24 +00:00
if newest != CURRENT_VERSION and newest is not None:
hass.states.set(
ENTITY_ID, newest, {ATTR_FRIENDLY_NAME: 'Update Available'})
2015-11-15 10:00:24 +00:00
event.track_time_change(hass, check_newest_version,
hour=[0, 12], minute=0, second=0)
2015-11-15 10:00:24 +00:00
check_newest_version()
return True
def get_newest_version():
2016-03-07 17:49:31 +00:00
"""Get the newest Home Assistant version from PyPI."""
try:
req = requests.get(PYPI_URL)
return req.json()['info']['version']
except requests.RequestException:
_LOGGER.exception('Could not contact PyPI to check for updates')
2015-11-29 21:49:05 +00:00
return None
except ValueError:
_LOGGER.exception('Received invalid response from PyPI')
2015-11-29 21:49:05 +00:00
return None
except KeyError:
_LOGGER.exception('Response from PyPI did not include version')
2015-11-29 21:49:05 +00:00
return None