Mock MQTT setup in hassio tests (#77245)

* Mock MQTT setup in hassio tests

* Tweak
This commit is contained in:
Erik Montnemery 2022-08-24 10:48:23 +02:00 committed by GitHub
parent c26d6879ae
commit 853fab0a68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,14 +1,38 @@
"""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
async def test_hassio_discovery_startup(hass, aioclient_mock, hassio_client):
@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_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,17 +63,13 @@ 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(
assert mock_mqtt.async_step_hassio.called
mock_mqtt.async_step_hassio.assert_called_with(
HassioServiceInfo(
config={
"broker": "mock-broker",
@ -63,7 +83,9 @@ async def test_hassio_discovery_startup(hass, aioclient_mock, hassio_client):
)
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,10 +170,6 @@ 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"},
@ -165,8 +180,8 @@ async def test_hassio_discovery_webhook(hass, aioclient_mock, hassio_client):
assert resp.status == HTTPStatus.OK
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",