Skip to content

Commit

Permalink
Fix test modifying real device registry (#3703)
Browse files Browse the repository at this point in the history
* Move `real_device` fixture to `conftest`

* Fix test modifying real device registry
  • Loading branch information
TheJulianJES authored Jan 11, 2025
1 parent d80730f commit 57bcec8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
22 changes: 22 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest
import zigpy.application
import zigpy.device
from zigpy.device import Device
import zigpy.quirks
import zigpy.types
from zigpy.zcl import foundation
Expand All @@ -21,6 +22,8 @@
PROFILE_ID,
)

from .async_mock import sentinel


class MockApp(zigpy.application.ControllerApplication):
"""App Controller."""
Expand Down Expand Up @@ -204,6 +207,25 @@ def _dev(
return _dev


@pytest.fixture(name="device_mock")
def real_device(MockAppController):
"""Device fixture with a single endpoint."""
ieee = sentinel.ieee
nwk = 0x2233
device = Device(MockAppController, ieee, nwk)

device.add_endpoint(1)
device[1].profile_id = 0x0104
device[1].device_type = 0x0051
device.model = "model"
device.manufacturer = "manufacturer"
device[1].add_input_cluster(0x0000)
device[1].add_input_cluster(0xEF00)
device[1].add_output_cluster(0x000A)
device[1].add_output_cluster(0x0019)
return device


@pytest.fixture
def assert_signature_matches_quirk():
"""Return a function that can be used to check if a given quirk matches a signature."""
Expand Down
9 changes: 5 additions & 4 deletions tests/test_quirks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import zigpy.endpoint
import zigpy.profiles
import zigpy.quirks as zq
from zigpy.quirks import CustomDevice
from zigpy.quirks import CustomDevice, DeviceRegistry
from zigpy.quirks.v2 import QuirkBuilder
import zigpy.types
from zigpy.zcl import foundation
Expand Down Expand Up @@ -845,8 +845,9 @@ def check_for_duplicate_cluster_ids(clusters) -> None:
check_for_duplicate_cluster_ids(ep_data.get(OUTPUT_CLUSTERS, []))


async def test_local_data_cluster(zigpy_device_from_v2_quirk) -> None:
async def test_local_data_cluster(device_mock) -> None:
"""Ensure reading attributes from a LocalDataCluster works as expected."""
registry = DeviceRegistry()

class TestLocalCluster(zhaquirks.LocalDataCluster):
"""Test cluster."""
Expand All @@ -856,11 +857,11 @@ class TestLocalCluster(zhaquirks.LocalDataCluster):
_VALID_ATTRIBUTES = [2]

(
QuirkBuilder("manufacturer-local-test", "model")
QuirkBuilder(device_mock.manufacturer, device_mock.model, registry=registry)
.adds(TestLocalCluster)
.add_to_registry()
)
device = zigpy_device_from_v2_quirk("manufacturer-local-test", "model")
device = registry.get_device(device_mock)
assert isinstance(device.endpoints[1].in_clusters[0x1234], TestLocalCluster)

# reading invalid attribute return unsupported attribute
Expand Down
22 changes: 0 additions & 22 deletions tests/test_tuya_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from unittest import mock

import pytest
from zigpy.device import Device
from zigpy.quirks.registry import DeviceRegistry
from zigpy.quirks.v2 import CustomDeviceV2
import zigpy.types as t
Expand All @@ -25,30 +24,9 @@
)
from zhaquirks.tuya.mcu import TuyaMCUCluster, TuyaOnOffNM

from .async_mock import sentinel

zhaquirks.setup()


@pytest.fixture(name="device_mock")
def real_device(MockAppController):
"""Device fixture with a single endpoint."""
ieee = sentinel.ieee
nwk = 0x2233
device = Device(MockAppController, ieee, nwk)

device.add_endpoint(1)
device[1].profile_id = 0x0104
device[1].device_type = 0x0051
device.model = "model"
device.manufacturer = "manufacturer"
device[1].add_input_cluster(0x0000)
device[1].add_input_cluster(0xEF00)
device[1].add_output_cluster(0x000A)
device[1].add_output_cluster(0x0019)
return device


@pytest.mark.parametrize(
"method_name,attr_name,exp_class",
[
Expand Down

0 comments on commit 57bcec8

Please sign in to comment.