Calling service from python script and getting result

I want to get a unified list of events from multiple calendars. Since this isn’t supported, my idea is to write a python script that calls calendar.list_events a bunch of times and then merges the results in a sorted way.

How do I structure the hass.services.call call to get results back? Currently I’m getting the following error

ERROR (SyncWorker_2) [homeassistant.components.python_script.get_calendar_multi.py] Error executing script: Service call requires responses but caller did not ask for responses
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 224, in execute
    exec(compiled.code, restricted_globals)  # noqa: S102
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "get_calendar_multi.py", line 23, in <module>
  File "/usr/src/homeassistant/homeassistant/core.py", line 1874, in call
    ).result()
      ^^^^^^^^
  File "/usr/local/lib/python3.11/concurrent/futures/_base.py", line 456, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/usr/src/homeassistant/homeassistant/core.py", line 1926, in async_call
    raise ValueError(

I’ve tried setting result_variable and response_variable on the service data that I send, but it makes no difference.

Ah, let me try this. I didn’t realize that jinja templates supported concatenating lists.

edit: Yep, this works!

The only thing I can’t really figure out how to do elegantly is to sort the events by time. The events come through as ISO timestamp strings, which may have different time zones. So a simple sort by attribute doesn’t cut it.

I ended up mapping a second list of just the sortable times, and building out a new object. For anyone who is curious, or has a better suggestion, here’s what I ended up with.

  - variables:
      all_events: >
        {{ cal1.events + cal2.events + cal3.events + cal4.events + cal5.events }}
      todays_events: >
        {%- set all_events_times = all_events | map(attribute='start') | map('as_timestamp') | list -%}
        {%- set data=namespace(new_objects=[]) -%}
        {%- for event in all_events -%}
        {%- set new_event = { 
          'summary': event.summary, 
          'start': event.start, 
          'start_sortable': all_events_times[loop.index-1]
          }
        -%}
        {%- set data.new_objects=data.new_objects + [new_event] -%}
        {%- endfor -%}
        {{  data.new_objects | sort(attribute='start_sortable') }}

strictly speaking, I don’t need to create a third attribute, especially since I’m always just converting it to a timestamp, but I haven’t cleaned up yet.