How to register service calls?

Can anyone point me at documentation and/or example code detailing how to implement a custom (non-async) entity service?

I’m extending the integration for the NAD media player, need to add new services that allow management of some new attributes.

The example for hass.services.register in the current documentation is out of date or doesn’t work (see “NoneType: None” error registering service action).

Thanks.

Please show your code.

For examples, just look at the builtin implementations

Thanks for replying Chris B.

I’ve put aside the NAD integration code for the moment, now just trying to get the documentation example code working. I started with exactly what’s presented in the documentation:

DOMAIN = “hello_action”

ATTR_NAME = “name”
DEFAULT_NAME = “World”

def setup(hass, config):
“”“Set up is called when Home Assistant is loading our component.”“”

def handle_hello(call):
    """Handle the service action call."""
    name = call.data.get(ATTR_NAME, DEFAULT_NAME)

    hass.states.set("hello_action.hello", name)

hass.services.register(DOMAIN, "hello", handle_hello)

# Return boolean to indicate that initialization was successful.
return True

It errors same as the NAD integration:

ERROR (MainThread) [homeassistant.helpers.service] Failed to load integration: hello_action
NoneType: None

No syntax errors, just the cryptic “NoneType: None”. Comment out the hass.services.register line, the error goes away.

Working from one of the examples you had forwarded, I evolved the documentation code to this:

import voluptuous as vol
from homeassistant.core import HomeAssistant, ServiceCall

DOMAIN = “hello_action”

ATTR_NAME = “name”
DEFAULT_NAME = “World”

EMPTY_SERVICE_SCHEMA = vol.Schema({})

def setup(hass, config):
“”“Set up is called when Home Assistant is loading our component.”“”

def handle_hello(call: ServiceCall) -> None:
    """Handle the service action call."""
    name = call.data.get(ATTR_NAME, DEFAULT_NAME)

    hass.states.set("hello_action.hello", name)

hass.services.register(DOMAIN, "hello", handle_hello, schema=EMPTY_SERVICE_SCHEMA,)

# Return boolean to indicate that initialization was successful.
return True

Still get the same error.

I wish I could find&review the declaration for the hass.services.register function - that might provide some clues - but I’ve only been able to find the hass.services.async_register function. At least I think that’s what that is?

Much appreciated.