Home Assistant Testing Playbook
Use this skill to design and implement tests/CI for a custom integration targeting Bronze/Silver quality scale.
When to Use
- Adding or improving tests (
tests/ structure, fixtures, snapshots)
- Ensuring config flow coverage and duplicate/reauth handling
- Verifying coordinator error handling/unavailable states
- Planning CI (hassfest, lint, pytest, coverage)
Test Layout
tests/
conftest.py: enable custom integrations; shared fixtures (MockConfigEntry, mock UDP/HTTP, snapshot extension)
test_config_flow.py: user/discovery/reauth/options flows
test_init.py: setup/unload/reload lifecycle
- Platform tests (e.g.,
test_sensor.py) per platform
fixtures/: sanitized API/device payloads for mocks/snapshots
Fixtures & Tools
pytest-homeassistant-custom-component: hass fixture, MockConfigEntry, aiohttp client mock (if HTTP), time helpers.
- Snapshot:
syrupy + HomeAssistantSnapshotExtension for diagnostics/device registry/entity attributes; redact secrets before snapshotting.
- Time travel:
freezer + async_fire_time_changed to advance coordinator timers.
- Coverage:
pytest --cov=custom_components.marstek --cov-report=xml --cov-fail-under=95.
- Examples: see references/EXAMPLES.md for skeleton tests (config flow, setup/unload, coordinator failures, diagnostics snapshots).
- Repo scaffold: see
tests/ and requirements_test.txt for pinned deps and starter tests.
Config Flow Test Matrix
- Happy path: discovery list → select → creates entry; asserts unique_id set.
- Errors:
cannot_connect, invalid_auth, invalid_discovery_info, already_configured aborts.
- DHCP/integration discovery: updates host when MAC matches; aborts duplicate.
- Options flow: returns form and creates entry; ensure reload listener is in place.
- Reauth (if applicable): asks only for changed credential; updates existing entry.
Coordinator & Entities
- Mock transport/library to avoid real network.
- Happy path: coordinator populates data; entities render expected state/attributes/units.
- Failure path: transport error →
UpdateFailed → entities become unavailable; last_update_success false.
- Debounce/refresh: multiple
async_request_refresh calls coalesce to one fetch (spy call count).
- Entity gating: only create entities when data keys exist; ensure unique_id stable (MAC-based) and device_info shared.
Actions/Commands (if present)
- Verify polling is paused during commands; retries/backoff executed; verification logic validates device state.
- Failure surfaces as error (no silent success); no extra coordinator fetches are triggered concurrently.
Diagnostics & Snapshots (Gold path)
- Add diagnostics view; snapshot output with syrupy; ensure secrets (tokens, IPs if needed) are redacted.
- Device registry snapshot: manufacturer/model/identifiers/connections are correct and stable.
CI Pipeline
- hassfest before tests; lint with
ruff and mypy --strict (required, not optional).
- Matrix on latest supported Python versions.
- Pin test deps in
requirements_test.txt to avoid drift.
- Optional: upload coverage (e.g., Codecov) for PR diffs.
Verification After Changes (MANDATORY)
After every code modification, you MUST run verification:
# 1. Type checking (strict mode enforced)
python3 -m mypy --strict custom_components/<domain>/
# 2. Run all tests
pytest tests/ -q
Do not consider work complete until both pass. This catches:
- Type annotation errors (missing return types, wrong argument types)
- Regressions in existing functionality
- Integration issues between components
Fix failures immediately before moving on.
Quick Checklist
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: homeassistant-testing-playbook3description: Practical test/CI playbook for Home Assistant custom components (config flow, coordinator, entities, diagnostics) Use when this capability is needed.4---56# Home Assistant Testing Playbook78Use this skill to design and implement tests/CI for a custom integration targeting Bronze/Silver quality scale.910## When to Use11- Adding or improving tests (`tests/` structure, fixtures, snapshots)12- Ensuring config flow coverage and duplicate/reauth handling13- Verifying coordinator error handling/unavailable states14- Planning CI (hassfest, lint, pytest, coverage)1516## Test Layout17- `tests/`18 - `conftest.py`: enable custom integrations; shared fixtures (MockConfigEntry, mock UDP/HTTP, snapshot extension)19 - `test_config_flow.py`: user/discovery/reauth/options flows20 - `test_init.py`: setup/unload/reload lifecycle21 - Platform tests (e.g., `test_sensor.py`) per platform22 - `fixtures/`: sanitized API/device payloads for mocks/snapshots2324## Fixtures & Tools25- `pytest-homeassistant-custom-component`: hass fixture, MockConfigEntry, aiohttp client mock (if HTTP), time helpers.26- Snapshot: `syrupy` + `HomeAssistantSnapshotExtension` for diagnostics/device registry/entity attributes; redact secrets before snapshotting.27- Time travel: `freezer` + `async_fire_time_changed` to advance coordinator timers.28- Coverage: `pytest --cov=custom_components.marstek --cov-report=xml --cov-fail-under=95`.29- Examples: see [references/EXAMPLES.md](references/EXAMPLES.md) for skeleton tests (config flow, setup/unload, coordinator failures, diagnostics snapshots).30 - Repo scaffold: see `tests/` and `requirements_test.txt` for pinned deps and starter tests.3132## Config Flow Test Matrix33- Happy path: discovery list → select → creates entry; asserts unique_id set.34- Errors: `cannot_connect`, `invalid_auth`, `invalid_discovery_info`, `already_configured` aborts.35- DHCP/integration discovery: updates host when MAC matches; aborts duplicate.36- Options flow: returns form and creates entry; ensure reload listener is in place.37- Reauth (if applicable): asks only for changed credential; updates existing entry.3839## Coordinator & Entities40- Mock transport/library to avoid real network.41- Happy path: coordinator populates data; entities render expected state/attributes/units.42- Failure path: transport error → `UpdateFailed` → entities become unavailable; `last_update_success` false.43- Debounce/refresh: multiple `async_request_refresh` calls coalesce to one fetch (spy call count).44- Entity gating: only create entities when data keys exist; ensure unique_id stable (MAC-based) and device_info shared.4546## Actions/Commands (if present)47- Verify polling is paused during commands; retries/backoff executed; verification logic validates device state.48- Failure surfaces as error (no silent success); no extra coordinator fetches are triggered concurrently.4950## Diagnostics & Snapshots (Gold path)51- Add diagnostics view; snapshot output with syrupy; ensure secrets (tokens, IPs if needed) are redacted.52- Device registry snapshot: manufacturer/model/identifiers/connections are correct and stable.5354## CI Pipeline55- hassfest before tests; lint with `ruff` and **`mypy --strict`** (required, not optional).56- Matrix on latest supported Python versions.57- Pin test deps in `requirements_test.txt` to avoid drift.58- Optional: upload coverage (e.g., Codecov) for PR diffs.5960## Verification After Changes (MANDATORY)6162**After every code modification**, you MUST run verification:6364```bash65# 1. Type checking (strict mode enforced)66python3 -m mypy --strict custom_components/<domain>/6768# 2. Run all tests69pytest tests/ -q70```7172Do not consider work complete until both pass. This catches:73- Type annotation errors (missing return types, wrong argument types)74- Regressions in existing functionality75- Integration issues between components7677Fix failures immediately before moving on.7879## Quick Checklist80- [ ] Config flow tests cover success + cannot_connect + invalid_auth + invalid_discovery_info + already_configured81- [ ] Options flow tested; reload listener present82- [ ] Coordinator tests cover success + UpdateFailed path → entities unavailable83- [ ] Entities only created for available data keys; unique IDs MAC-based84- [ ] Actions pause polling, retry, and verify outcomes85- [ ] Snapshots for diagnostics/device registry (if diagnostics enabled)86- [ ] CI runs hassfest, lint, pytest with coverage gate87- [ ] **mypy --strict passes (0 errors)**88- [ ] **All tests pass (pytest tests/ -q)**8990---91> Converted and distributed by [TomeVault](https://tomevault.io/claim/taurgis) — claim your Tome and manage your conversions.92<!-- tomevault:4.0:skill_md:2026-04-11 -->