jekalmin / extended_openai_conversation

Home Assistant custom component of conversation agent. It uses OpenAI to control your devices.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Deprecated Calendar Example

jleinenbach opened this issue · comments

Thank your for your great integration.
I use the following code as a replacement for the calendar events example:

  1. calendar.list_events is deprecated.
  2. There is no need to limit to a special calendar, just let it choose from all visible calendars.
  3. FYI: This does not work with the ical Sensor integration as integrated calendars don't have a unique id.
- spec:
    name: get_events
    description: Use this function to get list of calendar events.
    parameters:
      type: object
      properties:
        entity_id:
          type: string
          description: The entity ID of the calendar
        start_date_time:
          type: string
          description: The start date time in '%Y-%m-%dT%H:%M:%S%z' format
        end_date_time:
          type: string
          description: The end date time in '%Y-%m-%dT%H:%M:%S%z' format
      required:
      - entity_id
      - start_date_time
      - end_date_time
  function:
    type: script
    sequence:
    - service: calendar.get_events
      data:
        start_date_time: "{{start_date_time}}"
        end_date_time: "{{end_date_time}}"
      target:
        entity_id: "{{entity_id}}"
      response_variable: _function_result

Thanks for sharing your example!
In this way, it would be more flexible if you have multiple calendars.

Here's another one.

Features:

  • multiple calenders
  • multiple calenders at once with label_id
  • using anyOf and oneOf (I have no idea if this is documented somewhere)
  • more error handling
- spec:
    name: get_events
    description: Use this function to get a list of calendar events.
    parameters:
      type: object
      properties:
        target:
          type: object
          anyOf:
            - properties:
                entity_id:
                  type: string
                  description: The entity_id of the calendar. label_id or entity_id required.
              required:
                - entity_id
            - properties:
                label_id:
                  type: string
                  description: The label_id of calendars. label_id or entity_id required.
              required:
                - label_id
        start_date_time:
          type: string
          description: The start date time in '%Y-%m-%dT%H:%M:%S%z' format.
        final_time:
          type: object
          oneOf:
            - properties:
                end_date_time:
                  type: string
                  description: The end date time in '%Y-%m-%dT%H:%M:%S%z' format.
              required:
                - end_date_time
            - properties:
                duration:
                  type: object
                  description: Return active events from start_date_time until the specified duration. Expressed as a dictionary with days, hours, or minutes.
              required:
                - duration
      required:
        - start_date_time
        - final_time
        - target
  function:
    type: composite
    sequence:
      - type: script
        sequence:
          - variables:
              target_data: >
                {% set data = {} %}
                {% if target.entity_id is defined and 'entity_id' in target and target.entity_id.split('.')[0] == 'calendar' %}
                  {% set data = dict(data, **{'entity_id': target.entity_id}) %}
                {% endif %}
                {% if target.label_id|default('') in labels() %}
                  {% set data = dict(data, **{'label_id': target.label_id}) %}
                {% endif %}
                {{ data | tojson }}
              event_data: >
                {% set data = {} %}
                {% set data = dict(data, **{'start_date_time': start_date_time}) %}
                {% if 'end_date_time' in final_time and final_time.end_date_time %}
                  {% set data = dict(data, **{'end_date_time': (final_time.end_date_time | default(none))}) %}
                {% elif 'duration' in final_time and final_time.duration %}
                  {% set data = dict(data, **{'duration': (final_time.duration | default(none))}) %}
                {% endif %}
                {{ data | tojson }}
              data_filled: >
                {% set target_dict = (target_data | from_json) if target_data is string else target_data %}
                {% set event_dict = (event_data | from_json) if event_data is string else event_data %}
                {{ target_dict | length > 0 and event_dict | length > 0 }}
          - condition: template
            value_template: "{{ data_filled }}"
          - service: calendar.get_events
            data: "{{ event_data }}"
            target: "{{ target_data }}"
            response_variable: _function_result

@jekalmin
I just realized that you already fixed it in the example file, but it's still the deprecated service in the README file.
https://github.com/jekalmin/extended_openai_conversation/blob/main/README.md