Add ZHA group API (#29641)
* add skeleton to retrieve zigbee groups * get single group * add a group * return group members with group * add comment * fix group members * add function to add device to group * add group members * add remove from group method * add api to remove members from group * add remove groups method * clean up group add and remove * fix remove group * fix remove groups * add api to get only groupable devices * change var init * add tests * address review comment
This commit is contained in:
1 parent
7f948594eb
commit
1222aa8c56
6 files changed
+461
-3
No files matched your search
@@ -93,6 +93,8 @@ class FakeDevice:
|
||||
self.manufacturer = manufacturer
|
||||
self.model = model
|
||||
self.node_desc = zigpy.zdo.types.NodeDescriptor()
|
||||
self.add_to_group = CoroutineMock()
|
||||
self.remove_from_group = CoroutineMock()
|
||||
|
||||
|
||||
def make_device(
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
"""Test configuration for the ZHA component."""
|
||||
from unittest import mock
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
import zigpy
|
||||
from zigpy.application import ControllerApplication
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.zha.core.const import COMPONENTS, DATA_ZHA, DOMAIN
|
||||
@@ -12,6 +15,9 @@ from homeassistant.helpers.device_registry import async_get_registry as get_dev_
|
||||
|
||||
from .common import async_setup_entry
|
||||
|
||||
FIXTURE_GRP_ID = 0x1001
|
||||
FIXTURE_GRP_NAME = "fixture group"
|
||||
|
||||
|
||||
@pytest.fixture(name="config_entry")
|
||||
def config_entry_fixture(hass):
|
||||
@@ -43,6 +49,11 @@ async def zha_gateway_fixture(hass, config_entry):
|
||||
gateway = ZHAGateway(hass, {}, config_entry)
|
||||
gateway.zha_storage = zha_storage
|
||||
gateway.ha_device_registry = dev_reg
|
||||
gateway.application_controller = mock.MagicMock(spec_set=ControllerApplication)
|
||||
groups = zigpy.group.Groups(gateway.application_controller)
|
||||
groups.listener_event = mock.MagicMock()
|
||||
groups.add_group(FIXTURE_GRP_ID, FIXTURE_GRP_NAME, suppress_event=True)
|
||||
gateway.application_controller.groups = groups
|
||||
return gateway
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
"""Test ZHA API."""
|
||||
|
||||
import pytest
|
||||
import zigpy
|
||||
import zigpy.zcl.clusters.general as general
|
||||
|
||||
from homeassistant.components.light import DOMAIN as light_domain
|
||||
from homeassistant.components.switch import DOMAIN
|
||||
from homeassistant.components.websocket_api import const
|
||||
from homeassistant.components.zha.api import ID, TYPE, async_load_api
|
||||
@@ -15,9 +18,13 @@ from homeassistant.components.zha.core.const import (
|
||||
ATTR_NAME,
|
||||
ATTR_QUIRK_APPLIED,
|
||||
CLUSTER_TYPE_IN,
|
||||
GROUP_ID,
|
||||
GROUP_IDS,
|
||||
GROUP_NAME,
|
||||
)
|
||||
|
||||
from .common import async_init_zigpy_device
|
||||
from .conftest import FIXTURE_GRP_ID, FIXTURE_GRP_NAME
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -36,9 +43,22 @@ async def zha_client(hass, config_entry, zha_gateway, hass_ws_client):
|
||||
zha_gateway,
|
||||
)
|
||||
|
||||
await async_init_zigpy_device(
|
||||
hass,
|
||||
[general.OnOff.cluster_id, general.Basic.cluster_id, general.Groups.cluster_id],
|
||||
[],
|
||||
zigpy.profiles.zha.DeviceType.ON_OFF_LIGHT,
|
||||
zha_gateway,
|
||||
manufacturer="FakeGroupManufacturer",
|
||||
model="FakeGroupModel",
|
||||
ieee="01:2d:6f:00:0a:90:69:e8",
|
||||
)
|
||||
|
||||
# load up switch domain
|
||||
await hass.config_entries.async_forward_entry_setup(config_entry, DOMAIN)
|
||||
await hass.async_block_till_done()
|
||||
await hass.config_entries.async_forward_entry_setup(config_entry, light_domain)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return await hass_ws_client(hass)
|
||||
|
||||
@@ -114,15 +134,17 @@ async def test_device_cluster_commands(hass, config_entry, zha_gateway, zha_clie
|
||||
|
||||
|
||||
async def test_list_devices(hass, config_entry, zha_gateway, zha_client):
|
||||
"""Test getting entity cluster commands."""
|
||||
"""Test getting zha devices."""
|
||||
await zha_client.send_json({ID: 5, TYPE: "zha/devices"})
|
||||
|
||||
msg = await zha_client.receive_json()
|
||||
|
||||
devices = msg["result"]
|
||||
assert len(devices) == 1
|
||||
assert len(devices) == 2
|
||||
|
||||
msg_id = 100
|
||||
for device in devices:
|
||||
msg_id += 1
|
||||
assert device[ATTR_IEEE] is not None
|
||||
assert device[ATTR_MANUFACTURER] is not None
|
||||
assert device[ATTR_MODEL] is not None
|
||||
@@ -135,7 +157,7 @@ async def test_list_devices(hass, config_entry, zha_gateway, zha_client):
|
||||
assert entity_reference["entity_id"] is not None
|
||||
|
||||
await zha_client.send_json(
|
||||
{ID: 6, TYPE: "zha/device", ATTR_IEEE: device[ATTR_IEEE]}
|
||||
{ID: msg_id, TYPE: "zha/device", ATTR_IEEE: device[ATTR_IEEE]}
|
||||
)
|
||||
msg = await zha_client.receive_json()
|
||||
device2 = msg["result"]
|
||||
@@ -152,3 +174,151 @@ async def test_device_not_found(hass, config_entry, zha_gateway, zha_client):
|
||||
assert msg["type"] == const.TYPE_RESULT
|
||||
assert not msg["success"]
|
||||
assert msg["error"]["code"] == const.ERR_NOT_FOUND
|
||||
|
||||
|
||||
async def test_list_groups(hass, config_entry, zha_gateway, zha_client):
|
||||
"""Test getting zha zigbee groups."""
|
||||
await zha_client.send_json({ID: 7, TYPE: "zha/groups"})
|
||||
|
||||
msg = await zha_client.receive_json()
|
||||
assert msg["id"] == 7
|
||||
assert msg["type"] == const.TYPE_RESULT
|
||||
|
||||
groups = msg["result"]
|
||||
assert len(groups) == 1
|
||||
|
||||
for group in groups:
|
||||
assert group["group_id"] == FIXTURE_GRP_ID
|
||||
assert group["name"] == FIXTURE_GRP_NAME
|
||||
assert group["members"] == []
|
||||
|
||||
|
||||
async def test_get_group(hass, config_entry, zha_gateway, zha_client):
|
||||
"""Test getting a specific zha zigbee group."""
|
||||
await zha_client.send_json({ID: 8, TYPE: "zha/group", GROUP_ID: FIXTURE_GRP_ID})
|
||||
|
||||
msg = await zha_client.receive_json()
|
||||
assert msg["id"] == 8
|
||||
assert msg["type"] == const.TYPE_RESULT
|
||||
|
||||
group = msg["result"]
|
||||
assert group is not None
|
||||
assert group["group_id"] == FIXTURE_GRP_ID
|
||||
assert group["name"] == FIXTURE_GRP_NAME
|
||||
assert group["members"] == []
|
||||
|
||||
|
||||
async def test_get_group_not_found(hass, config_entry, zha_gateway, zha_client):
|
||||
"""Test not found response from get group API."""
|
||||
await zha_client.send_json({ID: 9, TYPE: "zha/group", GROUP_ID: 1234567})
|
||||
|
||||
msg = await zha_client.receive_json()
|
||||
|
||||
assert msg["id"] == 9
|
||||
assert msg["type"] == const.TYPE_RESULT
|
||||
assert not msg["success"]
|
||||
assert msg["error"]["code"] == const.ERR_NOT_FOUND
|
||||
|
||||
|
||||
async def test_list_groupable_devices(hass, config_entry, zha_gateway, zha_client):
|
||||
"""Test getting zha devices that have a group cluster."""
|
||||
|
||||
# Make device available
|
||||
zha_gateway.devices[
|
||||
zigpy.types.EUI64.convert("01:2d:6f:00:0a:90:69:e8")
|
||||
].set_available(True)
|
||||
|
||||
await zha_client.send_json({ID: 10, TYPE: "zha/devices/groupable"})
|
||||
|
||||
msg = await zha_client.receive_json()
|
||||
assert msg["id"] == 10
|
||||
assert msg["type"] == const.TYPE_RESULT
|
||||
|
||||
devices = msg["result"]
|
||||
assert len(devices) == 1
|
||||
|
||||
for device in devices:
|
||||
assert device[ATTR_IEEE] == "01:2d:6f:00:0a:90:69:e8"
|
||||
assert device[ATTR_MANUFACTURER] is not None
|
||||
assert device[ATTR_MODEL] is not None
|
||||
assert device[ATTR_NAME] is not None
|
||||
assert device[ATTR_QUIRK_APPLIED] is not None
|
||||
assert device["entities"] is not None
|
||||
|
||||
for entity_reference in device["entities"]:
|
||||
assert entity_reference[ATTR_NAME] is not None
|
||||
assert entity_reference["entity_id"] is not None
|
||||
|
||||
# Make sure there are no groupable devices when the device is unavailable
|
||||
# Make device unavailable
|
||||
zha_gateway.devices[
|
||||
zigpy.types.EUI64.convert("01:2d:6f:00:0a:90:69:e8")
|
||||
].set_available(False)
|
||||
|
||||
await zha_client.send_json({ID: 11, TYPE: "zha/devices/groupable"})
|
||||
|
||||
msg = await zha_client.receive_json()
|
||||
assert msg["id"] == 11
|
||||
assert msg["type"] == const.TYPE_RESULT
|
||||
|
||||
devices = msg["result"]
|
||||
assert len(devices) == 0
|
||||
|
||||
|
||||
async def test_add_group(hass, config_entry, zha_gateway, zha_client):
|
||||
"""Test adding and getting a new zha zigbee group."""
|
||||
await zha_client.send_json({ID: 12, TYPE: "zha/group/add", GROUP_NAME: "new_group"})
|
||||
|
||||
msg = await zha_client.receive_json()
|
||||
assert msg["id"] == 12
|
||||
assert msg["type"] == const.TYPE_RESULT
|
||||
|
||||
added_group = msg["result"]
|
||||
|
||||
assert added_group["name"] == "new_group"
|
||||
assert added_group["members"] == []
|
||||
|
||||
await zha_client.send_json({ID: 13, TYPE: "zha/groups"})
|
||||
|
||||
msg = await zha_client.receive_json()
|
||||
assert msg["id"] == 13
|
||||
assert msg["type"] == const.TYPE_RESULT
|
||||
|
||||
groups = msg["result"]
|
||||
assert len(groups) == 2
|
||||
|
||||
for group in groups:
|
||||
assert group["name"] == FIXTURE_GRP_NAME or group["name"] == "new_group"
|
||||
|
||||
|
||||
async def test_remove_group(hass, config_entry, zha_gateway, zha_client):
|
||||
"""Test removing a new zha zigbee group."""
|
||||
|
||||
await zha_client.send_json({ID: 14, TYPE: "zha/groups"})
|
||||
|
||||
msg = await zha_client.receive_json()
|
||||
assert msg["id"] == 14
|
||||
assert msg["type"] == const.TYPE_RESULT
|
||||
|
||||
groups = msg["result"]
|
||||
assert len(groups) == 1
|
||||
|
||||
await zha_client.send_json(
|
||||
{ID: 15, TYPE: "zha/group/remove", GROUP_IDS: [FIXTURE_GRP_ID]}
|
||||
)
|
||||
|
||||
msg = await zha_client.receive_json()
|
||||
assert msg["id"] == 15
|
||||
assert msg["type"] == const.TYPE_RESULT
|
||||
|
||||
groups_remaining = msg["result"]
|
||||
assert len(groups_remaining) == 0
|
||||
|
||||
await zha_client.send_json({ID: 16, TYPE: "zha/groups"})
|
||||
|
||||
msg = await zha_client.receive_json()
|
||||
assert msg["id"] == 16
|
||||
assert msg["type"] == const.TYPE_RESULT
|
||||
|
||||
groups = msg["result"]
|
||||
assert len(groups) == 0
|
||||
Reference in new issue
Block a user