Esempio n. 1
0
    def reply(
        self,
        general: bool,
        command_id: Union[foundation.Command, int, t.uint8_t],
        schema: Tuple,
        *args,
        manufacturer: Optional[Union[int, t.uint16_t]] = None,
        tsn: Optional[Union[int, t.uint8_t]] = None,
    ):
        if len(schema) != len(args) and foundation.Status not in schema:
            self.debug("Schema and args lengths do not match in reply")

        if tsn is None:
            tsn = self._endpoint.device.application.get_sequence()
        if general:
            hdr = foundation.ZCLHeader.general(tsn,
                                               command_id,
                                               manufacturer,
                                               is_reply=True)
        else:
            hdr = foundation.ZCLHeader.cluster(tsn,
                                               command_id,
                                               manufacturer,
                                               is_reply=True)
        hdr.manufacturer = manufacturer
        data = hdr.serialize() + t.serialize(args, schema)

        return self._endpoint.reply(self.cluster_id,
                                    tsn,
                                    data,
                                    command_id=command_id)
Esempio n. 2
0
    def request(self,
                general,
                command_id,
                schema,
                *args,
                manufacturer=None,
                expect_reply=True):
        if len(schema) != len(args):
            self.error("Schema and args lengths do not match in request")
            error = asyncio.Future()
            error.set_exception(
                ValueError(
                    "Wrong number of parameters for request, expected %d argument(s)"
                    % len(schema)))
            return error

        sequence = self._endpoint._device.application.get_sequence()
        if general:
            frame_control = 0x00
        else:
            frame_control = 0x01
        if manufacturer is not None:
            frame_control |= 0b0100
            manufacturer = manufacturer.to_bytes(2, 'little')
        else:
            manufacturer = b''
        data = bytes([frame_control]) + manufacturer + bytes(
            [sequence, command_id])
        data += t.serialize(args, schema)

        return self._endpoint.request(self.cluster_id,
                                      sequence,
                                      data,
                                      expect_reply=expect_reply)
Esempio n. 3
0
    def reply(self,
              general,
              command_id,
              schema,
              *args,
              manufacturer=None,
              tsn=None):
        if len(schema) != len(args) and foundation.Status not in schema:
            self.debug("Schema and args lengths do not match in reply")

        if tsn is None:
            tsn = self._endpoint._device.application.get_sequence()
        if general:
            hdr = foundation.ZCLHeader.general(tsn,
                                               command_id,
                                               manufacturer,
                                               is_reply=True)
        else:
            hdr = foundation.ZCLHeader.cluster(tsn,
                                               command_id,
                                               manufacturer,
                                               is_reply=True)
        hdr.manufacturer = manufacturer
        data = hdr.serialize() + t.serialize(args, schema)

        return self._endpoint.reply(self.cluster_id, tsn, data)
Esempio n. 4
0
    def request(
        self,
        general: bool,
        command_id: Union[foundation.Command, int, t.uint8_t],
        schema: Tuple,
        *args,
        manufacturer: Optional[Union[int, t.uint16_t]] = None,
        expect_reply: bool = True,
        tsn: Optional[Union[int, t.uint8_t]] = None,
    ):
        optional = len(
            [s for s in schema if hasattr(s, "optional") and s.optional])
        if len(schema) < len(args) or len(args) < len(schema) - optional:
            self.error("Schema and args lengths do not match in request")
            error = asyncio.Future()
            error.set_exception(
                ValueError(
                    "Wrong number of parameters for request, expected %d argument(s)"
                    % len(schema)))
            return error

        if tsn is None:
            tsn = self._endpoint.device.application.get_sequence()
        if general:
            hdr = foundation.ZCLHeader.general(tsn, command_id, manufacturer)
        else:
            hdr = foundation.ZCLHeader.cluster(tsn, command_id, manufacturer)
        hdr.manufacturer = manufacturer
        data = hdr.serialize() + t.serialize(args, schema)

        return self._endpoint.request(self.cluster_id,
                                      tsn,
                                      data,
                                      expect_reply=expect_reply,
                                      command_id=command_id)
Esempio n. 5
0
    def request(
        self, general, command_id, schema, *args, manufacturer=None, expect_reply=True
    ):
        optional = len([s for s in schema if hasattr(s, "optional") and s.optional])
        if len(schema) < len(args) or len(args) < len(schema) - optional:
            self.error("Schema and args lengths do not match in request")
            error = asyncio.Future()
            error.set_exception(
                ValueError(
                    "Wrong number of parameters for request, expected %d argument(s)"
                    % len(schema)
                )
            )
            return error

        sequence = self._endpoint._device.application.get_sequence()
        if general:
            hdr = foundation.ZCLHeader.general(sequence, command_id, manufacturer)
        else:
            hdr = foundation.ZCLHeader.cluster(sequence, command_id, manufacturer)
        hdr.manufacturer = manufacturer
        data = hdr.serialize() + t.serialize(args, schema)

        return self._endpoint.request(
            self.cluster_id,
            sequence,
            data,
            expect_reply=expect_reply,
            command_id=command_id,
        )
Esempio n. 6
0
def broadcast(app, command, grpid, radius, *args,
              broadcast_address=t.BroadcastAddress.RX_ON_WHEN_IDLE):
    sequence = app.get_sequence()
    data = sequence.to_bytes(1, 'little')
    schema = types.CLUSTERS[command][1]
    data += t.serialize(args, schema)
    return zigpy.device.broadcast(
        app, 0, command, 0, 0, grpid, radius, sequence, data,
        broadcast_address=broadcast_address
    )
Esempio n. 7
0
 async def _remote_at_command(self, options, name, *args):
     _LOGGER.debug("Remote AT command: %s %s", name, args)
     data = t.serialize(args, (AT_COMMANDS[name],))
     try:
         return await asyncio.wait_for(
             await self._command(options, name.encode("ascii"), data, *args),
             timeout=REMOTE_AT_COMMAND_TIMEOUT,
         )
     except asyncio.TimeoutError:
         _LOGGER.warning("No response to %s command", name)
         raise
Esempio n. 8
0
    def reply(self, command_id, schema, *args):
        if len(schema) != len(args):
            self.error("Schema and args lengths do not match in reply")
            error = asyncio.Future()
            error.set_exception(ValueError("Wrong number of parameters for reply, expected %d argument(s)" % len(schema)))
            return error

        sequence = self._endpoint._device.application.get_sequence()
        frame_control = 0b1001  # Cluster reply command
        data = bytes([frame_control, sequence, command_id])
        data += t.serialize(args, schema)

        return self._endpoint.reply(self.cluster_id, sequence, data)
Esempio n. 9
0
    async def _command(self, options, command, data, *args):
        _LOGGER.debug("Command %s %s", command, data)
        frame_id = self._seq
        self._seq = (self._seq % 255) + 1
        schema = (
            uint8_t,
            uint8_t,
            uint8_t,
            uint8_t,
            self.EUI64,
            self.NWK,
            Bytes,
            Bytes,
        )
        data = t.serialize(
            (
                0x32,
                0x00,
                options,
                frame_id,
                self._endpoint.device.application.ieee,
                self._endpoint.device.application.nwk,
                command,
                data,
            ),
            schema,
        )

        future = asyncio.Future()
        self._save_at_request(frame_id, future)

        try:
            await self._endpoint.device.application.request(
                self._endpoint.device,
                XBEE_PROFILE_ID,
                XBEE_AT_REQUEST_CLUSTER,
                XBEE_AT_ENDPOINT,
                XBEE_AT_ENDPOINT,
                self._endpoint.device.application.get_sequence(),
                data,
                expect_reply=False,
            )
        except Exception as e:
            future.set_exception(e)

        return future
Esempio n. 10
0
    def reply(self, general, command_id, schema, *args, manufacturer=None):
        if len(schema) != len(args) and foundation.Status not in schema:
            self.debug("Schema and args lengths do not match in reply")

        sequence = self._endpoint._device.application.get_sequence()
        frame_control = 0b1000  # Cluster reply command
        if not general:
            frame_control |= 0x01
        if manufacturer is not None:
            frame_control |= 0b0100
            manufacturer = manufacturer.to_bytes(2, 'little')
        else:
            manufacturer = b''
        data = bytes([frame_control]) + manufacturer + bytes(
            [sequence, command_id])
        data += t.serialize(args, schema)

        return self._endpoint.reply(self.cluster_id, sequence, data)
Esempio n. 11
0
    async def _command(self, options, command, data, *args):
        _LOGGER.debug("Command %s %s", command, data)
        frame_id = self._seq
        self._seq = (self._seq % 255) + 1
        schema = (
            uint8_t,
            uint8_t,
            uint8_t,
            uint8_t,
            self.EUI64,
            self.NWK,
            Bytes,
            Bytes,
        )
        data = t.serialize(
            (
                0x32,
                0x00,
                options,
                frame_id,
                self._endpoint.device.application.ieee,
                self._endpoint.device.application.nwk,
                command,
                data,
            ),
            schema,
        )
        result = await self._endpoint.device.application.request(
            self._endpoint.device,
            XBEE_PROFILE_ID,
            XBEE_AT_REQUEST_CLUSTER,
            XBEE_AT_ENDPOINT,
            XBEE_AT_ENDPOINT,
            self._endpoint.device.application.get_sequence(),
            data,
            expect_reply=False,
        )

        future = asyncio.Future()
        self._save_at_request(frame_id, future)
        if result[0] != foundation.Status.SUCCESS:
            future.set_exception(RuntimeError("AT Command request: {}".format(result)))
        return future
Esempio n. 12
0
 def _serialize(self, command, *args):
     schema = types.CLUSTERS[command][1]
     data = t.serialize(args, schema)
     return data
Esempio n. 13
0
 def _serialize(self, command, *args):
     sequence = self._device.application.get_sequence()
     data = sequence.to_bytes(1, 'little')
     schema = types.CLUSTERS[command][2]
     data += t.serialize(args, schema)
     return sequence, data