Make the rest of ZHA platforms to use ZHA class registry (#30261)

* Refactor ZHA component tests fixtures.

* Add tests for ZHA device discovery.

* Refactor ZHA registry MatchRule.

Allow callables as a matching criteria.
Allow sets for model & manufacturer.

* Minor ZHA class registry refactoring.

Less cluttered strict_matching registrations.

* Add entities only if there are any.

* Migrate rest of ZHA platforms to ZHA registry.

* Pylint fixes.
This commit is contained in:
Alexei Chetroi 2019-12-31 11:09:58 -05:00 committed by David F. Mulcahey
parent 5ed44297e6
commit a3061bda60
13 changed files with 2152 additions and 80 deletions

View file

@ -36,10 +36,10 @@ APPLICATION = FakeApplication()
class FakeEndpoint:
"""Fake endpoint for moking zigpy."""
def __init__(self, manufacturer, model):
def __init__(self, manufacturer, model, epid=1):
"""Init fake endpoint."""
self.device = None
self.endpoint_id = 1
self.endpoint_id = epid
self.in_clusters = {}
self.out_clusters = {}
self._cluster_attr = {}
@ -97,21 +97,23 @@ class FakeDevice:
self.remove_from_group = CoroutineMock()
def make_device(
in_cluster_ids, out_cluster_ids, device_type, ieee, manufacturer, model
):
def make_device(endpoints, ieee, manufacturer, model):
"""Make a fake device using the specified cluster classes."""
device = FakeDevice(ieee, manufacturer, model)
endpoint = FakeEndpoint(manufacturer, model)
endpoint.device = device
device.endpoints[endpoint.endpoint_id] = endpoint
endpoint.device_type = device_type
for epid, ep in endpoints.items():
endpoint = FakeEndpoint(manufacturer, model, epid)
endpoint.device = device
device.endpoints[epid] = endpoint
endpoint.device_type = ep["device_type"]
profile_id = ep.get("profile_id")
if profile_id:
endpoint.profile_id = profile_id
for cluster_id in in_cluster_ids:
endpoint.add_input_cluster(cluster_id)
for cluster_id in ep.get("in_clusters", []):
endpoint.add_input_cluster(cluster_id)
for cluster_id in out_cluster_ids:
endpoint.add_output_cluster(cluster_id)
for cluster_id in ep.get("out_clusters", []):
endpoint.add_output_cluster(cluster_id)
return device
@ -136,7 +138,16 @@ async def async_init_zigpy_device(
happens when the device is paired to the network for the first time.
"""
device = make_device(
in_cluster_ids, out_cluster_ids, device_type, ieee, manufacturer, model
{
1: {
"in_clusters": in_cluster_ids,
"out_clusters": out_cluster_ids,
"device_type": device_type,
}
},
ieee,
manufacturer,
model,
)
if is_new_join:
await gateway.async_device_initialized(device)

View file

@ -67,9 +67,7 @@ def nwk():
async def test_in_channel_config(cluster_id, bind_count, attrs, zha_gateway, hass):
"""Test ZHA core channel configuration for input clusters."""
zigpy_dev = make_device(
[cluster_id],
[],
0x1234,
{1: {"in_clusters": [cluster_id], "out_clusters": [], "device_type": 0x1234}},
"00:11:22:33:44:55:66:77",
"test manufacturer",
"test model",
@ -125,9 +123,7 @@ async def test_in_channel_config(cluster_id, bind_count, attrs, zha_gateway, has
async def test_out_channel_config(cluster_id, bind_count, zha_gateway, hass):
"""Test ZHA core channel configuration for output clusters."""
zigpy_dev = make_device(
[],
[cluster_id],
0x1234,
{1: {"out_clusters": [cluster_id], "in_clusters": [], "device_type": 0x1234}},
"00:11:22:33:44:55:66:77",
"test manufacturer",
"test model",

View file

@ -0,0 +1,55 @@
"""Test zha device discovery."""
import asyncio
from unittest import mock
import pytest
from homeassistant.components.zha.core.channels import EventRelayChannel
import homeassistant.components.zha.core.const as zha_const
import homeassistant.components.zha.core.discovery as disc
import homeassistant.components.zha.core.gateway as core_zha_gw
from .common import make_device
from .zha_devices_list import DEVICES
@pytest.mark.parametrize("device", DEVICES)
async def test_devices(device, zha_gateway: core_zha_gw.ZHAGateway, hass, config_entry):
"""Test device discovery."""
zigpy_device = make_device(
device["endpoints"],
"00:11:22:33:44:55:66:77",
device["manufacturer"],
device["model"],
)
with mock.patch(
"homeassistant.components.zha.core.discovery._async_create_cluster_channel",
wraps=disc._async_create_cluster_channel,
) as cr_ch:
await zha_gateway.async_device_restored(zigpy_device)
await hass.async_block_till_done()
tasks = [
hass.config_entries.async_forward_entry_setup(config_entry, component)
for component in zha_const.COMPONENTS
]
await asyncio.gather(*tasks)
await hass.async_block_till_done()
entity_ids = hass.states.async_entity_ids()
await hass.async_block_till_done()
zha_entities = {
ent for ent in entity_ids if ent.split(".")[0] in zha_const.COMPONENTS
}
event_channels = {
arg[0].cluster_id
for arg, kwarg in cr_ch.call_args_list
if kwarg.get("channel_class") == EventRelayChannel
}
assert zha_entities == set(device["entities"])
assert event_channels == set(device["event_channels"])

View file

@ -59,24 +59,68 @@ def channels():
True,
),
# manufacturer matching
(registries.MatchRule(manufacturer="no match"), False),
(registries.MatchRule(manufacturer=MANUFACTURER), True),
(registries.MatchRule(model=MODEL), True),
(registries.MatchRule(model="no match"), False),
(registries.MatchRule(manufacturers="no match"), False),
(registries.MatchRule(manufacturers=MANUFACTURER), True),
(registries.MatchRule(models=MODEL), True),
(registries.MatchRule(models="no match"), False),
# match everything
(
registries.MatchRule(
generic_ids={"channel_0x0006", "channel_0x0008"},
channel_names={"on_off", "level"},
manufacturer=MANUFACTURER,
model=MODEL,
manufacturers=MANUFACTURER,
models=MODEL,
),
True,
),
(
registries.MatchRule(
channel_names="on_off", manufacturers={"random manuf", MANUFACTURER}
),
True,
),
(
registries.MatchRule(
channel_names="on_off", manufacturers={"random manuf", "Another manuf"}
),
False,
),
(
registries.MatchRule(
channel_names="on_off", manufacturers=lambda x: x == MANUFACTURER
),
True,
),
(
registries.MatchRule(
channel_names="on_off", manufacturers=lambda x: x != MANUFACTURER
),
False,
),
(
registries.MatchRule(
channel_names="on_off", models={"random model", MODEL}
),
True,
),
(
registries.MatchRule(
channel_names="on_off", models={"random model", "Another model"}
),
False,
),
(
registries.MatchRule(channel_names="on_off", models=lambda x: x == MODEL),
True,
),
(
registries.MatchRule(channel_names="on_off", models=lambda x: x != MODEL),
False,
),
],
)
def test_registry_matching(rule, matched, zha_device, channels):
"""Test empty rule matching."""
"""Test strict rule matching."""
reg = registries.ZHAEntityRegistry()
assert reg._strict_matched(zha_device, channels, rule) is matched
@ -92,22 +136,22 @@ def test_registry_matching(rule, matched, zha_device, channels):
(registries.MatchRule(channel_names={"on_off", "level"}), True),
(registries.MatchRule(channel_names={"on_off", "level", "no match"}), False),
(
registries.MatchRule(channel_names={"on_off", "level"}, model="no match"),
registries.MatchRule(channel_names={"on_off", "level"}, models="no match"),
True,
),
(
registries.MatchRule(
channel_names={"on_off", "level"},
model="no match",
manufacturer="no match",
models="no match",
manufacturers="no match",
),
True,
),
(
registries.MatchRule(
channel_names={"on_off", "level"},
model="no match",
manufacturer=MANUFACTURER,
models="no match",
manufacturers=MANUFACTURER,
),
True,
),
@ -124,14 +168,14 @@ def test_registry_matching(rule, matched, zha_device, channels):
(
registries.MatchRule(
generic_ids={"channel_0x0006", "channel_0x0008", "channel_0x0009"},
model="mo match",
models="mo match",
),
False,
),
(
registries.MatchRule(
generic_ids={"channel_0x0006", "channel_0x0008", "channel_0x0009"},
model=MODEL,
models=MODEL,
),
True,
),
@ -143,17 +187,17 @@ def test_registry_matching(rule, matched, zha_device, channels):
True,
),
# manufacturer matching
(registries.MatchRule(manufacturer="no match"), False),
(registries.MatchRule(manufacturer=MANUFACTURER), True),
(registries.MatchRule(model=MODEL), True),
(registries.MatchRule(model="no match"), False),
(registries.MatchRule(manufacturers="no match"), False),
(registries.MatchRule(manufacturers=MANUFACTURER), True),
(registries.MatchRule(models=MODEL), True),
(registries.MatchRule(models="no match"), False),
# match everything
(
registries.MatchRule(
generic_ids={"channel_0x0006", "channel_0x0008"},
channel_names={"on_off", "level"},
manufacturer=MANUFACTURER,
model=MODEL,
manufacturers=MANUFACTURER,
models=MODEL,
),
True,
),

File diff suppressed because it is too large Load diff