2019-02-03 07:03:31 -05:00
|
|
|
"""Common test objects."""
|
|
|
|
import time
|
2020-02-09 21:45:35 -05:00
|
|
|
from unittest.mock import Mock
|
2019-07-30 15:19:24 -04:00
|
|
|
|
|
|
|
from asynctest import CoroutineMock
|
2019-10-21 19:30:56 -04:00
|
|
|
import zigpy.profiles.zha
|
|
|
|
import zigpy.types
|
|
|
|
import zigpy.zcl
|
|
|
|
import zigpy.zcl.clusters.general
|
2019-10-21 13:14:17 -04:00
|
|
|
import zigpy.zcl.foundation as zcl_f
|
2019-10-21 19:30:56 -04:00
|
|
|
import zigpy.zdo.types
|
2019-07-30 15:19:24 -04:00
|
|
|
|
2019-02-03 07:03:31 -05:00
|
|
|
from homeassistant.components.zha.core.const import (
|
2019-07-31 12:25:30 -07:00
|
|
|
DATA_ZHA,
|
|
|
|
DATA_ZHA_BRIDGE_ID,
|
|
|
|
DATA_ZHA_CONFIG,
|
|
|
|
DATA_ZHA_DISPATCHERS,
|
|
|
|
)
|
2019-02-03 07:03:31 -05:00
|
|
|
from homeassistant.util import slugify
|
2019-07-30 15:19:24 -04:00
|
|
|
|
2019-02-03 07:03:31 -05:00
|
|
|
|
|
|
|
class FakeApplication:
|
|
|
|
"""Fake application for mocking zigpy."""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Init fake application."""
|
2019-10-21 19:30:56 -04:00
|
|
|
self.ieee = zigpy.types.EUI64.convert("00:15:8d:00:02:32:4f:32")
|
2019-07-31 12:25:30 -07:00
|
|
|
self.nwk = 0x087D
|
2019-02-03 07:03:31 -05:00
|
|
|
|
|
|
|
|
|
|
|
APPLICATION = FakeApplication()
|
|
|
|
|
|
|
|
|
|
|
|
class FakeEndpoint:
|
|
|
|
"""Fake endpoint for moking zigpy."""
|
|
|
|
|
2019-12-31 11:09:58 -05:00
|
|
|
def __init__(self, manufacturer, model, epid=1):
|
2019-02-03 07:03:31 -05:00
|
|
|
"""Init fake endpoint."""
|
|
|
|
self.device = None
|
2019-12-31 11:09:58 -05:00
|
|
|
self.endpoint_id = epid
|
2019-02-03 07:03:31 -05:00
|
|
|
self.in_clusters = {}
|
|
|
|
self.out_clusters = {}
|
|
|
|
self._cluster_attr = {}
|
|
|
|
self.status = 1
|
2019-02-03 16:03:35 -05:00
|
|
|
self.manufacturer = manufacturer
|
|
|
|
self.model = model
|
2019-10-21 19:30:56 -04:00
|
|
|
self.profile_id = zigpy.profiles.zha.PROFILE_ID
|
2019-02-03 07:03:31 -05:00
|
|
|
self.device_type = None
|
|
|
|
|
|
|
|
def add_input_cluster(self, cluster_id):
|
|
|
|
"""Add an input cluster."""
|
2019-10-21 19:30:56 -04:00
|
|
|
cluster = zigpy.zcl.Cluster.from_id(self, cluster_id, is_server=True)
|
2019-02-03 07:03:31 -05:00
|
|
|
patch_cluster(cluster)
|
|
|
|
self.in_clusters[cluster_id] = cluster
|
2019-07-31 12:25:30 -07:00
|
|
|
if hasattr(cluster, "ep_attribute"):
|
2019-02-03 07:03:31 -05:00
|
|
|
setattr(self, cluster.ep_attribute, cluster)
|
|
|
|
|
|
|
|
def add_output_cluster(self, cluster_id):
|
|
|
|
"""Add an output cluster."""
|
2019-10-21 19:30:56 -04:00
|
|
|
cluster = zigpy.zcl.Cluster.from_id(self, cluster_id, is_server=False)
|
2019-02-03 07:03:31 -05:00
|
|
|
patch_cluster(cluster)
|
|
|
|
self.out_clusters[cluster_id] = cluster
|
|
|
|
|
|
|
|
|
|
|
|
def patch_cluster(cluster):
|
|
|
|
"""Patch a cluster for testing."""
|
2019-07-30 15:19:24 -04:00
|
|
|
cluster.bind = CoroutineMock(return_value=[0])
|
2019-08-04 18:20:03 -04:00
|
|
|
cluster.configure_reporting = CoroutineMock(return_value=[0])
|
2019-02-03 07:03:31 -05:00
|
|
|
cluster.deserialize = Mock()
|
|
|
|
cluster.handle_cluster_request = Mock()
|
2019-08-04 18:20:03 -04:00
|
|
|
cluster.read_attributes = CoroutineMock()
|
2019-02-03 07:03:31 -05:00
|
|
|
cluster.read_attributes_raw = Mock()
|
2019-08-04 18:20:03 -04:00
|
|
|
cluster.unbind = CoroutineMock(return_value=[0])
|
2019-02-03 07:03:31 -05:00
|
|
|
|
|
|
|
|
|
|
|
class FakeDevice:
|
|
|
|
"""Fake device for mocking zigpy."""
|
|
|
|
|
2020-01-27 19:43:26 -05:00
|
|
|
def __init__(self, ieee, manufacturer, model, node_desc=None):
|
2019-02-03 07:03:31 -05:00
|
|
|
"""Init fake device."""
|
|
|
|
self._application = APPLICATION
|
2019-10-21 19:30:56 -04:00
|
|
|
self.ieee = zigpy.types.EUI64.convert(ieee)
|
2019-07-31 12:25:30 -07:00
|
|
|
self.nwk = 0xB79C
|
2019-02-03 07:03:31 -05:00
|
|
|
self.zdo = Mock()
|
|
|
|
self.endpoints = {0: self.zdo}
|
|
|
|
self.lqi = 255
|
|
|
|
self.rssi = 8
|
|
|
|
self.last_seen = time.time()
|
|
|
|
self.status = 2
|
|
|
|
self.initializing = False
|
2019-02-03 16:03:35 -05:00
|
|
|
self.manufacturer = manufacturer
|
|
|
|
self.model = model
|
2019-10-21 19:30:56 -04:00
|
|
|
self.node_desc = zigpy.zdo.types.NodeDescriptor()
|
2019-12-09 14:50:04 -05:00
|
|
|
self.add_to_group = CoroutineMock()
|
|
|
|
self.remove_from_group = CoroutineMock()
|
2020-01-27 19:43:26 -05:00
|
|
|
if node_desc is None:
|
|
|
|
node_desc = b"\x02@\x807\x10\x7fd\x00\x00*d\x00\x00"
|
|
|
|
self.node_desc = zigpy.zdo.types.NodeDescriptor.deserialize(node_desc)[0]
|
2019-02-03 07:03:31 -05:00
|
|
|
|
|
|
|
|
|
|
|
def make_attribute(attrid, value, status=0):
|
|
|
|
"""Make an attribute."""
|
2019-10-21 19:30:56 -04:00
|
|
|
attr = zcl_f.Attribute()
|
2019-02-03 07:03:31 -05:00
|
|
|
attr.attrid = attrid
|
2019-10-21 19:30:56 -04:00
|
|
|
attr.value = zcl_f.TypeValue()
|
2019-02-03 07:03:31 -05:00
|
|
|
attr.value.value = value
|
|
|
|
return attr
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry):
|
|
|
|
"""Mock setup entry for zha."""
|
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_CONFIG] = {}
|
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_DISPATCHERS] = []
|
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_BRIDGE_ID] = APPLICATION.ieee
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2019-10-31 12:31:06 -04:00
|
|
|
async def find_entity_id(domain, zha_device, hass):
|
|
|
|
"""Find the entity id under the testing.
|
2019-02-03 16:03:35 -05:00
|
|
|
|
|
|
|
This is used to get the entity id in order to get the state from the state
|
|
|
|
machine so that we can test state changes.
|
|
|
|
"""
|
2019-10-31 12:31:06 -04:00
|
|
|
ieeetail = "".join([f"{o:02x}" for o in zha_device.ieee[:4]])
|
|
|
|
head = f"{domain}." + slugify(f"{zha_device.name} {ieeetail}")
|
|
|
|
|
|
|
|
enitiy_ids = hass.states.async_entity_ids(domain)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
for entity_id in enitiy_ids:
|
|
|
|
if entity_id.startswith(head):
|
|
|
|
return entity_id
|
|
|
|
return None
|
2019-02-03 07:03:31 -05:00
|
|
|
|
|
|
|
|
2019-02-03 16:03:35 -05:00
|
|
|
async def async_enable_traffic(hass, zha_gateway, zha_devices):
|
2019-02-03 07:03:31 -05:00
|
|
|
"""Allow traffic to flow through the gateway and the zha device."""
|
2019-02-03 16:03:35 -05:00
|
|
|
for zha_device in zha_devices:
|
|
|
|
zha_device.update_available(True)
|
2019-02-03 07:03:31 -05:00
|
|
|
await hass.async_block_till_done()
|
2019-02-03 16:03:35 -05:00
|
|
|
|
|
|
|
|
2019-10-21 13:14:17 -04:00
|
|
|
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)
|