Allow data from un-connectable sources in fjäråskupan (#77236)

This commit is contained in:
Joakim Plate 2022-08-31 05:40:25 +02:00 committed by GitHub
parent ff3d3088ee
commit 4185a70882
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 11 deletions

View File

@ -22,6 +22,7 @@ from homeassistant.components.bluetooth import (
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.dispatcher import ( from homeassistant.helpers.dispatcher import (
async_dispatcher_connect, async_dispatcher_connect,
@ -33,6 +34,11 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
from .const import DISPATCH_DETECTION, DOMAIN from .const import DISPATCH_DETECTION, DOMAIN
class UnableToConnect(HomeAssistantError):
"""Exception to indicate that we can not connect to device."""
PLATFORMS = [ PLATFORMS = [
Platform.BINARY_SENSOR, Platform.BINARY_SENSOR,
Platform.FAN, Platform.FAN,
@ -75,28 +81,39 @@ class Coordinator(DataUpdateCoordinator[State]):
async def _async_update_data(self) -> State: async def _async_update_data(self) -> State:
"""Handle an explicit update request.""" """Handle an explicit update request."""
if self._refresh_was_scheduled: if self._refresh_was_scheduled:
if async_address_present(self.hass, self.device.address): if async_address_present(self.hass, self.device.address, False):
return self.device.state return self.device.state
raise UpdateFailed( raise UpdateFailed(
"No data received within schedule, and device is no longer present" "No data received within schedule, and device is no longer present"
) )
await self.device.update() if (
ble_device := async_ble_device_from_address(
self.hass, self.device.address, True
)
) is None:
raise UpdateFailed("No connectable path to device")
async with self.device.connect(ble_device) as device:
await device.update()
return self.device.state return self.device.state
def detection_callback(self, service_info: BluetoothServiceInfoBleak) -> None: def detection_callback(self, service_info: BluetoothServiceInfoBleak) -> None:
"""Handle a new announcement of data.""" """Handle a new announcement of data."""
self.device.device = service_info.device
self.device.detection_callback(service_info.device, service_info.advertisement) self.device.detection_callback(service_info.device, service_info.advertisement)
self.async_set_updated_data(self.device.state) self.async_set_updated_data(self.device.state)
@asynccontextmanager @asynccontextmanager
async def async_connect_and_update(self) -> AsyncIterator[Device]: async def async_connect_and_update(self) -> AsyncIterator[Device]:
"""Provide an up to date device for use during connections.""" """Provide an up to date device for use during connections."""
if ble_device := async_ble_device_from_address(self.hass, self.device.address): if (
self.device.device = ble_device ble_device := async_ble_device_from_address(
async with self.device: self.hass, self.device.address, True
yield self.device )
) is None:
raise UnableToConnect("No connectable path to device")
async with self.device.connect(ble_device) as device:
yield device
self.async_set_updated_data(self.device.state) self.async_set_updated_data(self.device.state)
@ -126,7 +143,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
else: else:
_LOGGER.debug("Detected: %s", service_info) _LOGGER.debug("Detected: %s", service_info)
device = Device(service_info.device) device = Device(service_info.device.address)
device_info = DeviceInfo( device_info = DeviceInfo(
connections={(dr.CONNECTION_BLUETOOTH, service_info.address)}, connections={(dr.CONNECTION_BLUETOOTH, service_info.address)},
identifiers={(DOMAIN, service_info.address)}, identifiers={(DOMAIN, service_info.address)},
@ -149,6 +166,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
BluetoothCallbackMatcher( BluetoothCallbackMatcher(
manufacturer_id=20296, manufacturer_id=20296,
manufacturer_data_start=[79, 68, 70, 74, 65, 82], manufacturer_data_start=[79, 68, 70, 74, 65, 82],
connectable=False,
), ),
BluetoothScanningMode.ACTIVE, BluetoothScanningMode.ACTIVE,
) )

View File

@ -3,13 +3,14 @@
"name": "Fj\u00e4r\u00e5skupan", "name": "Fj\u00e4r\u00e5skupan",
"config_flow": true, "config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/fjaraskupan", "documentation": "https://www.home-assistant.io/integrations/fjaraskupan",
"requirements": ["fjaraskupan==1.0.2"], "requirements": ["fjaraskupan==2.0.0"],
"codeowners": ["@elupus"], "codeowners": ["@elupus"],
"iot_class": "local_polling", "iot_class": "local_polling",
"loggers": ["bleak", "fjaraskupan"], "loggers": ["bleak", "fjaraskupan"],
"dependencies": ["bluetooth"], "dependencies": ["bluetooth"],
"bluetooth": [ "bluetooth": [
{ {
"connectable": false,
"manufacturer_id": 20296, "manufacturer_id": 20296,
"manufacturer_data_start": [79, 68, 70, 74, 65, 82] "manufacturer_data_start": [79, 68, 70, 74, 65, 82]
} }

View File

@ -19,6 +19,7 @@ BLUETOOTH: list[dict[str, bool | str | int | list[int]]] = [
}, },
{ {
"domain": "fjaraskupan", "domain": "fjaraskupan",
"connectable": False,
"manufacturer_id": 20296, "manufacturer_id": 20296,
"manufacturer_data_start": [ "manufacturer_data_start": [
79, 79,

View File

@ -670,7 +670,7 @@ fivem-api==0.1.2
fixerio==1.0.0a0 fixerio==1.0.0a0
# homeassistant.components.fjaraskupan # homeassistant.components.fjaraskupan
fjaraskupan==1.0.2 fjaraskupan==2.0.0
# homeassistant.components.flipr # homeassistant.components.flipr
flipr-api==1.4.2 flipr-api==1.4.2

View File

@ -489,7 +489,7 @@ file-read-backwards==2.0.0
fivem-api==0.1.2 fivem-api==0.1.2
# homeassistant.components.fjaraskupan # homeassistant.components.fjaraskupan
fjaraskupan==1.0.2 fjaraskupan==2.0.0
# homeassistant.components.flipr # homeassistant.components.flipr
flipr-api==1.4.2 flipr-api==1.4.2