mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 05:37:44 +00:00
Use fixtures in deCONZ init tests (#121217)
This commit is contained in:
parent
6e5c3904b6
commit
178655647d
@ -19,6 +19,8 @@ from tests.common import MockConfigEntry
|
||||
from tests.components.light.conftest import mock_light_profiles # noqa: F401
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
|
||||
type ConfigEntryFactoryType = Callable[[ConfigEntry | None], ConfigEntry]
|
||||
|
||||
# Config entry fixtures
|
||||
|
||||
API_KEY = "1234567890ABCDEF"
|
||||
@ -29,13 +31,12 @@ PORT = 80
|
||||
|
||||
@pytest.fixture(name="config_entry")
|
||||
def fixture_config_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry_data: MappingProxyType[str, Any],
|
||||
config_entry_options: MappingProxyType[str, Any],
|
||||
config_entry_source: str,
|
||||
) -> ConfigEntry:
|
||||
"""Define a config entry fixture."""
|
||||
config_entry = MockConfigEntry(
|
||||
return MockConfigEntry(
|
||||
domain=DECONZ_DOMAIN,
|
||||
entry_id="1",
|
||||
unique_id=BRIDGEID,
|
||||
@ -43,8 +44,6 @@ def fixture_config_entry(
|
||||
options=config_entry_options,
|
||||
source=config_entry_source,
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
return config_entry
|
||||
|
||||
|
||||
@pytest.fixture(name="config_entry_data")
|
||||
@ -194,14 +193,15 @@ async def fixture_config_entry_factory(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
mock_requests: Callable[[str, str], None],
|
||||
) -> Callable[[], ConfigEntry]:
|
||||
) -> ConfigEntryFactoryType:
|
||||
"""Fixture factory that can set up UniFi network integration."""
|
||||
|
||||
async def __mock_setup_config_entry() -> ConfigEntry:
|
||||
mock_requests(config_entry.data[CONF_HOST])
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
async def __mock_setup_config_entry(entry=config_entry) -> ConfigEntry:
|
||||
entry.add_to_hass(hass)
|
||||
mock_requests(entry.data[CONF_HOST])
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
return config_entry
|
||||
return entry
|
||||
|
||||
return __mock_setup_config_entry
|
||||
|
||||
|
@ -10,10 +10,12 @@ from homeassistant.components.deconz import (
|
||||
)
|
||||
from homeassistant.components.deconz.const import DOMAIN as DECONZ_DOMAIN
|
||||
from homeassistant.components.deconz.errors import AuthenticationRequired, CannotConnect
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration
|
||||
from .conftest import ConfigEntryFactoryType
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
|
||||
ENTRY1_HOST = "1.2.3.4"
|
||||
@ -39,28 +41,30 @@ async def setup_entry(hass, entry):
|
||||
|
||||
|
||||
async def test_setup_entry_successful(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
hass: HomeAssistant, config_entry_setup: ConfigEntry
|
||||
) -> None:
|
||||
"""Test setup entry is successful."""
|
||||
config_entry = await setup_deconz_integration(hass, aioclient_mock)
|
||||
|
||||
assert hass.data[DECONZ_DOMAIN]
|
||||
assert config_entry.entry_id in hass.data[DECONZ_DOMAIN]
|
||||
assert hass.data[DECONZ_DOMAIN][config_entry.entry_id].master
|
||||
assert config_entry_setup.entry_id in hass.data[DECONZ_DOMAIN]
|
||||
assert hass.data[DECONZ_DOMAIN][config_entry_setup.entry_id].master
|
||||
|
||||
|
||||
async def test_setup_entry_fails_config_entry_not_ready(hass: HomeAssistant) -> None:
|
||||
async def test_setup_entry_fails_config_entry_not_ready(
|
||||
hass: HomeAssistant, config_entry_factory: ConfigEntryFactoryType
|
||||
) -> None:
|
||||
"""Failed authentication trigger a reauthentication flow."""
|
||||
with patch(
|
||||
"homeassistant.components.deconz.get_deconz_api",
|
||||
side_effect=CannotConnect,
|
||||
):
|
||||
await setup_deconz_integration(hass)
|
||||
await config_entry_factory()
|
||||
|
||||
assert hass.data[DECONZ_DOMAIN] == {}
|
||||
|
||||
|
||||
async def test_setup_entry_fails_trigger_reauth_flow(hass: HomeAssistant) -> None:
|
||||
async def test_setup_entry_fails_trigger_reauth_flow(
|
||||
hass: HomeAssistant, config_entry_factory: ConfigEntryFactoryType
|
||||
) -> None:
|
||||
"""Failed authentication trigger a reauthentication flow."""
|
||||
with (
|
||||
patch(
|
||||
@ -69,27 +73,25 @@ async def test_setup_entry_fails_trigger_reauth_flow(hass: HomeAssistant) -> Non
|
||||
),
|
||||
patch.object(hass.config_entries.flow, "async_init") as mock_flow_init,
|
||||
):
|
||||
await setup_deconz_integration(hass)
|
||||
await config_entry_factory()
|
||||
mock_flow_init.assert_called_once()
|
||||
|
||||
assert hass.data[DECONZ_DOMAIN] == {}
|
||||
|
||||
|
||||
async def test_setup_entry_multiple_gateways(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
hass: HomeAssistant, config_entry_factory: ConfigEntryFactoryType
|
||||
) -> None:
|
||||
"""Test setup entry is successful with multiple gateways."""
|
||||
config_entry = await setup_deconz_integration(hass, aioclient_mock)
|
||||
aioclient_mock.clear_requests()
|
||||
config_entry = await config_entry_factory()
|
||||
|
||||
data = {"config": {"bridgeid": "01234E56789B"}}
|
||||
with patch.dict(DECONZ_WEB_REQUEST, data):
|
||||
config_entry2 = await setup_deconz_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
entry_id="2",
|
||||
unique_id="01234E56789B",
|
||||
)
|
||||
entry2 = MockConfigEntry(
|
||||
domain=DECONZ_DOMAIN,
|
||||
entry_id="2",
|
||||
unique_id="01234E56789B",
|
||||
data=config_entry.data | {"host": "2.3.4.5"},
|
||||
)
|
||||
config_entry2 = await config_entry_factory(entry2)
|
||||
|
||||
assert len(hass.data[DECONZ_DOMAIN]) == 2
|
||||
assert hass.data[DECONZ_DOMAIN][config_entry.entry_id].master
|
||||
@ -97,31 +99,30 @@ async def test_setup_entry_multiple_gateways(
|
||||
|
||||
|
||||
async def test_unload_entry(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
hass: HomeAssistant, config_entry_setup: ConfigEntry
|
||||
) -> None:
|
||||
"""Test being able to unload an entry."""
|
||||
config_entry = await setup_deconz_integration(hass, aioclient_mock)
|
||||
assert hass.data[DECONZ_DOMAIN]
|
||||
|
||||
assert await async_unload_entry(hass, config_entry)
|
||||
assert await async_unload_entry(hass, config_entry_setup)
|
||||
assert not hass.data[DECONZ_DOMAIN]
|
||||
|
||||
|
||||
async def test_unload_entry_multiple_gateways(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
config_entry_factory,
|
||||
) -> None:
|
||||
"""Test being able to unload an entry and master gateway gets moved."""
|
||||
config_entry = await setup_deconz_integration(hass, aioclient_mock)
|
||||
aioclient_mock.clear_requests()
|
||||
config_entry = await config_entry_factory()
|
||||
|
||||
data = {"config": {"bridgeid": "01234E56789B"}}
|
||||
with patch.dict(DECONZ_WEB_REQUEST, data):
|
||||
config_entry2 = await setup_deconz_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
entry_id="2",
|
||||
unique_id="01234E56789B",
|
||||
)
|
||||
entry2 = MockConfigEntry(
|
||||
domain=DECONZ_DOMAIN,
|
||||
entry_id="2",
|
||||
unique_id="01234E56789B",
|
||||
data=config_entry.data | {"host": "2.3.4.5"},
|
||||
)
|
||||
config_entry2 = await config_entry_factory(entry2)
|
||||
|
||||
assert len(hass.data[DECONZ_DOMAIN]) == 2
|
||||
|
||||
@ -132,20 +133,18 @@ async def test_unload_entry_multiple_gateways(
|
||||
|
||||
|
||||
async def test_unload_entry_multiple_gateways_parallel(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
hass: HomeAssistant, config_entry_factory
|
||||
) -> None:
|
||||
"""Test race condition when unloading multiple config entries in parallel."""
|
||||
config_entry = await setup_deconz_integration(hass, aioclient_mock)
|
||||
aioclient_mock.clear_requests()
|
||||
config_entry = await config_entry_factory()
|
||||
|
||||
data = {"config": {"bridgeid": "01234E56789B"}}
|
||||
with patch.dict(DECONZ_WEB_REQUEST, data):
|
||||
config_entry2 = await setup_deconz_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
entry_id="2",
|
||||
unique_id="01234E56789B",
|
||||
)
|
||||
entry2 = MockConfigEntry(
|
||||
domain=DECONZ_DOMAIN,
|
||||
entry_id="2",
|
||||
unique_id="01234E56789B",
|
||||
data=config_entry.data | {"host": "2.3.4.5"},
|
||||
)
|
||||
config_entry2 = await config_entry_factory(entry2)
|
||||
|
||||
assert len(hass.data[DECONZ_DOMAIN]) == 2
|
||||
|
||||
|
@ -40,6 +40,8 @@ from homeassistant.const import (
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .conftest import ConfigEntryFactoryType
|
||||
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
|
||||
|
||||
@ -1082,7 +1084,7 @@ async def test_groups(
|
||||
)
|
||||
async def test_group_service_calls(
|
||||
hass: HomeAssistant,
|
||||
config_entry_factory: Callable[[], ConfigEntry],
|
||||
config_entry_factory: ConfigEntryFactoryType,
|
||||
group_payload: dict[str, Any],
|
||||
mock_put_request: Callable[[str, str], AiohttpClientMocker],
|
||||
input: dict[str, Any],
|
||||
|
@ -1,6 +1,5 @@
|
||||
"""deCONZ sensor platform tests."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from datetime import timedelta
|
||||
from typing import Any
|
||||
|
||||
@ -25,6 +24,8 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .conftest import ConfigEntryFactoryType
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
TEST_DATA = [
|
||||
@ -1093,7 +1094,7 @@ BAD_SENSOR_DATA = [
|
||||
@pytest.mark.parametrize(("sensor_type", "sensor_property"), BAD_SENSOR_DATA)
|
||||
async def test_dont_add_sensor_if_state_is_none(
|
||||
hass: HomeAssistant,
|
||||
config_entry_factory: Callable[[], ConfigEntry],
|
||||
config_entry_factory: ConfigEntryFactoryType,
|
||||
sensor_payload: dict[str, Any],
|
||||
sensor_type: str,
|
||||
sensor_property: str,
|
||||
@ -1205,7 +1206,7 @@ async def test_add_battery_later(hass: HomeAssistant, mock_deconz_websocket) ->
|
||||
@pytest.mark.parametrize("model_id", ["0x8030", "0x8031", "0x8034", "0x8035"])
|
||||
async def test_special_danfoss_battery_creation(
|
||||
hass: HomeAssistant,
|
||||
config_entry_factory: Callable[[], ConfigEntry],
|
||||
config_entry_factory: ConfigEntryFactoryType,
|
||||
sensor_payload: dict[str, Any],
|
||||
model_id: str,
|
||||
) -> None:
|
||||
|
@ -16,6 +16,8 @@ from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, STATE_UNAVA
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from .conftest import ConfigEntryFactoryType
|
||||
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
|
||||
|
||||
@ -127,7 +129,7 @@ async def test_power_plugs(
|
||||
async def test_remove_legacy_on_off_output_as_light(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
config_entry_factory: Callable[[], ConfigEntry],
|
||||
config_entry_factory: ConfigEntryFactoryType,
|
||||
) -> None:
|
||||
"""Test that switch platform cleans up legacy light entities."""
|
||||
assert entity_registry.async_get_or_create(
|
||||
|
Loading…
x
Reference in New Issue
Block a user