This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a Home Assistant custom integration for National Grid, based on the ludeeus/integration_blueprint template. It uses the aionatgrid Python library to access data from National Grid. It uses HACS for distribution and requires Home Assistant 2025.2.4+.
# Install dependencies (run first time or after requirements.txt changes)
scripts/setup
# Start Home Assistant with the integration loaded (creates config/ dir if needed)
scripts/develop
# Format and lint code (uses ruff)
scripts/lintThe devcontainer is configured for VS Code with Python 3.13 and exposes Home Assistant on port 8123.
The integration follows the standard Home Assistant custom component pattern:
-
__init__.py: Entry setup withasync_setup_entry/async_unload_entry. Configures the coordinator with 1-hour update interval and forwards to platforms (sensor, binary_sensor). -
coordinator.py:NationalGridDataUpdateCoordinatorextends Home Assistant'sDataUpdateCoordinator. Usesaionatgrid.NationalGridClientdirectly (no intermediate wrapper). ContainsAmiMeterIdentifier,MeterData, andNationalGridCoordinatorDatadataclasses. Fetches billing, usage, cost, and AMI data per meter. Catchesaionatgridexceptions and translates to HA-specific ones (ConfigEntryAuthFailed,UpdateFailed). -
config_flow.py:NationalGridFlowHandlerimplements UI configuration. Collects username/password, then presents account selection step. Supports reauthentication flow. -
entity.py:NationalGridEntitybase class extendsCoordinatorEntity. Sets up device info and unique_id from config entry. -
data.py:NationalGridConfigEntrytype alias for typed config entries.entry.runtime_datais the coordinator directly. -
const.py: Domain, logger, attribution,CONF_SELECTED_ACCOUNTS, unit constants (UNIT_KWH,UNIT_CCF,THERM_TO_CCF), andtherms_to_ccf()conversion helper. -
statistics.py: Imports long-term statistics into Home Assistant's recorder.async_import_all_statisticsprocesses hourly and interval data for each meter, converting units as needed (therms → CCF for gas). -
Platform files (
sensor.py,binary_sensor.py): Each defines entity descriptions and entity classes inheriting fromNationalGridEntity.
- All entities inherit from
NationalGridEntitywhich handles coordinator binding and device registration - Runtime data stored in
entry.runtime_dataas the coordinator directly (no wrapper dataclass) - Coordinator uses
aionatgrid.NationalGridClientdirectly with an HA-managed session - Uses
CoordinatorEntitypattern for automatic state updates
- When looking up Home Assistant developer documentation, use Context7 with the library ID
/home-assistant/developers.home-assistant. - When looking up
aionatgridlibrary documentation, use Context7 with the library ID/ryanmorash/aionatgrid.
- Uses ruff for formatting and linting
- Uses black-compatible formatting (via ruff)
- Type hints throughout with
TYPE_CHECKINGimports for circular dependency prevention