sok-ble 0.1.6__py3-none-any.whl → 0.1.9b2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
sok_ble/exceptions.py CHANGED
@@ -8,4 +8,3 @@ class BLEConnectionError(SokError):
8
8
 
9
9
  class InvalidResponseError(SokError):
10
10
  """Raised when an invalid response is received."""
11
-
@@ -10,8 +10,9 @@ from contextlib import asynccontextmanager
10
10
  from typing import AsyncIterator, Optional
11
11
 
12
12
  import async_timeout
13
- from bleak import BleakError
13
+ from bleak.backends.characteristic import BleakGATTCharacteristic
14
14
  from bleak.backends.device import BLEDevice
15
+ from bleak.exc import BleakError
15
16
 
16
17
  from sok_ble.const import UUID_RX, UUID_TX, _sok_command
17
18
  from sok_ble.exceptions import BLEConnectionError
@@ -114,7 +115,7 @@ class SokBluetoothDevice:
114
115
 
115
116
  queue: asyncio.Queue[bytes] = asyncio.Queue()
116
117
 
117
- def handler(_: int, data: bytearray) -> None:
118
+ def handler(_: BleakGATTCharacteristic, data: bytearray) -> None:
118
119
  queue.put_nowait(bytes(data))
119
120
 
120
121
  await client.start_notify(UUID_RX, handler)
@@ -136,6 +137,10 @@ class SokBluetoothDevice:
136
137
  await asyncio.sleep(0.2)
137
138
  continue
138
139
  raise
140
+ raise BleakError(
141
+ f"Failed to receive response 0x{expected:04X} "
142
+ f"from {self._ble_device.address}"
143
+ )
139
144
 
140
145
  async def async_update(self) -> None:
141
146
  """Poll the device for all telemetry and update attributes."""
@@ -180,13 +185,30 @@ class SokBluetoothDevice:
180
185
  parsed = SokParser.parse_all(responses)
181
186
  logger.debug("Parsed update: %s", parsed)
182
187
 
183
- self.voltage = parsed["voltage"]
184
- self.current = parsed["current"]
185
- self.soc = parsed["soc"]
186
- self.temperature = parsed["temperature"]
187
- self.capacity = parsed["capacity"]
188
- self.num_cycles = parsed["num_cycles"]
189
- self.cell_voltages = parsed["cell_voltages"]
188
+ voltage = parsed.get("voltage")
189
+ self.voltage = voltage if isinstance(voltage, (int, float)) else None
190
+
191
+ current = parsed.get("current")
192
+ self.current = current if isinstance(current, (int, float)) else None
193
+
194
+ soc = parsed.get("soc")
195
+ self.soc = soc if isinstance(soc, int) else None
196
+
197
+ temperature = parsed.get("temperature")
198
+ self.temperature = (
199
+ temperature if isinstance(temperature, (int, float)) else None
200
+ )
201
+
202
+ capacity = parsed.get("capacity")
203
+ self.capacity = capacity if isinstance(capacity, (int, float)) else None
204
+
205
+ num_cycles = parsed.get("num_cycles")
206
+ self.num_cycles = num_cycles if isinstance(num_cycles, int) else None
207
+
208
+ cell_voltages = parsed.get("cell_voltages")
209
+ self.cell_voltages = (
210
+ list(cell_voltages) if isinstance(cell_voltages, list) else None
211
+ )
190
212
 
191
213
  self.num_samples += 1
192
214
 
sok_ble/sok_parser.py CHANGED
@@ -14,19 +14,20 @@ logger = logging.getLogger(__name__)
14
14
 
15
15
  # Endian helper functions copied from the reference addon
16
16
 
17
+
17
18
  def get_le_short(data: Sequence[int] | bytes | bytearray, offset: int) -> int:
18
19
  """Read a little-endian signed short."""
19
- return struct.unpack_from('<h', bytes(data), offset)[0]
20
+ return struct.unpack_from("<h", bytes(data), offset)[0]
20
21
 
21
22
 
22
23
  def get_le_ushort(data: Sequence[int] | bytes | bytearray, offset: int) -> int:
23
24
  """Read a little-endian unsigned short."""
24
- return struct.unpack_from('<H', bytes(data), offset)[0]
25
+ return struct.unpack_from("<H", bytes(data), offset)[0]
25
26
 
26
27
 
27
28
  def get_le_int3(data: Sequence[int] | bytes | bytearray, offset: int) -> int:
28
29
  """Read a 3-byte little-endian signed integer."""
29
- b0, b1, b2 = bytes(data)[offset:offset + 3]
30
+ b0, b1, b2 = bytes(data)[offset : offset + 3]
30
31
  val = b0 | (b1 << 8) | (b2 << 16)
31
32
  if val & 0x800000:
32
33
  val -= 0x1000000
@@ -35,7 +36,7 @@ def get_le_int3(data: Sequence[int] | bytes | bytearray, offset: int) -> int:
35
36
 
36
37
  def get_be_uint3(data: Sequence[int] | bytes | bytearray, offset: int) -> int:
37
38
  """Read a 3-byte big-endian unsigned integer."""
38
- b0, b1, b2 = bytes(data)[offset:offset + 3]
39
+ b0, b1, b2 = bytes(data)[offset : offset + 3]
39
40
  return (b0 << 16) | (b1 << 8) | b2
40
41
 
41
42
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sok-ble
3
- Version: 0.1.6
3
+ Version: 0.1.9b2
4
4
  Summary: SOK BLE battery interface library
5
5
  Project-URL: Homepage, https://github.com/IAmTheMitchell/sok-ble
6
6
  Project-URL: Bug Tracker, https://github.com/IAmTheMitchell/sok-ble/issues
@@ -19,7 +19,8 @@ Description-Content-Type: text/markdown
19
19
 
20
20
  # SOK BLE
21
21
 
22
- ![CI](https://github.com/IAmTheMitchell/sok-ble/actions/workflows/ci.yml/badge.svg)
22
+ ![Tests](https://github.com/IAmTheMitchell/sok-ble/actions/workflows/test.yml/badge.svg)
23
+ ![Release](https://github.com/IAmTheMitchell/sok-ble/actions/workflows/release.yml/badge.svg)
23
24
 
24
25
  Python library for interacting with SOK Bluetooth-enabled batteries.
25
26
 
@@ -42,5 +43,6 @@ asyncio.run(main())
42
43
  ```
43
44
 
44
45
  ## References
46
+
45
47
  [@zuccaro's comment](https://github.com/Louisvdw/dbus-serialbattery/issues/350#issuecomment-1500658941)
46
- [Bluetooth-Devices/inkbird-ble](https://github.com/Bluetooth-Devices/inkbird-ble)
48
+ [Bluetooth-Devices/inkbird-ble](https://github.com/Bluetooth-Devices/inkbird-ble)
@@ -0,0 +1,9 @@
1
+ sok_ble/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ sok_ble/const.py,sha256=mHtJTbWz_dG3v1lhZrLDzMf-QAG9v5QWlHU9ZKsyYdg,998
3
+ sok_ble/exceptions.py,sha256=_4Fyb-2Ncct2RGjLe1em2M7_e5QcmjVJ92KxtAsbZlk,246
4
+ sok_ble/sok_bluetooth_device.py,sha256=DgzKu5nuBwxNETVzC7OS0Y4KwCWRaZ-q6r01o057CmU,9036
5
+ sok_ble/sok_parser.py,sha256=Wcn9oWVrHT1gLU7WB-3rUd2UpVfFwLz_gKQrDYE0H4M,4342
6
+ sok_ble-0.1.9b2.dist-info/METADATA,sha256=E7FE8hk2zRqnMX-jvFLI5mpoOmfXz4ck6Ub-OxW7O5Q,1593
7
+ sok_ble-0.1.9b2.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
8
+ sok_ble-0.1.9b2.dist-info/licenses/LICENSE,sha256=OoKRExpBDDpMbUut-KaARncGL5U8utex4D2ncZVtVF8,11346
9
+ sok_ble-0.1.9b2.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.27.0
2
+ Generator: hatchling 1.28.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,9 +0,0 @@
1
- sok_ble/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- sok_ble/const.py,sha256=mHtJTbWz_dG3v1lhZrLDzMf-QAG9v5QWlHU9ZKsyYdg,998
3
- sok_ble/exceptions.py,sha256=7H1yUqqnrKyi3LwxRCMF7RkuGp_rgdy9j35nrMOgz44,247
4
- sok_ble/sok_bluetooth_device.py,sha256=Mu-DW15qwl5EKXL84Wi1KHEWFUZ-cHiTPNgz3hwNTfk,8200
5
- sok_ble/sok_parser.py,sha256=8yug65x3RdkLKbYlP-fEWiysaHByyenKV_gAzhIKeU0,4337
6
- sok_ble-0.1.6.dist-info/METADATA,sha256=QQu2sGiAFSZ2jMVWot8fm96KK1yRcxcDVusQh2TdGsQ,1490
7
- sok_ble-0.1.6.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
8
- sok_ble-0.1.6.dist-info/licenses/LICENSE,sha256=OoKRExpBDDpMbUut-KaARncGL5U8utex4D2ncZVtVF8,11346
9
- sok_ble-0.1.6.dist-info/RECORD,,