Integration Services

Home Assistant provides ready-made services for a lot of things, but it doesn't always cover everything. Instead of trying to change Home Assistant, it is preferred to add it as a service under your own integration first. Once we see a pattern in these services, we can talk about generalizing them.

This is a simple "hello world" example to show the basics of registering a service. To use this example, create the file <config dir>/custom_components/hello_service/ and copy the below example code.

Services can be called from automations and from the service "Developer tools" in the frontend.

DOMAIN = "hello_service"
ATTR_NAME = "name"
def setup(hass, config):
"""Set up is called when Home Assistant is loading our component."""
def handle_hello(call):
"""Handle the service call."""
hass.states.set("hello_service.hello", name), "hello", handle_hello)
# Return boolean to indicate that initialization was successfully.
return True

Load the integration by adding the following to your configuration.yaml. When your component is loaded, a new service should be available to call.

# configuration.yaml entry

Open the frontend and in the sidebar, click the first icon in the developer tool section. This will open the Call Service developer tool. On the right, find your service and click on it. This will automatically fill in the correct values.

Pressing "Call Service" will now call your service without any parameters. This will cause your service to create a state with the default name 'World'. If you want to specify the name, you have to specify parameters. Add the following JSON as Service Data and press "Call Service again".

"name": "Planet"

The service will now overwrite the previous state with "Planet".

Service descriptions

Adding services is only useful if users know about them. In Home Assistant we use a services.yaml as part of your integration to describe the services.

Services are published under the domain name of your integration, so in services.yaml we only use the service name as the base key.

# Example services.yaml entry
# Description of the service
description: Sets fan speed.
# Different fields that your service accepts
# Key of the field
# Description of the field
description: Name(s) of the entities to set
# Example value that can be passed for this field
example: "fan.living_room"
description: Speed setting
example: "low"

Entity Services

Sometimes you want to provide extra services to control your entities. For example, the Sonos integration provides services to group and ungroup devices. Entity services are special because there are many different ways a user can specify entities. It can use areas, a group or a list of entities.

You need to register entity services in your platforms, like <your-domain>/ These services will be made available under your domain and not the media player domain. Example code:

from homeassistant.helpers import config_validation as cv, entity_platform, service
async def async_setup_entry(hass, entry):
"""Set up the media player platform for Sonos."""
platform = entity_platform.current_platform.get()
# This will call Entity.set_sleep_timer(sleep_time=VALUE)
vol.Required('sleep_time'): cv.time_period,

If you need more control over the service call, you can also pass an async function that instead of "set_sleep_timer":

async def custom_set_sleep_timer(entity, service_call):
await entity.set_sleep_timer(['sleep_time'])
Last updated on