ha-testing
/ha-testing
Pisze testy integracji Home Assistant z pytest i fixture hass, w tym mocki, pokrycie testami i przygotowanie do zgłoszenia do core.
- Autor
- l3digitalnet ↗
- Repozytorium
- claude-code-plugins ↗
- Licencja
- MIT
- Ostatnia zmiana repozytorium
- 31.08.2026
Ocena i bezpieczeństwo
- Pliki w paczce
- 1
- Skrypty
- 0
- Komentarze HTML
- 0
- Adresy URL w SKILL.md
- 0
Brak sygnałów. Repozytorium zmienione 31.08.2026, bez odwołań do wycofanych modeli i API.
MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.
Co robi i kiedy
Pisze testy integracji Home Assistant z pytest i fixture hass, w tym mocki, pokrycie testami i przygotowanie do zgłoszenia do core.
Oryginalny opis (EN)
Write tests for Home Assistant integrations using pytest and the hass fixture. Use when mentioning test, pytest, testing, test coverage, mock, fixture, or preparing an integration for core submission.
Jak działa
- Test Structure
- conftest.py
- testconfigflow.py (REQUIRED)
- testinit.py
- Running Tests
- Key Rules
Podgląd SKILL.md
---
name: ha-testing
description: Write tests for Home Assistant integrations using pytest and the hass fixture. Use when mentioning test, pytest, testing, test coverage, mock, fixture, or preparing an integration for core submission.
---
# Testing Home Assistant Integrations
Tests are **required** for Bronze tier on the Integration Quality Scale. At minimum: config flow tests for success, connection failure, and auth failure.
## Test Structure
```text
tests/
├── conftest.py # Shared fixtures
├── test_config_flow.py # Config flow tests (REQUIRED)
├── test_init.py # Setup/unload tests
├── test_sensor.py # Entity tests
└── test_coordinator.py # Coordinator tests
```
## conftest.py
```python
"""Fixtures for {Name} tests."""
from collections.abc import Generator
from unittest.mock import AsyncMock, patch
import pytest
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
@pytest.fixture(autouse=True)
def auto_enable_custom_integrations(enable_custom_integrations):
"""Load custom_components during tests (fixture provided by PHCC)."""
yield
MOCK_CONFIG = {
CONF_HOST: "192.168.1.100",
CONF_USERNAME: "admin",
CONF_PASSWORD: "password",
}
MOCK_DEVICE_INFO = {
"serial": "ABC123",
"name": "Test Device",
"model": "Model X",
}
MOCK_DATA = {
"devices": {
"device_1": {
"temperature": 22.5,
"humidity": 45,
},
},
}
@pytest.fixture
def mock_client() -> Generator[AsyncMock]:
with patch("custom_components.{domain}.MyClient", autospec=True) as mock:
client = mock.return_value
client.async_get_device_info = AsyncMock(return_value=MOCK_DEVICE_INFO)
client.async_get_data = AsyncMock(return_value=MOCK_DATA)
yield client
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
with patch(
"custom_components.{domain}.async_setup_entry",
return_value=True,
) as mock:
yield mock
```
## test_config_flow.py (REQUIRED)
These assertions assume the config flow catches client errors and maps them to the `errors["base"]` keys below. Mirror this mapping in `async_step_user` (see the ha-debugging skill for the `errors["base"] = "unknown"` fallback pattern):
```python
try:
await client.async_get_device_info()
except ConnectionError:
errors["base"] = "cannot_connect"
except InvalidAuth:
errors["base"] = "invalid_auth"
except Exception: # noqa: BLE001
errors["base"] = "unknown"
```
```python
"""Test config flow."""
from unittest.mock import AsyncMock
from homeassistant import config_entries
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from custom_components.{domain}.const import DOMAIN
from custom_components.{domain}.exceptions import InvalidAuth
from .conftest import MOCK_CONFIG
async def test_user_flow_success(
hass: HomeAssistant,
mock_client: AsyncMock,
mock_setup_entry: AsyncMock,
) -> None:
"""Test successful config flow."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
result = await hass.config_entries.flow.async_configure(
result["flow_id"], MOCK_CONFIG
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "Test Device"
assert result["data"] == MOCK_CONFIG
async def test_user_flow_cannot_connect(
hass: HomeAssistant,
mock_client: AsyncMock,
mock_setup_entry: AsyncMock,
) -> None:
"""Test connection failure."""
mock_client.async_get_device_info.side_effect = ConnectionError
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], MOCK_CONFIG
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "cannot_connect"}
async def test_user_flow_invalid_auth(
hass: HomeAssistant,
mock_client: AsyncMock,
mock_setup_entry: AsyncMock,
) -> None:
"""Test auth failure."""
mock_client.async_get_device_info.side_effect = InvalidAuth
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], MOCK_CONFIG
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "invalid_auth"}
```
## test_init.py
```python
"""Test setup and unload."""
from unittest.mock import AsyncMock
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from pytest_homeassistant_custom_component.common import MockConfigEntry
from custom_components.{domain}.const import DOMAIN
from .conftest import MOCK_CONFIG
async def test_setup_entry(hass: HomeAssistant, mock_client: AsyncMock) -> None:
"""Test successful setup."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.LOADED
async def test_unload_entry(hass: HomeAssistant, mock_client: AsyncMock) -> None:
"""Test successful unload."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.LOADED
await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.NOT_LOADED
async def test_setup_entry_not_ready(
hass: HomeAssistant, mock_client: AsyncMock
) -> None:
"""Test setup fails when cannot connect."""
mock_client.async_get_data.side_effect = ConnectionError
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.SETUP_RETRY
```
## Running Tests
`pytest-homeassistant-custom-component` (PHACC) pins its own compatible pytest, pytest-asyncio, and supporting plugin versions, so install only PHACC (pinned to the release matching your target HA version) and let it pull the stack — do not also install `pytest-asyncio` separately, as that invites version conflicts. Pin the version, e.g. `pip install pytest-homeassistant-custom-component==<version-for-your-HA>`.
The bare `async def test_*` examples above only run with PHACC's asyncio config. Add this minimal config so async tests are collected (otherwise pytest skips them with "async def functions are not natively supported"):
```toml
# pyproject.toml
[tool.pytest.ini_options]
asyncio_mode = "auto"
```
(Or the equivalent `[pytest]` / `asyncio_mode = auto` in `pytest.ini`.)
```bash
pip install pytest-homeassistant-custom-component==<version-for-your-HA>
pytest tests/ -v
pytest tests/ --cov=custom_components.{domain} --cov-report=html
```
## Key Rules
1. Config flow tests are **mandatory** for Bronze tier
2. Use `AsyncMock` for all async methods
3. Mock the client, not the coordinator
4. Call `await hass.async_block_till_done()` after setup
5. Assert `FlowResultType` enum values, not strings
Wersje
W bazie nie ma innych skilli o nazwie ha-testing.
Migawka bazy z 13.11.2026 · wersja paczki 02f1e56f8234 (skrót SHA-256 całej paczki)
Pobrania w ostatnich 30 dniach: 0 · ranking
Podobne skille
- 100python-pytest-ops
Stosuje wzorce testowania pytest w Python: fixtures, markery, parametrize, mocki, conftest, pokrycie kodu oraz testy jednostkowe i integracyjne. - 100ha-architecture
Wyjaśnia wnętrze rdzenia Home Assistant: magistralę zdarzeń, maszynę stanów, rejestr usług i ładowanie integracji, w tym obiekt hass. - 100pytest
Wspiera testowanie w Python z pytest: fixtures, parametrize, mockowanie, conftest.py i pokrycie kodu. - 100testing-python
Pomaga pisać i oceniać testy w pytest: fixtures, parametryzację, mocki, testy asynchroniczne i integrację z CI. - 100ha-validate
Waliduje YAML Home Assistant pod kątem błędów składni, niezdefiniowanych sekretów i zduplikowanych kluczy oraz uruchamia hass check_config. - 100test-harness
Generuje zestawy testów pytest: ścieżkę szczęśliwą, przypadki brzegowe, błędy, fixtury, mocki i wzorce asynchroniczne.