Skip to content
Snippets Groups Projects
Unverified Commit 853fab0a authored by Erik Montnemery's avatar Erik Montnemery Committed by GitHub
Browse files

Mock MQTT setup in hassio tests (#77245)

* Mock MQTT setup in hassio tests

* Tweak
parent c26d6879
No related branches found
No related tags found
No related merge requests found
"""Test config flow."""
from http import HTTPStatus
from unittest.mock import Mock, patch
from unittest.mock import AsyncMock, Mock, patch
import pytest
from homeassistant import config_entries
from homeassistant.components.hassio import HassioServiceInfo
from homeassistant.components.hassio.handler import HassioAPIError
from homeassistant.components.mqtt import DOMAIN as MQTT_DOMAIN
from homeassistant.const import EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STARTED
from homeassistant.setup import async_setup_component
from tests.common import MockModule, mock_entity_platform, mock_integration
@pytest.fixture
async def mock_mqtt(hass):
"""Mock the MQTT integration's config flow."""
mock_integration(hass, MockModule(MQTT_DOMAIN))
mock_entity_platform(hass, f"config_flow.{MQTT_DOMAIN}", None)
with patch.dict(config_entries.HANDLERS):
class MqttFlow(config_entries.ConfigFlow, domain=MQTT_DOMAIN):
"""Test flow."""
VERSION = 1
async def test_hassio_discovery_startup(hass, aioclient_mock, hassio_client):
async_step_hassio = AsyncMock(return_value={"type": "abort"})
yield MqttFlow
async def test_hassio_discovery_startup(hass, aioclient_mock, hassio_client, mock_mqtt):
"""Test startup and discovery after event."""
aioclient_mock.get(
"http://127.0.0.1/discovery",
......@@ -39,31 +63,29 @@ async def test_hassio_discovery_startup(hass, aioclient_mock, hassio_client):
assert aioclient_mock.call_count == 0
with patch(
"homeassistant.components.mqtt.config_flow.FlowHandler.async_step_hassio",
return_value={"type": "abort"},
) as mock_mqtt:
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done()
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()
assert aioclient_mock.call_count == 2
assert mock_mqtt.called
mock_mqtt.assert_called_with(
HassioServiceInfo(
config={
"broker": "mock-broker",
"port": 1883,
"username": "mock-user",
"password": "mock-pass",
"protocol": "3.1.1",
"addon": "Mosquitto Test",
}
)
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done()
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()
assert aioclient_mock.call_count == 2
assert mock_mqtt.async_step_hassio.called
mock_mqtt.async_step_hassio.assert_called_with(
HassioServiceInfo(
config={
"broker": "mock-broker",
"port": 1883,
"username": "mock-user",
"password": "mock-pass",
"protocol": "3.1.1",
"addon": "Mosquitto Test",
}
)
)
async def test_hassio_discovery_startup_done(hass, aioclient_mock, hassio_client):
async def test_hassio_discovery_startup_done(
hass, aioclient_mock, hassio_client, mock_mqtt
):
"""Test startup and discovery with hass discovery."""
aioclient_mock.post(
"http://127.0.0.1/supervisor/options",
......@@ -102,17 +124,14 @@ async def test_hassio_discovery_startup_done(hass, aioclient_mock, hassio_client
), patch(
"homeassistant.components.hassio.HassIO.get_info",
Mock(side_effect=HassioAPIError()),
), patch(
"homeassistant.components.mqtt.config_flow.FlowHandler.async_step_hassio",
return_value={"type": "abort"},
) as mock_mqtt:
):
await hass.async_start()
await async_setup_component(hass, "hassio", {})
await hass.async_block_till_done()
assert aioclient_mock.call_count == 2
assert mock_mqtt.called
mock_mqtt.assert_called_with(
assert mock_mqtt.async_step_hassio.called
mock_mqtt.async_step_hassio.assert_called_with(
HassioServiceInfo(
config={
"broker": "mock-broker",
......@@ -126,7 +145,7 @@ async def test_hassio_discovery_startup_done(hass, aioclient_mock, hassio_client
)
async def test_hassio_discovery_webhook(hass, aioclient_mock, hassio_client):
async def test_hassio_discovery_webhook(hass, aioclient_mock, hassio_client, mock_mqtt):
"""Test discovery webhook."""
aioclient_mock.get(
"http://127.0.0.1/discovery/testuuid",
......@@ -151,30 +170,26 @@ async def test_hassio_discovery_webhook(hass, aioclient_mock, hassio_client):
json={"result": "ok", "data": {"name": "Mosquitto Test"}},
)
with patch(
"homeassistant.components.mqtt.config_flow.FlowHandler.async_step_hassio",
return_value={"type": "abort"},
) as mock_mqtt:
resp = await hassio_client.post(
"/api/hassio_push/discovery/testuuid",
json={"addon": "mosquitto", "service": "mqtt", "uuid": "testuuid"},
)
await hass.async_block_till_done()
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()
resp = await hassio_client.post(
"/api/hassio_push/discovery/testuuid",
json={"addon": "mosquitto", "service": "mqtt", "uuid": "testuuid"},
)
await hass.async_block_till_done()
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()
assert resp.status == HTTPStatus.OK
assert aioclient_mock.call_count == 2
assert mock_mqtt.called
mock_mqtt.assert_called_with(
HassioServiceInfo(
config={
"broker": "mock-broker",
"port": 1883,
"username": "mock-user",
"password": "mock-pass",
"protocol": "3.1.1",
"addon": "Mosquitto Test",
}
)
assert resp.status == HTTPStatus.OK
assert aioclient_mock.call_count == 2
assert mock_mqtt.async_step_hassio.called
mock_mqtt.async_step_hassio.assert_called_with(
HassioServiceInfo(
config={
"broker": "mock-broker",
"port": 1883,
"username": "mock-user",
"password": "mock-pass",
"protocol": "3.1.1",
"addon": "Mosquitto Test",
}
)
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment