mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Improve performance of bluetooth coordinators (#98997)
This commit is contained in:
parent
c2713f0aed
commit
3ebf96143a
@ -39,6 +39,7 @@ class BasePassiveBluetoothCoordinator(ABC):
|
|||||||
self.mode = mode
|
self.mode = mode
|
||||||
self._last_unavailable_time = 0.0
|
self._last_unavailable_time = 0.0
|
||||||
self._last_name = address
|
self._last_name = address
|
||||||
|
self._available = async_address_present(hass, address, connectable)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_start(self) -> CALLBACK_TYPE:
|
def async_start(self) -> CALLBACK_TYPE:
|
||||||
@ -85,7 +86,17 @@ class BasePassiveBluetoothCoordinator(ABC):
|
|||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
"""Return if the device is available."""
|
"""Return if the device is available."""
|
||||||
return async_address_present(self.hass, self.address, self.connectable)
|
return self._available
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _async_handle_bluetooth_event_internal(
|
||||||
|
self,
|
||||||
|
service_info: BluetoothServiceInfoBleak,
|
||||||
|
change: BluetoothChange,
|
||||||
|
) -> None:
|
||||||
|
"""Handle a bluetooth event."""
|
||||||
|
self._available = True
|
||||||
|
self._async_handle_bluetooth_event(service_info, change)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_start(self) -> None:
|
def _async_start(self) -> None:
|
||||||
@ -93,7 +104,7 @@ class BasePassiveBluetoothCoordinator(ABC):
|
|||||||
self._on_stop.append(
|
self._on_stop.append(
|
||||||
async_register_callback(
|
async_register_callback(
|
||||||
self.hass,
|
self.hass,
|
||||||
self._async_handle_bluetooth_event,
|
self._async_handle_bluetooth_event_internal,
|
||||||
BluetoothCallbackMatcher(
|
BluetoothCallbackMatcher(
|
||||||
address=self.address, connectable=self.connectable
|
address=self.address, connectable=self.connectable
|
||||||
),
|
),
|
||||||
@ -123,3 +134,4 @@ class BasePassiveBluetoothCoordinator(ABC):
|
|||||||
"""Handle the device going unavailable."""
|
"""Handle the device going unavailable."""
|
||||||
self._last_unavailable_time = service_info.time
|
self._last_unavailable_time = service_info.time
|
||||||
self._last_name = service_info.name
|
self._last_name = service_info.name
|
||||||
|
self._available = False
|
||||||
|
@ -406,7 +406,7 @@ async def test_exception_from_update_method(
|
|||||||
"""Generate mock data."""
|
"""Generate mock data."""
|
||||||
nonlocal run_count
|
nonlocal run_count
|
||||||
run_count += 1
|
run_count += 1
|
||||||
if run_count == 1:
|
if run_count == 2:
|
||||||
raise Exception("Test exception")
|
raise Exception("Test exception")
|
||||||
return GENERIC_PASSIVE_BLUETOOTH_DATA_UPDATE
|
return GENERIC_PASSIVE_BLUETOOTH_DATA_UPDATE
|
||||||
|
|
||||||
@ -436,6 +436,7 @@ async def test_exception_from_update_method(
|
|||||||
processor.async_add_listener(MagicMock())
|
processor.async_add_listener(MagicMock())
|
||||||
|
|
||||||
inject_bluetooth_service_info(hass, GENERIC_BLUETOOTH_SERVICE_INFO)
|
inject_bluetooth_service_info(hass, GENERIC_BLUETOOTH_SERVICE_INFO)
|
||||||
|
saved_callback(GENERIC_BLUETOOTH_SERVICE_INFO, BluetoothChange.ADVERTISEMENT)
|
||||||
assert processor.available is True
|
assert processor.available is True
|
||||||
|
|
||||||
# We should go unavailable once we get an exception
|
# We should go unavailable once we get an exception
|
||||||
@ -473,7 +474,7 @@ async def test_bad_data_from_update_method(
|
|||||||
"""Generate mock data."""
|
"""Generate mock data."""
|
||||||
nonlocal run_count
|
nonlocal run_count
|
||||||
run_count += 1
|
run_count += 1
|
||||||
if run_count == 1:
|
if run_count == 2:
|
||||||
return "bad_data"
|
return "bad_data"
|
||||||
return GENERIC_PASSIVE_BLUETOOTH_DATA_UPDATE
|
return GENERIC_PASSIVE_BLUETOOTH_DATA_UPDATE
|
||||||
|
|
||||||
@ -503,6 +504,7 @@ async def test_bad_data_from_update_method(
|
|||||||
processor.async_add_listener(MagicMock())
|
processor.async_add_listener(MagicMock())
|
||||||
|
|
||||||
inject_bluetooth_service_info(hass, GENERIC_BLUETOOTH_SERVICE_INFO)
|
inject_bluetooth_service_info(hass, GENERIC_BLUETOOTH_SERVICE_INFO)
|
||||||
|
saved_callback(GENERIC_BLUETOOTH_SERVICE_INFO, BluetoothChange.ADVERTISEMENT)
|
||||||
assert processor.available is True
|
assert processor.available is True
|
||||||
|
|
||||||
# We should go unavailable once we get bad data
|
# We should go unavailable once we get bad data
|
||||||
|
Loading…
x
Reference in New Issue
Block a user