More than 1 Google Calendar Event in AppDaemon

I want to have the next 5 events of one Google Calendar available in AppDaemon.
I got it somehow working with this hacky solution (for testing):

import appdaemon.plugins.hass.hassapi as hass
from requests import get

class test_cal(hass.Hass):

    def initialize(self):
        self.load_cal()
    
    def load_cal(self):
        ha_url = "http://192.168.1.30:8123"
        token = self.args["token"]
        calendar = "calendar.geburtstage_und_jahrestag"
        start_date = "2019-02-04T00:00:00"
        end_date = "2019-04-04T00:00:00"
        headers = {'Authorization': "Bearer {}".format(token)}
        apiurl = "{}/api/calendars/{}?start={}Z&end={}Z".format(ha_url,calendar,start_date,end_date)
        r = get(apiurl, headers=headers, verify=False)
        self.log(r.json())

By this, I get a json with the raw data.
Can anyone help me achieving this with the “built-in” functionality, not with a separate definition of ha_url and a special token? I think it should be possible with some hass plugin class or method, but I couldn’t find our how. (I do not need the json raw data, I would be happy with the date and the description of the events, no matter in what format).

Or, to make it more general: How do I access stuff from the HASS API which is not predefined in AppDaemon?

Thank you!

1 Like

what kind of calendar are you using?
i ask because when i try the app i get nothing returned.
that can be because i use another kind of calendar.
if you go to dev page to services, what kind of services are available for you that relate to calendar?

I’m not sure if I understand what you are asking… I’ll try my best :slight_smile:
In my configuration.yaml I have:

google:
  client_id: !secret google_calendar_client_id
  client_secret: !secret google_calendar_secret

In google_calendars.yaml I have:

- cal_id: [email protected]
  entities:
  - device_id: geburtstage_und_jahrestag
    ignore_availability: true
    name: Geburtstage und Jahrestag
    track: true

Under “Services” I see:

google.found_calendar
google.scan_for_calendars

Nothing else with google or calendar. I have the “idea” of using this api url from this custom card, where I found it in the js code:
https://github.com/rdehuyss/homeassistant-lovelace-google-calendar-card
Line 26

hmm, so you are also using google calendar and not caldav or something like that.
interesting.
after more trying i get the data indeed also like that.

i suspected there needed to be any kind of service that we could use. but there is not.

i need some time to figure out if we can do this another way.
as soon as i know more ill get back to you.

Thank you!

oke, i did look into it and it seems there is no other way to get to that data at the moment then using requests.

i can tell you however that you can use the url and token from AD.

    def load_cal(self):
        ha_url = self.config["plugins"]["HASS"]["ha_url"]
        token = self.config["plugins"]["HASS"]["token"]

so i guess that at this moment you are stuck with the raw json data and you need to use that to get to the data you want.

i did play around with it a little and this gives the date/dateTime ,the title and the description

import appdaemon.plugins.hass.hassapi as hass
from requests import get
import json

class test_cal(hass.Hass):

    def initialize(self):
        self.load_cal()
    
    def load_cal(self):
        ha_url = self.config["plugins"]["HASS"]["ha_url"]
        token = self.config["plugins"]["HASS"]["token"]
        calendar = "calendar.feyenoord"
        start_date = "2019-02-10T00:00:00"
        end_date = "2019-02-25T00:00:00"
        headers = {'Authorization': "Bearer {}".format(token)}
        apiurl = "{}/api/calendars/{}?start={}Z&end={}Z".format(ha_url,calendar,start_date,end_date)
        r = get(apiurl, headers=headers, verify=False)
        list = json.loads(r.text)
        for element in list:
          #self.log(element)
          description = ""
          if "description" in element:
            description = element["description"]
          summary = ""
          if "summary" in element:
            summary = element["summary"]
          _date = ""
          if "date" in element["start"]:
            _date = element["start"]["date"]
          elif "dateTime" in element["start"]:
            _date = element["start"]["dateTime"]
          self.log("{}: {} ({})".format(_date,summary,description))
3 Likes

Thank you for your code. I’m playing with your json processing at the moment.
The token and ha_url part did not work for me. I use http://hassio/homeassistant as ha_url, which gets loaded from self.config correctly, but I get “Response [400]” (Bad Request) as a result r. When I change the token so a long lived token, I get a “Response [401]” (Unauthorized). The same for the manually entered ha_url with the token from self.config. I only got it working with the combination of manually defined ha_url (ip:port) and long lived token. But that’s OK for me at the moment. Maybe I’ll have a look on that later.

that the token doesnt work isnt that strange wit hassio.
its no long lived token and you use another platform then python.
you could add a key to appdaemon.yaml for yourself.
add

my_own_long_lived_token: TOKEN

and use

        token = self.config["plugins"]["HASS"]["my_own_log_lived_token"]

dont forget to restart AD after that.

are you sure you get the right url?
did you try

self.log(apiurl)

right after you did put the url together?
maybe there is a space, or something like that, that is bothering you.

Thanks, I was looking for the same problem. I think the google calendar component needs some tweaking :slight_smile: