2019-02-03 12:03:31 +00:00
|
|
|
"""Test zha switch."""
|
|
|
|
from unittest.mock import call, patch
|
|
|
|
from homeassistant.components.switch import DOMAIN
|
2019-02-07 08:14:19 +00:00
|
|
|
from homeassistant.const import STATE_ON, STATE_OFF, STATE_UNAVAILABLE
|
2019-02-03 12:03:31 +00:00
|
|
|
from tests.common import mock_coro
|
|
|
|
from .common import (
|
2019-02-03 21:03:35 +00:00
|
|
|
async_init_zigpy_device, make_attribute, make_entity_id,
|
2019-02-07 08:14:19 +00:00
|
|
|
async_test_device_join, async_enable_traffic
|
2019-02-03 12:03:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
ON = 1
|
|
|
|
OFF = 0
|
|
|
|
|
|
|
|
|
|
|
|
async def test_switch(hass, config_entry, zha_gateway):
|
|
|
|
"""Test zha switch platform."""
|
2019-02-18 15:55:41 +00:00
|
|
|
from zigpy.zcl.clusters.general import OnOff, Basic
|
2019-02-03 12:03:31 +00:00
|
|
|
from zigpy.zcl.foundation import Status
|
|
|
|
|
|
|
|
# create zigpy device
|
|
|
|
zigpy_device = await async_init_zigpy_device(
|
2019-02-18 15:55:41 +00:00
|
|
|
hass, [OnOff.cluster_id, Basic.cluster_id], [], None, zha_gateway)
|
2019-02-03 12:03:31 +00:00
|
|
|
|
|
|
|
# load up switch domain
|
|
|
|
await hass.config_entries.async_forward_entry_setup(
|
|
|
|
config_entry, DOMAIN)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
cluster = zigpy_device.endpoints.get(1).on_off
|
|
|
|
entity_id = make_entity_id(DOMAIN, zigpy_device, cluster)
|
2019-03-28 02:50:52 +00:00
|
|
|
zha_device = zha_gateway.get_device(zigpy_device.ieee)
|
2019-02-07 08:14:19 +00:00
|
|
|
|
|
|
|
# test that the switch was created and that its state 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])
|
2019-02-03 12:03:31 +00:00
|
|
|
|
|
|
|
# test that the state has changed from unavailable to off
|
|
|
|
assert hass.states.get(entity_id).state == STATE_OFF
|
|
|
|
|
|
|
|
# turn on at switch
|
|
|
|
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_ON
|
|
|
|
|
|
|
|
# turn off at switch
|
|
|
|
attr.value.value = 0
|
|
|
|
cluster.handle_message(False, 0, 0x0a, [[attr]])
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert hass.states.get(entity_id).state == STATE_OFF
|
|
|
|
|
2019-02-06 18:33:21 +00:00
|
|
|
# turn on from HA
|
2019-02-03 12:03:31 +00:00
|
|
|
with patch(
|
|
|
|
'zigpy.zcl.Cluster.request',
|
2019-06-07 15:16:34 +00:00
|
|
|
return_value=mock_coro([0x00, Status.SUCCESS])):
|
2019-02-03 12:03:31 +00:00
|
|
|
# turn on via UI
|
|
|
|
await hass.services.async_call(DOMAIN, 'turn_on', {
|
|
|
|
'entity_id': entity_id
|
|
|
|
}, blocking=True)
|
|
|
|
assert len(cluster.request.mock_calls) == 1
|
|
|
|
assert cluster.request.call_args == call(
|
|
|
|
False, ON, (), expect_reply=True, manufacturer=None)
|
|
|
|
|
2019-02-06 18:33:21 +00:00
|
|
|
# turn off from HA
|
2019-02-03 12:03:31 +00:00
|
|
|
with patch(
|
|
|
|
'zigpy.zcl.Cluster.request',
|
2019-06-07 15:16:34 +00:00
|
|
|
return_value=mock_coro([0x01, Status.SUCCESS])):
|
2019-02-03 12:03:31 +00:00
|
|
|
# turn off via UI
|
|
|
|
await hass.services.async_call(DOMAIN, 'turn_off', {
|
|
|
|
'entity_id': entity_id
|
|
|
|
}, blocking=True)
|
|
|
|
assert len(cluster.request.mock_calls) == 1
|
|
|
|
assert cluster.request.call_args == call(
|
|
|
|
False, OFF, (), expect_reply=True, manufacturer=None)
|
2019-02-03 21:03:35 +00:00
|
|
|
|
2019-02-06 18:33:21 +00:00
|
|
|
# test joining a new switch to the network and HA
|
2019-02-03 21:03:35 +00:00
|
|
|
await async_test_device_join(
|
2019-02-07 08:14:19 +00:00
|
|
|
hass, zha_gateway, OnOff.cluster_id, DOMAIN)
|