How to call media_player volume set from python script?

As I mentioned before the python documentation is very laky, and what should be a very simple and streamline operation is becoming a tedious trial and error process.
What is the proper way of translating this yaml:

-   service: media_player.volume_set
     entity_id: media_player.downstairs
     data:
       volume_level: '1.0'

In a python script?
This is what I tried:

hass.services.call("media_player","volume_set",{"volume_level":1, "entity_id": device})

But it complains about entity_id not being valid:

Error executing script: not a valid value for dictionary value @ data['entity_id']

If I remove it, what I get is an error saying that I should provide that. So, what is the right way of doing that?

Found a solution

Good for you, but please share your solution in this community so that others with a similar problem ,landing on this post in the future, can use the solution as well. See: I’ve solved it!

1 Like

Yes, that is indeed my plan. But that message was send from my phone as soon as I realized to prevent anyone putting time into it.

So, TLDR, here is the correct syntax for setting the volume:

hass.services.call("media_player","volume_set",{"volume_level":0.6, "entity_id": "entity-id"})

Turns out that the error was not pointing that the property were not allowed, no, it was trying to say (in a confusing manner) that the value of the property was incorrect. That was the first tricky part

What is the other tricky part? the name/id of the entity. First confusing part is that the name of the entity to set the volume is different from the name you use to to play a sound using the notify service. On top of that, for some reason the name/id of the device for the media player needs to be prefixed with media_player., while the one for the notification does notr.
Here you can see one example with the two different names:

device_volume = "media_player.echo_1"
device_play = "alexa_media_echo_1"

hass.services.call("media_player","volume_set",{"volume_level":0.6, "entity_id": device_volume})
hass.services.call("notify",device_play,call_data)