Skip to content

Commit

Permalink
Provide the ZHA config entry as a reusable fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly committed Aug 17, 2023
1 parent ca37ff7 commit 021def4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
33 changes: 20 additions & 13 deletions tests/components/zha/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Test configuration for the ZHA component."""
from collections.abc import Callable
from collections.abc import Callable, Generator
import itertools
import time
from unittest.mock import AsyncMock, MagicMock, patch
Expand Down Expand Up @@ -123,10 +123,10 @@ def zigpy_app_controller():


@pytest.fixture(name="config_entry")
async def config_entry_fixture(hass):
async def config_entry_fixture(hass) -> MockConfigEntry:
"""Fixture representing a config entry."""
entry = MockConfigEntry(
version=2,
return MockConfigEntry(
version=3,
domain=zha_const.DOMAIN,
data={
zigpy.config.CONF_DEVICE: {zigpy.config.CONF_DEVICE_PATH: "/dev/ttyUSB0"},
Expand All @@ -146,23 +146,30 @@ async def config_entry_fixture(hass):
}
},
)
entry.add_to_hass(hass)
return entry


@pytest.fixture
def setup_zha(hass, config_entry, zigpy_app_controller):
"""Set up ZHA component."""
zha_config = {zha_const.CONF_ENABLE_QUIRKS: False}

p1 = patch(
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."""
zha_config = {zha_const.CONF_ENABLE_QUIRKS: False}

async def _setup(config=None):
config_entry.add_to_hass(hass)
config = config or {}
with p1:

with mock_zigpy_connect:
status = await async_setup_component(
hass, zha_const.DOMAIN, {zha_const.DOMAIN: {**zha_config, **config}}
)
Expand Down
16 changes: 2 additions & 14 deletions tests/components/zha/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,22 +956,10 @@ async def test_user_port_config(probe_mock, hass: HomeAssistant) -> None:
],
)
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:
"""Test zigpy-cc to zigpy-znp config migration."""
config_entry = MockConfigEntry(
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.data = {**config_entry.data, CONF_RADIO_TYPE: old_type}
config_entry.version = 2
config_entry.add_to_hass(hass)

Expand Down
8 changes: 5 additions & 3 deletions tests/components/zha/test_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

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 (
get_diagnostics_for_config_entry,
get_diagnostics_for_device,
Expand Down Expand Up @@ -57,11 +58,13 @@ def zigpy_device(zigpy_device_mock):
async def test_diagnostics_for_config_entry(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
config_entry,
config_entry: MockConfigEntry,
zha_device_joined,
zigpy_device,
) -> None:
"""Test diagnostics for config entry."""
config_entry.add_to_hass(hass)

await zha_device_joined(zigpy_device)

gateway = hass.data[DATA_ZHA][DATA_ZHA_GATEWAY]
Expand All @@ -86,12 +89,11 @@ async def test_diagnostics_for_config_entry(
async def test_diagnostics_for_device(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
config_entry,
config_entry: MockConfigEntry,
zha_device_joined,
zigpy_device,
) -> None:
"""Test diagnostics for device."""

zha_device: ZHADevice = await zha_device_joined(zigpy_device)
dev_reg = async_get(hass)
device = dev_reg.async_get_device(identifiers={("zha", str(zha_device.ieee))})
Expand Down

0 comments on commit 021def4

Please sign in to comment.