mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +00:00
Make updater more robust (#4625)
This commit is contained in:
parent
b35fa4f1c1
commit
b1ef5042f9
@ -41,6 +41,11 @@ CONFIG_SCHEMA = vol.Schema({DOMAIN: {
|
|||||||
vol.Optional(CONF_REPORTING, default=True): cv.boolean
|
vol.Optional(CONF_REPORTING, default=True): cv.boolean
|
||||||
}}, extra=vol.ALLOW_EXTRA)
|
}}, extra=vol.ALLOW_EXTRA)
|
||||||
|
|
||||||
|
RESPONSE_SCHEMA = vol.Schema({
|
||||||
|
vol.Required('version'): str,
|
||||||
|
vol.Required('release-notes'): cv.url,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
def _create_uuid(hass, filename=UPDATER_UUID_FILE):
|
def _create_uuid(hass, filename=UPDATER_UUID_FILE):
|
||||||
"""Create UUID and save it in a file."""
|
"""Create UUID and save it in a file."""
|
||||||
@ -83,7 +88,12 @@ def setup(hass, config):
|
|||||||
|
|
||||||
def check_newest_version(hass, huuid):
|
def check_newest_version(hass, huuid):
|
||||||
"""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, releasenotes = get_newest_version(huuid)
|
result = get_newest_version(huuid)
|
||||||
|
|
||||||
|
if result is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
newest, releasenotes = result
|
||||||
|
|
||||||
if newest is None or 'dev' in CURRENT_VERSION:
|
if newest is None or 'dev' in CURRENT_VERSION:
|
||||||
return
|
return
|
||||||
@ -129,21 +139,24 @@ def get_newest_version(huuid):
|
|||||||
if not huuid:
|
if not huuid:
|
||||||
info_object = {}
|
info_object = {}
|
||||||
|
|
||||||
|
res = None
|
||||||
try:
|
try:
|
||||||
req = requests.post(UPDATER_URL, json=info_object, timeout=5)
|
req = requests.post(UPDATER_URL, json=info_object, timeout=5)
|
||||||
res = req.json()
|
res = req.json()
|
||||||
|
res = RESPONSE_SCHEMA(res)
|
||||||
|
|
||||||
_LOGGER.info(("Submitted analytics to Home Assistant servers. "
|
_LOGGER.info(("Submitted analytics to Home Assistant servers. "
|
||||||
"Information submitted includes %s"), info_object)
|
"Information submitted includes %s"), info_object)
|
||||||
return (res['version'], res['release-notes'])
|
return (res['version'], res['release-notes'])
|
||||||
except requests.RequestException:
|
except requests.RequestException:
|
||||||
_LOGGER.exception("Could not contact Home Assistant Update to check"
|
_LOGGER.error("Could not contact Home Assistant Update to check "
|
||||||
"for updates")
|
"for updates")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
_LOGGER.exception("Received invalid response from Home Assistant"
|
_LOGGER.error("Received invalid response from Home Assistant Update")
|
||||||
"Update")
|
|
||||||
return None
|
return None
|
||||||
except KeyError:
|
|
||||||
_LOGGER.exception("Response from Home Assistant Update did not"
|
except vol.Invalid:
|
||||||
"include version")
|
_LOGGER.error('Got unexpected response: %s', res)
|
||||||
return None
|
return None
|
||||||
|
@ -6,6 +6,7 @@ import os
|
|||||||
|
|
||||||
import requests
|
import requests
|
||||||
import requests_mock
|
import requests_mock
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.bootstrap import setup_component
|
from homeassistant.bootstrap import setup_component
|
||||||
from homeassistant.components import updater
|
from homeassistant.components import updater
|
||||||
@ -86,7 +87,7 @@ class TestUpdater(unittest.TestCase):
|
|||||||
mock_get.side_effect = ValueError
|
mock_get.side_effect = ValueError
|
||||||
self.assertIsNone(updater.get_newest_version(uuid))
|
self.assertIsNone(updater.get_newest_version(uuid))
|
||||||
|
|
||||||
mock_get.side_effect = KeyError
|
mock_get.side_effect = vol.Invalid('Expected dictionary')
|
||||||
self.assertIsNone(updater.get_newest_version(uuid))
|
self.assertIsNone(updater.get_newest_version(uuid))
|
||||||
|
|
||||||
def test_uuid_function(self):
|
def test_uuid_function(self):
|
||||||
@ -119,3 +120,13 @@ class TestUpdater(unittest.TestCase):
|
|||||||
|
|
||||||
assert len(history) == 1
|
assert len(history) == 1
|
||||||
assert history[0].json() == {}
|
assert history[0].json() == {}
|
||||||
|
|
||||||
|
@patch('homeassistant.components.updater.get_newest_version')
|
||||||
|
def test_error_during_fetch_works(
|
||||||
|
self, mock_get_newest_version):
|
||||||
|
"""Test if no entity is created if same version."""
|
||||||
|
mock_get_newest_version.return_value = None
|
||||||
|
|
||||||
|
updater.check_newest_version(self.hass, None)
|
||||||
|
|
||||||
|
self.assertIsNone(self.hass.states.get(updater.ENTITY_ID))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user