Пример #1
0
    def get_timer(self, idx: int) -> Tuple[BaseTimer, int]:
        """Get all the configured timers of the Switchbot."""

        LOG.info("get timer: %d", idx)
        try:
            self.adapter.start()
            self._connect()
            self._activate_notifications()

            if self.password:
                cmd = b'\x57\x18' + self.password
            else:
                cmd = b'\x57\x08'

            timer_id = (idx * 16 + 3).to_bytes(1, byteorder='big')
            cmd += timer_id

            # trigger and wait for notification
            value = self._write_cmd_and_wait_for_notification(handle=0x16,
                                                              cmd=cmd)
            self._handle_switchbot_status_msg(value=value)

            # parse result
            timer, num_timer = parse_timer_cmd(value)

        finally:
            self.adapter.stop()

        return timer, num_timer
Пример #2
0
    def get_timers(self, n_timers: int = 5) -> List[BaseTimer]:
        """Get the configured Switchbot timers"""

        LOG.info("get timers")
        try:
            self.adapter.start()
            self._connect()
            self._activate_notifications()

            if self.password:
                base_cmd = b'\x57\x18' + self.password
            else:
                base_cmd = b'\x57\x08'

            timers = []

            for i in range(0, n_timers):
                timer_id = (i * 16 + 3).to_bytes(1, byteorder='big')
                cmd = base_cmd + timer_id

                # trigger and wait for notification
                value = self._write_cmd_and_wait_for_notification(handle=0x16,
                                                                  cmd=cmd)
                self._handle_switchbot_status_msg(value=value)

                # parse result
                timer, _ = parse_timer_cmd(value)

                if timer is None:
                    # timer not set => all later also not set
                    break

                # add to timers
                timers.append(timer)

        finally:
            self.adapter.stop()

        return timers