Debian Codegraph @

CLAUDE.md

raw
1 # CLAUDE.md
2
3 Guidance for Claude Code (and other AI agents) working in this repository.
4
5 ## What this library does
6
7 `govee-ble` parses BLE advertisement payloads from Govee sensors (thermometers,
8 hygrometers, motion/door/vibration sensors, presence sensors, BBQ probes, air
9 quality monitors, etc.) and exposes the readings as `SensorUpdate` objects from
10 `sensor-state-data`. It is consumed by Home Assistant's `govee_ble` integration.
11
12 The library is **stateless across packets** beyond the per-device accumulator
13 that `bluetooth-sensor-state-data.BluetoothData` provides. It does not poll, it
14 does not connect — it only decodes raw advertisement bytes.
15
16 ## Layout
17
18 ```
19 src/govee_ble/
20 ├── __init__.py # Public re-exports (GoveeBluetoothDeviceData, SensorType, …)
21 └── parser.py # All decoding logic + GoveeBluetoothDeviceData class
22 tests/
23 └── test_parser.py # Single fixture-driven test file (~5800 lines)
24 ```
25
26 Everything device-specific lives in `parser.py::GoveeBluetoothDeviceData._process_mfr_data`.
27 Each model is a branch keyed off `(msg_length, local_name | mgr_id | service_uuid)`.
28 When adding a model, follow the existing pattern — don't refactor the dispatch
29 into a registry just to be tidy; the linear branches are intentionally easy to
30 diff against device captures.
31
32 ## Commands
33
34 ```bash
35 poetry install # install (incl. dev deps)
36 poetry run pytest # run tests with coverage
37 poetry run pytest tests/test_parser.py::test_h5075 -v # single test
38 poetry run pre-commit run -a # full lint pass (ruff, mypy, codespell, …)
39 ```
40
41 `make` is not used here — Poetry + pre-commit only.
42
43 ## Conventions
44
45 - **Python 3.10+** (`from __future__ import annotations` everywhere).
46 `pyupgrade --py310-plus` runs in pre-commit.
47 - **Line length: 88** (ruff default). ruff-format rewrites to 88, so write to 88. Long log/error strings that ruff leaves alone are the only exception.
48 - **Type hints required** on all non-test code (`disallow_untyped_defs = true`
49 in mypy strict config). Tests are exempt.
50 - **Conventional Commits** are enforced by commitlint in CI and by commitizen
51 in pre-commit. Use `feat:`, `fix:`, `chore:`, `test:`, `docs:`, `refactor:`.
52 The `release` job on `main` runs python-semantic-release — a `feat:` bumps
53 minor, a `fix:` bumps patch, and any merge to `main` may publish to PyPI.
54 Be deliberate about commit prefixes.
55 - **Branch naming for AI agents**: always `koan/<short-description>`. Never
56 push to `main`, never merge.
57
58 ## Testing rules
59
60 - Tests live in one file (`tests/test_parser.py`). When adding a model, add a
61 test next to the closest existing one — keep tests grouped by model.
62 - Use real captured advertisement bytes wherever possible. The test file is
63 largely "feed these bytes, assert this `SensorUpdate`" — mirror that style.
64 - `BluetoothServiceInfo` is the input type. Construct it with the same kwargs
65 the existing tests use; don't invent helpers for one-off cases.
66 - Coverage is reported on every PR via codecov. New parser branches without
67 tests will fail `codecov/patch`.
68
69 ## CI gates a PR must pass
70
71 1. `pre-commit.ci` — ruff, ruff-format, mypy, codespell, pyupgrade,
72 prettier, commitizen, plus the standard pre-commit-hooks.
73 2. `Lint Commit Messages` — commitlint, conventional commits.
74 3. `test (3.10 / 3.11 / 3.12 / 3.13 / 3.14, ubuntu-latest)` — full pytest.
75 4. `codecov/patch` and `codecov/project` — coverage thresholds.
76
77 If pre-commit autofixes are needed, the `pre-commit.ci` bot pushes them as
78 `chore(pre-commit.ci): auto fixes` — don't fight that, just rebase.
79
80 ## Adding support for a new Govee model
81
82 1. Capture raw advertisement bytes (BLE sniffer or HA debug log).
83 2. Add a branch in `_process_mfr_data` matched by `msg_length` plus
84 `local_name`/`mgr_id`/service UUID, following the closest existing model.
85 3. If it has buttons / motion / sleepy power profile, add an entry to
86 `_MODEL_DB` in `parser.py` with the right `SensorType`.
87 4. Add a fixture-driven test in `tests/test_parser.py`. Include both a
88 "valid reading" and an "out-of-range/error flag" case if the device
89 reports an error bit.
90 5. Run `poetry run pytest -k <model>` then `poetry run pre-commit run -a`.
91
92 ## Things not to do
93
94 - Don't introduce stateful logic in the parser without an explicit reason —
95 the library is consumed by Home Assistant and the parser is expected to be
96 pure per-packet. State that filters or smooths readings belongs upstream.
97 - Don't refactor the model dispatch into a registry/decorator pattern. The
98 linear `if msg_length == X` branches are deliberately easy to grep and to
99 diff against new device captures.
100 - Don't touch `CHANGELOG.md` by hand — semantic-release owns it.
101 - Don't bump the version in `pyproject.toml` or `__init__.py` — semantic-release
102 owns those too (see `[tool.semantic_release]`).