GCAL bindings - refreshed version

Hi,
I refreshed gCal bindings by adding Oauth2 support and upgraded Google Calendar API. This change has been requested to merge to official code, but I need some volunteers to test changes before code will be merged.

If you want to help with testing:

  1. please download snapshot version from github:
    https://github.com/SirAd/openhab/releases/download/gcal_v01/org.openhab.io.gcal-1.9.0-SNAPSHOT.jar

  2. put jar file into addons directory

  3. add to openhab.cfg

gcal:client_id=923938667558-3agtjnqnlfq4ku2v6veoradnt88291du.apps.googleusercontent.com
gcal:client_secret=ow_sZCtGTD316vnWm7zOGXfL
gcal:calendar_name=primary

  1. start OpenHAB first time and follow information from console. You must login to provided URL and put provided code to authorize openHAB to use your google calendar.

If you want to use alternate calendar (e.g shared calendar) just put calendar name in openhab.cfg under gcal:calendar_name

if you want to create own google credentials please follow tutorial
https://developers.google.com/identity/sign-in/web/devconsole-project

to create project and Credential. IMPORTANT - Under Application type choose OTHER
When credentials are created click Library -> Calendar API -> Enable API

Regards,
Adam

2 Likes

Thank you very much for refreshing this service so people can schedule commands and updates in the future from their Google Calendar, without having to go through IFTTT or CalDAV.

I have some observations using your provided JAR:

  • It’s very convenient to use your Google API credentials for the purpose of testing (in Step 3 above), but users should be informed that this is what they are entering into their openhab.cfg file (just for the sake of disclosure and to understand the potential ramifications).
  • The steps to create one’s own credentials should probably be spelled out in greater detail, as there are some pitfalls along the way. I downloaded the file client_id.json and used the id and secret in that file. Was that correct?
  • I think I did it right because this appeared in the log:
[INFO ] [g.internal.GCalEventDownloader] - ################################################################################################
[INFO ] [g.internal.GCalEventDownloader] - # Google-Integration: U S E R   I N T E R A C T I O N   R E Q U I R E D !!
[INFO ] [g.internal.GCalEventDownloader] - # 1. Open URL 'https://www.google.com/device'
[INFO ] [g.internal.GCalEventDownloader] - # 2. Type provided code ZPWT-UVXXS 
[INFO ] [g.internal.GCalEventDownloader] - # 3. Grant openHAB access to your Google calendar
[INFO ] [g.internal.GCalEventDownloader] - # 4. openHAB will automatically detect the permiossions and complete the authentication process
[INFO ] [g.internal.GCalEventDownloader] - # NOTE: You will only have 1800 mins before openHAB gives up waiting for the access!!!
[INFO ] [g.internal.GCalEventDownloader] - ################################################################################################

I authorized the app against my Google account and I received no warnings or errors in doing so. However, following the instructions in the wiki page, the command was not sent at the intended time (or yet). The only message logged was the one shown above. This test was performed on openHAB 1.8.3 runtime.

I think people’s ability to make use of your excellent contribution will succeed or fail based on the quality of the documentation explaining how to use it. I see the wiki page is very out of date, and seems to describe the current state (before your PR) as essentially unusable. So I don’t see any reason not to update the wiki now, with very clear and complete documentation for how to succeed.

Just an update to report that the GCal service worked!

After my report 4 days ago which didn’t appear to work, I have since restarted the openHAB server and made another test, and it worked just fine. Thank you @SirAd.

I would really encourage others to try it out. If you are a Google Calendar user, you can use either your default calendar, or make a special one, and schedule future events very easily this way.

To polish up the wiki page and get more testing done, we can revive this very useful openHAB component!

I was able to get this set up in OH2. However, it’s not executing the scheduled commands.

I see this in the log, which all looks good to me.

2016-11-12 09:23:38.148 [DEBUG] [io.gcal.internal.GCalEventDownloader] - found 1 calendar events to process
2016-11-12 09:23:38.148 [DEBUG] [io.gcal.internal.GCalEventDownloader] - given event content doesn't match regular expression to extract start-, end commands - using whole content as startCommand (send PS15Z_Switch ON)
2016-11-12 09:23:38.149 [INFO ] [io.gcal.internal.GCalEventDownloader] - created new startJob 'Turn on PS15Z_Switch' with details 'SchedulerJob [jobKey=xxxxxxxxxxxxxxxxx@google.com_start, jobGroup=gcal, 1 triggers=[Sat Nov 12 09:30:00 EST 2016], content=send PS15Z_Switch ON'
2016-11-12 09:30:00.001 [DEBUG] [gcal.internal.util.ExecuteCommandJob] - About to execute CommandJob with arguments [send, PS15Z_Switch, ON, ]

The binding makes a call to this static method to execute the command, which doesn’t seem to be working.

ConsoleInterpreter.handleRequest(args, new LogConsole());

OTOH, if I run send PS15Z_Switch ON from the karaf console, it works.

Any ideas why it’s not working from GCal?

Found it! I think the code should be refactored to remove use of ConsoleInterpreter, and have the GCal code do the work instead. @Kai, any advice otherwise?

/**
 * This class provides generic methods for handling console input (i.e. pure strings).
 *
 * NOTE: This class is only kept for backward compatibility so that openHAB 1 Add-ons still compile.
 * It must not be used productively!
 *
 * @author Kai Kreuzer
 * @since 0.4.0
 *
 */

…added this issue:

1 Like

Yep, that would explain why it doesn’t do anything… :slight_smile:

static public void handleRequest(String[] args, Console console) {
}

Thanks @watou for tracking that down and opening the issue!

1 Like

That’s great news! Is there a list of attributes, the GCal Binding can get from the google API?
that’s (apart from perfomance) is one of the main advantages over the CalDAV-binding: getting all meta-information, such as alerts. I’d like to use the calender also as trigger - and using not only the beginning of an event as an trigger, but the actual alert is a great advantage.
I’ll try to install the changed binding and get back with results.

The GCal binding isn’t actually a binding. Its job is to read calendar events from a Google Calendar, and then interpret the contents of each entry’s description field as a set of instructions to perform when the event starts and optionally ends. The GCal persistence service works with it to write instructions to perform in the future to future calendar entries, say a week in the future. The GCal “binding” will then perform those instructions in the future just like manually added calendar entries.

The latest code only works on the 1.8 runtime; changes are in process to have it work on 2.0 as well.

OK, I see. My use cases would be something like:

  1. at the time of the respective alert let the loudspeaker in the house tell you to leave the house for the appointment
  2. send direct commands to items (e.g. heating, lighting, …)

I had some logic like this with smarthome.py, but was restricted as the alerts don’t whow up in ical-files. So the time to leave the house for school would be the same as the time to leave the house for the doctor (which is way farther, so the alert is way earlier).

Unfortunately, I’m using OH2, so if I understand correctly at present state the plugin won’t work?

Your use cases are met by the GCal service, and it will soon be updated to function properly on OH2. Right now the code will do everything except actually perform the update, command or run the script expression. You can track progress towards OH2 compatibility here:

Many thanks to @SirAd for his reviving GCal!

Yes, there is possibility to fetch extra information from event (e.g. reminders configuration). This implementation does not use it, but it would be possible to implement additional trigger based on reminder. You can use Google API playground available https://developers.google.com/oauthplayground/ to see what attributes are exposed by API

1 Like

@SirAd @watou
I see the PR with the OH2 fixes was merged. Thanks!!

I would like to try it out in OH2, but I don’t see where to install gcal from PaperUI. Can I just pull the jar from Cloudbees to put in addons?
https://openhab.ci.cloudbees.com/job/openHAB1-Addons/ws/bundles/io/org.openhab.io.gcal/target/

You can put the JAR in your addons directory and it ought to install and start. Note that if you want to also use GCal persistence, you will need that bundle as it was updated as well. Please let us know how it works for you!

Looks good. I can now see the ON/OFF events being logged in events.log. Thanks guys!!

I’m getting
2016-12-03 14:00:23.978 [ERROR] [b.core.service.AbstractActiveService] - Error while executing background thread Google Calender Event-Downloader
java.lang.NullPointerException
at org.openhab.io.gcal.internal.GCalEventDownloader.processEntries(GCalEventDownloader.java:295)[238:org.openhab.io.gcal:1.9.0.201612030211]
at org.openhab.io.gcal.internal.GCalEventDownloader.execute(GCalEventDownloader.java:140)[238:org.openhab.io.gcal:1.9.0.201612030211]
at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:173)[204:org.openhab.core.compat1x:2.0.0.201612010742]

By any chance, is the title of your calendar event empty?

Yeah, I think that’s it. I added an event with an empty title, and it generated a NPE when processing the event.

@SirAd You probably need to check eventTitle for null before using it.

true :flushed:

I opened an issue for this.

Should be fixed in next nightly build: