core/tests/components/madvr/test_config_flow.py

246 lines
8.2 KiB
Python
Raw Normal View History

Add madvr envy integration (#120382) * feat: Add madvr envy * fix: await and pass entry directly * fix: add attributes and unique id for sensors * fix: reflect power state well, improve state detection * fix: don't connect on init, add options, add reload on change, keep on during test * fix: cancel tasks on unload * fix: test connection via library * fix: wait for boot time * docs: add readme and license * fix: broken pipe in lib * fix: detect out of band power off * fix: improve extra attributes * fix: fix unloading, add config flow test, limit to one platform * fix: use conf, refresh coordinator, other comments * fix: remove event data * fix: fix tests passing, remove wake on lan * fix: dont allow to proceed unless connection works * chore: update dep * fix: update config flow, add constants * fix: write state, use runtime data instead * fix: remove await * fix: move unloading and stuff to coordinator/init * fix: pass in config entry with correct type * fix: move queue and tasks to library * fix: config flow error flow, tests, name, and update lib * fix: update lib, leave connection open on setup * fix: update lib * fix: address comments, remove wol from lib * fix: remove unneeded options * fix: remove fields * fix: simplify code, address comments * fix: move error to lib * fix: fix test * fix: stronger types * fix: update lib * fix: missing text from options flow * chore: remove options flow * chore: remove import * chore: update comments * fix: get mac from device, persist * fix: add mac stuff to test * fix: startup import errors * chore: stale comment * fix: get mac from persisted config * chore: update lib * fix: persist mac in a better way * feat: use mac as unique ID for entry * fix: use unique ID from mac, add proper device * fix: will not be set in init potentially * fix: access mac * fix: optimize, move error to lib * feat: add coordinator test, use conf * fix: use one mock, add init test * fix: not async * feat: add remote test * fix: types * fix: patch client, expand remote tests * fix: use snapshot test * fix: update branding * fix: add description, fix type check * fix: update tests * fix: test * fix: update test * fix: camelcase * Fix * feat: strict typing * fix: strict typing in lib * fix: type will never be None * fix: reference to mac, all tests passing --------- Co-authored-by: Joostlek <joostlek@outlook.com>
2024-07-07 18:41:53 +00:00
"""Tests for the MadVR config flow."""
from collections.abc import AsyncGenerator
from unittest.mock import AsyncMock, patch
import pytest
from homeassistant.components.madvr.const import DEFAULT_NAME, DOMAIN
from homeassistant.config_entries import SOURCE_RECONFIGURE, SOURCE_USER
Add madvr envy integration (#120382) * feat: Add madvr envy * fix: await and pass entry directly * fix: add attributes and unique id for sensors * fix: reflect power state well, improve state detection * fix: don't connect on init, add options, add reload on change, keep on during test * fix: cancel tasks on unload * fix: test connection via library * fix: wait for boot time * docs: add readme and license * fix: broken pipe in lib * fix: detect out of band power off * fix: improve extra attributes * fix: fix unloading, add config flow test, limit to one platform * fix: use conf, refresh coordinator, other comments * fix: remove event data * fix: fix tests passing, remove wake on lan * fix: dont allow to proceed unless connection works * chore: update dep * fix: update config flow, add constants * fix: write state, use runtime data instead * fix: remove await * fix: move unloading and stuff to coordinator/init * fix: pass in config entry with correct type * fix: move queue and tasks to library * fix: config flow error flow, tests, name, and update lib * fix: update lib, leave connection open on setup * fix: update lib * fix: address comments, remove wol from lib * fix: remove unneeded options * fix: remove fields * fix: simplify code, address comments * fix: move error to lib * fix: fix test * fix: stronger types * fix: update lib * fix: missing text from options flow * chore: remove options flow * chore: remove import * chore: update comments * fix: get mac from device, persist * fix: add mac stuff to test * fix: startup import errors * chore: stale comment * fix: get mac from persisted config * chore: update lib * fix: persist mac in a better way * feat: use mac as unique ID for entry * fix: use unique ID from mac, add proper device * fix: will not be set in init potentially * fix: access mac * fix: optimize, move error to lib * feat: add coordinator test, use conf * fix: use one mock, add init test * fix: not async * feat: add remote test * fix: types * fix: patch client, expand remote tests * fix: use snapshot test * fix: update branding * fix: add description, fix type check * fix: update tests * fix: test * fix: update test * fix: camelcase * Fix * feat: strict typing * fix: strict typing in lib * fix: type will never be None * fix: reference to mac, all tests passing --------- Co-authored-by: Joostlek <joostlek@outlook.com>
2024-07-07 18:41:53 +00:00
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from .const import MOCK_CONFIG, MOCK_MAC, MOCK_MAC_NEW
Add madvr envy integration (#120382) * feat: Add madvr envy * fix: await and pass entry directly * fix: add attributes and unique id for sensors * fix: reflect power state well, improve state detection * fix: don't connect on init, add options, add reload on change, keep on during test * fix: cancel tasks on unload * fix: test connection via library * fix: wait for boot time * docs: add readme and license * fix: broken pipe in lib * fix: detect out of band power off * fix: improve extra attributes * fix: fix unloading, add config flow test, limit to one platform * fix: use conf, refresh coordinator, other comments * fix: remove event data * fix: fix tests passing, remove wake on lan * fix: dont allow to proceed unless connection works * chore: update dep * fix: update config flow, add constants * fix: write state, use runtime data instead * fix: remove await * fix: move unloading and stuff to coordinator/init * fix: pass in config entry with correct type * fix: move queue and tasks to library * fix: config flow error flow, tests, name, and update lib * fix: update lib, leave connection open on setup * fix: update lib * fix: address comments, remove wol from lib * fix: remove unneeded options * fix: remove fields * fix: simplify code, address comments * fix: move error to lib * fix: fix test * fix: stronger types * fix: update lib * fix: missing text from options flow * chore: remove options flow * chore: remove import * chore: update comments * fix: get mac from device, persist * fix: add mac stuff to test * fix: startup import errors * chore: stale comment * fix: get mac from persisted config * chore: update lib * fix: persist mac in a better way * feat: use mac as unique ID for entry * fix: use unique ID from mac, add proper device * fix: will not be set in init potentially * fix: access mac * fix: optimize, move error to lib * feat: add coordinator test, use conf * fix: use one mock, add init test * fix: not async * feat: add remote test * fix: types * fix: patch client, expand remote tests * fix: use snapshot test * fix: update branding * fix: add description, fix type check * fix: update tests * fix: test * fix: update test * fix: camelcase * Fix * feat: strict typing * fix: strict typing in lib * fix: type will never be None * fix: reference to mac, all tests passing --------- Co-authored-by: Joostlek <joostlek@outlook.com>
2024-07-07 18:41:53 +00:00
from tests.common import MockConfigEntry
@pytest.fixture(autouse=True)
async def avoid_wait() -> AsyncGenerator[None]:
Add madvr envy integration (#120382) * feat: Add madvr envy * fix: await and pass entry directly * fix: add attributes and unique id for sensors * fix: reflect power state well, improve state detection * fix: don't connect on init, add options, add reload on change, keep on during test * fix: cancel tasks on unload * fix: test connection via library * fix: wait for boot time * docs: add readme and license * fix: broken pipe in lib * fix: detect out of band power off * fix: improve extra attributes * fix: fix unloading, add config flow test, limit to one platform * fix: use conf, refresh coordinator, other comments * fix: remove event data * fix: fix tests passing, remove wake on lan * fix: dont allow to proceed unless connection works * chore: update dep * fix: update config flow, add constants * fix: write state, use runtime data instead * fix: remove await * fix: move unloading and stuff to coordinator/init * fix: pass in config entry with correct type * fix: move queue and tasks to library * fix: config flow error flow, tests, name, and update lib * fix: update lib, leave connection open on setup * fix: update lib * fix: address comments, remove wol from lib * fix: remove unneeded options * fix: remove fields * fix: simplify code, address comments * fix: move error to lib * fix: fix test * fix: stronger types * fix: update lib * fix: missing text from options flow * chore: remove options flow * chore: remove import * chore: update comments * fix: get mac from device, persist * fix: add mac stuff to test * fix: startup import errors * chore: stale comment * fix: get mac from persisted config * chore: update lib * fix: persist mac in a better way * feat: use mac as unique ID for entry * fix: use unique ID from mac, add proper device * fix: will not be set in init potentially * fix: access mac * fix: optimize, move error to lib * feat: add coordinator test, use conf * fix: use one mock, add init test * fix: not async * feat: add remote test * fix: types * fix: patch client, expand remote tests * fix: use snapshot test * fix: update branding * fix: add description, fix type check * fix: update tests * fix: test * fix: update test * fix: camelcase * Fix * feat: strict typing * fix: strict typing in lib * fix: type will never be None * fix: reference to mac, all tests passing --------- Co-authored-by: Joostlek <joostlek@outlook.com>
2024-07-07 18:41:53 +00:00
"""Mock sleep."""
with patch("homeassistant.components.madvr.config_flow.RETRY_INTERVAL", 0):
yield
async def test_full_flow(
hass: HomeAssistant,
mock_madvr_client: AsyncMock,
mock_setup_entry: AsyncMock,
) -> None:
"""Test full config flow."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: MOCK_CONFIG[CONF_HOST], CONF_PORT: MOCK_CONFIG[CONF_PORT]},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"] == {
CONF_HOST: MOCK_CONFIG[CONF_HOST],
CONF_PORT: MOCK_CONFIG[CONF_PORT],
}
assert result["result"].unique_id == MOCK_MAC
mock_madvr_client.open_connection.assert_called_once()
mock_madvr_client.async_add_tasks.assert_called_once()
mock_madvr_client.async_cancel_tasks.assert_called_once()
async def test_flow_errors(
hass: HomeAssistant,
mock_madvr_client: AsyncMock,
mock_setup_entry: AsyncMock,
) -> None:
"""Test error handling in config flow."""
mock_madvr_client.open_connection.side_effect = TimeoutError
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: MOCK_CONFIG[CONF_HOST], CONF_PORT: MOCK_CONFIG[CONF_PORT]},
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "cannot_connect"}
mock_madvr_client.open_connection.side_effect = None
mock_madvr_client.connected = False
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: MOCK_CONFIG[CONF_HOST], CONF_PORT: MOCK_CONFIG[CONF_PORT]},
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "cannot_connect"}
mock_madvr_client.connected = True
mock_madvr_client.mac_address = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: MOCK_CONFIG[CONF_HOST], CONF_PORT: MOCK_CONFIG[CONF_PORT]},
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "no_mac"}
# ensure an error is recoverable
mock_madvr_client.mac_address = MOCK_MAC
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: MOCK_CONFIG[CONF_HOST], CONF_PORT: MOCK_CONFIG[CONF_PORT]},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == DEFAULT_NAME
assert result["data"] == {
CONF_HOST: MOCK_CONFIG[CONF_HOST],
CONF_PORT: MOCK_CONFIG[CONF_PORT],
}
# Verify method calls
assert mock_madvr_client.open_connection.call_count == 4
assert mock_madvr_client.async_add_tasks.call_count == 2
# the first call will not call this due to timeout as expected
assert mock_madvr_client.async_cancel_tasks.call_count == 2
async def test_duplicate(
hass: HomeAssistant,
mock_madvr_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test duplicate config entries."""
mock_config_entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: MOCK_CONFIG[CONF_HOST], CONF_PORT: MOCK_CONFIG[CONF_PORT]},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
async def test_reconfigure_flow(
hass: HomeAssistant,
mock_madvr_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test reconfigure flow."""
mock_config_entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_RECONFIGURE, "entry_id": mock_config_entry.entry_id},
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure"
assert result["errors"] == {}
# define new host
new_host = "192.168.1.100"
# make sure setting port works
new_port = 44078
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: new_host, CONF_PORT: new_port},
)
# should get the abort with success result
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
# Verify that the config entry was updated
assert mock_config_entry.data[CONF_HOST] == new_host
assert mock_config_entry.data[CONF_PORT] == new_port
# Verify that the connection was tested
mock_madvr_client.open_connection.assert_called()
mock_madvr_client.async_add_tasks.assert_called()
mock_madvr_client.async_cancel_tasks.assert_called()
async def test_reconfigure_new_device(
hass: HomeAssistant,
mock_madvr_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test reconfigure flow."""
mock_config_entry.add_to_hass(hass)
# test reconfigure with a new device (should fail)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_RECONFIGURE, "entry_id": mock_config_entry.entry_id},
)
# define new host
new_host = "192.168.1.100"
# make sure setting port works
new_port = 44078
# modify test_connection so it returns new_mac
mock_madvr_client.mac_address = MOCK_MAC_NEW
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: new_host, CONF_PORT: new_port},
)
# unique id should remain unchanged with new device, should fail
assert mock_config_entry.unique_id == MOCK_MAC
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "set_up_new_device"
async def test_reconfigure_flow_errors(
hass: HomeAssistant,
mock_madvr_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test error handling in reconfigure flow."""
mock_config_entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_RECONFIGURE, "entry_id": mock_config_entry.entry_id},
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure"
# Test CannotConnect error
mock_madvr_client.open_connection.side_effect = TimeoutError
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "192.168.1.100", CONF_PORT: 44077},
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "cannot_connect"}
# Test no_mac error
mock_madvr_client.open_connection.side_effect = None
mock_madvr_client.connected = True
mock_madvr_client.mac_address = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "192.168.1.100", CONF_PORT: 44077},
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "no_mac"}
# Ensure errors are recoverable
mock_madvr_client.mac_address = MOCK_MAC
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "192.168.1.100", CONF_PORT: 44077},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"