Add support for ZHA door locks #2 (#24344)

* Add support for DoorLock cluster
* Add test for zha lock
* Change lock_state report to REPORT_CONFIG_IMMEDIATE
* Update channel command wrapper to return the entire result
This allows for return values other than result[1]

* Fix tests
* Fix lint
* Update DoorLock test to work with updated zigpy schema
* Fix lint
* Fix unlock test
This commit is contained in:
presslab-us authored and Alexei Chetroi committed 2019-06-07 11:16:33 -04:00
1 parent 592d30d495
commit cb460a85ba
11 files changed
+305 -34

No files matched your search

+4 -4
View File
@@ -57,9 +57,9 @@ async def test_light(hass, config_entry, zha_gateway, monkeypatch):
level_device_on_off_cluster = zigpy_device_level.endpoints.get(1).on_off
level_device_level_cluster = zigpy_device_level.endpoints.get(1).level
on_off_mock = MagicMock(side_effect=asyncio.coroutine(MagicMock(
return_value=(sentinel.data, Status.SUCCESS))))
return_value=[sentinel.data, Status.SUCCESS])))
level_mock = MagicMock(side_effect=asyncio.coroutine(MagicMock(
return_value=(sentinel.data, Status.SUCCESS))))
return_value=[sentinel.data, Status.SUCCESS])))
monkeypatch.setattr(level_device_on_off_cluster, 'request', on_off_mock)
monkeypatch.setattr(level_device_level_cluster, 'request', level_mock)
level_entity_id = make_entity_id(DOMAIN, zigpy_device_level,
@@ -137,7 +137,7 @@ async def async_test_on_off_from_hass(hass, cluster, entity_id):
from zigpy.zcl.foundation import Status
with patch(
'zigpy.zcl.Cluster.request',
return_value=mock_coro([Status.SUCCESS, Status.SUCCESS])):
return_value=mock_coro([0x00, Status.SUCCESS])):
# turn on via UI
await hass.services.async_call(DOMAIN, 'turn_on', {
'entity_id': entity_id
@@ -154,7 +154,7 @@ async def async_test_off_from_hass(hass, cluster, entity_id):
from zigpy.zcl.foundation import Status
with patch(
'zigpy.zcl.Cluster.request',
return_value=mock_coro([Status.SUCCESS, Status.SUCCESS])):
return_value=mock_coro([0x01, Status.SUCCESS])):
# turn off via UI
await hass.services.async_call(DOMAIN, 'turn_off', {
'entity_id': entity_id
+88
View File
@@ -0,0 +1,88 @@
"""Test zha lock."""
from unittest.mock import patch
from homeassistant.const import (
STATE_LOCKED, STATE_UNLOCKED, STATE_UNAVAILABLE)
from homeassistant.components.lock import DOMAIN
from tests.common import mock_coro
from .common import (
async_init_zigpy_device, make_attribute, make_entity_id,
async_enable_traffic)
LOCK_DOOR = 0
UNLOCK_DOOR = 1
async def test_lock(hass, config_entry, zha_gateway):
"""Test zha lock platform."""
from zigpy.zcl.clusters.closures import DoorLock
from zigpy.zcl.clusters.general import Basic
# create zigpy device
zigpy_device = await async_init_zigpy_device(
hass, [DoorLock.cluster_id, Basic.cluster_id], [], None, zha_gateway)
# load up lock domain
await hass.config_entries.async_forward_entry_setup(
config_entry, DOMAIN)
await hass.async_block_till_done()
cluster = zigpy_device.endpoints.get(1).door_lock
entity_id = make_entity_id(DOMAIN, zigpy_device, cluster)
zha_device = zha_gateway.get_device(zigpy_device.ieee)
# test that the lock 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 unlocked
assert hass.states.get(entity_id).state == STATE_UNLOCKED
# set state to locked
attr = make_attribute(0, 1)
cluster.handle_message(False, 1, 0x0a, [[attr]])
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_LOCKED
# set state to unlocked
attr.value.value = 2
cluster.handle_message(False, 0, 0x0a, [[attr]])
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_UNLOCKED
# lock from HA
await async_lock(hass, cluster, entity_id)
# unlock from HA
await async_unlock(hass, cluster, entity_id)
async def async_lock(hass, cluster, entity_id):
"""Test lock functionality from hass."""
from zigpy.zcl.foundation import Status
with patch(
'zigpy.zcl.Cluster.request',
return_value=mock_coro([Status.SUCCESS, ])):
# lock via UI
await hass.services.async_call(DOMAIN, 'lock', {
'entity_id': entity_id
}, blocking=True)
assert cluster.request.call_count == 1
assert cluster.request.call_args[0][0] is False
assert cluster.request.call_args[0][1] == LOCK_DOOR
async def async_unlock(hass, cluster, entity_id):
"""Test lock functionality from hass."""
from zigpy.zcl.foundation import Status
with patch(
'zigpy.zcl.Cluster.request',
return_value=mock_coro([Status.SUCCESS, ])):
# lock via UI
await hass.services.async_call(DOMAIN, 'unlock', {
'entity_id': entity_id
}, blocking=True)
assert cluster.request.call_count == 1
assert cluster.request.call_args[0][0] is False
assert cluster.request.call_args[0][1] == UNLOCK_DOOR
+2 -2
View File
@@ -54,7 +54,7 @@ async def test_switch(hass, config_entry, zha_gateway):
# turn on from HA
with patch(
'zigpy.zcl.Cluster.request',
return_value=mock_coro([Status.SUCCESS, Status.SUCCESS])):
return_value=mock_coro([0x00, Status.SUCCESS])):
# turn on via UI
await hass.services.async_call(DOMAIN, 'turn_on', {
'entity_id': entity_id
@@ -66,7 +66,7 @@ async def test_switch(hass, config_entry, zha_gateway):
# turn off from HA
with patch(
'zigpy.zcl.Cluster.request',
return_value=mock_coro([Status.SUCCESS, Status.SUCCESS])):
return_value=mock_coro([0x01, Status.SUCCESS])):
# turn off via UI
await hass.services.async_call(DOMAIN, 'turn_off', {
'entity_id': entity_id