diff --git a/homeassistant/components/hassio/discovery.py b/homeassistant/components/hassio/discovery.py index ba5c8c3f789..3c5242607c1 100644 --- a/homeassistant/components/hassio/discovery.py +++ b/homeassistant/components/hassio/discovery.py @@ -5,7 +5,7 @@ import logging from aiohttp import web from aiohttp.web_exceptions import HTTPServiceUnavailable -from homeassistant.core import callback +from homeassistant.core import callback, CoreState from homeassistant.const import EVENT_HOMEASSISTANT_START from homeassistant.components.http import HomeAssistantView @@ -40,8 +40,11 @@ def async_setup_discovery(hass, hassio, config): if jobs: await asyncio.wait(jobs) - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_START, async_discovery_start_handler) + if hass.state == CoreState.running: + hass.async_create_task(async_discovery_start_handler(None)) + else: + hass.bus.async_listen_once( + EVENT_HOMEASSISTANT_START, async_discovery_start_handler) hass.http.register_view(hassio_discovery) diff --git a/tests/components/hassio/conftest.py b/tests/components/hassio/conftest.py index fb3a172a45c..f9ad1c578de 100644 --- a/tests/components/hassio/conftest.py +++ b/tests/components/hassio/conftest.py @@ -4,6 +4,7 @@ from unittest.mock import patch, Mock import pytest +from homeassistant.core import CoreState from homeassistant.setup import async_setup_component from homeassistant.components.hassio.handler import HassIO, HassioAPIError @@ -33,6 +34,7 @@ def hassio_client(hassio_env, hass, aiohttp_client): patch('homeassistant.components.hassio.HassIO.' 'get_homeassistant_info', Mock(side_effect=HassioAPIError())): + hass.state = CoreState.starting hass.loop.run_until_complete(async_setup_component(hass, 'hassio', { 'http': { 'api_password': API_PASSWORD diff --git a/tests/components/hassio/test_discovery.py b/tests/components/hassio/test_discovery.py index 98d0835c102..c8926a1cd18 100644 --- a/tests/components/hassio/test_discovery.py +++ b/tests/components/hassio/test_discovery.py @@ -1,6 +1,8 @@ """Test config flow.""" from unittest.mock import patch, Mock +from homeassistant.setup import async_setup_component +from homeassistant.components.hassio.handler import HassioAPIError from homeassistant.const import EVENT_HOMEASSISTANT_START, HTTP_HEADER_HA_AUTH from tests.common import mock_coro @@ -29,6 +31,8 @@ async def test_hassio_discovery_startup(hass, aioclient_mock, hassio_client): 'result': 'ok', 'data': {'name': "Mosquitto Test"} }) + assert aioclient_mock.call_count == 0 + with patch('homeassistant.components.mqtt.' 'config_flow.FlowHandler.async_step_hassio', Mock(return_value=mock_coro({"type": "abort"}))) as mock_mqtt: @@ -44,6 +48,55 @@ async def test_hassio_discovery_startup(hass, aioclient_mock, hassio_client): }) +async def test_hassio_discovery_startup_done(hass, aioclient_mock, + hassio_client): + """Test startup and discovery with hass discovery.""" + aioclient_mock.get( + "http://127.0.0.1/discovery", json={ + 'result': 'ok', 'data': {'discovery': [ + { + "service": "mqtt", "uuid": "test", + "addon": "mosquitto", "config": + { + 'broker': 'mock-broker', + 'port': 1883, + 'username': 'mock-user', + 'password': 'mock-pass', + 'protocol': '3.1.1' + } + } + ]}}) + aioclient_mock.get( + "http://127.0.0.1/addons/mosquitto/info", json={ + 'result': 'ok', 'data': {'name': "Mosquitto Test"} + }) + + with patch('homeassistant.components.hassio.HassIO.update_hass_api', + Mock(return_value=mock_coro({"result": "ok"}))), \ + patch('homeassistant.components.hassio.HassIO.' + 'get_homeassistant_info', + Mock(side_effect=HassioAPIError())), \ + patch('homeassistant.components.mqtt.' + 'config_flow.FlowHandler.async_step_hassio', + Mock(return_value=mock_coro({"type": "abort"})) + ) as mock_mqtt: + await hass.async_start() + await async_setup_component(hass, 'hassio', { + 'http': { + 'api_password': API_PASSWORD + } + }) + await hass.async_block_till_done() + + assert aioclient_mock.call_count == 2 + assert mock_mqtt.called + mock_mqtt.assert_called_with({ + 'broker': 'mock-broker', 'port': 1883, 'username': 'mock-user', + 'password': 'mock-pass', 'protocol': '3.1.1', + 'addon': 'Mosquitto Test', + }) + + async def test_hassio_discovery_webhook(hass, aioclient_mock, hassio_client): """Test discovery webhook.""" aioclient_mock.get(