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

How to use service responses in pyscript? #494

Closed
ralle12345 opened this issue Jul 20, 2023 · 6 comments
Closed

How to use service responses in pyscript? #494

ralle12345 opened this issue Jul 20, 2023 · 6 comments

Comments

@ralle12345
Copy link

Hi

I tried to use the new service responses feature of HA2023.07 with the following pyscript code:

calendar.list_events(entity='calendar.awb_koeln', response_variable='agenda', start_date_time='2023-07-22 00:00:00', duration='24')

which leads to this error:

Exception in  line 1:
    calendar.list_events(entity='calendar.awb_koeln', response_variable='agenda', start_date_time='2023-07-22 00:00:00', duration='24')
                                                                                                                                  ^
ValueError: Service call requires responses but caller did not ask for responses

the following YAML works fine in the develpoper tools:

service: calendar.list_events
target:
  entity_id: calendar.awb_koln
data:
  start_date_time: 2023-07-22T00:00:00.000Z
  duration: 24
response_variable: agenda

Does pyscript need an update to work with the new feature?

@matzman666
Copy link
Contributor

Yes, pyscript needs an update to support service responses. It's actually not hard to implement, just adding a few returns to specific places does the trick. If I find time during the weekend I may create a pull request which adds support for service responses.

In the mean time you can use the service API provided directly by home assistant. For this to work you need to enable "Access hass as a global variable?" in the settings. Then you can access the home assistant service API using the hass object. Here's an example of getting a response from a service which works in pyscript:

retval = hass.services.async_call("calendar", "list_events", 
    {
        "entity_id": 'calendar.awb_koeln', 
        "start_date_time": '2023-07-22 00:00:00', 
        "duration": 24
    },
    blocking = True,
    return_response = True
)

You can not only access the service API this way but the complete home assistant API which opens up a lot of possibilities in pyscript scripts ;-)

@ralle12345
Copy link
Author

I just tried your example, but it also yields an error in jupyther:

Exception in  line 5:
            "duration": 24
            ^
HomeAssistantError: Service call requested response data but did not match any entities

@matzman666
Copy link
Contributor

Wow, what a misleading error message. It is neither caused by '"duration": 24' nor by the fact that we requested response data.

The culprit is that the entity_id does not exist. I was testing the code snippet with a calendar entity that existed on my system and replaced it with the one from your example when posting it. But while doing so I added an additional 'e' that shouldn't be there. So remove the 'e' from 'awb_koeln' then it should hopefully works. Damn Umlaute and the lack of them in English.

@ralle12345
Copy link
Author

Damn, I should have seen that, too. Thanks a lot for your patience with me 👍. I leave this open for the 'native' pyscript implementation, though.

@matzman666
Copy link
Contributor

Corresponding pull request: #495

@craigbarratt
Copy link
Member

Thanks @matzman666 for the PR. This was just released in pyscript 1.5.0.

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

No branches or pull requests

3 participants