Skip to content

Commit

Permalink
husqvarna_automower_ble: Try to be more robust against drop outs
Browse files Browse the repository at this point in the history
Signed-off-by: Alistair Francis <alistair@alistair23.me>
  • Loading branch information
alistair23 committed Feb 4, 2024
1 parent 0734699 commit 3cd6a8e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
10 changes: 9 additions & 1 deletion homeassistant/components/husqvarna_automower_ble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
from automower_ble.mower import Mower

from bleak.backends.device import BLEDevice
from bleak_retry_connector import (
close_stale_connections_by_address,
get_device,
)

from homeassistant.components import bluetooth
from homeassistant.config_entries import ConfigEntry
Expand All @@ -33,10 +37,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

mower = Mower(channel_id, address)

await close_stale_connections_by_address(address)

LOGGER.debug(
"connecting to " + address + " with channel ID " + str(channel_id) + "..."
)
device = bluetooth.async_ble_device_from_address(hass, address, connectable=True)
device = bluetooth.async_ble_device_from_address(
hass, address, connectable=True
) or await get_device(address)
if await mower.connect(device) == False:
return False
LOGGER.debug("connected and paired")
Expand Down
56 changes: 44 additions & 12 deletions homeassistant/components/husqvarna_automower_ble/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

from automower_ble.mower import Mower

from bleak import BleakError
from bleak_retry_connector import (
close_stale_connections_by_address,
get_device,
)

from homeassistant.components import bluetooth
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
Expand Down Expand Up @@ -54,25 +60,51 @@ async def async_shutdown(self) -> None:
if await self.mower.is_connected():
await self.mower.disconnect()

async def _async_find_device(self):
_LOGGER.debug("Trying to reconnect")
await close_stale_connections_by_address(self.address)

device = bluetooth.async_ble_device_from_address(
self.hass, self.address, connectable=True
) or await get_device(self.address)
if not device:
raise UpdateFailed(f"Can't find device")

if await self.mower.connect(device) == False:
raise UpdateFailed(f"Failed to connect")

async def _async_update_data(self) -> dict[str, bytes]:
"""Poll the device."""
_LOGGER.debug("Polling device")

data: dict[str, bytes] = {}

if not await self.mower.is_connected():
device = bluetooth.async_ble_device_from_address(
self.hass, self.address, connectable=True
)
if await self.mower.connect(device) == False:
return

data["battery_level"] = await self.mower.battery_level()
_LOGGER.debug(data["battery_level"])
data["activity"] = await self.mower.mower_activity()
_LOGGER.debug(data["activity"])
data["state"] = await self.mower.mower_state()
_LOGGER.debug(data["state"])
await self._async_find_device()

try:
data["battery_level"] = await self.mower.battery_level()
_LOGGER.debug(data["battery_level"])
if data["battery_level"] is None:
await self._async_find_device()
raise UpdateFailed("Error getting data from device")

data["activity"] = await self.mower.mower_activity()
_LOGGER.debug(data["activity"])
if data["activity"] is None:
await self._async_find_device()
raise UpdateFailed("Error getting data from device")

data["state"] = await self.mower.mower_state()
_LOGGER.debug(data["state"])
if data["state"] is None:
await self._async_find_device()
raise UpdateFailed("Error getting data from device")

except BleakError as err:
_LOGGER.error("Error getting data from device")
await self._async_find_device()
raise UpdateFailed("Error getting data from device") from err

return data

Expand Down

0 comments on commit 3cd6a8e

Please sign in to comment.