Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine Bluetooth update coordinator subscriptions to reduce code duplication #97503

Merged
merged 2 commits into from
Jul 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions homeassistant/components/bluetooth/update_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ def __init__(
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
Expand Down Expand Up @@ -93,27 +92,31 @@ def available(self) -> bool:
@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(
Expand Down