Provide the ZHA config entry as a reusable fixture

This commit is contained in:
puddly 2023-08-17 15:41:30 -04:00
parent ca37ff7a4d
commit 021def4441
3 changed files with 27 additions and 30 deletions

View file

@ -1,5 +1,5 @@
"""Test configuration for the ZHA component.""" """Test configuration for the ZHA component."""
from collections.abc import Callable from collections.abc import Callable, Generator
import itertools import itertools
import time import time
from unittest.mock import AsyncMock, MagicMock, patch from unittest.mock import AsyncMock, MagicMock, patch
@ -123,10 +123,10 @@ def zigpy_app_controller():
@pytest.fixture(name="config_entry") @pytest.fixture(name="config_entry")
async def config_entry_fixture(hass): async def config_entry_fixture(hass) -> MockConfigEntry:
"""Fixture representing a config entry.""" """Fixture representing a config entry."""
entry = MockConfigEntry( return MockConfigEntry(
version=2, version=3,
domain=zha_const.DOMAIN, domain=zha_const.DOMAIN,
data={ data={
zigpy.config.CONF_DEVICE: {zigpy.config.CONF_DEVICE_PATH: "/dev/ttyUSB0"}, zigpy.config.CONF_DEVICE: {zigpy.config.CONF_DEVICE_PATH: "/dev/ttyUSB0"},
@ -146,23 +146,30 @@ async def config_entry_fixture(hass):
} }
}, },
) )
entry.add_to_hass(hass)
return entry
@pytest.fixture @pytest.fixture
def setup_zha(hass, config_entry, zigpy_app_controller): def mock_zigpy_connect(
zigpy_app_controller: ControllerApplication,
) -> Generator[ControllerApplication, None, None]:
"""Patch the zigpy radio connection with our mock application."""
with patch(
"bellows.zigbee.application.ControllerApplication.new",
return_value=zigpy_app_controller,
) as mock_app:
yield mock_app
@pytest.fixture
def setup_zha(hass, config_entry: MockConfigEntry, mock_zigpy_connect):
"""Set up ZHA component.""" """Set up ZHA component."""
zha_config = {zha_const.CONF_ENABLE_QUIRKS: False} zha_config = {zha_const.CONF_ENABLE_QUIRKS: False}
p1 = patch(
"bellows.zigbee.application.ControllerApplication.new",
return_value=zigpy_app_controller,
)
async def _setup(config=None): async def _setup(config=None):
config_entry.add_to_hass(hass)
config = config or {} config = config or {}
with p1:
with mock_zigpy_connect:
status = await async_setup_component( status = await async_setup_component(
hass, zha_const.DOMAIN, {zha_const.DOMAIN: {**zha_config, **config}} hass, zha_const.DOMAIN, {zha_const.DOMAIN: {**zha_config, **config}}
) )

View file

@ -956,22 +956,10 @@ async def test_user_port_config(probe_mock, hass: HomeAssistant) -> None:
], ],
) )
async def test_migration_ti_cc_to_znp( async def test_migration_ti_cc_to_znp(
old_type, new_type, hass: HomeAssistant, config_entry old_type, new_type, hass: HomeAssistant, config_entry: MockConfigEntry
) -> None: ) -> None:
"""Test zigpy-cc to zigpy-znp config migration.""" """Test zigpy-cc to zigpy-znp config migration."""
config_entry = MockConfigEntry( config_entry.data = {**config_entry.data, CONF_RADIO_TYPE: old_type}
domain=DOMAIN,
unique_id=old_type + new_type,
data={
CONF_RADIO_TYPE: old_type,
CONF_DEVICE: {
CONF_DEVICE_PATH: "/dev/ttyUSB1",
CONF_BAUDRATE: 115200,
CONF_FLOWCONTROL: None,
},
},
)
config_entry.version = 2 config_entry.version = 2
config_entry.add_to_hass(hass) config_entry.add_to_hass(hass)

View file

@ -15,6 +15,7 @@ from homeassistant.helpers.device_registry import async_get
from .conftest import SIG_EP_INPUT, SIG_EP_OUTPUT, SIG_EP_PROFILE, SIG_EP_TYPE from .conftest import SIG_EP_INPUT, SIG_EP_OUTPUT, SIG_EP_PROFILE, SIG_EP_TYPE
from tests.common import MockConfigEntry
from tests.components.diagnostics import ( from tests.components.diagnostics import (
get_diagnostics_for_config_entry, get_diagnostics_for_config_entry,
get_diagnostics_for_device, get_diagnostics_for_device,
@ -57,11 +58,13 @@ def zigpy_device(zigpy_device_mock):
async def test_diagnostics_for_config_entry( async def test_diagnostics_for_config_entry(
hass: HomeAssistant, hass: HomeAssistant,
hass_client: ClientSessionGenerator, hass_client: ClientSessionGenerator,
config_entry, config_entry: MockConfigEntry,
zha_device_joined, zha_device_joined,
zigpy_device, zigpy_device,
) -> None: ) -> None:
"""Test diagnostics for config entry.""" """Test diagnostics for config entry."""
config_entry.add_to_hass(hass)
await zha_device_joined(zigpy_device) await zha_device_joined(zigpy_device)
gateway = hass.data[DATA_ZHA][DATA_ZHA_GATEWAY] gateway = hass.data[DATA_ZHA][DATA_ZHA_GATEWAY]
@ -86,12 +89,11 @@ async def test_diagnostics_for_config_entry(
async def test_diagnostics_for_device( async def test_diagnostics_for_device(
hass: HomeAssistant, hass: HomeAssistant,
hass_client: ClientSessionGenerator, hass_client: ClientSessionGenerator,
config_entry, config_entry: MockConfigEntry,
zha_device_joined, zha_device_joined,
zigpy_device, zigpy_device,
) -> None: ) -> None:
"""Test diagnostics for device.""" """Test diagnostics for device."""
zha_device: ZHADevice = await zha_device_joined(zigpy_device) zha_device: ZHADevice = await zha_device_joined(zigpy_device)
dev_reg = async_get(hass) dev_reg = async_get(hass)
device = dev_reg.async_get_device(identifiers={("zha", str(zha_device.ieee))}) device = dev_reg.async_get_device(identifiers={("zha", str(zha_device.ieee))})