Skip to main content
The 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

The HomeAssistant 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
Use background tasks for operations that should not prevent Home Assistant from starting or stopping, such as continuous monitoring loops or periodic sync operations.

Job Execution

The HassJob 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:
Always use run_callback_threadsafe when calling Home Assistant methods from background threads to avoid race conditions and data corruption.

Accessing the Instance

In contexts where passing hass is impractical, you can retrieve it:
homeassistant/core.py
Use async_get_hass() sparingly. It’s better to pass hass explicitly to maintain clear dependencies and improve testability.

Configuration Management

The config object provides access to system configuration:

Data Storage

The hass.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.