Python script: how to call a service?

I am creating a python script to set the alarm time of my Squeezebox radio.
Setting the alarm via the Home Assistant GUI works flawlessly as follows:

Selection_002

But how do I trigger it from the python script?
My code is:

hass.services.call(‘media_player.squeezebox_call_method’, { “entity_id”: “media_player.squeezebox_radio”, “command”: “alarm”, “parameters”: [“update”, “id:582c05af”, “time:24000”] })

It results in the error (in log):

2018-05-10 09:29:43 ERROR (Thread-18) [homeassistant.components.python_script.wekkertijd.py] Error executing script: ‘dict’ object has no attribute ‘lower’
Traceback (most recent call last):
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/python_script.py”, line 166, in execute
exec(compiled.code, restricted_globals, local)
File “wekkertijd.py”, line 45, in
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/core.py”, line 905, in call
self._hass.loop
File “/usr/lib/python3.5/concurrent/futures/_base.py”, line 405, in result
return self.__get_result()
File “/usr/lib/python3.5/concurrent/futures/_base.py”, line 357, in __get_result
raise self._exception
File “/usr/lib/python3.5/asyncio/tasks.py”, line 239, in _step
result = coro.send(None)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/core.py”, line 932, in async_call
ATTR_SERVICE: service.lower(),
AttributeError: ‘dict’ object has no attribute ‘lower’

At this point, the Squeezebox API is not yet called, so the error comes from HomeAssistant itself.
I don’t understand what is meant with ‘dict’ and ‘lower’, those terms are not in my code.

I run HomeAssistant on Raspbian by the way, not Hass.io

it doesn’t look like that error is coming from squeezebox, it looks like its coming from wekkertijd.py. Is that a custom component of yours? I don’t see it in the HA repository.

The wekkertijd.py is the python script that I wrote containing the above code (of course later it will be expanded to calculate the desired alarm clock time) and run via:

Ah, my mistake. I missed that portion of your post. Calling a script from the ui services tab would use the python_script.wekkertijd.

The python code for calling the squeezebox would be:

hass.services.call('media_player', 'squeezebox_call_method', { “entity_id”: “media_player.squeezebox_radio”, “command”: “alarm”, “parameters”: [“update”, “id:582c05af”, “time:24000”] }, False)
3 Likes

It’s working now, thank you!

Do you have any idea how to play the media_extractor service in python?

rob, try:

hass.services.call('media_extractor', 'play_media', {"entity_id": "media_player.speaker","media_content_id":"https://www.youtube.com/watch?v=IsgM7HjEVKU","media_content_type": "music"}, False)

Pretty sure that should work. I’ll post this in the other thread too.

Hi Pedro,

Can you please help me with this:

hass.services.call('script', 'dimmer',  {
      'light_id': light_id,
      'swipe_action': swipe_action,
    }, False)

I wanted to use python to call script.dimmer with variables light_id and swipe_action. I cannot seem to fire off the script with this python code. Can you please point me in the right direction? Thank you.

use 'script' and 'turn_on' as the service and add 'entity_id': 'script.dimmer' to the dictionary.

Hi Pedro, thanks for your quick response.
So I followed your instructions and make the code like this:

hass.services.call('script', 'turn_on', {
      'entity_id': script.dimmer,
      'light_id': light_id,
      'swipe_action': swipe_action,
    }, False)

Is this correct? Thank you!

script.dimmer needs to be in quotes and you should be good to go

Hi Pedro,

It still doesn’t work. The script didn’t get called.
Below is the python script:

event = data.get('event')
label = data.get('label')
index = data.get('index')
node_id = int(data.get('node_id'))

# we only care about index == 10, the end of the swipe 'parameter'
# and for safety we also only look at valueChanged
if (index == '10') and event == 'valueChanged':
    value = int(data.get('value', '0'))
    # unpack byte 2 (button id)
    scene_id = value >> 24 & 0xff

    # unpack byte 3 (direction)
    scene_value_id = value >> 16 & 0xff
    # offset by 10 to avoid colissions
    scene_value_id = scene_value_id + 10

    scene_value_label = 'Swipe Down'
    if scene_value_id == 11:
        scene_value_label = 'Swipe Up'

    if '7' in node_id:
      node = 'bedroom'
    if '4' in node_id:
      node = 'livingroom'

    if 'livingroom' in node:
      if '1' in scene_id:
        light_id = 'light.living_room_lights'
      if '2' in scene_id:
        light_id = 'light.dining_lights'
      if '3' in scene_id:
        light_id = 'light.corner_lights'
      if '4' in scene_id:
        light_id = 'light.stairs_lights'
    elif 'bedroom' in node:
      if '1' in scene_id:
        light_id = 'light.bedroom_lights'

    swipe_action = scene_value_label
    hass.services.call('script', 'turn_on', {
      'entity_id': 'script.dimmer',
      'light_id': light_id,
      'swipe_action': swipe_action,
    }, False)

I have searched in the forum but couldn’t find any good examples of calling script service from python. I still don’t know where it goes wrong. Thank you for your help.

You still don’t have quotes around script.dimmer.

Sorry that is in my script now. That one I posted was copy & paste. I will correct it.

HI Pedro,

If you can help me further with this script, I created a new topic here. Just to keep all posts in one place.
Thank you.

HI can some one help! I’m trying to execute this Python scrip: import requests

payload = {“activity_uuid”:“AFDF357D-23C0-45FC-8109-1E89C1B8B4B9”}

requests.post(“http://192.168.1.97:47147/api/v1/runactivity”, json=payload)