Beispiel #1
0
async def async_binding_operation(zha_gateway, source_ieee, target_ieee,
                                  operation):
    """Create or remove a direct zigbee binding between 2 devices."""
    from zigpy.zdo import types as zdo_types
    source_device = zha_gateway.get_device(source_ieee)
    target_device = zha_gateway.get_device(target_ieee)

    clusters_to_bind = await get_matched_clusters(source_device, target_device)

    bind_tasks = []
    for cluster_pair in clusters_to_bind:
        destination_address = zdo_types.MultiAddress()
        destination_address.addrmode = 3
        destination_address.ieee = target_device.ieee
        destination_address.endpoint = \
            cluster_pair.target_cluster.endpoint.endpoint_id

        zdo = cluster_pair.source_cluster.endpoint.device.zdo

        _LOGGER.debug(
            "processing binding operation for: %s %s %s",
            "{}: [{}]".format(ATTR_SOURCE_IEEE, source_ieee),
            "{}: [{}]".format(ATTR_TARGET_IEEE, target_ieee),
            "{}: {}".format('cluster', cluster_pair.source_cluster.cluster_id))
        bind_tasks.append(
            zdo.request(operation, source_device.ieee,
                        cluster_pair.source_cluster.endpoint.endpoint_id,
                        cluster_pair.source_cluster.cluster_id,
                        destination_address))
    await asyncio.gather(*bind_tasks)
Beispiel #2
0
def test_multi_address_invalid():
    ma = types.MultiAddress()
    ma.addrmode = 255
    with pytest.raises(ValueError):
        ma.serialize()

    with pytest.raises(ValueError):
        types.MultiAddress.deserialize(b"\xffnot read")
Beispiel #3
0
def app():
    app = mock.MagicMock()
    app.ieee = t.EUI64(map(t.uint8_t, [8, 9, 10, 11, 12, 13, 14, 15]))
    app.get_sequence.return_value = DEFAULT_SEQUENCE
    dst_addr = zdo_types.MultiAddress()
    dst_addr.addrmode = 3
    dst_addr.ieee = app.ieee
    dst_addr.endpoint = 1
    app.get_dst_address.return_value = dst_addr
    return app
Beispiel #4
0
def test_multi_address_1():
    ma = types.MultiAddress()
    ma.addrmode = 1
    ma.nwk = 123
    ser = ma.serialize()

    ma2, data = types.MultiAddress.deserialize(ser)
    assert data == b""
    assert ma2.addrmode == ma.addrmode
    assert ma2.nwk == ma.nwk
Beispiel #5
0
    async def _async_group_binding_operation(
        self,
        group_id: int,
        operation: zdo_types.ZDOCmd,
        cluster_bindings: list[ClusterBinding],
    ) -> None:
        """Create or remove a direct zigbee binding between a device and a group."""

        zdo = self._zigpy_device.zdo
        op_msg = "0x%04x: %s %s, ep: %s, cluster: %s to group: 0x%04x"
        destination_address = zdo_types.MultiAddress()
        destination_address.addrmode = types.uint8_t(1)
        destination_address.nwk = types.uint16_t(group_id)

        tasks = []

        for cluster_binding in cluster_bindings:
            if cluster_binding.endpoint_id == 0:
                continue
            if (
                cluster_binding.id
                in self._zigpy_device.endpoints[
                    cluster_binding.endpoint_id
                ].out_clusters
            ):
                op_params = (
                    self.nwk,
                    operation.name,
                    str(self.ieee),
                    cluster_binding.endpoint_id,
                    cluster_binding.id,
                    group_id,
                )
                zdo.debug(f"processing {op_msg}", *op_params)
                tasks.append(
                    (
                        zdo.request(
                            operation,
                            self.ieee,
                            cluster_binding.endpoint_id,
                            cluster_binding.id,
                            destination_address,
                        ),
                        op_msg,
                        op_params,
                    )
                )
        res = await asyncio.gather(*(t[0] for t in tasks), return_exceptions=True)
        for outcome, log_msg in zip(res, tasks):
            if isinstance(outcome, Exception):
                fmt = f"{log_msg[1]} failed: %s"
            else:
                fmt = f"{log_msg[1]} completed: %s"
            zdo.debug(fmt, *(log_msg[2] + (outcome,)))
Beispiel #6
0
def test_multi_address_3():
    ma = types.MultiAddress()
    ma.addrmode = 3
    ma.ieee = t.EUI64(map(t.uint8_t, [0, 1, 2, 3, 4, 5, 6, 7]))
    ma.endpoint = 1
    ser = ma.serialize()

    ma2, data = types.MultiAddress.deserialize(ser)
    assert data == b""
    assert ma2.addrmode == ma.addrmode
    assert ma2.ieee == ma.ieee
    assert ma2.endpoint == ma.endpoint
Beispiel #7
0
    def get_dst_address(self, cluster):
        """Helper to get a dst address for bind/unbind operations.

        Allows radios to provide correct information especially for radios which listen
        on specific endpoints only.
        :param cluster: cluster instance to be bound to coordinator
        :returns: returns a "destination address"
        """
        dstaddr = zdo_types.MultiAddress()
        dstaddr.addrmode = 3
        dstaddr.ieee = self.ieee
        dstaddr.endpoint = self.get_endpoint_id(cluster.cluster_id, cluster.is_server)
        return dstaddr
Beispiel #8
0
def test_multi_address_1():
    ma = types.MultiAddress()
    ma.addrmode = 1
    ma.nwk = 123
    ser = ma.serialize()

    assert "ieee" not in repr(ma)
    assert "endpoint" not in repr(ma)
    assert "nwk" in repr(ma)

    ma2, data = types.MultiAddress.deserialize(ser)
    assert data == b""
    assert ma2.addrmode == ma.addrmode
    assert ma2.nwk == ma.nwk
Beispiel #9
0
    async def bind(self):
        """Bind cluster to a group."""
        # Ensure coordinator is a member of the group
        application = self._endpoint.device.application
        coordinator = application.get_device(application.ieee)
        await coordinator.add_to_group(self.COORDINATOR_GROUP_ID)

        # Bind cluster to group
        dstaddr = zdotypes.MultiAddress()
        dstaddr.addrmode = 1
        dstaddr.nwk = self.COORDINATOR_GROUP_ID
        dstaddr.endpoint = self._endpoint.endpoint_id
        return await self._endpoint.device.zdo.Bind_req(
            self._endpoint.device.ieee, self._endpoint.endpoint_id,
            self.cluster_id, dstaddr)
Beispiel #10
0
async def async_binding_operation(zha_gateway, source_ieee, target_ieee,
                                  operation):
    """Create or remove a direct zigbee binding between 2 devices."""

    source_device = zha_gateway.get_device(source_ieee)
    target_device = zha_gateway.get_device(target_ieee)

    clusters_to_bind = await get_matched_clusters(source_device, target_device)

    bind_tasks = []
    for cluster_pair in clusters_to_bind:
        destination_address = zdo_types.MultiAddress()
        destination_address.addrmode = 3
        destination_address.ieee = target_device.ieee
        destination_address.endpoint = cluster_pair.target_cluster.endpoint.endpoint_id

        zdo = cluster_pair.source_cluster.endpoint.device.zdo

        op_msg = "cluster: %s %s --> [%s]"
        op_params = (
            cluster_pair.source_cluster.cluster_id,
            operation.name,
            target_ieee,
        )
        zdo.debug(f"processing {op_msg}", *op_params)

        bind_tasks.append((
            zdo.request(
                operation,
                source_device.ieee,
                cluster_pair.source_cluster.endpoint.endpoint_id,
                cluster_pair.source_cluster.cluster_id,
                destination_address,
            ),
            op_msg,
            op_params,
        ))
    res = await asyncio.gather(*(t[0] for t in bind_tasks),
                               return_exceptions=True)
    for outcome, log_msg in zip(res, bind_tasks):
        if isinstance(outcome, Exception):
            fmt = f"{log_msg[1]} failed: %s"
        else:
            fmt = f"{log_msg[1]} completed: %s"
        zdo.debug(fmt, *(log_msg[2] + (outcome, )))
Beispiel #11
0
 def destination_address(self) -> zdo_types.MultiAddress:
     """Return a ZDO multi address instance."""
     return zdo_types.MultiAddress(addrmode=3,
                                   ieee=self.target_ieee,
                                   endpoint=self.target_ep_id)