Leverage zigpy for IEEE address conversions (#27972)

* Refactor EUI64 conversions.

* Update ZHA dependencies.

* Update tests.
This commit is contained in:
Alexei Chetroi authored and David F. Mulcahey committed 2019-10-21 13:14:17 -04:00
1 parent 643257d911
commit a0c50f4794
13 files changed
+135 -81

No files matched your search

+13 -4
View File
@@ -3,6 +3,8 @@ import time
from unittest.mock import Mock, patch
from asynctest import CoroutineMock
from zigpy.types.named import EUI64
import zigpy.zcl.foundation as zcl_f
from homeassistant.components.zha.core.const import (
DATA_ZHA,
@@ -10,7 +12,6 @@ from homeassistant.components.zha.core.const import (
DATA_ZHA_CONFIG,
DATA_ZHA_DISPATCHERS,
)
from homeassistant.components.zha.core.helpers import convert_ieee
from homeassistant.util import slugify
from tests.common import mock_coro
@@ -21,7 +22,7 @@ class FakeApplication:
def __init__(self):
"""Init fake application."""
self.ieee = convert_ieee("00:15:8d:00:02:32:4f:32")
self.ieee = EUI64.convert("00:15:8d:00:02:32:4f:32")
self.nwk = 0x087D
@@ -71,7 +72,6 @@ def patch_cluster(cluster):
cluster.configure_reporting = CoroutineMock(return_value=[0])
cluster.deserialize = Mock()
cluster.handle_cluster_request = Mock()
cluster.handle_cluster_general_request = Mock()
cluster.read_attributes = CoroutineMock()
cluster.read_attributes_raw = Mock()
cluster.unbind = CoroutineMock(return_value=[0])
@@ -83,7 +83,7 @@ class FakeDevice:
def __init__(self, ieee, manufacturer, model):
"""Init fake device."""
self._application = APPLICATION
self.ieee = convert_ieee(ieee)
self.ieee = EUI64.convert(ieee)
self.nwk = 0xB79C
self.zdo = Mock()
self.endpoints = {0: self.zdo}
@@ -230,3 +230,12 @@ async def async_test_device_join(
domain, zigpy_device, cluster, use_suffix=device_type is None
)
assert hass.states.get(entity_id) is not None
def make_zcl_header(command_id: int, global_command: bool = True) -> zcl_f.ZCLHeader:
"""Cluster.handle_message() ZCL Header helper."""
if global_command:
frc = zcl_f.FrameControl(zcl_f.FrameType.GLOBAL_COMMAND)
else:
frc = zcl_f.FrameControl(zcl_f.FrameType.CLUSTER_COMMAND)
return zcl_f.ZCLHeader(frc, tsn=1, command_id=command_id)
+11 -5
View File
@@ -1,12 +1,16 @@
"""Test zha binary sensor."""
from zigpy.zcl.foundation import Command
from homeassistant.components.binary_sensor import DOMAIN
from homeassistant.const import STATE_ON, STATE_OFF, STATE_UNAVAILABLE
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE
from .common import (
async_enable_traffic,
async_init_zigpy_device,
async_test_device_join,
make_attribute,
make_entity_id,
async_test_device_join,
async_enable_traffic,
make_zcl_header,
)
@@ -74,13 +78,15 @@ async def async_test_binary_sensor_on_off(hass, cluster, entity_id):
"""Test getting on and off messages for binary sensors."""
# binary sensor on
attr = make_attribute(0, 1)
cluster.handle_message(1, 0x0A, [[attr]])
hdr = make_zcl_header(Command.Report_Attributes)
cluster.handle_message(hdr, [[attr]])
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_ON
# binary sensor off
attr.value.value = 0
cluster.handle_message(0, 0x0A, [[attr]])
cluster.handle_message(hdr, [[attr]])
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_OFF
+12 -5
View File
@@ -1,19 +1,25 @@
"""Test ZHA Device Tracker."""
from datetime import timedelta
import time
from zigpy.zcl.foundation import Command
from homeassistant.components.device_tracker import DOMAIN, SOURCE_TYPE_ROUTER
from homeassistant.const import STATE_HOME, STATE_NOT_HOME, STATE_UNAVAILABLE
from homeassistant.components.zha.core.registries import (
SMARTTHINGS_ARRIVAL_SENSOR_DEVICE_TYPE,
)
from homeassistant.const import STATE_HOME, STATE_NOT_HOME, STATE_UNAVAILABLE
import homeassistant.util.dt as dt_util
from .common import (
async_enable_traffic,
async_init_zigpy_device,
async_test_device_join,
make_attribute,
make_entity_id,
async_test_device_join,
async_enable_traffic,
make_zcl_header,
)
from tests.common import async_fire_time_changed
@@ -67,10 +73,11 @@ async def test_device_tracker(hass, config_entry, zha_gateway):
# turn state flip
attr = make_attribute(0x0020, 23)
cluster.handle_message(1, 0x0A, [[attr]])
hdr = make_zcl_header(Command.Report_Attributes)
cluster.handle_message(hdr, [[attr]])
attr = make_attribute(0x0021, 200)
cluster.handle_message(1, 0x0A, [[attr]])
cluster.handle_message(hdr, [[attr]])
zigpy_device.last_seen = time.time() + 10
next_update = dt_util.utcnow() + timedelta(seconds=30)
+20 -7
View File
@@ -1,18 +1,30 @@
"""Test zha fan."""
from unittest.mock import call, patch
from zigpy.zcl.foundation import Command
from homeassistant.components import fan
from homeassistant.const import STATE_ON, STATE_OFF, STATE_UNAVAILABLE
from homeassistant.components.fan import ATTR_SPEED, DOMAIN, SERVICE_SET_SPEED
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF
from tests.common import mock_coro
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,
async_init_zigpy_device,
async_test_device_join,
make_attribute,
make_entity_id,
async_test_device_join,
async_enable_traffic,
make_zcl_header,
)
from tests.common import mock_coro
async def test_fan(hass, config_entry, zha_gateway):
"""Test zha fan platform."""
@@ -44,13 +56,14 @@ async def test_fan(hass, config_entry, zha_gateway):
# turn on at fan
attr = make_attribute(0, 1)
cluster.handle_message(1, 0x0A, [[attr]])
hdr = make_zcl_header(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(0, 0x0A, [[attr]])
cluster.handle_message(hdr, [[attr]])
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_OFF
+10 -4
View File
@@ -2,6 +2,8 @@
import asyncio
from unittest.mock import MagicMock, call, patch, sentinel
from zigpy.zcl.foundation import Command
from homeassistant.components.light import DOMAIN
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE
@@ -11,6 +13,7 @@ from .common import (
async_test_device_join,
make_attribute,
make_entity_id,
make_zcl_header,
)
from tests.common import mock_coro
@@ -123,13 +126,14 @@ 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)
cluster.handle_message(1, 0x0A, [[attr]])
hdr = make_zcl_header(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 light
attr.value.value = 0
cluster.handle_message(0, 0x0A, [[attr]])
cluster.handle_message(hdr, [[attr]])
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_OFF
@@ -138,7 +142,8 @@ 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)
cluster.handle_message(1, 0x0A, [[attr]])
hdr = make_zcl_header(Command.Report_Attributes)
cluster.handle_message(hdr, [[attr]])
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_ON
@@ -243,7 +248,8 @@ async def async_test_level_on_off_from_hass(
async def async_test_dimmer_from_light(hass, cluster, entity_id, level, expected_state):
"""Test dimmer functionality from the light."""
attr = make_attribute(0, level)
cluster.handle_message(1, 0x0A, [[attr]])
hdr = make_zcl_header(Command.Report_Attributes)
cluster.handle_message(hdr, [[attr]])
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
+12 -5
View File
@@ -1,15 +1,21 @@
"""Test zha lock."""
from unittest.mock import patch
from homeassistant.const import STATE_LOCKED, STATE_UNLOCKED, STATE_UNAVAILABLE
from zigpy.zcl.foundation import Command
from homeassistant.components.lock import DOMAIN
from tests.common import mock_coro
from homeassistant.const import STATE_LOCKED, STATE_UNAVAILABLE, STATE_UNLOCKED
from .common import (
async_enable_traffic,
async_init_zigpy_device,
make_attribute,
make_entity_id,
async_enable_traffic,
make_zcl_header,
)
from tests.common import mock_coro
LOCK_DOOR = 0
UNLOCK_DOOR = 1
@@ -43,13 +49,14 @@ async def test_lock(hass, config_entry, zha_gateway):
# set state to locked
attr = make_attribute(0, 1)
cluster.handle_message(1, 0x0A, [[attr]])
hdr = make_zcl_header(Command.Report_Attributes)
cluster.handle_message(hdr, [[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(0, 0x0A, [[attr]])
cluster.handle_message(hdr, [[attr]])
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_UNLOCKED
+9 -4
View File
@@ -1,12 +1,16 @@
"""Test zha sensor."""
from zigpy.zcl.foundation import Command
from homeassistant.components.sensor import DOMAIN
from homeassistant.const import STATE_UNKNOWN, STATE_UNAVAILABLE
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN
from .common import (
async_enable_traffic,
async_init_zigpy_device,
async_test_device_join,
make_attribute,
make_entity_id,
async_test_device_join,
async_enable_traffic,
make_zcl_header,
)
@@ -177,7 +181,8 @@ async def send_attribute_report(hass, cluster, attrid, value):
device is paired to the zigbee network.
"""
attr = make_attribute(attrid, value)
cluster.handle_message(1, 0x0A, [[attr]])
hdr = make_zcl_header(Command.Report_Attributes)
cluster.handle_message(hdr, [[attr]])
await hass.async_block_till_done()
+13 -6
View File
@@ -1,16 +1,22 @@
"""Test zha switch."""
from unittest.mock import call, patch
from zigpy.zcl.foundation import Command
from homeassistant.components.switch import DOMAIN
from homeassistant.const import STATE_ON, STATE_OFF, STATE_UNAVAILABLE
from tests.common import mock_coro
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE
from .common import (
async_enable_traffic,
async_init_zigpy_device,
async_test_device_join,
make_attribute,
make_entity_id,
async_test_device_join,
async_enable_traffic,
make_zcl_header,
)
from tests.common import mock_coro
ON = 1
OFF = 0
@@ -44,13 +50,14 @@ async def test_switch(hass, config_entry, zha_gateway):
# turn on at switch
attr = make_attribute(0, 1)
cluster.handle_message(1, 0x0A, [[attr]])
hdr = make_zcl_header(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 switch
attr.value.value = 0
cluster.handle_message(0, 0x0A, [[attr]])
cluster.handle_message(hdr, [[attr]])
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_OFF