From 7ae802bb0db1169cb8a0264585c95cf70f7693ea Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 1 Apr 2020 15:25:04 -0700 Subject: [PATCH] Cloud do checks during setup (#33507) * Update cloud to do more tasks during async_setup * Upgrade hass_nabucasa to 0.33 --- homeassistant/components/cloud/__init__.py | 21 +++--------- homeassistant/components/cloud/manifest.json | 2 +- homeassistant/package_constraints.txt | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/cloud/test_binary_sensor.py | 34 ++++++++++---------- tests/components/cloud/test_init.py | 8 +---- 7 files changed, 27 insertions(+), 44 deletions(-) diff --git a/homeassistant/components/cloud/__init__.py b/homeassistant/components/cloud/__init__.py index 0a0b9f0fe88..a40529b6fe4 100644 --- a/homeassistant/components/cloud/__init__.py +++ b/homeassistant/components/cloud/__init__.py @@ -10,7 +10,6 @@ from homeassistant.const import ( CONF_MODE, CONF_NAME, CONF_REGION, - EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, ) from homeassistant.core import callback @@ -191,11 +190,7 @@ async def async_setup(hass, config): client = CloudClient(hass, prefs, websession, alexa_conf, google_conf) cloud = hass.data[DOMAIN] = Cloud(client, **kwargs) - async def _startup(event): - """Startup event.""" - await cloud.start() - - hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _startup) + await cloud.start() async def _shutdown(event): """Shutdown event.""" @@ -230,17 +225,11 @@ async def async_setup(hass, config): return loaded = True - hass.async_create_task( - hass.helpers.discovery.async_load_platform( - "binary_sensor", DOMAIN, {}, config - ) - ) - hass.async_create_task( - hass.helpers.discovery.async_load_platform("stt", DOMAIN, {}, config) - ) - hass.async_create_task( - hass.helpers.discovery.async_load_platform("tts", DOMAIN, {}, config) + await hass.helpers.discovery.async_load_platform( + "binary_sensor", DOMAIN, {}, config ) + await hass.helpers.discovery.async_load_platform("stt", DOMAIN, {}, config) + await hass.helpers.discovery.async_load_platform("tts", DOMAIN, {}, config) cloud.iot.register_on_connect(_on_connect) diff --git a/homeassistant/components/cloud/manifest.json b/homeassistant/components/cloud/manifest.json index cfbb221c164..b8c2bc277f0 100644 --- a/homeassistant/components/cloud/manifest.json +++ b/homeassistant/components/cloud/manifest.json @@ -2,7 +2,7 @@ "domain": "cloud", "name": "Home Assistant Cloud", "documentation": "https://www.home-assistant.io/integrations/cloud", - "requirements": ["hass-nabucasa==0.32.2"], + "requirements": ["hass-nabucasa==0.33.0"], "dependencies": ["http", "webhook", "alexa"], "after_dependencies": ["google_assistant"], "codeowners": ["@home-assistant/cloud"] diff --git a/homeassistant/package_constraints.txt b/homeassistant/package_constraints.txt index ca19feba5ef..f450fb6283c 100644 --- a/homeassistant/package_constraints.txt +++ b/homeassistant/package_constraints.txt @@ -11,7 +11,7 @@ ciso8601==2.1.3 cryptography==2.8 defusedxml==0.6.0 distro==1.4.0 -hass-nabucasa==0.32.2 +hass-nabucasa==0.33.0 home-assistant-frontend==20200401.0 importlib-metadata==1.5.0 jinja2>=2.11.1 diff --git a/requirements_all.txt b/requirements_all.txt index 0cbc59f76c4..3239eef2b2e 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -674,7 +674,7 @@ habitipy==0.2.0 hangups==0.4.9 # homeassistant.components.cloud -hass-nabucasa==0.32.2 +hass-nabucasa==0.33.0 # homeassistant.components.mqtt hbmqtt==0.9.5 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index a5d9daf91d9..e4419872e5a 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -264,7 +264,7 @@ ha-ffmpeg==2.0 hangups==0.4.9 # homeassistant.components.cloud -hass-nabucasa==0.32.2 +hass-nabucasa==0.33.0 # homeassistant.components.mqtt hbmqtt==0.9.5 diff --git a/tests/components/cloud/test_binary_sensor.py b/tests/components/cloud/test_binary_sensor.py index 24b0563890b..c4ad22abb2f 100644 --- a/tests/components/cloud/test_binary_sensor.py +++ b/tests/components/cloud/test_binary_sensor.py @@ -1,24 +1,23 @@ """Tests for the cloud binary sensor.""" from unittest.mock import Mock +from asynctest import patch + from homeassistant.components.cloud.const import DISPATCHER_REMOTE_UPDATE from homeassistant.setup import async_setup_component async def test_remote_connection_sensor(hass): """Test the remote connection sensor.""" - from homeassistant.components.cloud import binary_sensor as bin_sensor - - bin_sensor.WAIT_UNTIL_CHANGE = 0 - assert await async_setup_component(hass, "cloud", {"cloud": {}}) await hass.async_block_till_done() assert hass.states.get("binary_sensor.remote_ui") is None # Fake connection/discovery - org_cloud = hass.data["cloud"] - await org_cloud.iot._on_connect[-1]() + await hass.helpers.discovery.async_load_platform( + "binary_sensor", "cloud", {}, {"cloud": {}} + ) # Mock test env cloud = hass.data["cloud"] = Mock() @@ -29,17 +28,18 @@ async def test_remote_connection_sensor(hass): assert state is not None assert state.state == "unavailable" - cloud.remote.is_connected = False - cloud.remote.certificate = object() - hass.helpers.dispatcher.async_dispatcher_send(DISPATCHER_REMOTE_UPDATE, {}) - await hass.async_block_till_done() + with patch("homeassistant.components.cloud.binary_sensor.WAIT_UNTIL_CHANGE", 0): + cloud.remote.is_connected = False + cloud.remote.certificate = object() + hass.helpers.dispatcher.async_dispatcher_send(DISPATCHER_REMOTE_UPDATE, {}) + await hass.async_block_till_done() - state = hass.states.get("binary_sensor.remote_ui") - assert state.state == "off" + state = hass.states.get("binary_sensor.remote_ui") + assert state.state == "off" - cloud.remote.is_connected = True - hass.helpers.dispatcher.async_dispatcher_send(DISPATCHER_REMOTE_UPDATE, {}) - await hass.async_block_till_done() + cloud.remote.is_connected = True + hass.helpers.dispatcher.async_dispatcher_send(DISPATCHER_REMOTE_UPDATE, {}) + await hass.async_block_till_done() - state = hass.states.get("binary_sensor.remote_ui") - assert state.state == "on" + state = hass.states.get("binary_sensor.remote_ui") + assert state.state == "on" diff --git a/tests/components/cloud/test_init.py b/tests/components/cloud/test_init.py index 9dd8695b9b2..10a7bc38c05 100644 --- a/tests/components/cloud/test_init.py +++ b/tests/components/cloud/test_init.py @@ -6,7 +6,7 @@ import pytest from homeassistant.components import cloud from homeassistant.components.cloud.const import DOMAIN from homeassistant.components.cloud.prefs import STORAGE_KEY -from homeassistant.const import EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP +from homeassistant.const import EVENT_HOMEASSISTANT_STOP from homeassistant.core import Context from homeassistant.exceptions import Unauthorized from homeassistant.setup import async_setup_component @@ -103,12 +103,6 @@ async def test_remote_services(hass, mock_cloud_fixture, hass_read_only_user): async def test_startup_shutdown_events(hass, mock_cloud_fixture): """Test if the cloud will start on startup event.""" - with patch("hass_nabucasa.Cloud.start", return_value=mock_coro()) as mock_start: - hass.bus.async_fire(EVENT_HOMEASSISTANT_START) - await hass.async_block_till_done() - - assert mock_start.called - with patch("hass_nabucasa.Cloud.stop", return_value=mock_coro()) as mock_stop: hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP) await hass.async_block_till_done()