Understanding the Event Bus
The event bus (EventBus) allows firing and listening for events throughout Home Assistant. Every state change, service call, and custom integration action can be represented as an event.
Key Event Types
Home Assistant Core defines several built-in event types:EVENT_STATE_CHANGED- Fired when an entity’s state changesEVENT_STATE_REPORTED- Fired when state is updated but unchangedEVENT_HOMEASSISTANT_START- Fired when Home Assistant startsEVENT_HOMEASSISTANT_STARTED- Fired after all components are loadedEVENT_HOMEASSISTANT_STOP- Fired when Home Assistant begins shutdownEVENT_SERVICE_REGISTERED- Fired when a service is registeredEVENT_CALL_SERVICE- Fired when a service is called
homeassistant/const.py
Listening to Events
Basic Event Listener
Useasync_listen to subscribe to events. This method must be run in the event loop:
homeassistant/core.py:1565
Event Filters
Event filters allow you to pre-filter events before your handler is called, improving performance:homeassistant/core.py:1569
Tracking State Changes
Track State Change Events
For entity-specific state tracking, use the optimizedasync_track_state_change_event helper:
homeassistant/helpers/event.py:309
Track by Domain
Track when entities are added to specific domains:homeassistant/helpers/event.py:653
Advanced Tracking Patterns
Filtered State Change Tracking
Useasync_track_state_change_filtered for dynamic entity tracking:
homeassistant/helpers/event.py:867
Template Tracking
Track template results and react to changes:homeassistant/helpers/event.py:1343
Time-Based Event Tracking
Track Point in Time
Schedule a callback at a specific time:homeassistant/helpers/event.py:1544
Firing Events
Fire Internal Events
For internal core use, fire events directly without extra validation:homeassistant/core.py:1492
Fire Public Events
For integration events that may be consumed externally:homeassistant/core.py:1470
Event Dispatching Optimization
The event system uses several optimizations:- Entity ID Indexing: State change events are indexed by entity_id for fast routing
- Event Filters: Pre-filtering prevents unnecessary job creation
- Call Soon: Events are dispatched with
call_soonto ensure proper event loop iteration - Job Types: Callbacks are executed directly, coroutines are scheduled efficiently
homeassistant/helpers/event.py:340-367
Best Practices
Use @callback Decorator
Always decorate synchronous event handlers with@callback:
Async Handlers
For async operations, use coroutine functions:Clean Up Listeners
Always remove listeners when they’re no longer needed:Avoid Long-Running Operations
Event handlers should be fast. For long operations, create a task:Performance Considerations
- Use specific tracking helpers -
async_track_state_change_eventis more efficient than manual filtering - Implement event filters - Pre-filter events to avoid unnecessary processing
- Index by entity_id - The event system automatically optimizes entity-specific tracking
- Batch updates - When possible, batch multiple state changes together