appdaemon icon indicating copy to clipboard operation
appdaemon copied to clipboard

Support return_result from HASS services.

Open chatziko opened this issue 2 years ago • 19 comments

Is there an existing feature request for this?

  • [X] I have searched the existing issues

Your feature request

HASS 2023.07 adds support for returning a response from services. AppDaemon's call_service already has a return_result option, which works eg with template/render, but it doesn't seem to work with the newly added services (conversation.process and calendar.list_events). It would be super useful to allow receiving a response from any such service.

chatziko avatar Jul 07 '23 12:07 chatziko

Hello @chatziko,

It can work with those services, as all you need to do is to add the “return_result=True” as part of the parameters and it should work.

did you try that, and still no luck?

kind regards

Odianosen25 avatar Jul 08 '23 05:07 Odianosen25

Thanks for the response. Yes I did, first I tried with template/render:

res = await self.call_service('template/render', template='{{ now() }}', return_result=True)

> res: 2023-07-08 12:26:20.715988+03:00

But conversation/process returns an empty array:

res = await self.call_service('conversation/process', text='how many lights are on?', language='en', return_result=True)

> res: []

Finally I created a script that returns a response:

test_script:
  sequence:
    - variables:
        result:
          foo: bar
    - stop: ""
      response_variable: result

When I run it from HASS Developer Tools it returns {"foo": "bar"}. But from AppDaemon it seems to return entity information instead of the actual response:

res = await self.call_service('script/test_script', return_result=True)

> res: [{'entity_id': 'script.test_script', 'state': 'off', 'attributes': {'last_triggered': '2023-07-08T09:35:22.178766+00:00', 'mode': 'single', 'current': 0, 'friendly_name': 'test_script'}, 'last_changed': '2023-07-08T09:35:22.179882+00:00', 'last_updated': '2023-07-08T09:35:22.179882+00:00', 'context': {'id': '01H4TDP722NWBG7QE67MRMT7VW', 'parent_id': None, 'user_id': '136f70ca22da47a4ace327d7656a4395'}}]

The above are tested with HASS 2023.7.1 and AppDaemon addon 0.13.1.

chatziko avatar Jul 08 '23 09:07 chatziko

Ok so it is returning something @chatziko, just not what you would expect. This could be in internal HA issue and not necessarily from AD.

Can you kindly try running an external script from AD, and using AD’s credentials call into HA and compare the results if you haven’t already?

kind regards

Odianosen25 avatar Jul 10 '23 04:07 Odianosen25

You're right, I just reported it in HA. You could close this (or maybe leave it open to show that this feature is relevant for AppDaemon).

chatziko avatar Jul 10 '23 16:07 chatziko

Ok cool thanks @chatziko, will close it as it helps us. But glad you figured it out.

Odianosen25 avatar Jul 10 '23 18:07 Odianosen25

Looked at the issue upstream, but AppDaemon should use the WebSocket for service calls instead.

It already has that connect, it will be faster and above all, does support everything Home Assistant has to offer (as our own UI is built on it too).

The Home Assistant project does not consider the REST API to be maintained and it is not likely that will get more or new features. Meaning effectively closing this issue, is saying AppDaemon won't support the requested feature.

../Frenck

frenck avatar Jul 25 '23 20:07 frenck

Maybe re-open this issue then? In the long run we certainly need to be able to get service responses in AppDaemon.

AppDaemon already keeps a websocket to HA open, it shouldn't be hard to use it for service calls.

chatziko avatar Jul 26 '23 08:07 chatziko

Yes, we want to do this when time permits along with some other optimizations so I’ll re-open the issue.

acockburn avatar Jul 26 '23 10:07 acockburn

I am looking into setting up calendar based automations and stumbled upon this issue (service call results in a HASS: Code: 500, error: 500 Internal Server Error). While I understand that websockets are not yet supported, is there currently another way to interact with the calendar/get_events service? Or is it expected that this does not currently work yet?

I am not sure if I am the issue here, since the request seems legit to me, so I figured I would ask here before investing hours into debugging :sweat_smile:

markusressel avatar Jan 25 '24 00:01 markusressel

A workaround is to create a simple wrapper script on the HA side, which calls calendar/get_events, gets the result, and sends it back to appdaemon via an event.

chatziko avatar Jan 25 '24 07:01 chatziko

Thx @chatziko , I got it working as a POC. It took quite a bit of trial and error though, so - even though it is slightly off-topic for this issue - if anyone is interested, I have created a simple writeup here: https://markusressel.de/blog/post/calendar-integration-between-home-assistant-and-appdaemon

markusressel avatar Jan 28 '24 21:01 markusressel

For anyone interested, I created a generic workaround that overrides call_service and automatically routes the service through a generic wrapper script when return_result is given. It allows you to simply do:

list = await self.call_service(
    "todo.get_items",
    entity_id="todo.shopping_list",
    return_result=True,
)

https://gist.github.com/chatziko/74a5eacad3fd934d2ec734dab17aa4c0

chatziko avatar Feb 02 '24 13:02 chatziko

Great! Thanks, I’m interested and will give it a try.

Greg.

On Fri, 2 Feb 2024 at 13:15, Kostas Chatzikokolakis < @.***> wrote:

For anyone interested, I created a generic workaround that overrides call_service and automatically routes the service through a generic wrapper script when return_result is given. It allows you to simply do:

list = await self.call_service( "todo.get_items", entity_id="todo.shopping_list", return_result=True, )

https://gist.github.com/chatziko/74a5eacad3fd934d2ec734dab17aa4c0

— Reply to this email directly, view it on GitHub https://github.com/AppDaemon/appdaemon/issues/1837#issuecomment-1923780708, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIPW6Y6AOR5777IR7EJL35DYRTRIDAVCNFSM6AAAAAA2BZAHZ2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMRTG44DANZQHA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

grewhit25 avatar Feb 02 '24 14:02 grewhit25

Thanks for coming up with a workaround @chatziko.

We are looking at this - it means switching from the HASS REST API to using the stream with is a fairly substantial change, but necessary for other reasons.

acockburn avatar Feb 02 '24 17:02 acockburn

For anyone interested, I created a generic workaround that overrides call_service and automatically routes the service through a generic wrapper script when return_result is given. It allows you to simply do:

list = await self.call_service(
    "todo.get_items",
    entity_id="todo.shopping_list",
    return_result=True,
)

https://gist.github.com/chatziko/74a5eacad3fd934d2ec734dab17aa4c0

Thanks for sharing. I have troubles implementing your workaround. Do you maybe have a full working example?

balk77 avatar May 04 '24 06:05 balk77

Well, the gist does contain a full example, not sure what else you need. Can you describe your troubles?

chatziko avatar May 07 '24 17:05 chatziko