mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Combine Bluetooth update coordinator subscriptions to reduce code duplication (#97503)
This commit is contained in:
parent
5c4e47c127
commit
37cdd51183
@ -41,8 +41,7 @@ class BasePassiveBluetoothCoordinator(ABC):
|
||||
self.logger = logger
|
||||
self.address = address
|
||||
self.connectable = connectable
|
||||
self._cancel_track_unavailable: CALLBACK_TYPE | None = None
|
||||
self._cancel_bluetooth_advertisements: CALLBACK_TYPE | None = None
|
||||
self._on_stop: list[CALLBACK_TYPE] = []
|
||||
self.mode = mode
|
||||
self._last_unavailable_time = 0.0
|
||||
self._last_name = address
|
||||
@ -97,27 +96,31 @@ class BasePassiveBluetoothCoordinator(ABC):
|
||||
@callback
|
||||
def _async_start(self) -> None:
|
||||
"""Start the callbacks."""
|
||||
self._cancel_bluetooth_advertisements = async_register_callback(
|
||||
self.hass,
|
||||
self._async_handle_bluetooth_event,
|
||||
BluetoothCallbackMatcher(
|
||||
address=self.address, connectable=self.connectable
|
||||
),
|
||||
self.mode,
|
||||
self._on_stop.append(
|
||||
async_register_callback(
|
||||
self.hass,
|
||||
self._async_handle_bluetooth_event,
|
||||
BluetoothCallbackMatcher(
|
||||
address=self.address, connectable=self.connectable
|
||||
),
|
||||
self.mode,
|
||||
)
|
||||
)
|
||||
self._cancel_track_unavailable = async_track_unavailable(
|
||||
self.hass, self._async_handle_unavailable, self.address, self.connectable
|
||||
self._on_stop.append(
|
||||
async_track_unavailable(
|
||||
self.hass,
|
||||
self._async_handle_unavailable,
|
||||
self.address,
|
||||
self.connectable,
|
||||
)
|
||||
)
|
||||
|
||||
@callback
|
||||
def _async_stop(self) -> None:
|
||||
"""Stop the callbacks."""
|
||||
if self._cancel_bluetooth_advertisements is not None:
|
||||
self._cancel_bluetooth_advertisements()
|
||||
self._cancel_bluetooth_advertisements = None
|
||||
if self._cancel_track_unavailable is not None:
|
||||
self._cancel_track_unavailable()
|
||||
self._cancel_track_unavailable = None
|
||||
for unsub in self._on_stop:
|
||||
unsub()
|
||||
self._on_stop.clear()
|
||||
|
||||
@callback
|
||||
def _async_handle_unavailable(
|
||||
|
Loading…
x
Reference in New Issue
Block a user