Пример #1
0
 def run(self):
     """Thread that keeps connection alive."""
     cached_char = Characteristic(BLE_TEMP_UUID, BLE_TEMP_HANDLE)
     adapter = GATTToolBackend()
     while True:
         try:
             _LOGGER.debug("Connecting to %s", self.name)
             # We need concurrent connect, so lets not reset the device
             adapter.start(reset_on_start=False)
             # Seems only one connection can be initiated at a time
             with CONNECT_LOCK:
                 device = adapter.connect(
                     self.mac, CONNECT_TIMEOUT, BLEAddressType.random
                 )
             if SKIP_HANDLE_LOOKUP:
                 # HACK: inject handle mapping collected offline
                 # pylint: disable=protected-access
                 device._characteristics[UUID(BLE_TEMP_UUID)] = cached_char
             # Magic: writing this makes device happy
             device.char_write_handle(0x1B, bytearray([255]), False)
             device.subscribe(BLE_TEMP_UUID, self._update)
             _LOGGER.info("Subscribed to %s", self.name)
             while self.keep_going:
                 # protect against stale connections, just read temperature
                 device.char_read(BLE_TEMP_UUID, timeout=CONNECT_TIMEOUT)
                 self.event.wait(60)
             break
         except (BLEError, NotConnectedError, NotificationTimeout) as ex:
             _LOGGER.error("Exception: %s ", str(ex))
         finally:
             adapter.stop()
Пример #2
0
    def setUp(self):
        self.patchers = []
        self.patchers.append(
            patch('pygatt.backends.gatttool.gatttool.pexpect.spawn'))
        self.spawn = self.patchers[0].start()
        self.spawn.return_value.isalive.return_value = False
        self.spawn.return_value.read_nonblocking.side_effect = (
            pexpect.EOF(value=None))
        self.patchers.append(
            patch('pygatt.backends.gatttool.gatttool.subprocess'))
        self.patchers[1].start()

        # Just keep saying we got the "Connected" response
        def rate_limited_expect(*args, **kwargs):
            # Sleep a little bit to stop the receive thread from spinning and
            # eating 100% CPU during the tests
            time.sleep(0.001)
            # This is hacky, but we sort the event list in the GATTTool receiver
            # and hard code where we expect the "Connected" event to be.
            return 4

        self.spawn.return_value.expect.side_effect = rate_limited_expect

        self.backend = GATTToolBackend()
        self.backend.start()
 def setUp(self):
     raise SkipTest()
     self.patchers = []
     self.patchers.append(
         patch('pygatt.backends.gatttool.gatttool.pexpect.spawn'))
     self.spawn = self.patchers[0].start()
     self.spawn.return_value.isalive.return_value = False
     self.patchers.append(
         patch('pygatt.backends.gatttool.gatttool.subprocess'))
     self.patchers[1].start()
     self.backend = GATTToolBackend()
     self.mock_expect = patch.object(self.backend, '_expect').start()
     self.backend.start()