Skip to content

Commit

Permalink
Feature/retry connector (#81)
Browse files Browse the repository at this point in the history
* use retry connector
* adapted link quality sensor
* removed max_retry setting
* Update manifest.json
* Revert dependency changes (of commit 96cdac9)
  • Loading branch information
patman15 authored Nov 11, 2024
1 parent 620959c commit 7e11369
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Platform | Description | Unit | Details
`sensor` | stored energy | `Wh` | currently stored energy
`sensor` | temperature | `°C` | individual temperature values are available as attribute to this sensor, if the BMS supports multiple sensors
`sensor` | voltage | `V` | overall battery voltage
`sensor`* | link quality | `%` | successful BMS queries out of all attempts
`sensor`* | link quality | `%` | successful BMS queries from the last hundred update periods
`sensor`* | RSSI | `dBm`| received signal strength indicator

*) In case sensors are reported `unavailable` please enable the diagnostic sensors, i.e. `RSSI` and `link quality` and check your connection quality. The value of `link quality` results from (temporarily) bad `RSSI` values, which are impacted by disturbances of the Bluetooth communication.
Expand Down
2 changes: 1 addition & 1 deletion custom_components/bms_ble/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
] # available BMS types
DOMAIN: Final = "bms_ble"
LOGGER: Final = logging.getLogger(__package__)
UPDATE_INTERVAL: Final = 30 # in seconds
UPDATE_INTERVAL: Final = 30 # [s]

# attributes (do not change)
ATTR_CELL_VOLTAGES: Final = "cell_voltages" # [V]
Expand Down
6 changes: 5 additions & 1 deletion custom_components/bms_ble/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from collections import deque
from datetime import timedelta
from time import monotonic

from bleak.backends.device import BLEDevice
from bleak.exc import BleakError
Expand Down Expand Up @@ -92,6 +93,7 @@ async def _async_update_data(self) -> BMSsample:

LOGGER.debug("%s: BMS data update", self.name)

start = monotonic()
try:
battery_info = await self._device.async_update()
if not battery_info:
Expand All @@ -111,7 +113,9 @@ async def _async_update_data(self) -> BMSsample:
f"device communicating failed: {err!s} ({type(err).__name__})"
) from err
finally:
self._link_q.append(False)
self._link_q.extend(
[False] * (1 + int((monotonic() - start) / UPDATE_INTERVAL))
)

self._link_q[-1] = True # set success
LOGGER.debug("BMS data sample %s", battery_info)
Expand Down
1 change: 1 addition & 0 deletions custom_components/bms_ble/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"integration_type": "device",
"iot_class": "local_polling",
"issue_tracker": "https://github.com/patman15/BMS_BLE-HA/issues",
"loggers": ["bleak_retry_connector"],
"requirements": [],
"version": "1.9.0"
}
11 changes: 9 additions & 2 deletions custom_components/bms_ble/plugins/basebms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from bleak.backends.characteristic import BleakGATTCharacteristic
from bleak.backends.device import BLEDevice
from bleak.exc import BleakError
from bleak_retry_connector import establish_connection

from custom_components.bms_ble.const import (
ATTR_BATTERY_CHARGING,
Expand Down Expand Up @@ -58,7 +59,7 @@ def __init__(
self._notification_method: Final = notification_handler
self._ble_device: Final = ble_device
self._reconnect: Final = reconnect
self._client: Final = BleakClient(
self._client = BleakClient(
self._ble_device,
disconnected_callback=self._on_disconnect,
services=[*self.uuid_services()],
Expand Down Expand Up @@ -184,7 +185,13 @@ async def _connect(self) -> None:
return

self.logger.debug("Connecting BMS (%s)", self._ble_device.name)
await self._client.connect()
self._client = await establish_connection(
client_class=BleakClient,
device=self._ble_device,
name=self._ble_device.address,
disconnected_callback=self._on_disconnect,
services=[*self.uuid_services()],
)
await self._init_characteristics()

async def disconnect(self) -> None:
Expand Down

0 comments on commit 7e11369

Please sign in to comment.