Skip to content

Commit

Permalink
accept write responses
Browse files Browse the repository at this point in the history
  • Loading branch information
patman15 committed Jan 8, 2025
1 parent abdd2f7 commit 367299b
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions custom_components/bms_ble/plugins/daly_bms.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Module to support Daly Smart BMS."""

import asyncio
#import asyncio
from collections.abc import Callable
from datetime import datetime as dt
from typing import Any, Final

from bleak.backends.device import BLEDevice
from bleak.exc import BleakError

#from bleak.exc import BleakError
from bleak.uuids import normalize_uuid_str

from custom_components.bms_ble.const import (
Expand Down Expand Up @@ -101,8 +102,8 @@ def _calc_values() -> set[str]:
ATTR_TEMPERATURE,
}

def _not_handler(self, _sender, data: bytearray) -> None:
self._log.debug("RX BLE data on 2a05: %s", data)
# def _not_handler(self, _sender, data: bytearray) -> None:
# self._log.debug("RX BLE data on 2a05: %s", data)

async def _init_connection(self) -> None:
"""Connect to the BMS and setup notification if not connected."""
Expand All @@ -111,17 +112,17 @@ async def _init_connection(self) -> None:
if not self.name.startswith("DL-FB4"):
return

await self._client.start_notify("2a05", self._not_handler)
# await self._client.start_notify("2a05", self._not_handler)

for char in ["ff01", "ff02", "fff1", "fff2", "fffa", "fffb", "fff3"]:
try:
self._log.debug(
"Reading %s: %s", char, await self._client.read_gatt_char(char)
)
asyncio.sleep(0.2)
except (BleakError, TimeoutError) as ex:
self._log.debug("Exception reading %s: %s", char, ex)
continue
# for char in ["ff01", "ff02", "fff1", "fff2", "fffa", "fffb", "fff3"]:
# try:
# self._log.debug(
# "Reading %s: %s", char, await self._client.read_gatt_char(char)
# )
# asyncio.sleep(0.3)
# except (BleakError, TimeoutError) as ex:
# self._log.debug("Exception reading %s: %s", char, ex)
# continue

# await self._await_reply(
# b"\xa5\x40\x02\x08\x00\x00\x00\x00\x00\x00\x00\x00\xef",
Expand Down Expand Up @@ -153,8 +154,12 @@ def _notification_handler(self, _sender, data: bytearray) -> None:

if (
len(data) < BMS.HEAD_LEN
or data[0:2] != BMS.HEAD_READ
or int(data[2]) + 1 != len(data) - len(BMS.HEAD_READ) - BMS.CRC_LEN
or (data[0:2] != BMS.HEAD_READ and data[0:2] != BMS.HEAD_WRITE)
or (
data[0:2] == BMS.HEAD_READ
and int(data[2]) + 1 != len(data) - len(BMS.HEAD_READ) - BMS.CRC_LEN
)
or (data[0:2] == BMS.HEAD_WRITE and len(data) != 8)
):
self._log.debug("response data is invalid")
return
Expand Down

0 comments on commit 367299b

Please sign in to comment.