ServiceRegistry Overview
TheServiceRegistry provides a centralized system for managing services:
homeassistant/core.py
Services are organized in a two-level dictionary structure:
domain -> service_name -> Service. This enables O(1) lookups for service execution.Service Object Structure
Each registered service is represented by aService object:
homeassistant/core.py
Service Components
job
HassJob wrapping the service function for optimized execution
schema
Voluptuous schema for validating service data
supports_response
Whether the service can return data to the caller
description_placeholders
Translation placeholders for service descriptions
ServiceCall Object
When a service is called, it receives aServiceCall object:
homeassistant/core.py
ServiceCall Attributes
- domain: The domain of the service (e.g., “light”)
- service: The service name (e.g., “turn_on”)
- data: Read-only dictionary of service parameters
- context: Context tracking who/what triggered the call
- return_response: Whether the caller expects return data
Registering Services
Basic Registration
1
Service Function Defined
Create an async function that accepts a ServiceCall parameter.
2
Schema Created
Define a voluptuous schema to validate service data.
3
Service Registered
Call
async_register to register the service with the registry.4
Event Fired
The registry fires
EVENT_SERVICE_REGISTERED to notify listeners.Registration with Response Support
Response Support Modes
homeassistant/core.py
- NONE: Traditional services that perform actions without returning data
- OPTIONAL: Services that can optionally return data when
return_response=True - ONLY: Read-only services that must be called with
return_response=True
Calling Services
Basic Service Call
Service Call with Response
Service Call Flow
homeassistant/core.py
1
Handler Lookup
The service handler is retrieved from the registry.
2
Response Validation
Response support flags are validated against the request.
3
Data Validation
Service data is validated against the service schema.
4
Event Fired
EVENT_CALL_SERVICE is fired to notify listeners.
5
Service Executed
The service handler is executed (blocking or background).
Service Execution
Services are executed based on their job type:homeassistant/core.py
The job type is determined automatically when the service is registered, enabling optimal execution without runtime checks.
Service Schema Validation
Schemas use the Voluptuous library for validation:Common Validators
Checking Service Availability
Removing Services
Unregister a service when your component unloads:Always remove services during component cleanup to prevent orphaned service registrations that reference unloaded code.
Service Events
Service operations trigger events:EVENT_SERVICE_REGISTERED
Fired when a service is registered:EVENT_CALL_SERVICE
Fired when a service is called:EVENT_SERVICE_REMOVED
Fired when a service is unregistered:Error Handling
Service calls can raise several exceptions:Best Practices
Service Description Files
Services are documented inservices.yaml:
Performance Considerations
- Use callbacks: Decorate simple services with
@callbackwhen possible - Avoid blocking I/O: Use async operations or executor jobs for I/O
- Validate early: Schema validation prevents invalid data from reaching handlers
- Background execution: Use
blocking=Falsefor fire-and-forget operations - Batch operations: Consider batching multiple entity operations in a single service