Creating Components
This guide walks you through creating a complete Home Assistant integration from scratch. We’ll build a simple integration that demonstrates all the key concepts.Integration Structure
Every integration follows a standard directory structure:Step 1: Create the Manifest
Themanifest.json file defines your integration’s metadata:
Step 2: Implement the Main Component
The__init__.py file is the entry point for your integration.
YAML-Based Setup (Legacy)
For simple integrations or those requiring YAML configuration:__init__.py
Config Entry-Based Setup (Recommended)
For modern integrations with UI configuration:__init__.py
The
async_setup_entry method is called when a user configures your integration through the UI. See Config Flow for implementation details.Step 3: Create Platform Files
Each platform (light, sensor, etc.) lives in its own file.Example: Light Platform
light.py
Example: Sensor Platform
sensor.py
Step 4: Add Constants
Create aconst.py file for constants:
const.py
Step 5: Testing Your Integration
Best Practices
Use Async/Await
Handle Errors Gracefully
Use Type Hints
Follow Naming Conventions
- Integration domain: lowercase with underscores (e.g.,
my_integration) - Class names: PascalCase (e.g.,
MyLight) - Functions: snake_case (e.g.,
async_setup_entry) - Private members: prefix with underscore (e.g.,
_attr_name)
Real-World Example: Demo Integration
The Demo integration is a great reference:homeassistant/components/demo/__init__.py:163
Next Steps
Integration Manifest
Learn about all manifest.json options
Config Flow
Add UI-based configuration to your integration
Entity Platforms
Deep dive into entity platform implementation