Remove hour delay before checking for updates (#31368)

* Check for updates at startup

* Add 100% test coverage for update_coordinator

* Address comments
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2020-02-01 08:14:28 -08:00
1 parent b373c202c9
commit c67f53dc43
5 files changed
+223 -198

No files matched your search

+33 -123
View File
@@ -1,20 +1,15 @@
"""The tests for the Updater component."""
import asyncio
from datetime import timedelta
from unittest.mock import Mock, patch
from unittest.mock import Mock
from asynctest import patch
import pytest
from homeassistant.components import updater
from homeassistant.helpers.update_coordinator import UpdateFailed
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
from tests.common import (
MockDependency,
async_fire_time_changed,
mock_component,
mock_coro,
)
from tests.common import MockDependency, mock_component, mock_coro
NEW_VERSION = "10000.0"
MOCK_VERSION = "10.0"
@@ -32,95 +27,39 @@ def mock_distro():
yield
@pytest.fixture(autouse=True)
def mock_version():
"""Mock current version."""
with patch("homeassistant.components.updater.current_version", MOCK_VERSION):
yield
@pytest.fixture(name="mock_get_newest_version")
def mock_get_newest_version_fixture():
"""Fixture to mock get_newest_version."""
with patch("homeassistant.components.updater.get_newest_version") as mock:
with patch(
"homeassistant.components.updater.get_newest_version",
return_value=(NEW_VERSION, RELEASE_NOTES),
) as mock:
yield mock
@pytest.fixture(name="mock_get_uuid")
@pytest.fixture(name="mock_get_uuid", autouse=True)
def mock_get_uuid_fixture():
"""Fixture to mock get_uuid."""
with patch("homeassistant.components.updater._load_uuid") as mock:
yield mock
@pytest.fixture(name="mock_utcnow")
def mock_utcnow_fixture():
"""Fixture to mock utcnow."""
with patch("homeassistant.components.updater.dt_util") as mock:
yield mock.utcnow
async def test_new_version_shows_entity_startup(
hass, mock_get_uuid, mock_get_newest_version
):
"""Test if binary sensor is unavailable at first."""
mock_get_uuid.return_value = MOCK_HUUID
mock_get_newest_version.return_value = mock_coro((NEW_VERSION, RELEASE_NOTES))
res = await async_setup_component(hass, updater.DOMAIN, {updater.DOMAIN: {}})
assert res, "Updater failed to set up"
await hass.async_block_till_done()
assert hass.states.is_state("binary_sensor.updater", "unavailable")
assert "newest_version" not in hass.states.get("binary_sensor.updater").attributes
assert "release_notes" not in hass.states.get("binary_sensor.updater").attributes
async def test_rename_entity(hass, mock_get_uuid, mock_get_newest_version, mock_utcnow):
"""Test if renaming the binary sensor works correctly."""
mock_get_uuid.return_value = MOCK_HUUID
mock_get_newest_version.return_value = mock_coro((NEW_VERSION, RELEASE_NOTES))
now = dt_util.utcnow()
later = now + timedelta(hours=1)
mock_utcnow.return_value = now
res = await async_setup_component(hass, updater.DOMAIN, {updater.DOMAIN: {}})
assert res, "Updater failed to set up"
await hass.async_block_till_done()
assert hass.states.is_state("binary_sensor.updater", "unavailable")
assert hass.states.get("binary_sensor.new_entity_id") is None
entity_registry = await hass.helpers.entity_registry.async_get_registry()
entity_registry.async_update_entity(
"binary_sensor.updater", new_entity_id="binary_sensor.new_entity_id"
)
await hass.async_block_till_done()
assert hass.states.is_state("binary_sensor.new_entity_id", "unavailable")
assert hass.states.get("binary_sensor.updater") is None
with patch("homeassistant.components.updater.current_version", MOCK_VERSION):
async_fire_time_changed(hass, later)
await hass.async_block_till_done()
assert hass.states.is_state("binary_sensor.new_entity_id", "on")
assert hass.states.get("binary_sensor.updater") is None
async def test_new_version_shows_entity_true(
hass, mock_get_uuid, mock_get_newest_version, mock_utcnow
hass, mock_get_uuid, mock_get_newest_version
):
"""Test if sensor is true if new version is available."""
mock_get_uuid.return_value = MOCK_HUUID
mock_get_newest_version.return_value = mock_coro((NEW_VERSION, RELEASE_NOTES))
now = dt_util.utcnow()
later = now + timedelta(hours=1)
mock_utcnow.return_value = now
res = await async_setup_component(hass, updater.DOMAIN, {updater.DOMAIN: {}})
assert res, "Updater failed to set up"
assert await async_setup_component(hass, updater.DOMAIN, {updater.DOMAIN: {}})
await hass.async_block_till_done()
with patch("homeassistant.components.updater.current_version", MOCK_VERSION):
async_fire_time_changed(hass, later)
await hass.async_block_till_done()
assert hass.states.is_state("binary_sensor.updater", "on")
assert (
hass.states.get("binary_sensor.updater").attributes["newest_version"]
@@ -133,23 +72,15 @@ async def test_new_version_shows_entity_true(
async def test_same_version_shows_entity_false(
hass, mock_get_uuid, mock_get_newest_version, mock_utcnow
hass, mock_get_uuid, mock_get_newest_version
):
"""Test if sensor is false if no new version is available."""
mock_get_uuid.return_value = MOCK_HUUID
mock_get_newest_version.return_value = mock_coro((MOCK_VERSION, ""))
now = dt_util.utcnow()
later = now + timedelta(hours=1)
mock_utcnow.return_value = now
res = await async_setup_component(hass, updater.DOMAIN, {updater.DOMAIN: {}})
assert res, "Updater failed to set up"
assert await async_setup_component(hass, updater.DOMAIN, {updater.DOMAIN: {}})
await hass.async_block_till_done()
with patch("homeassistant.components.updater.current_version", MOCK_VERSION):
async_fire_time_changed(hass, later)
await hass.async_block_till_done()
assert hass.states.is_state("binary_sensor.updater", "off")
assert (
@@ -159,29 +90,18 @@ async def test_same_version_shows_entity_false(
assert "release_notes" not in hass.states.get("binary_sensor.updater").attributes
async def test_disable_reporting(
hass, mock_get_uuid, mock_get_newest_version, mock_utcnow
):
async def test_disable_reporting(hass, mock_get_uuid, mock_get_newest_version):
"""Test we do not gather analytics when disable reporting is active."""
mock_get_uuid.return_value = MOCK_HUUID
mock_get_newest_version.return_value = mock_coro((MOCK_VERSION, ""))
now = dt_util.utcnow()
later = now + timedelta(hours=1)
mock_utcnow.return_value = now
res = await async_setup_component(
assert await async_setup_component(
hass, updater.DOMAIN, {updater.DOMAIN: {"reporting": False}}
)
assert res, "Updater failed to set up"
await hass.async_block_till_done()
with patch("homeassistant.components.updater.current_version", MOCK_VERSION):
async_fire_time_changed(hass, later)
await hass.async_block_till_done()
assert hass.states.is_state("binary_sensor.updater", "off")
res = await updater.get_newest_version(hass, MOCK_HUUID, MOCK_CONFIG)
await updater.get_newest_version(hass, MOCK_HUUID, MOCK_CONFIG)
call = mock_get_newest_version.mock_calls[0][1]
assert call[0] is hass
assert call[1] is None
@@ -215,9 +135,10 @@ async def test_error_fetching_new_version_timeout(hass):
with patch(
"homeassistant.helpers.system_info.async_get_system_info",
Mock(return_value=mock_coro({"fake": "bla"})),
), patch("async_timeout.timeout", side_effect=asyncio.TimeoutError):
res = await updater.get_newest_version(hass, MOCK_HUUID, False)
assert res is None
), patch("async_timeout.timeout", side_effect=asyncio.TimeoutError), pytest.raises(
UpdateFailed
):
await updater.get_newest_version(hass, MOCK_HUUID, False)
async def test_error_fetching_new_version_bad_json(hass, aioclient_mock):
@@ -227,9 +148,8 @@ async def test_error_fetching_new_version_bad_json(hass, aioclient_mock):
with patch(
"homeassistant.helpers.system_info.async_get_system_info",
Mock(return_value=mock_coro({"fake": "bla"})),
):
res = await updater.get_newest_version(hass, MOCK_HUUID, False)
assert res is None
), pytest.raises(UpdateFailed):
await updater.get_newest_version(hass, MOCK_HUUID, False)
async def test_error_fetching_new_version_invalid_response(hass, aioclient_mock):
@@ -245,31 +165,21 @@ async def test_error_fetching_new_version_invalid_response(hass, aioclient_mock)
with patch(
"homeassistant.helpers.system_info.async_get_system_info",
Mock(return_value=mock_coro({"fake": "bla"})),
):
res = await updater.get_newest_version(hass, MOCK_HUUID, False)
assert res is None
), pytest.raises(UpdateFailed):
await updater.get_newest_version(hass, MOCK_HUUID, False)
async def test_new_version_shows_entity_after_hour_hassio(
hass, mock_get_uuid, mock_get_newest_version, mock_utcnow
hass, mock_get_uuid, mock_get_newest_version
):
"""Test if binary sensor gets updated if new version is available / Hass.io."""
mock_get_uuid.return_value = MOCK_HUUID
mock_get_newest_version.return_value = mock_coro((NEW_VERSION, RELEASE_NOTES))
mock_component(hass, "hassio")
hass.data["hassio_hass_version"] = "999.0"
now = dt_util.utcnow()
later = now + timedelta(hours=1)
mock_utcnow.return_value = now
res = await async_setup_component(hass, updater.DOMAIN, {updater.DOMAIN: {}})
assert res, "Updater failed to set up"
assert await async_setup_component(hass, updater.DOMAIN, {updater.DOMAIN: {}})
await hass.async_block_till_done()
with patch("homeassistant.components.updater.current_version", MOCK_VERSION):
async_fire_time_changed(hass, later)
await hass.async_block_till_done()
assert hass.states.is_state("binary_sensor.updater", "on")
assert (