async def detection_callback( ble_device: BLEDevice, advertisement_data: AdvertisementData) -> None: if not device_filter(ble_device, advertisement_data): return _LOGGER.debug("Detection: %s %s - %s", ble_device.name, ble_device, advertisement_data) data = state.devices.get(ble_device.address) if data: data.device.detection_callback(ble_device, advertisement_data) data.coordinator.async_set_updated_data(data.device.state) else: device = Device(ble_device) device.detection_callback(ble_device, advertisement_data) async def async_update_data(): """Handle an explicit update request.""" await device.update() return device.state coordinator: DataUpdateCoordinator[State] = DataUpdateCoordinator( hass, logger=_LOGGER, name="Fjaraskupan Updater", update_interval=timedelta(seconds=120), update_method=async_update_data, ) coordinator.async_set_updated_data(device.state) device_info: DeviceInfo = { "identifiers": {(DOMAIN, ble_device.address)}, "manufacturer": "Fjäråskupan", "name": "Fjäråskupan", } device_state = DeviceState(device, coordinator, device_info) state.devices[ble_device.address] = device_state async_dispatcher_send(hass, f"{DISPATCH_DETECTION}.{entry.entry_id}", device_state)
async def detection_callback( ble_device: BLEDevice, advertisement_data: AdvertisementData) -> None: if data := state.coordinators.get(ble_device.address): _LOGGER.debug("Update: %s %s - %s", ble_device.name, ble_device, advertisement_data) data.detection_callback(ble_device, advertisement_data) else: if not device_filter(ble_device, advertisement_data): return _LOGGER.debug("Detected: %s %s - %s", ble_device.name, ble_device, advertisement_data) device = Device(ble_device) device_info = DeviceInfo( identifiers={(DOMAIN, ble_device.address)}, manufacturer="Fjäråskupan", name="Fjäråskupan", ) coordinator: Coordinator = Coordinator(hass, device, device_info) coordinator.detection_callback(ble_device, advertisement_data) state.coordinators[ble_device.address] = coordinator async_dispatcher_send(hass, f"{DISPATCH_DETECTION}.{entry.entry_id}", coordinator) scanner.register_detection_callback(detection_callback)
ble_device: BLEDevice, advertisement_data: AdvertisementData) -> None: if data := state.devices.get(ble_device.address): _LOGGER.debug("Update: %s %s - %s", ble_device.name, ble_device, advertisement_data) data.device.detection_callback(ble_device, advertisement_data) data.coordinator.async_set_updated_data(data.device.state) else: if not device_filter(ble_device, advertisement_data): return _LOGGER.debug("Detected: %s %s - %s", ble_device.name, ble_device, advertisement_data) device = Device(ble_device) device.detection_callback(ble_device, advertisement_data) async def async_update_data(): """Handle an explicit update request.""" await device.update() return device.state coordinator: DataUpdateCoordinator[State] = DataUpdateCoordinator( hass, logger=_LOGGER, name="Fjaraskupan Updater", update_interval=timedelta(seconds=120), update_method=async_update_data, ) coordinator.async_set_updated_data(device.state)
state = EntryState({}) hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = state def detection_callback(service_info: BluetoothServiceInfoBleak, change: BluetoothChange) -> None: if change != BluetoothChange.ADVERTISEMENT: return if data := state.coordinators.get(service_info.address): _LOGGER.debug("Update: %s", service_info) data.detection_callback(service_info) else: _LOGGER.debug("Detected: %s", service_info) device = Device(service_info.device) device_info = DeviceInfo( connections={(dr.CONNECTION_BLUETOOTH, service_info.address)}, identifiers={(DOMAIN, service_info.address)}, manufacturer="Fjäråskupan", name="Fjäråskupan", ) coordinator: Coordinator = Coordinator(hass, device, device_info) coordinator.detection_callback(service_info) state.coordinators[service_info.address] = coordinator async_dispatcher_send(hass, f"{DISPATCH_DETECTION}.{entry.entry_id}", coordinator)