Fix ZHA switch group test (#44021)

This commit is contained in:
David F. Mulcahey 2020-12-07 11:43:35 -05:00 committed by GitHub
parent c9c3a8fe38
commit 70133f2096
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,6 +7,7 @@ import zigpy.zcl.clusters.general as general
import zigpy.zcl.foundation as zcl_f
from homeassistant.components.switch import DOMAIN
from homeassistant.components.zha.core.group import GroupMember
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE
from .common import (
@ -67,15 +68,16 @@ async def device_switch_1(hass, zigpy_device_mock, zha_device_joined):
zigpy_device = zigpy_device_mock(
{
1: {
"in_clusters": [general.OnOff.cluster_id],
"in_clusters": [general.OnOff.cluster_id, general.Groups.cluster_id],
"out_clusters": [],
"device_type": zha.DeviceType.COLOR_DIMMABLE_LIGHT,
"device_type": zha.DeviceType.ON_OFF_SWITCH,
}
},
ieee=IEEE_GROUPABLE_DEVICE,
)
zha_device = await zha_device_joined(zigpy_device)
zha_device.available = True
await hass.async_block_till_done()
return zha_device
@ -86,15 +88,16 @@ async def device_switch_2(hass, zigpy_device_mock, zha_device_joined):
zigpy_device = zigpy_device_mock(
{
1: {
"in_clusters": [general.OnOff.cluster_id],
"in_clusters": [general.OnOff.cluster_id, general.Groups.cluster_id],
"out_clusters": [],
"device_type": zha.DeviceType.COLOR_DIMMABLE_LIGHT,
"device_type": zha.DeviceType.ON_OFF_SWITCH,
}
},
ieee=IEEE_GROUPABLE_DEVICE2,
)
zha_device = await zha_device_joined(zigpy_device)
zha_device.available = True
await hass.async_block_till_done()
return zha_device
@ -157,7 +160,7 @@ async def test_switch(hass, zha_device_joined_restored, zigpy_device):
await async_test_rejoin(hass, zigpy_device, [cluster], (1,))
async def async_test_zha_group_switch_entity(
async def test_zha_group_switch_entity(
hass, device_switch_1, device_switch_2, coordinator
):
"""Test the switch entity for a ZHA group."""
@ -168,30 +171,38 @@ async def async_test_zha_group_switch_entity(
device_switch_1._zha_gateway = zha_gateway
device_switch_2._zha_gateway = zha_gateway
member_ieee_addresses = [device_switch_1.ieee, device_switch_2.ieee]
members = [
GroupMember(device_switch_1.ieee, 1),
GroupMember(device_switch_2.ieee, 1),
]
# test creating a group with 2 members
zha_group = await zha_gateway.async_create_zigpy_group(
"Test Group", member_ieee_addresses
)
zha_group = await zha_gateway.async_create_zigpy_group("Test Group", members)
await hass.async_block_till_done()
assert zha_group is not None
assert len(zha_group.members) == 2
for member in zha_group.members:
assert member.ieee in member_ieee_addresses
assert member.device.ieee in member_ieee_addresses
assert member.group == zha_group
assert member.endpoint is not None
entity_id = async_find_group_entity_id(hass, DOMAIN, zha_group)
assert hass.states.get(entity_id) is not None
group_cluster_on_off = zha_group.endpoint[general.OnOff.cluster_id]
dev1_cluster_on_off = device_switch_1.endpoints[1].on_off
dev2_cluster_on_off = device_switch_2.endpoints[1].on_off
dev1_cluster_on_off = device_switch_1.device.endpoints[1].on_off
dev2_cluster_on_off = device_switch_2.device.endpoints[1].on_off
# test that the lights were created and that they are unavailable
await async_enable_traffic(hass, [device_switch_1, device_switch_2], enabled=False)
await hass.async_block_till_done()
# test that the lights were created and that they are off
assert hass.states.get(entity_id).state == STATE_UNAVAILABLE
# allow traffic to flow through the gateway and device
await async_enable_traffic(hass, zha_group.members)
await async_enable_traffic(hass, [device_switch_1, device_switch_2])
await hass.async_block_till_done()
# test that the lights were created and are off
assert hass.states.get(entity_id).state == STATE_OFF
@ -207,7 +218,7 @@ async def async_test_zha_group_switch_entity(
)
assert len(group_cluster_on_off.request.mock_calls) == 1
assert group_cluster_on_off.request.call_args == call(
False, ON, (), expect_reply=True, manufacturer=None, tsn=None
False, ON, (), expect_reply=True, manufacturer=None, tries=1, tsn=None
)
assert hass.states.get(entity_id).state == STATE_ON
@ -222,28 +233,32 @@ async def async_test_zha_group_switch_entity(
)
assert len(group_cluster_on_off.request.mock_calls) == 1
assert group_cluster_on_off.request.call_args == call(
False, OFF, (), expect_reply=True, manufacturer=None, tsn=None
False, OFF, (), expect_reply=True, manufacturer=None, tries=1, tsn=None
)
assert hass.states.get(entity_id).state == STATE_OFF
# test some of the group logic to make sure we key off states correctly
await dev1_cluster_on_off.on()
await dev2_cluster_on_off.on()
await send_attributes_report(hass, dev1_cluster_on_off, {0: 1})
await send_attributes_report(hass, dev2_cluster_on_off, {0: 1})
await hass.async_block_till_done()
# test that group light is on
assert hass.states.get(entity_id).state == STATE_ON
await dev1_cluster_on_off.off()
await send_attributes_report(hass, dev1_cluster_on_off, {0: 0})
await hass.async_block_till_done()
# test that group light is still on
assert hass.states.get(entity_id).state == STATE_ON
await dev2_cluster_on_off.off()
await send_attributes_report(hass, dev2_cluster_on_off, {0: 0})
await hass.async_block_till_done()
# test that group light is now off
assert hass.states.get(entity_id).state == STATE_OFF
await dev1_cluster_on_off.on()
await send_attributes_report(hass, dev1_cluster_on_off, {0: 1})
await hass.async_block_till_done()
# test that group light is now back on
assert hass.states.get(entity_id).state == STATE_ON