-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fix some issues and move to bluetooth update coordinator
- Loading branch information
Showing
7 changed files
with
143 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
"""AC Infinity Coordinator.""" | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
import contextlib | ||
import logging | ||
|
||
from ac_infinity_ble import ACInfinityController | ||
import async_timeout | ||
from bleak.backends.device import BLEDevice | ||
|
||
from homeassistant.components import bluetooth | ||
from homeassistant.components.bluetooth.active_update_coordinator import ( | ||
ActiveBluetoothDataUpdateCoordinator, | ||
) | ||
from homeassistant.core import CoreState, HomeAssistant, callback | ||
|
||
|
||
DEVICE_STARTUP_TIMEOUT = 30 | ||
|
||
|
||
class ACInfinityDataUpdateCoordinator(ActiveBluetoothDataUpdateCoordinator[None]): | ||
"""Class to manage fetching switchbot data.""" | ||
|
||
def __init__( | ||
self, | ||
hass: HomeAssistant, | ||
logger: logging.Logger, | ||
ble_device: BLEDevice, | ||
controller: ACInfinityController, | ||
) -> None: | ||
"""Initialize global switchbot data updater.""" | ||
super().__init__( | ||
hass=hass, | ||
logger=logger, | ||
address=ble_device.address, | ||
needs_poll_method=self._needs_poll, | ||
poll_method=self._async_update, | ||
mode=bluetooth.BluetoothScanningMode.ACTIVE, | ||
connectable=True, | ||
) | ||
self.ble_device = ble_device | ||
self.controller = controller | ||
self._ready_event = asyncio.Event() | ||
self._was_unavailable = True | ||
|
||
@callback | ||
def _needs_poll( | ||
self, | ||
service_info: bluetooth.BluetoothServiceInfoBleak, | ||
seconds_since_last_poll: float | None, | ||
) -> bool: | ||
# Only poll if hass is running, we need to poll, | ||
# and we actually have a way to connect to the device | ||
return ( | ||
self.hass.state == CoreState.running | ||
and (seconds_since_last_poll is None or seconds_since_last_poll > 30) | ||
and bool( | ||
bluetooth.async_ble_device_from_address( | ||
self.hass, service_info.device.address, connectable=True | ||
) | ||
) | ||
) | ||
|
||
async def _async_update( | ||
self, service_info: bluetooth.BluetoothServiceInfoBleak | ||
) -> None: | ||
"""Poll the device.""" | ||
await self.controller.update() | ||
|
||
@callback | ||
def _async_handle_unavailable( | ||
self, service_info: bluetooth.BluetoothServiceInfoBleak | ||
) -> None: | ||
"""Handle the device going unavailable.""" | ||
super()._async_handle_unavailable(service_info) | ||
self._was_unavailable = True | ||
|
||
@callback | ||
def _async_handle_bluetooth_event( | ||
self, | ||
service_info: bluetooth.BluetoothServiceInfoBleak, | ||
change: bluetooth.BluetoothChange, | ||
) -> None: | ||
"""Handle a Bluetooth event.""" | ||
self.ble_device = service_info.device | ||
self.controller.set_ble_device_and_advertisement_data( | ||
service_info.device, service_info.advertisement | ||
) | ||
if self.controller.name: | ||
self._ready_event.set() | ||
self.logger.debug( | ||
"%s: AC Infinity data: %s", self.ble_device.address, self.controller.state | ||
) | ||
self._was_unavailable = False | ||
super()._async_handle_bluetooth_event(service_info, change) | ||
|
||
async def async_wait_ready(self) -> bool: | ||
"""Wait for the device to be ready.""" | ||
with contextlib.suppress(asyncio.TimeoutError): | ||
async with async_timeout.timeout(DEVICE_STARTUP_TIMEOUT): | ||
await self._ready_event.wait() | ||
return True | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters