Skip to main content
Services are the primary way to trigger actions in Home Assistant. Understanding how to register and handle services properly is essential for creating integrations that users can control through automations, scripts, and the UI.

Service Registry

The ServiceRegistry manages all available services in Home Assistant:
Reference: homeassistant/core.py:2477

Registering Services

Basic Service Registration

Reference: homeassistant/core.py:2570

Service with Response Data

Services can return data to callers:
Reference: homeassistant/core.py:2533

Service Response Types

Service Handler Types

Callback Handler

For synchronous operations:
Reference: homeassistant/core.py:287

Async Handler

For async operations (most common):

Executor Handler

For blocking operations:
Reference: homeassistant/core.py:347

Service Call Object

ServiceCall Properties

Calling Services

Basic Service Call

Reference: homeassistant/core.py:2712

Fire and Forget

Service Call with Response

Reference: homeassistant/core.py:2752

Service Call with Target

Target allows specifying entities, devices, or areas:

Entity Services

Entity services automatically route calls to entity methods:
Reference: homeassistant/helpers/service.py

Service Validation

Schema Validation

Use voluptuous schemas for robust validation:

Common Validation Helpers

Service Events

Listening for Service Calls

Service Registration Events

Reference: homeassistant/core.py:2644

Removing Services

Reference: homeassistant/core.py:2655

Advanced Patterns

Service with Context Propagation

Dynamic Service Registration

Service with Multiple Response Types

Best Practices

1. Always Validate Input

2. Use Appropriate Handler Types

3. Handle Errors Gracefully

4. Document Services

Create services.yaml in your integration:

5. Clean Up on Unload

Common Pitfalls

  • Don’t forget schema validation (security and UX)
  • Don’t block the event loop in service handlers
  • Always handle missing entities gracefully
  • Clean up services when integration unloads
  • Use context propagation for state changes
  • Document all services in services.yaml

Performance Tips

  1. Use @callback for fast handlers - Avoid task creation overhead
  2. Batch operations - Group multiple entity updates
  3. Validate early - Reject invalid calls quickly
  4. Use entity services - More efficient for entity operations
  5. Limit blocking operations - Use executor for I/O