From ca6fa1313e376c3d7b6d1890e85608522794ebfe Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 21 Oct 2016 23:30:40 -0700 Subject: [PATCH] Fix updater, add new fields (#3982) * Fix updater * Add Docker and virtualenv checks * Add log line informing user of update/analytics * Remove str --- homeassistant/components/updater.py | 10 +++++++++- tests/components/test_updater.py | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/updater.py b/homeassistant/components/updater.py index f1467f0e4cb..40899af9803 100644 --- a/homeassistant/components/updater.py +++ b/homeassistant/components/updater.py @@ -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') diff --git a/tests/components/test_updater.py b/tests/components/test_updater.py index 8333a721beb..7cc2ba8d962 100644 --- a/tests/components/test_updater.py +++ b/tests/components/test_updater.py @@ -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() == {}