sok-ble 0.1.8__tar.gz → 0.1.9a5__tar.gz

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.
Files changed (37) hide show
  1. sok_ble-0.1.9a5/.github/workflows/prerelease.yml +105 -0
  2. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/.github/workflows/release.yml +2 -2
  3. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/.github/workflows/test.yml +6 -2
  4. sok_ble-0.1.8/.github/copilot-instructions.md → sok_ble-0.1.9a5/AGENTS.md +33 -0
  5. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/PKG-INFO +3 -3
  6. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/pyproject.toml +4 -3
  7. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/src/sok_ble/sok_bluetooth_device.py +39 -8
  8. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/tests/test_derived.py +1 -1
  9. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/tests/test_device_full.py +1 -3
  10. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/tests/test_device_minimal.py +38 -3
  11. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/tests/test_integration_mock.py +1 -3
  12. sok_ble-0.1.9a5/uv.lock +557 -0
  13. sok_ble-0.1.8/AGENTS.md +0 -35
  14. sok_ble-0.1.8/uv.lock +0 -490
  15. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/.github/FUNDING.yml +0 -0
  16. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/.github/dependabot.yml +0 -0
  17. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/.gitignore +0 -0
  18. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/.python-version +0 -0
  19. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/.release-please-manifest.json +0 -0
  20. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/CHANGELOG.md +0 -0
  21. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/LICENSE +0 -0
  22. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/README.md +0 -0
  23. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/prompts.md +0 -0
  24. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/release-please-config.json +0 -0
  25. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/requirements-dev.txt +0 -0
  26. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/spec.md +0 -0
  27. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/src/sok_ble/__init__.py +0 -0
  28. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/src/sok_ble/const.py +0 -0
  29. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/src/sok_ble/exceptions.py +0 -0
  30. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/src/sok_ble/sok_parser.py +0 -0
  31. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/tests/.gitkeep +0 -0
  32. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/tests/__init__.py +0 -0
  33. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/tests/test_const.py +0 -0
  34. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/tests/test_exceptions.py +0 -0
  35. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/tests/test_parser_full.py +0 -0
  36. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/tests/test_parser_info.py +0 -0
  37. {sok_ble-0.1.8 → sok_ble-0.1.9a5}/todo.md +0 -0
@@ -0,0 +1,105 @@
1
+ name: Prerelease
2
+
3
+ on:
4
+ pull_request:
5
+ branches: [main]
6
+ types: [opened]
7
+ workflow_dispatch:
8
+ inputs:
9
+ release_branch:
10
+ description: Branch to build from
11
+ required: false
12
+ default: release-please--branches--main
13
+ semantic_version:
14
+ description: Optional semantic version to publish (x.x.x)
15
+ required: false
16
+ prerelease:
17
+ description: Prerelease type (defaults to beta)
18
+ required: false
19
+ type: choice
20
+ options:
21
+ - beta
22
+ - alpha
23
+ beta_iteration:
24
+ description: Optional alpha/beta iteration identifier (defaults to run number)
25
+ required: false
26
+
27
+ permissions:
28
+ contents: write
29
+ id-token: write
30
+
31
+ jobs:
32
+ publish-beta:
33
+ if: |
34
+ github.event_name == 'workflow_dispatch' ||
35
+ (github.event_name == 'pull_request' && startsWith(github.event.pull_request.head.ref, 'release-please--'))
36
+ runs-on: ubuntu-latest
37
+ environment: release
38
+ steps:
39
+ - uses: actions/checkout@v6
40
+ with:
41
+ ref: ${{ github.event_name == 'workflow_dispatch' && (inputs.release_branch || 'release-please--main') || github.event.pull_request.head.ref }}
42
+ fetch-depth: 0
43
+
44
+ - name: Install uv
45
+ uses: astral-sh/setup-uv@v7
46
+
47
+ - name: Set up Python
48
+ run: uv python install
49
+
50
+ - name: Derive beta version
51
+ id: version
52
+ env:
53
+ BETA_ITERATION: ${{ github.event_name == 'workflow_dispatch' && inputs.beta_iteration || '' }}
54
+ SEMANTIC_VERSION: ${{ github.event_name == 'workflow_dispatch' && inputs.semantic_version || '' }}
55
+ PRERELEASE_ID: ${{ github.event_name == 'workflow_dispatch' && inputs.prerelease || '' }}
56
+ run: |
57
+ python - <<'PY'
58
+ from pathlib import Path
59
+ import os
60
+ import re
61
+ import sys
62
+
63
+ pyproject = Path("pyproject.toml")
64
+ content = pyproject.read_text(encoding="utf-8")
65
+
66
+ match = re.search(r'^version\s*=\s*"(?P<version>[^"]+)"', content, flags=re.MULTILINE)
67
+ if not match:
68
+ sys.exit("Version not found in pyproject.toml")
69
+
70
+ semver_input = os.environ.get("SEMANTIC_VERSION", "").strip()
71
+ prerelease_input = os.environ.get("PRERELEASE_ID", "").strip().lower()
72
+ prerelease_map = {"alpha": "a", "beta": "b", "a": "a", "b": "b"}
73
+ if prerelease_input and prerelease_input not in prerelease_map:
74
+ sys.exit("Invalid prerelease identifier. Use 'alpha' or 'beta'.")
75
+
76
+ if semver_input:
77
+ if not re.fullmatch(r"\d+\.\d+\.\d+", semver_input):
78
+ sys.exit("Invalid semantic version. Use x.x.x format.")
79
+ base_version = semver_input
80
+ else:
81
+ base_version = match.group("version")
82
+
83
+ prerelease = prerelease_map.get(prerelease_input, "b")
84
+ iteration = os.environ.get("BETA_ITERATION") or os.environ.get("GITHUB_RUN_NUMBER")
85
+ beta_version = f"{base_version}{prerelease}{iteration}"
86
+
87
+ new_content = re.sub(
88
+ r'^version\s*=\s*".*"',
89
+ f'version = "{beta_version}"',
90
+ content,
91
+ count=1,
92
+ flags=re.MULTILINE,
93
+ )
94
+ pyproject.write_text(new_content, encoding="utf-8")
95
+
96
+ print(f"Publishing beta version {beta_version}")
97
+ with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output:
98
+ output.write(f"beta_version={beta_version}\n")
99
+ PY
100
+
101
+ - name: Build package
102
+ run: uv build
103
+
104
+ - name: Publish beta to PyPI
105
+ run: uv publish
@@ -37,12 +37,12 @@ jobs:
37
37
  id-token: write
38
38
  contents: write
39
39
  steps:
40
- - uses: actions/checkout@v4
40
+ - uses: actions/checkout@v6
41
41
  with:
42
42
  fetch-depth: 0
43
43
 
44
44
  - name: Install uv
45
- uses: astral-sh/setup-uv@v5
45
+ uses: astral-sh/setup-uv@v7
46
46
 
47
47
  - name: Set up Python
48
48
  run: uv python install
@@ -22,12 +22,13 @@ jobs:
22
22
  - "3.11"
23
23
  - "3.12"
24
24
  - "3.13"
25
+ - "3.14"
25
26
 
26
27
  steps:
27
- - uses: actions/checkout@v4
28
+ - uses: actions/checkout@v6
28
29
 
29
30
  - name: Install uv and set the python version
30
- uses: astral-sh/setup-uv@v5
31
+ uses: astral-sh/setup-uv@v7
31
32
  with:
32
33
  python-version: ${{ matrix.python-version }}
33
34
 
@@ -42,3 +43,6 @@ jobs:
42
43
 
43
44
  - name: Lint with Ruff
44
45
  run: uv run ruff check . --output-format=github
46
+
47
+ - name: Type check with ty
48
+ run: uv run ty check . --output-format=github
@@ -32,3 +32,36 @@ sok-ble is a library written in Python. Its purpose is to connect to SOK LiFePO4
32
32
 
33
33
  - Use conventional commits for all changes
34
34
  - Prefix all commit messages with fix:; feat:; build:; chore:; ci:; docs:; style:; refactor:; perf:; or test: as appropriate.
35
+
36
+ # Before Checking In Code
37
+
38
+ - Fix all code formatting and quality issues in the entire codebase.
39
+ - Ensure all new code is covered by appropriate unit tests.
40
+
41
+ ## Python
42
+
43
+ Fix all Python formatting and linting issues.
44
+
45
+ ### Steps:
46
+
47
+ 1. **Format with ruff**: `uv run ruff format .`
48
+ 2. **Lint with ruff**: `uv run ruff check . --output-format=github`
49
+ 3. **Type check with ty**: `uv run ty check . --output-format=github`
50
+ 4. **Run unit tests**: `uv run pytest tests`
51
+
52
+ ## General Process:
53
+
54
+ 1. Run automated formatters first.
55
+ 2. Fix remaining linting issues manually.
56
+ 3. Resolve type checking errors.
57
+ 4. Verify all tests pass with no errors.
58
+ 5. Review changes before committing.
59
+
60
+ ## Common Issues:
61
+
62
+ - Import order conflicts between tools
63
+ - Line length violations
64
+ - Unused imports/variables
65
+ - Type annotation requirements
66
+ - Missing return types
67
+ - Inconsistent quotes/semicolons
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sok-ble
3
- Version: 0.1.8
3
+ Version: 0.1.9a5
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
@@ -13,8 +13,8 @@ Classifier: Operating System :: OS Independent
13
13
  Classifier: Programming Language :: Python :: 3
14
14
  Requires-Python: >=3.11
15
15
  Requires-Dist: async-timeout>=4.0.3
16
- Requires-Dist: bleak-retry-connector>=3.9.0
17
- Requires-Dist: bleak>=0.22.3
16
+ Requires-Dist: bleak-retry-connector>=4.4.3
17
+ Requires-Dist: bleak>=1.0.1
18
18
  Description-Content-Type: text/markdown
19
19
 
20
20
  # SOK BLE
@@ -1,13 +1,13 @@
1
1
  [project]
2
2
  name = "sok-ble"
3
- version = "0.1.8"
3
+ version = "0.1.9a5"
4
4
  description = "SOK BLE battery interface library"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.11"
7
7
  dependencies = [
8
8
  "async-timeout>=4.0.3",
9
- "bleak>=0.22.3",
10
- "bleak-retry-connector>=3.9.0",
9
+ "bleak>=1.0.1",
10
+ "bleak-retry-connector>=4.4.3",
11
11
  ]
12
12
  authors = [
13
13
  {name = "Mitchell Carlson", email = "mitchell.carlson.pro@gmail.com"}
@@ -24,6 +24,7 @@ dev = [
24
24
  "pytest>=7.0.1",
25
25
  "pytest-asyncio>=1.0.0",
26
26
  "ruff>=0.12.3",
27
+ "ty>=0.0.15",
27
28
  ]
28
29
 
29
30
  [project.urls]
@@ -10,6 +10,7 @@ from contextlib import asynccontextmanager
10
10
  from typing import AsyncIterator, Optional
11
11
 
12
12
  import async_timeout
13
+ from bleak.backends.characteristic import BleakGATTCharacteristic
13
14
  from bleak.backends.device import BLEDevice
14
15
  from bleak.exc import BleakError
15
16
 
@@ -86,6 +87,15 @@ class SokBluetoothDevice:
86
87
  self._ble_device.address,
87
88
  err,
88
89
  )
90
+ if client is not None:
91
+ try:
92
+ await asyncio.shield(client.disconnect())
93
+ except (BleakError, asyncio.TimeoutError):
94
+ logger.debug(
95
+ "Failed to disconnect after connect error for %s",
96
+ self._ble_device.address,
97
+ )
98
+ client = None
89
99
  await asyncio.sleep(0.5)
90
100
  else:
91
101
  raise BLEConnectionError(
@@ -114,7 +124,7 @@ class SokBluetoothDevice:
114
124
 
115
125
  queue: asyncio.Queue[bytes] = asyncio.Queue()
116
126
 
117
- def handler(_: int, data: bytearray) -> None:
127
+ def handler(_: BleakGATTCharacteristic, data: bytearray) -> None:
118
128
  queue.put_nowait(bytes(data))
119
129
 
120
130
  await client.start_notify(UUID_RX, handler)
@@ -136,6 +146,10 @@ class SokBluetoothDevice:
136
146
  await asyncio.sleep(0.2)
137
147
  continue
138
148
  raise
149
+ raise BleakError(
150
+ f"Failed to receive response 0x{expected:04X} "
151
+ f"from {self._ble_device.address}"
152
+ )
139
153
 
140
154
  async def async_update(self) -> None:
141
155
  """Poll the device for all telemetry and update attributes."""
@@ -180,13 +194,30 @@ class SokBluetoothDevice:
180
194
  parsed = SokParser.parse_all(responses)
181
195
  logger.debug("Parsed update: %s", parsed)
182
196
 
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"]
197
+ voltage = parsed.get("voltage")
198
+ self.voltage = voltage if isinstance(voltage, (int, float)) else None
199
+
200
+ current = parsed.get("current")
201
+ self.current = current if isinstance(current, (int, float)) else None
202
+
203
+ soc = parsed.get("soc")
204
+ self.soc = soc if isinstance(soc, int) else None
205
+
206
+ temperature = parsed.get("temperature")
207
+ self.temperature = (
208
+ temperature if isinstance(temperature, (int, float)) else None
209
+ )
210
+
211
+ capacity = parsed.get("capacity")
212
+ self.capacity = capacity if isinstance(capacity, (int, float)) else None
213
+
214
+ num_cycles = parsed.get("num_cycles")
215
+ self.num_cycles = num_cycles if isinstance(num_cycles, int) else None
216
+
217
+ cell_voltages = parsed.get("cell_voltages")
218
+ self.cell_voltages = (
219
+ list(cell_voltages) if isinstance(cell_voltages, list) else None
220
+ )
190
221
 
191
222
  self.num_samples += 1
192
223
 
@@ -5,7 +5,7 @@ from sok_ble.sok_bluetooth_device import SokBluetoothDevice
5
5
 
6
6
 
7
7
  def make_device():
8
- return SokBluetoothDevice(BLEDevice("00:11:22:33:44:55", "Test", None, -60))
8
+ return SokBluetoothDevice(BLEDevice("00:11:22:33:44:55", "Test", None))
9
9
 
10
10
 
11
11
  def test_power_property():
@@ -40,9 +40,7 @@ async def test_full_update(monkeypatch):
40
40
  device_mod, "BleakClientWithServiceCache", lambda *a, **k: dummy
41
41
  )
42
42
 
43
- dev = device_mod.SokBluetoothDevice(
44
- BLEDevice("00:11:22:33:44:55", "Test", None, -60)
45
- )
43
+ dev = device_mod.SokBluetoothDevice(BLEDevice("00:11:22:33:44:55", "Test", None))
46
44
 
47
45
  await dev.async_update()
48
46
 
@@ -1,7 +1,10 @@
1
+ import asyncio
2
+
1
3
  import pytest
2
4
  from bleak.backends.device import BLEDevice
3
5
 
4
6
  from sok_ble import sok_bluetooth_device as device_mod
7
+ from sok_ble.exceptions import BLEConnectionError
5
8
 
6
9
 
7
10
  class DummyClient:
@@ -35,12 +38,44 @@ async def test_minimal_update(monkeypatch):
35
38
  monkeypatch.setattr(device_mod, "establish_connection", None, raising=False)
36
39
  monkeypatch.setattr(device_mod, "BleakClientWithServiceCache", DummyClient)
37
40
 
38
- dev = device_mod.SokBluetoothDevice(
39
- BLEDevice("00:11:22:33:44:55", "Test", None, -60)
40
- )
41
+ dev = device_mod.SokBluetoothDevice(BLEDevice("00:11:22:33:44:55", "Test", None))
41
42
 
42
43
  await dev.async_update()
43
44
 
44
45
  assert dev.voltage == pytest.approx(13.066)
45
46
  assert dev.current == 10.0
46
47
  assert dev.soc == 65
48
+
49
+
50
+ @pytest.mark.asyncio
51
+ async def test_disconnect_on_connect_failure(monkeypatch):
52
+ class FailingClient:
53
+ disconnect_calls = 0
54
+
55
+ def __init__(self, *args, **kwargs):
56
+ return None
57
+
58
+ async def connect(self):
59
+ return True
60
+
61
+ async def disconnect(self):
62
+ FailingClient.disconnect_calls += 1
63
+ return True
64
+
65
+ @property
66
+ def services(self):
67
+ raise asyncio.TimeoutError
68
+
69
+ async def fast_sleep(*args, **kwargs):
70
+ return None
71
+
72
+ monkeypatch.setattr(device_mod, "establish_connection", None, raising=False)
73
+ monkeypatch.setattr(device_mod, "BleakClientWithServiceCache", FailingClient)
74
+ monkeypatch.setattr(device_mod.asyncio, "sleep", fast_sleep)
75
+
76
+ dev = device_mod.SokBluetoothDevice(BLEDevice("00:11:22:33:44:55", "Test", None))
77
+
78
+ with pytest.raises(BLEConnectionError):
79
+ await dev.async_update()
80
+
81
+ assert FailingClient.disconnect_calls == 3
@@ -45,9 +45,7 @@ async def test_async_update_full_flow(monkeypatch):
45
45
 
46
46
  monkeypatch.setattr(device_mod.SokBluetoothDevice, "_connect", fake_connect)
47
47
 
48
- dev = device_mod.SokBluetoothDevice(
49
- BLEDevice("00:11:22:33:44:55", "Test", None, -60)
50
- )
48
+ dev = device_mod.SokBluetoothDevice(BLEDevice("00:11:22:33:44:55", "Test", None))
51
49
 
52
50
  await dev.async_update()
53
51