Fix updater, add new fields (#3982)

* Fix updater

* Add Docker and virtualenv checks

* Add log line informing user of update/analytics

* Remove str
pull/3987/head
Paulus Schoutsen 2016-10-21 23:30:40 -07:00 committed by GitHub
parent ea91d24eb2
commit ca6fa1313e
2 changed files with 26 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import logging
import json
import platform
import uuid
import os
# pylint: disable=no-name-in-module,import-error
from distutils.version import StrictVersion
@ -63,6 +64,7 @@ def setup(hass, config):
_LOGGER.warning('Updater not supported in development version')
return False
config = config.get(DOMAIN, {})
huuid = _load_uuid(hass) if config.get(CONF_REPORTING) else None
# Update daily, start 1 hour after startup
@ -91,7 +93,9 @@ def get_newest_version(huuid):
info_object = {'uuid': huuid, 'version': CURRENT_VERSION,
'timezone': dt_util.DEFAULT_TIME_ZONE.zone,
'os_name': platform.system(), "arch": platform.machine(),
'python_version': platform.python_version()}
'python_version': platform.python_version(),
'virtualenv': (os.environ.get('VIRTUAL_ENV') is not None),
'docker': False}
if platform.system() == 'Windows':
info_object['os_version'] = platform.win32_ver()[0]
@ -102,6 +106,7 @@ def get_newest_version(huuid):
linux_dist = distro.linux_distribution(full_distribution_name=False)
info_object['distribution'] = linux_dist[0]
info_object['os_version'] = linux_dist[1]
info_object['docker'] = os.path.isfile('/.dockerenv')
if not huuid:
info_object = {}
@ -109,6 +114,9 @@ def get_newest_version(huuid):
try:
req = requests.post(UPDATER_URL, json=info_object)
res = req.json()
_LOGGER.info(('The latest version is %s. '
'Information submitted includes %s'),
res['version'], info_object)
return (res['version'], res['release-notes'])
except requests.RequestException:
_LOGGER.exception('Could not contact HASS Update to check for updates')

View File

@ -5,6 +5,7 @@ from unittest.mock import patch
import os
import requests
import requests_mock
from homeassistant.bootstrap import setup_component
from homeassistant.components import updater
@ -111,3 +112,19 @@ class TestUpdater(unittest.TestCase):
assert uuid != uuid2
finally:
os.remove(path)
@requests_mock.Mocker()
def test_reporting_false_works(self, m):
"""Test we do not send any data."""
m.post(updater.UPDATER_URL,
json={'version': '0.15',
'release-notes': 'https://home-assistant.io'})
response = updater.get_newest_version(None)
assert response == ('0.15', 'https://home-assistant.io')
history = m.request_history
assert len(history) == 1
assert history[0].json() == {}