Skip to main content
Services are the primary way to trigger actions in Home Assistant. The Service Registry manages registration, validation, and execution of callable services across all domains.

ServiceRegistry Overview

The ServiceRegistry 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 a Service 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 a ServiceCall 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

Services can only return response data when called with blocking=True and return_response=True. Attempting to use return_response=True with blocking=False will raise a ServiceValidationError.

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:
When blocking=False, exceptions are caught and logged automatically by _run_service_call_catch_exceptions. Only blocking calls propagate exceptions to the caller.

Best Practices

Use async handlers: Always prefer async service handlers for better performance
Define schemas: Always define validation schemas to catch errors early
Return JSON-serializable data: Response data must be JSON-serializable
Handle contexts: Propagate contexts to maintain audit trails
Document services: Use service descriptions and description_placeholders

Service Description Files

Services are documented in services.yaml:
This enables automatic UI generation and API documentation.

Performance Considerations

  1. Use callbacks: Decorate simple services with @callback when possible
  2. Avoid blocking I/O: Use async operations or executor jobs for I/O
  3. Validate early: Schema validation prevents invalid data from reaching handlers
  4. Background execution: Use blocking=False for fire-and-forget operations
  5. Batch operations: Consider batching multiple entity operations in a single service