async def start(self, *args, **kwargs): """Mock Start.""" nonlocal called_start called_start += 1 if called_start == 1: raise BleakError("org.bluez.Error.InProgress") if called_start == 2: raise BleakError("org.freedesktop.DBus.Error.UnknownObject")
async def start(self, *args, **kwargs): """Mock Start.""" nonlocal called_start called_start += 1 if called_start == 1: return # Start ok the first time if called_start < 4: raise BleakError("Failed to start")
def add_service(self, service: BleakGATTService): """Add a :py:class:`~BleakGATTService` to the service collection. Should not be used by end user, but rather by `bleak` itself. """ if service.uuid not in self.__services: self.__services[service.uuid] = service else: raise BleakError( "This service is already present in this BleakGATTServiceCollection!" )
def add_descriptor(self, descriptor: BleakGATTDescriptor): """Add a :py:class:`~BleakGATTDescriptor` to the service collection. Should not be used by end user, but rather by `bleak` itself. """ if descriptor.handle not in self.__descriptors: self.__descriptors[descriptor.handle] = descriptor self.__characteristics[ descriptor.characteristic_handle].add_descriptor(descriptor) else: raise BleakError( "This descriptor is already present in this BleakGATTServiceCollection!" )
def add_characteristic(self, characteristic: BleakGATTCharacteristic): """Add a :py:class:`~BleakGATTCharacteristic` to the service collection. Should not be used by end user, but rather by `bleak` itself. """ if characteristic.handle not in self.__characteristics: self.__characteristics[characteristic.handle] = characteristic self.__services[characteristic.service_uuid].add_characteristic( characteristic) else: raise BleakError( "This characteristic is already present in this BleakGATTServiceCollection!" )
def get_characteristic( self, specifier: Union[int, str, UUID]) -> BleakGATTCharacteristic: """Get a characteristic by handle (int) or UUID (str or uuid.UUID)""" if isinstance(specifier, int): return self.characteristics.get(specifier, None) else: # Assume uuid usage. x = list( filter(lambda x: x.uuid == str(specifier), self.characteristics.values())) if len(x) > 1: raise BleakError( "Multiple Characteristics with this UUID, refer to your desired characteristic by the `handle` attribute instead." ) else: return x[0] if x else None
def __init__(self, address_or_ble_device: str | BLEDevice, *args: Any, **kwargs: Any) -> None: """Initialize the BleakClient.""" if isinstance(address_or_ble_device, BLEDevice): super().__init__(address_or_ble_device, *args, **kwargs) return report( "attempted to call BleakClient with an address instead of a BLEDevice", exclude_integrations={"bluetooth"}, error_if_core=False, ) assert MANAGER is not None ble_device = MANAGER.async_ble_device_from_address( address_or_ble_device, True) if ble_device is None: raise BleakError( f"No device found for address {address_or_ble_device}") super().__init__(ble_device, *args, **kwargs)
async def test_adapter_needs_reset_at_start(hass, caplog, one_adapter, error): """Test we cycle the adapter when it needs a restart.""" with patch( "homeassistant.components.bluetooth.scanner.OriginalBleakScanner.start", side_effect=[BleakError(error), None], ), patch( "homeassistant.components.bluetooth.util.recover_adapter", return_value=True ) as mock_recover_adapter: await async_setup_with_one_adapter(hass) hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) await hass.async_block_till_done() assert len(mock_recover_adapter.mock_calls) == 1 hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP) await hass.async_block_till_done()
def get_service(self, specifier: Union[int, str, UUID]) -> BleakGATTService: """Get a service by handle (int) or UUID (str or uuid.UUID)""" if isinstance(specifier, int): return self.services.get(specifier, None) else: _specifier = str(specifier).lower() # Assume uuid usage. x = list( filter( lambda x: x.uuid.lower() == _specifier, self.services.values(), ) ) if len(x) > 1: raise BleakError( "Multiple Services with this UUID, refer to your desired service by the `handle` attribute instead." ) else: return x[0] if x else None
def extract_service_handle_from_path(path): try: return int(path[-4:], 16) except Exception as e: raise BleakError( f"Could not parse service handle from path: {path}") from e
async def read_gatt_char(self, *args, **kwargs): """Mock BleakClient.read_gatt_char.""" raise BleakError("Failed")