Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_groups(tmpdir, monkeypatch):
monkeypatch.setattr(
Device, "schedule_initialize", mock_dev_init(Status.ENDPOINTS_INIT)
)
group_id, group_name = 0x1221, "app db Test Group 0x1221"
async def mock_request(*args, **kwargs):
return [ZCLStatus.SUCCESS, group_id]
monkeypatch.setattr(zigpy.zcl.Cluster, "request", mock_request)
db = os.path.join(str(tmpdir), "test.db")
app = await make_app(db)
ieee = make_ieee()
app.handle_join(99, ieee, 0)
dev = app.get_device(ieee)
ep = dev.add_endpoint(1)
ep.profile_id = 260
ep.device_type = profiles.zha.DeviceType.PUMP
ep.add_input_cluster(4)
app.device_initialized(dev)
ieee_b = make_ieee(2)
app.handle_join(100, ieee_b, 0)
dev_b = app.get_device(ieee_b)
def _mk_rar(attrid, value, status=0):
r = zcl.foundation.ReadAttributeRecord()
r.attrid = attrid
r.status = status
r.value = zcl.foundation.TypeValue()
r.value.value = value
return r
def test_attribute_report(cluster):
attr = zcl.foundation.Attribute()
attr.attrid = 4
attr.value = zcl.foundation.TypeValue()
attr.value.value = "manufacturer"
hdr = mock.MagicMock(auto_spec=foundation.ZCLHeader)
hdr.command_id = foundation.Command.Report_Attributes
hdr.frame_control.is_general = True
hdr.frame_control.is_cluster = False
cluster.handle_message(hdr, [[attr]])
assert cluster._attr_cache[4] == "manufacturer"
attr.attrid = 0x89AB
cluster.handle_message(hdr, [[attr]])
assert cluster._attr_cache[attr.attrid] == "manufacturer"
def mock_type(*args, **kwargs):
raise ValueError
with mock.patch.dict(cluster.attributes, {0xAAAA: ("Name", mock_type)}):
def ota_cluster():
ep = mock.MagicMock()
ep.device.application.ota = mock.MagicMock(spec_set=ota.OTA)
return zcl.Cluster._registry[0x0019](ep)
This creates a new fake device and adds it to the network. It is meant to
simulate pairing a new device to the network so that code pathways that
only trigger during device joins can be tested.
"""
# create zigpy device mocking out the zigbee network operations
with patch(
"zigpy.zcl.Cluster.configure_reporting",
return_value=mock_coro([zcl_f.Status.SUCCESS, zcl_f.Status.SUCCESS]),
):
with patch(
"zigpy.zcl.Cluster.bind",
return_value=mock_coro([zcl_f.Status.SUCCESS, zcl_f.Status.SUCCESS]),
):
await async_init_zigpy_device(
hass,
[cluster_id, zigpy.zcl.clusters.general.Basic.cluster_id],
[],
device_type,
zha_gateway,
ieee="00:0d:6f:00:0a:90:69:f7",
is_new_join=True,
)
assert hass.states.get(entity_id) is not None
def populate_listener_registry():
"""Populate the listener registry."""
from zigpy import zcl
LISTENER_REGISTRY.update({
zcl.clusters.general.Alarms.cluster_id: ClusterListener,
zcl.clusters.general.Commissioning.cluster_id: ClusterListener,
zcl.clusters.general.Identify.cluster_id: ClusterListener,
zcl.clusters.general.Groups.cluster_id: ClusterListener,
zcl.clusters.general.Scenes.cluster_id: ClusterListener,
zcl.clusters.general.Partition.cluster_id: ClusterListener,
zcl.clusters.general.Ota.cluster_id: ClusterListener,
zcl.clusters.general.PowerProfile.cluster_id: ClusterListener,
zcl.clusters.general.ApplianceControl.cluster_id: ClusterListener,
zcl.clusters.general.PollControl.cluster_id: ClusterListener,
zcl.clusters.general.GreenPowerProxy.cluster_id: ClusterListener,
zcl.clusters.general.OnOffConfiguration.cluster_id: ClusterListener,
zcl.clusters.general.OnOff.cluster_id: OnOffListener,
zcl.clusters.general.LevelControl.cluster_id: LevelListener,
zcl.clusters.lighting.Color.cluster_id: ColorListener,
zcl.clusters.homeautomation.ElectricalMeasurement.cluster_id:
ActivePowerListener,
zcl.clusters.general.PowerConfiguration.cluster_id: BatteryListener,
def add_input_cluster(self, cluster_id, cluster=None):
"""Adds an endpoint's input cluster
(a server cluster supported by the device)
"""
if cluster_id in self.in_clusters and cluster is None:
return self.in_clusters[cluster_id]
if cluster is None:
cluster = zigpy.zcl.Cluster.from_id(self, cluster_id, is_server=True)
self.in_clusters[cluster_id] = cluster
if hasattr(cluster, "ep_attribute"):
self._cluster_attr[cluster.ep_attribute] = cluster
if hasattr(self._device.application, "_dblistener"):
listener = zigpy.zcl.ClusterPersistingListener(
self._device.application._dblistener, cluster
)
cluster.add_listener(listener)
return cluster
{
# this works for now but if we hit conflicts we can break it out to
# a different dict that is keyed by manufacturer
SMARTTHINGS_ACCELERATION_CLUSTER: BINARY_SENSOR,
SMARTTHINGS_HUMIDITY_CLUSTER: SENSOR,
zcl.clusters.closures.DoorLock: LOCK,
zcl.clusters.general.AnalogInput.cluster_id: SENSOR,
zcl.clusters.general.MultistateInput.cluster_id: SENSOR,
zcl.clusters.general.OnOff: SWITCH,
zcl.clusters.general.PowerConfiguration: SENSOR,
zcl.clusters.homeautomation.ElectricalMeasurement: SENSOR,
zcl.clusters.hvac.Fan: FAN,
zcl.clusters.measurement.IlluminanceMeasurement: SENSOR,
zcl.clusters.measurement.OccupancySensing: BINARY_SENSOR,
zcl.clusters.measurement.PressureMeasurement: SENSOR,
zcl.clusters.measurement.RelativeHumidity: SENSOR,
zcl.clusters.measurement.TemperatureMeasurement: SENSOR,
zcl.clusters.security.IasZone: BINARY_SENSOR,
zcl.clusters.smartenergy.Metering: SENSOR,
}
)
SINGLE_OUTPUT_CLUSTER_DEVICE_CLASS.update(
{zcl.clusters.general.OnOff: BINARY_SENSOR}
)
SENSOR_TYPES.update(
{
SMARTTHINGS_HUMIDITY_CLUSTER: SENSOR_HUMIDITY,
zcl.clusters.general.PowerConfiguration.cluster_id: SENSOR_BATTERY,
zcl.clusters.homeautomation.ElectricalMeasurement.cluster_id: SENSOR_ELECTRICAL_MEASUREMENT,
zcl.clusters.measurement.IlluminanceMeasurement.cluster_id: SENSOR_ILLUMINANCE,
CustomClusterType = "CustomCluster"
CustomDeviceType = "CustomDevice"
CustomEndpointType = "CustomEndpoint"
DeviceType = "Device"
EndpointType = "Endpoint"
ZDOType = "ZDO"
if TYPE_CHECKING:
import zigpy.application
import zigpy.device
import zigpy.endpoint
import zigpy.quirks
import zigpy.zcl
import zigpy.zdo
ClusterType = zigpy.zcl.Cluster
ControllerApplicationType = zigpy.application.ControllerApplication
CustomClusterType = zigpy.quirks.CustomCluster
CustomDeviceType = zigpy.quirks.CustomDevice
CustomEndpointType = zigpy.quirks.CustomEndpoint
DeviceType = zigpy.device.Device
EndpointType = zigpy.endpoint.Endpoint
ZDOType = zigpy.zdo.ZDO