Airthings / airthings-ble

Library to control Airthings devices through BLE, primarily meant to be used in Home Assistant.
MIT License
21 stars 11 forks source link

Refactor to better handle failures and reduce battery drain #24

Closed bdraco closed 9 months ago

bdraco commented 10 months ago

Note the battery drain reduction change is only effective with this change in HA

diff --git a/homeassistant/components/airthings_ble/__init__.py b/homeassistant/components/airthings_ble/__init__.py
index 1d62442f14d..a26675cc310 100644
--- a/homeassistant/components/airthings_ble/__init__.py
+++ b/homeassistant/components/airthings_ble/__init__.py
@@ -40,11 +40,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
             f"Could not find Airthings device with address {address}"
         )

+    airthings = AirthingsBluetoothDeviceData(_LOGGER, elevation, is_metric)
+
     async def _async_update_method() -> AirthingsDevice:
         """Get data from Airthings BLE."""
         ble_device = bluetooth.async_ble_device_from_address(hass, address)
-        airthings = AirthingsBluetoothDeviceData(_LOGGER, elevation, is_metric)
-
         try:
             data = await airthings.update_device(ble_device)  # type: ignore[arg-type]
         except Exception as err:
LaStrada commented 9 months ago

2 linter errors, but other than that this PR seems to be working. I've tested this the last few days and I have had zero errors (except my previous comment).