Ejemplo n.º 1
0
 def test_should_set_battery_level(self):
     service = BatteryService(self.mockBLE)
     service.register_services()
     service.set_battery_level_percentage(10)
     self.assertTrue(
         self.mockBLE.has_been_called_with(
             'gatts_write', (MOCK_BATTERY_LEVEL_HANDLE, b'\x0A'), times=1))
Ejemplo n.º 2
0
 def test_should_stop_advertising(self):
     service = BatteryService(self.mockBLE)
     service.register_services()
     service.start()
     service.stop()
     self.assertTrue(
         self.mockBLE.has_been_called_with('gap_advertise', (None, ),
                                           times=1))
Ejemplo n.º 3
0
 def test_should_register_BLE_battery_service_and_write_initial_value(self):
     service = BatteryService(self.mockBLE)
     expected_services = _create_expected_services()
     service.register_services()
     self.assertTrue(
         self.mockBLE.has_been_called_with('gatts_register_services',
                                           (expected_services, ),
                                           times=1))
     self.assertTrue(
         self.mockBLE.has_been_called(method='gatts_write', times=1))
Ejemplo n.º 4
0
 def test_should_advertise_services(self):
     service = BatteryService(self.mockBLE)
     service.register_services()
     service.start()
     self.assertTrue(
         self.mockBLE.has_been_called_with(
             'gap_advertise', {
                 'interval_us': 500000,
                 'adv_data': _create_expected_advertising_payload()
             },
             times=1))
Ejemplo n.º 5
0
 def test_should_notify_connect_centrals_on_battery_level_change(self):
     self.mockBLE.when('gatts_read', (MOCK_BATTERY_LEVEL_HANDLE, ),
                       return_value=b'\x0A')
     connect_data = (124, 'A_type', 'address')
     service = BatteryService(self.mockBLE)
     service.register_services()
     service.start()
     self.assertFalse(self.mockBLE.has_been_called(method='gatts_notify'))
     # triggering 'private' method !!
     service._irq_handler(_IRQ_CENTRAL_CONNECT, connect_data)
     service.set_battery_level_percentage(23)
     self.assertTrue(
         self.mockBLE.has_been_called(method='gatts_notify', times=1))
Ejemplo n.º 6
0
 def test_should_round_values_to_nearest_integer(self):
     service = BatteryService(self.mockBLE)
     service.register_services()
     service.set_battery_level_percentage(0.21)
     self.assertTrue(
         self.mockBLE.has_been_called_with(
             'gatts_write', (MOCK_BATTERY_LEVEL_HANDLE, b'\x00'), times=1))
     service.set_battery_level_percentage(0.87)
     self.assertTrue(
         self.mockBLE.has_been_called_with(
             'gatts_write', (MOCK_BATTERY_LEVEL_HANDLE, b'\x01'), times=1))
     service.set_battery_level_percentage(99.8)
     self.assertTrue(
         self.mockBLE.has_been_called_with(
             'gatts_write', (MOCK_BATTERY_LEVEL_HANDLE, b'\x64'), times=1))
     service.set_battery_level_percentage(99.1)
     self.assertTrue(
         self.mockBLE.has_been_called_with(
             'gatts_write', (MOCK_BATTERY_LEVEL_HANDLE, b'\x63'), times=1))
Ejemplo n.º 7
0
 def test_should_read_battery_level_percentage(self):
     self.mockBLE.when('gatts_read', (MOCK_BATTERY_LEVEL_HANDLE, ),
                       return_value=b'\x0A')
     service = BatteryService(self.mockBLE)
     service.register_services()
     self.assertEqual(service.read_battery_level_percentage(), 10)
Ejemplo n.º 8
0
def create_battery_service():
    service = BatteryService(BLE())
    service.register_services()
    service.start()
    return service