Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions homeassistant/components/hassio/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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

Expand Down Expand Up @@ -40,8 +40,11 @@ async def async_discovery_start_handler(event):
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)

Expand Down
51 changes: 51 additions & 0 deletions tests/components/hassio/test_discovery.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -44,6 +46,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())), \
Comment thread
pvizeli marked this conversation as resolved.
patch('homeassistant.components.mqtt.'
Comment thread
pvizeli marked this conversation as resolved.
'config_flow.FlowHandler.async_step_hassio',
Comment thread
pvizeli marked this conversation as resolved.
Mock(return_value=mock_coro({"type": "abort"}))
Comment thread
pvizeli marked this conversation as resolved.
) as mock_mqtt:
Comment thread
pvizeli marked this conversation as resolved.
Outdated
await hass.async_start()
await async_setup_component(hass, 'hassio', {
Comment thread
pvizeli marked this conversation as resolved.
'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(
Expand Down