Implemented suggestions from Paulus for updater

1) Better Error handling when making PyPI requests.
2) More efficient event scheduling.
3) ENTITY_ID in constant
3) friendly_name from constant
pull/627/head
Ryan Kraus 2015-11-15 17:23:56 -05:00
parent 9b5385c565
commit 919c20a263
1 changed files with 19 additions and 13 deletions

View File

@ -10,13 +10,15 @@ import logging
import requests import requests
from homeassistant.const import __version__ as CURRENT_VERSION from homeassistant.const import __version__ as (
CURRENT_VERSION, ATTR_FRIENDLY_NAME)
from homeassistant.helpers import event from homeassistant.helpers import event
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
PYPI_URL = 'https://pypi.python.org/pypi/homeassistant/json' PYPI_URL = 'https://pypi.python.org/pypi/homeassistant/json'
DEPENDENCIES = [] DEPENDENCIES = []
DOMAIN = 'updater' DOMAIN = 'updater'
ENTITY_ID = 'updater.updater'
def setup(hass, config): def setup(hass, config):
@ -24,16 +26,25 @@ def setup(hass, config):
def check_newest_version(_=None): def check_newest_version(_=None):
''' check if a new version is available and report if one is ''' ''' check if a new version is available and report if one is '''
newest = get_newest_version()
try:
newest = get_newest_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
if newest != CURRENT_VERSION and newest is not None: if newest != CURRENT_VERSION and newest is not None:
hass.states.set( hass.states.set(
'{}.Update'.format(DOMAIN), newest, ENTITY_ID, newest, {'friendly_name': 'Update Available'})
{'friendly_name': 'Update Available'})
event.track_time_change(hass, check_newest_version, event.track_time_change(hass, check_newest_version,
hour=12, minute=0, second=0) hour=[0, 12], minute=0, second=0)
event.track_time_change(hass, check_newest_version,
hour=0, minute=0, second=0)
check_newest_version() check_newest_version()
@ -42,10 +53,5 @@ def setup(hass, config):
def get_newest_version(): def get_newest_version():
''' Get the newest HA version form PyPI ''' ''' Get the newest HA version form PyPI '''
try: req = requests.get(PYPI_URL)
req = requests.get(PYPI_URL)
except OSError:
_LOGGER.warning('Could not contact PyPI to check for updates')
return
return req.json()['info']['version'] return req.json()['info']['version']