HomeAssistant class is the heart of the system, coordinating all operations and managing the asyncio event loop. Every integration, automation, and service interacts with this central hub.
Core Class Structure
TheHomeAssistant object is the main entry point for all Home Assistant operations:
homeassistant/core.py
Key Components
data
Shared storage dictionary for cross-component communication
loop
The asyncio event loop managing all async operations
bus
Event bus for publishing and subscribing to events
services
Registry of all available services across domains
states
State machine tracking all entity states
config
System configuration including paths and settings
Lifecycle Management
Startup Process
Home Assistant follows a well-defined startup sequence:homeassistant/core.py
1
Event Loop Started
The asyncio event loop begins running in the main thread.
2
Core Integrations Load
Essential integrations like
homeassistant and persistent_notification are loaded.3
START Event Fired
The
EVENT_HOMEASSISTANT_START event signals integrations to begin setup.4
Tasks Complete
All startup tasks must complete within 15 seconds (TIMEOUT_EVENT_START).
5
STARTED Event Fired
The
EVENT_HOMEASSISTANT_STARTED event indicates the system is fully operational.Shutdown Process
Shutdown occurs in multiple stages to ensure clean termination:homeassistant/core.py
The shutdown process has timeouts at each stage to prevent a single misbehaving integration from blocking shutdown indefinitely.
Task Management
Home Assistant provides sophisticated task management capabilities:Creating Tasks
homeassistant/core.py
Eager Start: When
eager_start=True, the task begins executing immediately rather than waiting for the next event loop iteration. This significantly improves performance for short-running tasks.Background Tasks
Background tasks are designed for long-running operations:homeassistant/core.py
Job Execution
TheHassJob system provides type-aware job execution:
homeassistant/core.py
Job Types
Thread Safety
Home Assistant enforces strict thread safety:homeassistant/core.py
Calling from External Threads
When calling Home Assistant methods from external threads:Accessing the Instance
In contexts where passinghass is impractical, you can retrieve it:
homeassistant/core.py
Configuration Management
The config object provides access to system configuration:Data Storage
Thehass.data dictionary provides shared storage:
Use the integration domain as the key to avoid conflicts. For complex data structures, consider using
HassKey for type safety.