Skip to content

Commit

Permalink
revert start/stop notify
Browse files Browse the repository at this point in the history
  • Loading branch information
patman15 committed Jun 13, 2024
1 parent 31eb88e commit f8b8545
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 40 deletions.
2 changes: 1 addition & 1 deletion custom_components/bms_ble/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@
"issue_tracker": "https://github.com/patman15/BMS_BLE-HA/issues",
"loggers": ["bms_ble", "ogt_bms", "daly_bms", "jikong_bms"],
"requirements": [],
"version": "1.3.0b2"
"version": "1.3.0b3"
}
44 changes: 13 additions & 31 deletions custom_components/bms_ble/plugins/jikong_bms.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def __init__(self, ble_device: BLEDevice, reconnect: bool = False) -> None:
self._data_event = asyncio.Event()
self._connected = False # flag to indicate active BLE connection
self._char_write_handle: int | None = None
self._char_notify_handle: int | None = None
self._FIELDS: list[tuple[str, int, int, bool, Callable[[int], int | float]]] = [
(ATTR_TEMPERATURE, 144, 2, True, lambda x: float(x / 10)),
(ATTR_VOLTAGE, 150, 4, False, lambda x: float(x / 1000)),
Expand Down Expand Up @@ -169,7 +168,8 @@ async def _connect(self) -> None:
services=[UUID_SERVICE],
)
await self._client.connect()

char_notify_handle: int | None = None
self._char_write_handle = None
for service in self._client.services:
for char in service.characteristics:
# value: bytearray = bytearray()
Expand All @@ -185,27 +185,27 @@ async def _connect(self) -> None:
)
if char.uuid == UUID_CHAR:
if "notify" in char.properties:
self._char_notify_handle = char.handle
char_notify_handle = char.handle
if (
"write" in char.properties
or "write-without-response" in char.properties
):
self._char_write_handle = char.handle
if self._char_notify_handle is None or self._char_write_handle is None:
if char_notify_handle is None or self._char_write_handle is None:
LOGGER.debug(
"(%s) Failed to detect characteristics", self._ble_device.name
)
await self._client.disconnect()
return

LOGGER.debug(
"(%s) Using characteristics handle #%i (notify), #%i (write)",
self._ble_device.name,
self._char_notify_handle,
char_notify_handle,
self._char_write_handle,
)

await self._start_notify()
await self._client.start_notify(
char_notify_handle or 0, self._notification_handler
)

# query device info
await self._client.write_gatt_char(
Expand All @@ -216,25 +216,6 @@ async def _connect(self) -> None:
else:
LOGGER.debug("BMS %s already connected", self._ble_device.name)

async def _start_notify(self) -> None:
"""Start notification from BMS characteristic."""

assert self._client
await self._client.start_notify(
self._char_notify_handle or 0, self._notification_handler
)

# request cell info update
await self._client.write_gatt_char(
self._char_write_handle or 0, data=self._cmd(b"\x96")
)

async def _stop_notify(self) -> None:
"""Stop notification from BMS characteristic."""

assert self._client
await self._client.stop_notify(self._char_notify_handle or 0)

async def disconnect(self) -> None:
"""Disconnect the BMS and includes stoping notifications."""

Expand All @@ -247,8 +228,6 @@ async def disconnect(self) -> None:
LOGGER.warning("Disconnect failed!")

self._client = None
self._char_notify_handle = None
self._char_write_handle = None

def _crc(self, frame: bytes):
"""Calculate Jikong frame CRC."""
Expand All @@ -275,9 +254,12 @@ async def async_update(self) -> dict[str, int | float | bool]:
)
return {}

await self._start_notify()
# query cell info
await self._client.write_gatt_char(
self._char_write_handle or 0, data=self._cmd(b"\x96")
)

await asyncio.wait_for(self._wait_event(), timeout=BAT_TIMEOUT)
await self._stop_notify()

if self._data_final is None:
return {}
Expand Down
8 changes: 0 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,6 @@ async def start_notify( # type: ignore
assert self._connected, "start_notify called, but client not connected."
self._notify_callback = callback

async def stop_notify( # type: ignore
self, char_specifier: Union[BleakGATTCharacteristic, int, str]
) -> None:
"""Mock stop_notify."""
LOGGER.debug("MockBleakClient stop_notify for %s", char_specifier)
assert self._connected, "stop_notify called, but client not connected."
self._notify_callback = None

async def write_gatt_char( # type: ignore
self,
char_specifier: BleakGATTCharacteristic | int | str,
Expand Down

0 comments on commit f8b8545

Please sign in to comment.