Prerequisites
Before you begin, ensure you have:- A working Home Assistant development environment
- Python 3.12 or later installed
- Familiarity with async/await patterns in Python
- Understanding of the device or service you want to integrate
Integration Structure
A basic integration consists of several key files:Step 1: Create the Manifest
Themanifest.json file defines your integration’s metadata. This is required for Home Assistant to recognize your integration.
manifest.json
Key Manifest Fields
- domain: Unique identifier for your integration (lowercase, no spaces)
- name: Human-readable name displayed in the UI
- config_flow: Set to
trueif your integration uses the UI configuration flow - iot_class: How the integration communicates (see IoT class documentation)
- requirements: Python packages required by your integration
- codeowners: GitHub usernames responsible for maintaining this integration
Step 2: Define Constants
Create aconst.py file to store configuration keys and constants:
const.py
Step 3: Implement the Main Integration
The__init__.py file contains the core integration setup logic:
__init__.py
Key Functions
async_setup_entry
This is the main entry point for modern integrations. It:- Initializes API clients or data coordinators
- Stores shared data in
hass.data[DOMAIN] - Forwards setup to entity platforms
- Returns
Trueon success or raisesConfigEntryNotReadyon failure
async_unload_entry
Cleans up when the integration is removed:- Unloads all entity platforms
- Cleans up stored data
- Closes API connections if needed
Step 4: Create a Config Flow
The config flow handles user configuration through the UI. See the Config Entries guide for detailed information.config_flow.py
Step 5: Create Entity Platforms
Entity platforms provide the actual functionality. For example, a sensor platform:sensor.py
Step 6: Add Translations
Create astrings.json file for UI translations:
strings.json
Testing Your Integration
Before submitting your integration:- Run tests: Ensure your integration has comprehensive test coverage
- Check code quality: Run
pylintandmypyon your code - Test manually: Install your integration in a development Home Assistant instance
- Review the checklist: Follow the integration quality scale requirements
Best Practices
Use DataUpdateCoordinator
For integrations that poll data, useDataUpdateCoordinator to efficiently manage updates:
Handle Connection Errors
Always handle connection errors gracefully:- Raise
ConfigEntryNotReadyduring setup if the device is temporarily unavailable - Raise
ConfigEntryAuthFailedif authentication fails - Set entity
availableproperty based on connection status
Use Unique IDs
Always provide unique IDs for entities to enable customization:Follow the Quality Scale
Aim for at least Silver quality scale:- Use config flow (no YAML configuration)
- Implement proper entity naming
- Handle errors gracefully
- Provide translations
- Include tests
Next Steps
- Learn about Config Entries
- Create Entity Components
- Implement Device Automation
- Review existing integrations for examples
- Read the Integration Quality Scale
Common Patterns
Single Config Entry Integrations
For services that should only have one configuration:manifest.json
Virtual Integrations
For integrations that re-use another integration’s code:manifest.json