Development guidelines for building a Home Assistant custom component that integrates with ResMed's myAir CPAP service API
Development skill for building and maintaining a Home Assistant custom component that pulls daily CPAP data from ResMed's myAir service using an undocumented API.
This skill guides AI assistants in developing a Home Assistant integration following HA best practices, modern Python conventions (3.13+), and comprehensive testing with pytest.
The integration follows Home Assistant's standard custom component layout:
Components are modular with distinct files for:
When deviating from any guideline in this document, explicitly state:
1. Which rule(s) are not being followed
2. Concise reason (e.g., instruction conflict, missing permissions, missing dependencies, user denial, safety policy)
This explanation must appear in the same response where the deviation occurs.
```python
def fetch_myair_data(username: str, password: str) -> dict[str, Any]:
"""Fetch CPAP data from ResMed myAir API.
Args:
username: User's myAir account username
password: User's myAir account password
Returns:
Dictionary containing sleep data metrics
Raises:
AuthenticationError: If credentials are invalid
APIError: If API request fails
"""
```
Run locally before committing:
```bash
pre-commit run --all-files
```
```python
try:
data = fetch_data()
except Exception as err:
_LOGGER.error("Error: %s", err)
try:
data = fetch_data()
except (AuthenticationError, APIError) as err:
_LOGGER.error("Failed to fetch myAir data: %s", err)
raise
```
```python
import pytest
@pytest.mark.parametrize("sleep_score,expected_state", [
(95, "excellent"),
(75, "good"),
(50, "fair"),
(30, "poor"),
])
def test_sleep_score_classification(sleep_score: int, expected_state: str) -> None:
"""Test sleep score classification across various scores."""
assert classify_sleep_score(sleep_score) == expected_state
```
```bash
pytest
pytest # not pytest --disable-warnings
pytest tests/test_sensor.py::test_sleep_score
```
If pytest fails due to missing dependencies:
1. Install missing dev dependencies into `./.venv` if permitted, or
2. Report missing package(s) with exact `pip install` command and fail gracefully
This repository uses:
**Important**: Avoid recommending `tox` by default. Some environments may have legacy VS Code tasks referencing `tox` which can be misleading.
Preferred workflow:
```bash
pre-commit run --all-files
pytest
```
Always run these commands in the repository venv (`./.venv`) unless user specifies otherwise.
The AI assistant will only create branches or open PRs when:
1. User explicitly requests it, or
2. User includes hashtag `#github-pull-request_copilot-coding-agent` to hand off to asynchronous coding agent
```python
from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up ResMed myAir sensors from a config entry."""
coordinator = hass.data[DOMAIN][entry.entry_id]
sensors = [
MyAirSleepScoreSensor(coordinator),
MyAirUsageHoursSensor(coordinator),
]
async_add_entities(sensors)
class MyAirSleepScoreSensor(SensorEntity):
"""Sensor for myAir sleep score."""
def __init__(self, coordinator: MyAirDataUpdateCoordinator) -> None:
"""Initialize the sensor.
Args:
coordinator: Data update coordinator for myAir data
"""
self._coordinator = coordinator
self._attr_name = "Sleep Score"
self._attr_unique_id = f"{coordinator.entry_id}_sleep_score"
```
Use `config_flow.py` for user-friendly setup:
Store all constants in `const.py`:
Use Home Assistant's `DataUpdateCoordinator` for efficient data fetching:
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/resmed-myair-home-assistant-integration/raw