custom-components / pyscript

Pyscript adds rich Python scripting to HASS
Apache License 2.0
874 stars 46 forks source link

How to use service responses in pyscript? #494

Closed ralle12345 closed 1 year ago

ralle12345 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

Corresponding pull request: #495

craigbarratt commented 1 year ago

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