Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support return_result from HASS services. #1837

Open
1 task done
chatziko opened this issue Jul 7, 2023 · 19 comments
Open
1 task done

Support return_result from HASS services. #1837

chatziko opened this issue Jul 7, 2023 · 19 comments
Labels
enhancement New feature or request

Comments

@chatziko
Copy link

chatziko commented Jul 7, 2023

Is there an existing feature request for this?

  • 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 chatziko added the enhancement New feature or request label Jul 7, 2023
@Odianosen25
Copy link
Collaborator

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

@chatziko
Copy link
Author

chatziko commented Jul 8, 2023

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.

@Odianosen25
Copy link
Collaborator

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

@chatziko
Copy link
Author

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).

@Odianosen25
Copy link
Collaborator

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

@frenck
Copy link
Contributor

frenck commented Jul 25, 2023

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

@chatziko
Copy link
Author

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.

@acockburn
Copy link
Member

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

@markusressel
Copy link

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 😅

@chatziko
Copy link
Author

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.

@markusressel
Copy link

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

@chatziko
Copy link
Author

chatziko commented Feb 2, 2024

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

@grewhit25
Copy link

grewhit25 commented Feb 2, 2024 via email

@acockburn
Copy link
Member

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.

@balk77
Copy link
Contributor

balk77 commented May 4, 2024

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?

@chatziko
Copy link
Author

chatziko commented May 7, 2024

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

@ArdJonker
Copy link

I run into the same problem and would appreciate 💙 this being solved!

@hacscred
Copy link

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.

Has there been any news on this front? There are some other issues in which related issues have been discussed and some documentation for a 4.5 release has also been written.

@acockburn
Copy link
Member

Yes indeed - as you spotted, this will be fixed in 4.5 when it releases. There is currently a working version in dev but it will be rewritten before the final release to accommodate other changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

9 participants