Skip to main content
The homeassistant.core module contains the fundamental classes that power Home Assistant.

HomeAssistant Class

The HomeAssistant class is the root object of the Home Assistant home automation system.

Initialization

str
required
Path to the configuration directory

Key Attributes

HassDict
Dictionary that any component can store data on. This is the primary way to share data between components.
EventBus
The event bus for firing and listening to events.
ServiceRegistry
The service registry for registering and calling services.
StateMachine
The state machine for tracking entity states.
Config
Configuration object containing Home Assistant settings.
CoreState
Current state of Home Assistant (NOT_RUNNING, STARTING, RUNNING, STOPPING, STOPPED).
asyncio.AbstractEventLoop
The asyncio event loop.

Methods

async_start()

Finalize startup from inside the event loop.

async_stop(exit_code: int = 0, *, force: bool = False)

Stop Home Assistant and shut down all threads.
int
default:"0"
Exit code to return
bool
default:"False"
Force stop regardless of current state

async_create_task(target, name=None, eager_start=True)

Create a task from within the event loop.
Coroutine
required
Coroutine to execute
str
Name for the task (useful for debugging)
bool
default:"True"
Whether to start the task eagerly
asyncio.Task
The created task

async_create_background_task(target, name, eager_start=True)

Create a background task that won’t block startup or shutdown.
Coroutine
required
Coroutine to execute
str
required
Name for the task
bool
default:"True"
Whether to start the task eagerly

async_add_executor_job(target, *args)

Add an executor job from within the event loop.
Callable
required
Function to execute in the executor
Any
Arguments to pass to the function

State Class

Represents the state of an entity.

Initialization

str
required
The entity ID (format: domain.object_id)
str
required
The state value
Mapping[str, Any]
Additional attributes for the state
datetime
When the state was last changed
datetime
When the state or attributes were last updated
Context
Context in which the state was created

Attributes

str
The entity ID
str
The state value
ReadOnlyDict[str, Any]
Read-only dictionary of attributes
str
Domain portion of the entity ID
str
Object ID portion of the entity ID
datetime
When the state was last changed
datetime
When the state or attributes were last updated
datetime
Last time the state was reported
Context
Context object containing user_id, parent_id, and id

Methods

as_dict()

Return a read-only dictionary representation of the state.

Event Class

Represents an event within the event bus.

Initialization

str
required
Type of the event
Mapping[str, Any]
Event data
EventOrigin
default:"EventOrigin.local"
Origin of the event (local or remote)
Context
Context in which the event was fired

Attributes

str
Type of the event
Mapping[str, Any]
Event data dictionary
EventOrigin
Origin of the event (local or remote)
datetime
When the event was fired
Context
Context object

ServiceCall Class

Represents a call to a service.

Initialization

HomeAssistant
required
Home Assistant instance
str
required
Domain of the service
str
required
Name of the service
dict[str, Any]
Service call data
Context
Context for the service call
bool
default:"False"
Whether to return a response

Attributes

str
Service domain
str
Service name
ReadOnlyDict[str, Any]
Service call data
Context
Context object
bool
Whether a response is expected

Context Class

Represents the context that triggered something.

Initialization

str
ID of the user who triggered the action
str
ID of the parent context
str
Unique ID for this context (auto-generated if not provided)

Attributes

str
Unique context ID (ULID format)
str | None
User ID if triggered by a user
str | None
Parent context ID if this is a child context

Helper Functions

callback(func)

Decorator to mark a method as safe to call from within the event loop.

async_get_hass()

Return the HomeAssistant instance from within the event loop.
This function raises HomeAssistantError if called from the wrong thread. Use sparingly and prefer passing the hass instance as a parameter.

split_entity_id(entity_id: str)

Split a state entity ID into domain and object ID.
str
required
Entity ID to split
tuple[str, str]
Tuple of (domain, object_id)

valid_entity_id(entity_id: str)

Test if an entity ID is in a valid format.

CoreState Enum

Represents the current state of Home Assistant.
str
Home Assistant is not running
str
Home Assistant is starting up
str
Home Assistant is running
str
Home Assistant is stopping
str
Final write stage during shutdown
str
Home Assistant has stopped

EventOrigin Enum

Represents the origin of an event.
str
Event originated locally
str
Event originated from a remote source