2019-02-04 11:51:32 +00:00
|
|
|
"""Test zha fan."""
|
2020-02-12 21:12:14 +00:00
|
|
|
from unittest.mock import call
|
2019-10-21 17:14:17 +00:00
|
|
|
|
2020-02-10 02:45:35 +00:00
|
|
|
import pytest
|
2019-10-21 23:30:56 +00:00
|
|
|
import zigpy.zcl.clusters.hvac as hvac
|
2019-10-21 17:14:17 +00:00
|
|
|
|
2019-02-04 11:51:32 +00:00
|
|
|
from homeassistant.components import fan
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.components.fan import ATTR_SPEED, DOMAIN, SERVICE_SET_SPEED
|
2019-10-21 17:14:17 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
SERVICE_TURN_OFF,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
STATE_OFF,
|
|
|
|
STATE_ON,
|
|
|
|
STATE_UNAVAILABLE,
|
|
|
|
)
|
|
|
|
|
2019-02-04 11:51:32 +00:00
|
|
|
from .common import (
|
2019-10-21 17:14:17 +00:00
|
|
|
async_enable_traffic,
|
2020-02-12 21:12:14 +00:00
|
|
|
async_test_rejoin,
|
2019-10-31 16:31:06 +00:00
|
|
|
find_entity_id,
|
2020-03-13 23:17:50 +00:00
|
|
|
send_attributes_report,
|
2019-02-04 11:51:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-02-10 02:45:35 +00:00
|
|
|
@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)
|
2019-02-04 11:51:32 +00:00
|
|
|
|
|
|
|
|
2020-02-12 21:12:14 +00:00
|
|
|
async def test_fan(hass, zha_device_joined_restored, zigpy_device):
|
2020-02-10 02:45:35 +00:00
|
|
|
"""Test zha fan platform."""
|
2019-02-04 11:51:32 +00:00
|
|
|
|
2020-02-10 02:45:35 +00:00
|
|
|
zha_device = await zha_device_joined_restored(zigpy_device)
|
2019-02-04 11:51:32 +00:00
|
|
|
cluster = zigpy_device.endpoints.get(1).fan
|
2019-10-31 16:31:06 +00:00
|
|
|
entity_id = await find_entity_id(DOMAIN, zha_device, hass)
|
|
|
|
assert entity_id is not None
|
2019-02-04 11:51:32 +00:00
|
|
|
|
2019-02-07 08:14:19 +00:00
|
|
|
# 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
|
2020-02-12 21:12:14 +00:00
|
|
|
await async_enable_traffic(hass, [zha_device])
|
2019-02-07 08:14:19 +00:00
|
|
|
|
|
|
|
# test that the state has changed from unavailable to off
|
2019-02-04 11:51:32 +00:00
|
|
|
assert hass.states.get(entity_id).state == STATE_OFF
|
|
|
|
|
|
|
|
# turn on at fan
|
2020-03-13 23:17:50 +00:00
|
|
|
await send_attributes_report(hass, cluster, {1: 2, 0: 1, 2: 3})
|
2019-02-04 11:51:32 +00:00
|
|
|
assert hass.states.get(entity_id).state == STATE_ON
|
|
|
|
|
|
|
|
# turn off at fan
|
2020-03-13 23:17:50 +00:00
|
|
|
await send_attributes_report(hass, cluster, {1: 1, 0: 0, 2: 2})
|
2019-02-04 11:51:32 +00:00
|
|
|
assert hass.states.get(entity_id).state == STATE_OFF
|
|
|
|
|
|
|
|
# turn on from HA
|
2020-02-12 21:12:14 +00:00
|
|
|
cluster.write_attributes.reset_mock()
|
|
|
|
await async_turn_on(hass, entity_id)
|
|
|
|
assert len(cluster.write_attributes.mock_calls) == 1
|
|
|
|
assert cluster.write_attributes.call_args == call({"fan_mode": 2})
|
2019-02-04 11:51:32 +00:00
|
|
|
|
|
|
|
# turn off from HA
|
2020-02-12 21:12:14 +00:00
|
|
|
cluster.write_attributes.reset_mock()
|
|
|
|
await async_turn_off(hass, entity_id)
|
|
|
|
assert len(cluster.write_attributes.mock_calls) == 1
|
|
|
|
assert cluster.write_attributes.call_args == call({"fan_mode": 0})
|
2019-02-04 11:51:32 +00:00
|
|
|
|
|
|
|
# change speed from HA
|
2020-02-12 21:12:14 +00:00
|
|
|
cluster.write_attributes.reset_mock()
|
|
|
|
await async_set_speed(hass, entity_id, speed=fan.SPEED_HIGH)
|
|
|
|
assert len(cluster.write_attributes.mock_calls) == 1
|
|
|
|
assert cluster.write_attributes.call_args == call({"fan_mode": 3})
|
2019-02-04 11:51:32 +00:00
|
|
|
|
|
|
|
# test adding new fan to the network and HA
|
2020-02-12 21:12:14 +00:00
|
|
|
await async_test_rejoin(hass, zigpy_device, [cluster], (1,))
|
2019-02-04 11:51:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_turn_on(hass, entity_id, speed=None):
|
|
|
|
"""Turn fan on."""
|
|
|
|
data = {
|
2019-07-31 19:25:30 +00:00
|
|
|
key: value
|
|
|
|
for key, value in [(ATTR_ENTITY_ID, entity_id), (ATTR_SPEED, speed)]
|
|
|
|
if value is not None
|
2019-02-04 11:51:32 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data, blocking=True)
|
2019-02-04 11:51:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_turn_off(hass, entity_id):
|
|
|
|
"""Turn fan off."""
|
|
|
|
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data, blocking=True)
|
2019-02-04 11:51:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_set_speed(hass, entity_id, speed=None):
|
|
|
|
"""Set speed for specified fan."""
|
|
|
|
data = {
|
2019-07-31 19:25:30 +00:00
|
|
|
key: value
|
|
|
|
for key, value in [(ATTR_ENTITY_ID, entity_id), (ATTR_SPEED, speed)]
|
|
|
|
if value is not None
|
2019-02-04 11:51:32 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.services.async_call(DOMAIN, SERVICE_SET_SPEED, data, blocking=True)
|