HA Dev Newbie: async vs sync calls & question about services.yaml

Hey there,

I was doing my first steps in HA development, by contributing to a component I’m using: I’m adding turn_on/turn_off support to this camera custom component: https://github.com/gman-php/reolink/tree/turn-on-off-support

Two things though came up to my mind which I couldn’t answer, even after reading through the “Building integrations” section (https://developers.home-assistant.io/docs/creating_platform_index):

  1. The component I contribute to extends the Camera component. The camera component defines an async and a sync version of turn_on/turn_off. I have now implemented the async versions in the custom components and it works fine. My questions though:
  • Do I also have to implement the sync versions?
  • How does HA decide if it calls the sync or the async version? I saw this thread here, but that doesn’t 100% answer it: Async_setup_entry() and synchronous component - is it possible?
    Or is it determined on which setup() is implemented? So if the component implements async_setup(), HA will only call async version of the functions and the sync ones can be ignored? But why does the camera component then have both versions at all, having the sync ones return “NotImplemented” exception?
  1. services.yaml: If I inherit from a component, in this case the Camera component, do I also need to list all services again, even if the Camera component has definitions for them already? The custom component I’m contributing to has a few service definitions in there which are already listed by the Camera component. But if I look at the Developer Tools => Services section, I can also see the definitions for turn_on/turn_off there when selecting the camera using that custom component. So I guess I don’t have to do add the definitions there? They are just inherited?

Thanks and greetings,

Andy!

Hi there,
No, you don’t have to implement the sync versions. HA prefers async development and first checks for the async method. If it’s not implemented - the sync method is called instead.
It doesn’t depend on sync or async_setup. You can mix sync and async methods, but it is advised to do as much as possible in this way or another + async is always preferred.

1 Like

Awesome, thanks!! That’s already super helpful!

Somebody has some guidance about my 2nd question? Do I need to list all services again in the custom component’s services.yaml, even if the component I’m inheriting from has them already?

Hi,
you must define the services you are providing with YOUR services. This file is only a helper to the users to know what datas to be pass to each services.

1 Like

Thanks, that clarifies it! :slight_smile: