1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-25 05:26:47 +00:00

Update zha to use new fan entity model (#45758)

* Update zha to use new fan entity model

* fixes

* tweaks for zha

* pylint

* augment test cover
This commit is contained in:
J. Nick Koston
2021-02-17 08:03:11 -10:00
committed by GitHub
parent 4ab0151fb1
commit b2df9aaaf1
2 changed files with 220 additions and 72 deletions

View File

@@ -2,6 +2,7 @@
from unittest.mock import AsyncMock, call, patch
import pytest
from zigpy.exceptions import ZigbeeException
import zigpy.profiles.zha as zha
import zigpy.zcl.clusters.general as general
import zigpy.zcl.clusters.hvac as hvac
@@ -9,8 +10,11 @@ import zigpy.zcl.foundation as zcl_f
from homeassistant.components import fan
from homeassistant.components.fan import (
ATTR_PERCENTAGE,
ATTR_PRESET_MODE,
ATTR_SPEED,
DOMAIN,
SERVICE_SET_PRESET_MODE,
SERVICE_SET_SPEED,
SPEED_HIGH,
SPEED_LOW,
@@ -20,6 +24,11 @@ from homeassistant.components.fan import (
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
from homeassistant.components.zha.core.discovery import GROUP_PROBE
from homeassistant.components.zha.core.group import GroupMember
from homeassistant.components.zha.fan import (
PRESET_MODE_AUTO,
PRESET_MODE_ON,
PRESET_MODE_SMART,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TURN_OFF,
@@ -173,6 +182,12 @@ async def test_fan(hass, zha_device_joined_restored, zigpy_device):
assert len(cluster.write_attributes.mock_calls) == 1
assert cluster.write_attributes.call_args == call({"fan_mode": 3})
# change preset_mode from HA
cluster.write_attributes.reset_mock()
await async_set_preset_mode(hass, entity_id, preset_mode=PRESET_MODE_ON)
assert len(cluster.write_attributes.mock_calls) == 1
assert cluster.write_attributes.call_args == call({"fan_mode": 4})
# test adding new fan to the network and HA
await async_test_rejoin(hass, zigpy_device, [cluster], (1,))
@@ -206,6 +221,17 @@ async def async_set_speed(hass, entity_id, speed=None):
await hass.services.async_call(DOMAIN, SERVICE_SET_SPEED, data, blocking=True)
async def async_set_preset_mode(hass, entity_id, preset_mode=None):
"""Set preset_mode for specified fan."""
data = {
key: value
for key, value in [(ATTR_ENTITY_ID, entity_id), (ATTR_PRESET_MODE, preset_mode)]
if value is not None
}
await hass.services.async_call(DOMAIN, SERVICE_SET_PRESET_MODE, data, blocking=True)
@patch(
"zigpy.zcl.clusters.hvac.Fan.write_attributes",
new=AsyncMock(return_value=zcl_f.WriteAttributesResponse.deserialize(b"\x00")[0]),
@@ -276,6 +302,24 @@ async def test_zha_group_fan_entity(hass, device_fan_1, device_fan_2, coordinato
assert len(group_fan_cluster.write_attributes.mock_calls) == 1
assert group_fan_cluster.write_attributes.call_args[0][0] == {"fan_mode": 3}
# change preset mode from HA
group_fan_cluster.write_attributes.reset_mock()
await async_set_preset_mode(hass, entity_id, preset_mode=PRESET_MODE_ON)
assert len(group_fan_cluster.write_attributes.mock_calls) == 1
assert group_fan_cluster.write_attributes.call_args[0][0] == {"fan_mode": 4}
# change preset mode from HA
group_fan_cluster.write_attributes.reset_mock()
await async_set_preset_mode(hass, entity_id, preset_mode=PRESET_MODE_AUTO)
assert len(group_fan_cluster.write_attributes.mock_calls) == 1
assert group_fan_cluster.write_attributes.call_args[0][0] == {"fan_mode": 5}
# change preset mode from HA
group_fan_cluster.write_attributes.reset_mock()
await async_set_preset_mode(hass, entity_id, preset_mode=PRESET_MODE_SMART)
assert len(group_fan_cluster.write_attributes.mock_calls) == 1
assert group_fan_cluster.write_attributes.call_args[0][0] == {"fan_mode": 6}
# test some of the group logic to make sure we key off states correctly
await send_attributes_report(hass, dev1_fan_cluster, {0: 0})
await send_attributes_report(hass, dev2_fan_cluster, {0: 0})
@@ -296,14 +340,74 @@ async def test_zha_group_fan_entity(hass, device_fan_1, device_fan_2, coordinato
assert hass.states.get(entity_id).state == STATE_OFF
@patch(
"zigpy.zcl.clusters.hvac.Fan.write_attributes",
new=AsyncMock(side_effect=ZigbeeException),
)
async def test_zha_group_fan_entity_failure_state(
hass, device_fan_1, device_fan_2, coordinator, caplog
):
"""Test the fan entity for a ZHA group when writing attributes generates an exception."""
zha_gateway = get_zha_gateway(hass)
assert zha_gateway is not None
zha_gateway.coordinator_zha_device = coordinator
coordinator._zha_gateway = zha_gateway
device_fan_1._zha_gateway = zha_gateway
device_fan_2._zha_gateway = zha_gateway
member_ieee_addresses = [device_fan_1.ieee, device_fan_2.ieee]
members = [GroupMember(device_fan_1.ieee, 1), GroupMember(device_fan_2.ieee, 1)]
# test creating a group with 2 members
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.device.ieee in member_ieee_addresses
assert member.group == zha_group
assert member.endpoint is not None
entity_domains = GROUP_PROBE.determine_entity_domains(hass, zha_group)
assert len(entity_domains) == 2
assert LIGHT_DOMAIN in entity_domains
assert DOMAIN in entity_domains
entity_id = async_find_group_entity_id(hass, DOMAIN, zha_group)
assert hass.states.get(entity_id) is not None
group_fan_cluster = zha_group.endpoint[hvac.Fan.cluster_id]
await async_enable_traffic(hass, [device_fan_1, device_fan_2], enabled=False)
await hass.async_block_till_done()
# test that the fans were created and that they are unavailable
assert hass.states.get(entity_id).state == STATE_UNAVAILABLE
# allow traffic to flow through the gateway and device
await async_enable_traffic(hass, [device_fan_1, device_fan_2])
# test that the fan group entity was created and is off
assert hass.states.get(entity_id).state == STATE_OFF
# turn on from HA
group_fan_cluster.write_attributes.reset_mock()
await async_turn_on(hass, entity_id)
await hass.async_block_till_done()
assert len(group_fan_cluster.write_attributes.mock_calls) == 1
assert group_fan_cluster.write_attributes.call_args[0][0] == {"fan_mode": 2}
assert "Could not set fan mode" in caplog.text
@pytest.mark.parametrize(
"plug_read, expected_state, expected_speed",
"plug_read, expected_state, expected_speed, expected_percentage",
(
(None, STATE_OFF, None),
({"fan_mode": 0}, STATE_OFF, SPEED_OFF),
({"fan_mode": 1}, STATE_ON, SPEED_LOW),
({"fan_mode": 2}, STATE_ON, SPEED_MEDIUM),
({"fan_mode": 3}, STATE_ON, SPEED_HIGH),
(None, STATE_OFF, None, None),
({"fan_mode": 0}, STATE_OFF, SPEED_OFF, 0),
({"fan_mode": 1}, STATE_ON, SPEED_LOW, 33),
({"fan_mode": 2}, STATE_ON, SPEED_MEDIUM, 66),
({"fan_mode": 3}, STATE_ON, SPEED_HIGH, 100),
),
)
async def test_fan_init(
@@ -313,6 +417,7 @@ async def test_fan_init(
plug_read,
expected_state,
expected_speed,
expected_percentage,
):
"""Test zha fan platform."""
@@ -324,6 +429,8 @@ async def test_fan_init(
assert entity_id is not None
assert hass.states.get(entity_id).state == expected_state
assert hass.states.get(entity_id).attributes[ATTR_SPEED] == expected_speed
assert hass.states.get(entity_id).attributes[ATTR_PERCENTAGE] == expected_percentage
assert hass.states.get(entity_id).attributes[ATTR_PRESET_MODE] is None
async def test_fan_update_entity(
@@ -341,6 +448,8 @@ async def test_fan_update_entity(
assert entity_id is not None
assert hass.states.get(entity_id).state == STATE_OFF
assert hass.states.get(entity_id).attributes[ATTR_SPEED] == SPEED_OFF
assert hass.states.get(entity_id).attributes[ATTR_PERCENTAGE] == 0
assert hass.states.get(entity_id).attributes[ATTR_PRESET_MODE] is None
assert cluster.read_attributes.await_count == 1
await async_setup_component(hass, "homeassistant", {})
@@ -358,5 +467,7 @@ async def test_fan_update_entity(
"homeassistant", "update_entity", {"entity_id": entity_id}, blocking=True
)
assert hass.states.get(entity_id).state == STATE_ON
assert hass.states.get(entity_id).attributes[ATTR_PERCENTAGE] == 33
assert hass.states.get(entity_id).attributes[ATTR_SPEED] == SPEED_LOW
assert hass.states.get(entity_id).attributes[ATTR_PRESET_MODE] is None
assert cluster.read_attributes.await_count == 3