2019-02-04 11:51:47 +00:00
|
|
|
"""Test zha light."""
|
2019-02-27 13:34:38 +00:00
|
|
|
import asyncio
|
|
|
|
from unittest.mock import MagicMock, call, patch, sentinel
|
|
|
|
|
2019-10-21 23:30:56 +00:00
|
|
|
import zigpy.profiles.zha
|
|
|
|
import zigpy.types
|
|
|
|
import zigpy.zcl.clusters.general as general
|
|
|
|
import zigpy.zcl.foundation as zcl_f
|
2019-10-21 17:14:17 +00:00
|
|
|
|
2019-02-04 11:51:47 +00:00
|
|
|
from homeassistant.components.light import DOMAIN
|
2019-02-27 13:34:38 +00:00
|
|
|
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE
|
|
|
|
|
2019-02-04 11:51:47 +00:00
|
|
|
from .common import (
|
2019-07-31 19:25:30 +00:00
|
|
|
async_enable_traffic,
|
|
|
|
async_init_zigpy_device,
|
|
|
|
async_test_device_join,
|
2019-10-31 16:31:06 +00:00
|
|
|
find_entity_id,
|
2019-07-31 19:25:30 +00:00
|
|
|
make_attribute,
|
2019-10-21 17:14:17 +00:00
|
|
|
make_zcl_header,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-02-27 13:34:38 +00:00
|
|
|
|
|
|
|
from tests.common import mock_coro
|
2019-02-04 11:51:47 +00:00
|
|
|
|
|
|
|
ON = 1
|
|
|
|
OFF = 0
|
|
|
|
|
|
|
|
|
2019-02-27 13:34:38 +00:00
|
|
|
async def test_light(hass, config_entry, zha_gateway, monkeypatch):
|
2019-02-04 11:51:47 +00:00
|
|
|
"""Test zha light platform."""
|
|
|
|
|
|
|
|
# create zigpy devices
|
|
|
|
zigpy_device_on_off = await async_init_zigpy_device(
|
|
|
|
hass,
|
2019-10-21 23:30:56 +00:00
|
|
|
[general.OnOff.cluster_id, general.Basic.cluster_id],
|
2019-02-04 11:51:47 +00:00
|
|
|
[],
|
2019-10-21 23:30:56 +00:00
|
|
|
zigpy.profiles.zha.DeviceType.ON_OFF_LIGHT,
|
2019-07-31 19:25:30 +00:00
|
|
|
zha_gateway,
|
2019-10-31 16:31:06 +00:00
|
|
|
ieee="00:0d:6f:11:0a:90:69:e6",
|
2019-02-04 11:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
zigpy_device_level = await async_init_zigpy_device(
|
|
|
|
hass,
|
2019-10-21 23:30:56 +00:00
|
|
|
[
|
|
|
|
general.OnOff.cluster_id,
|
|
|
|
general.LevelControl.cluster_id,
|
|
|
|
general.Basic.cluster_id,
|
|
|
|
],
|
2019-02-04 11:51:47 +00:00
|
|
|
[],
|
2019-10-21 23:30:56 +00:00
|
|
|
zigpy.profiles.zha.DeviceType.ON_OFF_LIGHT,
|
2019-02-04 11:51:47 +00:00
|
|
|
zha_gateway,
|
|
|
|
ieee="00:0d:6f:11:0a:90:69:e7",
|
|
|
|
manufacturer="FakeLevelManufacturer",
|
2019-07-31 19:25:30 +00:00
|
|
|
model="FakeLevelModel",
|
2019-02-04 11:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# load up light domain
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setup(config_entry, DOMAIN)
|
2019-02-04 11:51:47 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# on off light
|
|
|
|
on_off_device_on_off_cluster = zigpy_device_on_off.endpoints.get(1).on_off
|
2019-03-28 02:50:52 +00:00
|
|
|
on_off_zha_device = zha_gateway.get_device(zigpy_device_on_off.ieee)
|
2019-10-31 16:31:06 +00:00
|
|
|
on_off_entity_id = await find_entity_id(DOMAIN, on_off_zha_device, hass)
|
|
|
|
assert on_off_entity_id is not None
|
2019-02-04 11:51:47 +00:00
|
|
|
|
|
|
|
# dimmable light
|
|
|
|
level_device_on_off_cluster = zigpy_device_level.endpoints.get(1).on_off
|
|
|
|
level_device_level_cluster = zigpy_device_level.endpoints.get(1).level
|
2019-07-31 19:25:30 +00:00
|
|
|
on_off_mock = MagicMock(
|
|
|
|
side_effect=asyncio.coroutine(
|
2019-10-21 23:30:56 +00:00
|
|
|
MagicMock(return_value=[sentinel.data, zcl_f.Status.SUCCESS])
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
level_mock = MagicMock(
|
|
|
|
side_effect=asyncio.coroutine(
|
2019-10-21 23:30:56 +00:00
|
|
|
MagicMock(return_value=[sentinel.data, zcl_f.Status.SUCCESS])
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
monkeypatch.setattr(level_device_on_off_cluster, "request", on_off_mock)
|
|
|
|
monkeypatch.setattr(level_device_level_cluster, "request", level_mock)
|
2019-03-28 02:50:52 +00:00
|
|
|
level_zha_device = zha_gateway.get_device(zigpy_device_level.ieee)
|
2019-10-31 16:31:06 +00:00
|
|
|
level_entity_id = await find_entity_id(DOMAIN, level_zha_device, hass)
|
|
|
|
assert level_entity_id is not None
|
2019-02-07 08:14:19 +00:00
|
|
|
|
|
|
|
# test that the lights were created and that they are unavailable
|
|
|
|
assert hass.states.get(on_off_entity_id).state == STATE_UNAVAILABLE
|
|
|
|
assert hass.states.get(level_entity_id).state == STATE_UNAVAILABLE
|
|
|
|
|
|
|
|
# allow traffic to flow through the gateway and device
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_enable_traffic(hass, zha_gateway, [on_off_zha_device, level_zha_device])
|
2019-02-04 11:51:47 +00:00
|
|
|
|
|
|
|
# test that the lights were created and are off
|
|
|
|
assert hass.states.get(on_off_entity_id).state == STATE_OFF
|
|
|
|
assert hass.states.get(level_entity_id).state == STATE_OFF
|
|
|
|
|
|
|
|
# test turning the lights on and off from the light
|
|
|
|
await async_test_on_off_from_light(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, on_off_device_on_off_cluster, on_off_entity_id
|
|
|
|
)
|
2019-02-04 11:51:47 +00:00
|
|
|
|
|
|
|
await async_test_on_off_from_light(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, level_device_on_off_cluster, level_entity_id
|
|
|
|
)
|
2019-02-04 11:51:47 +00:00
|
|
|
|
|
|
|
# test turning the lights on and off from the HA
|
|
|
|
await async_test_on_off_from_hass(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, on_off_device_on_off_cluster, on_off_entity_id
|
|
|
|
)
|
2019-02-04 11:51:47 +00:00
|
|
|
|
|
|
|
await async_test_level_on_off_from_hass(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, level_device_on_off_cluster, level_device_level_cluster, level_entity_id
|
|
|
|
)
|
2019-02-04 11:51:47 +00:00
|
|
|
|
|
|
|
# test turning the lights on and off from the light
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_test_on_from_light(hass, level_device_on_off_cluster, level_entity_id)
|
2019-02-04 11:51:47 +00:00
|
|
|
|
|
|
|
# test getting a brightness change from the network
|
|
|
|
await async_test_dimmer_from_light(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, level_device_level_cluster, level_entity_id, 150, STATE_ON
|
|
|
|
)
|
2019-02-04 11:51:47 +00:00
|
|
|
|
|
|
|
# test adding a new light to the network and HA
|
|
|
|
await async_test_device_join(
|
2019-10-21 23:30:56 +00:00
|
|
|
hass,
|
|
|
|
zha_gateway,
|
|
|
|
general.OnOff.cluster_id,
|
2019-10-31 16:31:06 +00:00
|
|
|
on_off_entity_id,
|
2019-10-21 23:30:56 +00:00
|
|
|
device_type=zigpy.profiles.zha.DeviceType.ON_OFF_LIGHT,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-02-04 11:51:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_test_on_off_from_light(hass, cluster, entity_id):
|
|
|
|
"""Test on off functionality from the light."""
|
|
|
|
# turn on at light
|
|
|
|
attr = make_attribute(0, 1)
|
2019-10-21 23:30:56 +00:00
|
|
|
hdr = make_zcl_header(zcl_f.Command.Report_Attributes)
|
2019-10-21 17:14:17 +00:00
|
|
|
cluster.handle_message(hdr, [[attr]])
|
2019-02-04 11:51:47 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert hass.states.get(entity_id).state == STATE_ON
|
|
|
|
|
|
|
|
# turn off at light
|
|
|
|
attr.value.value = 0
|
2019-10-21 17:14:17 +00:00
|
|
|
cluster.handle_message(hdr, [[attr]])
|
2019-02-04 11:51:47 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert hass.states.get(entity_id).state == STATE_OFF
|
|
|
|
|
|
|
|
|
|
|
|
async def async_test_on_from_light(hass, cluster, entity_id):
|
|
|
|
"""Test on off functionality from the light."""
|
|
|
|
# turn on at light
|
|
|
|
attr = make_attribute(0, 1)
|
2019-10-21 23:30:56 +00:00
|
|
|
hdr = make_zcl_header(zcl_f.Command.Report_Attributes)
|
2019-10-21 17:14:17 +00:00
|
|
|
cluster.handle_message(hdr, [[attr]])
|
2019-02-04 11:51:47 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert hass.states.get(entity_id).state == STATE_ON
|
|
|
|
|
|
|
|
|
|
|
|
async def async_test_on_off_from_hass(hass, cluster, entity_id):
|
|
|
|
"""Test on off functionality from hass."""
|
|
|
|
with patch(
|
2019-10-21 23:30:56 +00:00
|
|
|
"zigpy.zcl.Cluster.request",
|
|
|
|
return_value=mock_coro([0x00, zcl_f.Status.SUCCESS]),
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2019-02-04 11:51:47 +00:00
|
|
|
# turn on via UI
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, "turn_on", {"entity_id": entity_id}, blocking=True
|
|
|
|
)
|
2019-02-27 13:34:38 +00:00
|
|
|
assert cluster.request.call_count == 1
|
2019-02-04 11:51:47 +00:00
|
|
|
assert cluster.request.call_args == call(
|
2019-07-31 19:25:30 +00:00
|
|
|
False, ON, (), expect_reply=True, manufacturer=None
|
|
|
|
)
|
2019-02-04 11:51:47 +00:00
|
|
|
|
|
|
|
await async_test_off_from_hass(hass, cluster, entity_id)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_test_off_from_hass(hass, cluster, entity_id):
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Test turning off the light from Home Assistant."""
|
2019-02-04 11:51:47 +00:00
|
|
|
with patch(
|
2019-10-21 23:30:56 +00:00
|
|
|
"zigpy.zcl.Cluster.request",
|
|
|
|
return_value=mock_coro([0x01, zcl_f.Status.SUCCESS]),
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2019-02-04 11:51:47 +00:00
|
|
|
# turn off via UI
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, "turn_off", {"entity_id": entity_id}, blocking=True
|
|
|
|
)
|
2019-02-27 13:34:38 +00:00
|
|
|
assert cluster.request.call_count == 1
|
2019-02-04 11:51:47 +00:00
|
|
|
assert cluster.request.call_args == call(
|
2019-07-31 19:25:30 +00:00
|
|
|
False, OFF, (), expect_reply=True, manufacturer=None
|
|
|
|
)
|
2019-02-04 11:51:47 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_test_level_on_off_from_hass(
|
|
|
|
hass, on_off_cluster, level_cluster, entity_id
|
|
|
|
):
|
2019-02-04 11:51:47 +00:00
|
|
|
"""Test on off functionality from hass."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-02-27 13:34:38 +00:00
|
|
|
# turn on via UI
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, "turn_on", {"entity_id": entity_id}, blocking=True
|
|
|
|
)
|
2019-02-27 13:34:38 +00:00
|
|
|
assert on_off_cluster.request.call_count == 1
|
|
|
|
assert level_cluster.request.call_count == 0
|
|
|
|
assert on_off_cluster.request.call_args == call(
|
2019-07-31 19:25:30 +00:00
|
|
|
False, 1, (), expect_reply=True, manufacturer=None
|
|
|
|
)
|
2019-02-27 13:34:38 +00:00
|
|
|
on_off_cluster.request.reset_mock()
|
|
|
|
level_cluster.request.reset_mock()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, "turn_on", {"entity_id": entity_id, "transition": 10}, blocking=True
|
|
|
|
)
|
2019-02-27 13:34:38 +00:00
|
|
|
assert on_off_cluster.request.call_count == 1
|
|
|
|
assert level_cluster.request.call_count == 1
|
|
|
|
assert on_off_cluster.request.call_args == call(
|
2019-07-31 19:25:30 +00:00
|
|
|
False, 1, (), expect_reply=True, manufacturer=None
|
|
|
|
)
|
2019-02-27 13:34:38 +00:00
|
|
|
assert level_cluster.request.call_args == call(
|
2019-07-31 19:25:30 +00:00
|
|
|
False,
|
|
|
|
4,
|
2019-10-21 23:30:56 +00:00
|
|
|
(zigpy.types.uint8_t, zigpy.types.uint16_t),
|
2019-07-31 19:25:30 +00:00
|
|
|
254,
|
|
|
|
100.0,
|
|
|
|
expect_reply=True,
|
|
|
|
manufacturer=None,
|
|
|
|
)
|
2019-02-27 13:34:38 +00:00
|
|
|
on_off_cluster.request.reset_mock()
|
|
|
|
level_cluster.request.reset_mock()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, "turn_on", {"entity_id": entity_id, "brightness": 10}, blocking=True
|
|
|
|
)
|
2019-02-27 13:34:38 +00:00
|
|
|
assert on_off_cluster.request.call_count == 1
|
|
|
|
assert level_cluster.request.call_count == 1
|
|
|
|
assert on_off_cluster.request.call_args == call(
|
2019-07-31 19:25:30 +00:00
|
|
|
False, 1, (), expect_reply=True, manufacturer=None
|
|
|
|
)
|
2019-02-27 13:34:38 +00:00
|
|
|
assert level_cluster.request.call_args == call(
|
2019-07-31 19:25:30 +00:00
|
|
|
False,
|
|
|
|
4,
|
2019-10-21 23:30:56 +00:00
|
|
|
(zigpy.types.uint8_t, zigpy.types.uint16_t),
|
2019-07-31 19:25:30 +00:00
|
|
|
10,
|
2019-09-23 13:08:44 +00:00
|
|
|
0,
|
2019-07-31 19:25:30 +00:00
|
|
|
expect_reply=True,
|
|
|
|
manufacturer=None,
|
|
|
|
)
|
2019-02-27 13:34:38 +00:00
|
|
|
on_off_cluster.request.reset_mock()
|
|
|
|
level_cluster.request.reset_mock()
|
|
|
|
|
|
|
|
await async_test_off_from_hass(hass, on_off_cluster, entity_id)
|
2019-02-04 11:51:47 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_test_dimmer_from_light(hass, cluster, entity_id, level, expected_state):
|
2019-02-04 11:51:47 +00:00
|
|
|
"""Test dimmer functionality from the light."""
|
|
|
|
attr = make_attribute(0, level)
|
2019-10-21 23:30:56 +00:00
|
|
|
hdr = make_zcl_header(zcl_f.Command.Report_Attributes)
|
2019-10-21 17:14:17 +00:00
|
|
|
cluster.handle_message(hdr, [[attr]])
|
2019-02-04 11:51:47 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert hass.states.get(entity_id).state == expected_state
|
|
|
|
# hass uses None for brightness of 0 in state attributes
|
|
|
|
if level == 0:
|
|
|
|
level = None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.states.get(entity_id).attributes.get("brightness") == level
|