Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get a response when using callService and calendar.list_events? #421

Open
tonka3000 opened this issue Sep 16, 2023 · 8 comments
Open

Comments

@tonka3000
Copy link

Hi,

I try to use callService to get the calendar events of an entity. I have an issue because I always get the error

{
  code: 'unknown_error',
  message: 'Service call requires responses but caller did not ask for responses'
}

The message make sense but I could not find a way to add the response variable as it is done on the service side. I either became an error from typescript or an error from the server that the key is not allowed.

How can I get response data via callService?

Here is my code snippet.

const rc = await callService(
    con,
    "calendar",
    "list_events",
    {
      duration: {
        hours: 96,
        minutes: 0,
        seconds: 0,
      },
    },
    { entity_id: "calendar.mytestcalendar" } 
  );

My Home Assistant instance is on 2023.9.2.

Thanks in advance
Michael

@shannonhochkins
Copy link

Did you figure this out @tonka3000 ? Running into the same issue

@tonka3000
Copy link
Author

@shannonhochkins Sadly no. Still not sure how to do it.

@shannonhochkins
Copy link

I got it to work using sendMessagePromise, did you want me to show you?

@shannonhochkins
Copy link

You're not by chance using ha-component-kit are you?

@tonka3000
Copy link
Author

I got it to work using sendMessagePromise, did you want me to show you?

Sure

You're not by chance using ha-component-kit are you?

No. I build https://www.raycast.com/tonka3000/homeassistant

@shannonhochkins
Copy link

With sendMessagePromise from the Connection object,

store.connection?.sendMessagePromise({
      type: "execute_script",
      sequence: [{
        "service": "calendar.list_events",
        "data": {
          "duration": {
            "hours": 24,
            "minutes": 0,
            "seconds": 0
          }
        },
        "target": {
          "entity_id": "calendar.ENTITY_NAME"
        },
        "response_variable": "service_result"
      },
      {
        "stop": "done",
        "response_variable": "service_result"
      }]
  })

This gave me a response correctly, however the summary html is stripped out and replaced with new line characters, if you want the html value of the event, you'll need to use the api, start/end dates as Date values

  async callApi (endpoint: string, options: RequestInit) => {
    try {
      return fetch(`${hassUrl}${endpoint}`, {
        ...options,
        headers: {
          Authorization: "Bearer " + connection?.options.auth?.accessToken,
          "Content-type": "application/json;charset=UTF-8",
        },
      }).then((response) => response.json());
    } catch (e) {
      console.error(e);
      throw new Error(
        `Error with request to Home Assistant for endpoint: "${endpoint}", please check your request params are correct.`,
      );
    }
  }
  // usage
  const params = encodeURI(
    `?start=${start.toISOString()}&end=${end.toISOString()}`
  );
  const events = await callApi<CalendarEvent[]>(`/api/calendars/${entity.entity_id}${params}`, {
        method: "GET",
      });

keep in mind you'll need to enable api: in the configuration of home assistant to use the api

@shannonhochkins
Copy link

I've been building something completely different, but there might be some reference there for you - the calendar card is still in development

https://shannonhochkins.github.io/ha-component-kit/

@tonka3000
Copy link
Author

@shannonhochkins The project looks awesome. Great work 🎉

Yeah, we building something different but share the js lib in common.

Thanks for your snippet, I will try to use it your way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants