Is it available to call Appdaemon's method from HA entity?

my room light for configuration.yaml in HA as below

# Room Light
  - platform: template
    switches:
      room_light:
        turn_off:
         # nothing now
       turn_on:
         # nothing now

but I want to do turn_off, turn_on via AppDamon’s methods.
Is there any idea for this?

Actually, I want to add my lights into HA and the ligths are controlled by REST API(apartment server)

However, I need to add “access_token” into header for every APIs.
I can retrive “access_token” with login API and it’s expired in every hours.

So I’m trying to make it with python script in AppDaemon, not using RESTful switch or commands.

HA

  1. Call AppDaemon’s method for turn on / off and states.
    (Don’t need to care about acess tokens, something…)

AppDaemon

  1. Retrive access_token in every 1 hours
  2. If called turn_on or turn_off commands from HA, using access token to call apartment’s API.

Is there any way to call AppDaemon’s method from HA?

You can look my previous method to understand in my situation lol :slight_smile:
https://community.home-assistant.io/t/how-can-i-integrate-my-lights-into-home-assistant-controlled-by-rest-api

Hi, I do that with a combination of script and AppDaemon event trigger. The script in the middle is not necessary, but I use it to generalize the functionality.

The template switch action looks like this:

turn_on:
  service: script.app_daemon_event
  data:
    entity_id: switch.some_entity_id
    payload: "on"
turn_off:
  service: script.app_daemon_event
  data:
    entity_id: switch.some_entity_id
    payload: "off"

The script:

app_daemon_event:
  alias: App Daemon Event
  sequence:
  - event: app_daemon
    event_data:
      entity_id: '{{entity_id}}'
      payload: '{{payload}}'
  mode: single

Then you can register a callback to this event in AppDaemon app:

def initialize(self):
    self.listen_event(self.onSwitchPressed, "app_daemon", entity_id = "switch.some_entity_id")

def onSwitchPressed(self, event_name, data, kwargs):
	if data['payload'] == "on":
		self.log("Turned on")
	else:
		self.log("Turned off")

So, AppDaemon method onSwitchPressed is called everytime you use the switch.

2 Likes

Thanks a lot ! It worked !