hass-core/tests/components/zha/test_fan.py

136 lines
4.3 KiB
Python
Raw Normal View History

"""Test zha fan."""
from unittest.mock import call, patch
import pytest
import zigpy.zcl.clusters.hvac as hvac
import zigpy.zcl.foundation as zcl_f
from homeassistant.components import fan
2019-07-31 12:25:30 -07:00
from homeassistant.components.fan import ATTR_SPEED, DOMAIN, SERVICE_SET_SPEED
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_OFF,
STATE_ON,
STATE_UNAVAILABLE,
)
from .common import (
async_enable_traffic,
find_entity_id,
2019-07-31 12:25:30 -07:00
make_attribute,
make_zcl_header,
)
from tests.common import mock_coro
@pytest.fixture
def zigpy_device(zigpy_device_mock):
"""Device tracker zigpy device."""
endpoints = {
1: {"in_clusters": [hvac.Fan.cluster_id], "out_clusters": [], "device_type": 0}
}
return zigpy_device_mock(endpoints)
async def test_fan(hass, zha_gateway, zha_device_joined_restored, zigpy_device):
"""Test zha fan platform."""
zha_device = await zha_device_joined_restored(zigpy_device)
cluster = zigpy_device.endpoints.get(1).fan
entity_id = await find_entity_id(DOMAIN, zha_device, hass)
assert entity_id is not None
# test that the fan was created and that it is unavailable
assert hass.states.get(entity_id).state == STATE_UNAVAILABLE
# allow traffic to flow through the gateway and device
await async_enable_traffic(hass, zha_gateway, [zha_device])
# test that the state has changed from unavailable to off
assert hass.states.get(entity_id).state == STATE_OFF
# turn on at fan
attr = make_attribute(0, 1)
hdr = make_zcl_header(zcl_f.Command.Report_Attributes)
cluster.handle_message(hdr, [[attr]])
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_ON
# turn off at fan
attr.value.value = 0
cluster.handle_message(hdr, [[attr]])
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_OFF
# turn on from HA
with patch(
2019-07-31 12:25:30 -07:00
"zigpy.zcl.Cluster.write_attributes",
return_value=mock_coro([zcl_f.Status.SUCCESS, zcl_f.Status.SUCCESS]),
2019-07-31 12:25:30 -07:00
):
# turn on via UI
await async_turn_on(hass, entity_id)
assert len(cluster.write_attributes.mock_calls) == 1
2019-07-31 12:25:30 -07:00
assert cluster.write_attributes.call_args == call({"fan_mode": 2})
# turn off from HA
with patch(
2019-07-31 12:25:30 -07:00
"zigpy.zcl.Cluster.write_attributes",
return_value=mock_coro([zcl_f.Status.SUCCESS, zcl_f.Status.SUCCESS]),
2019-07-31 12:25:30 -07:00
):
# turn off via UI
await async_turn_off(hass, entity_id)
assert len(cluster.write_attributes.mock_calls) == 1
2019-07-31 12:25:30 -07:00
assert cluster.write_attributes.call_args == call({"fan_mode": 0})
# change speed from HA
with patch(
2019-07-31 12:25:30 -07:00
"zigpy.zcl.Cluster.write_attributes",
return_value=mock_coro([zcl_f.Status.SUCCESS, zcl_f.Status.SUCCESS]),
2019-07-31 12:25:30 -07:00
):
# turn on via UI
await async_set_speed(hass, entity_id, speed=fan.SPEED_HIGH)
assert len(cluster.write_attributes.mock_calls) == 1
2019-07-31 12:25:30 -07:00
assert cluster.write_attributes.call_args == call({"fan_mode": 3})
# test adding new fan to the network and HA
cluster.bind.reset_mock()
cluster.configure_reporting.reset_mock()
await zha_gateway.async_device_initialized(zigpy_device)
await hass.async_block_till_done()
assert cluster.bind.call_count == 1
assert cluster.bind.await_count == 1
assert cluster.configure_reporting.call_count == 1
assert cluster.configure_reporting.await_count == 1
async def async_turn_on(hass, entity_id, speed=None):
"""Turn fan on."""
data = {
2019-07-31 12:25:30 -07:00
key: value
for key, value in [(ATTR_ENTITY_ID, entity_id), (ATTR_SPEED, speed)]
if value is not None
}
2019-07-31 12:25:30 -07:00
await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data, blocking=True)
async def async_turn_off(hass, entity_id):
"""Turn fan off."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
2019-07-31 12:25:30 -07:00
await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data, blocking=True)
async def async_set_speed(hass, entity_id, speed=None):
"""Set speed for specified fan."""
data = {
2019-07-31 12:25:30 -07:00
key: value
for key, value in [(ATTR_ENTITY_ID, entity_id), (ATTR_SPEED, speed)]
if value is not None
}
2019-07-31 12:25:30 -07:00
await hass.services.async_call(DOMAIN, SERVICE_SET_SPEED, data, blocking=True)