PHP: Google Calendar to control Domoticz

From Domoticz
Revision as of 09:52, 18 December 2015 by Egregius (talk | contribs) (PHP: Google Calendar to control Domoticz)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Purpose

Use appointments in Google Calendar to control Domoticz

Dependencies

Best combined with PHP: Store switch states and values in variables and PHP: Functions to control Domoticz

Installation

Get all files from the gcal folder. Best is to clone or download zip at https://github.com/Egregius/PHP-Custom-Floorplan-for-Domoticz You need a Google Calendar https://calendar.google.com You also need a Google Developer API. I used the tutorial at https://developers.google.com/google-apps/calendar/quickstart/php as a base of the script below.

Usage

All commands and switch names are converted to lowercase. Some commands can be sent in Dutch and English.
Example subjects of appointments:
Switch Kitchen On
Schakel Kitchen aan
Dim living 25
Setpoint bathroom 22
Wake Tobi


secure/gcal/gcal.php

<?php
require '/var/www/secure/gcal/autoload.php';
define('APPLICATION_NAME', 'Domoticz');
define('CREDENTIALS_PATH', '/var/www/secure/gcal/gcal.token.json');
define('CLIENT_SECRET_PATH', '/var/www/secure/gcal/gcal.json');
define('SCOPES', implode(' ', array(Google_Service_Calendar::CALENDAR_READONLY)));
if (php_sapi_name() != 'cli') {throw new Exception('This application must be run on the command line.');}
function getClient() {
  $client = new Google_Client();
  $client->setApplicationName(APPLICATION_NAME);
  $client->setScopes(SCOPES);
  $client->setAuthConfigFile(CLIENT_SECRET_PATH);
  $client->setAccessType('offline');
  $credentialsPath = CREDENTIALS_PATH;
  if (file_exists($credentialsPath)) {
    $accessToken = file_get_contents($credentialsPath);
  } else {
    $authUrl = $client->createAuthUrl();
    printf("Open the following link in your browser:\n%s\n", $authUrl);
    print 'Enter verification code: ';
    $authCode = trim(fgets(STDIN));
    $accessToken = $client->authenticate($authCode);
    if(!file_exists(dirname($credentialsPath))) {
      mkdir(dirname($credentialsPath), 0700, true);
    }
    file_put_contents($credentialsPath, $accessToken);
    printf("Credentials saved to %s\n", $credentialsPath);
  }
  $client->setAccessToken($accessToken);

  if ($client->isAccessTokenExpired()) {
    $client->refreshToken($client->getRefreshToken());
    file_put_contents($credentialsPath, $client->getAccessToken());
  }
  return $client;
}
$client = getClient();
$service = new Google_Service_Calendar($client);
$optParams = array('maxResults' => 5,'orderBy' => 'startTime','singleEvents' => TRUE,'timeMin' => date('c'),);
$results = $service->events->listEvents($calendarId, $optParams);

$Sthuis=='Off'||$Sslapen=='On'?$update=$driemin:$update=$eenuur;
if (count($results->getItems()) > 0) {
  foreach ($results->getItems() as $event) {
    if(isset($event->start->dateTime)) $start = strtotime($event->start->dateTime);
    if (empty($start)) {
      $start = strtotime($event->start->date);
    }
	$datetime = strftime("%a %e %b %k:%M:%S", $start);
	printf("Event: %s at %s", $event->getSummary(), $datetime);
	if($start<=$vijfsec){
		printf(" -> Now");
		$item = explode(" ", $event->getSummary());
		$action = strtolower($item[0]);
		if($action=="licht") $action = "schakel";
		else if($action=="dim") $action = "dimmer";
		else if($action=="opstaan") $action = "wake";
		else if($action=="slaap") $action = "sleep";
		$place = strtolower($item[1]);
		if(isset($item[2])) {
			$detail = strtolower($item[2]);
			if($detail=="on") $detail = "On";
			else if($detail=="off") $detail = "Off";
			else if($detail=="aan") $detail = "On";
			else if($detail=="uit") $detail = "Off";
		}
		if ($action=="wake") {
			if(${'Dlevel'.$place}<1&&$mc->get('dimmer'.$place)!=2) {$mc->set('dimmer'.$place,2);Dim(${'DI'.$place},${'Dlevel'.$place}+1);printf("-> Switched");}
		} else if($action=="sleep") {
			if(${'Dlevel'.$place}>1&&$mc->get('dimmer'.$place)!=1) {$mc->set('dimmer'.$place,1);Dim(${'DI'.$place},${'Dlevel'.$place}-1);printf("-> Switched");}
		} else if($action=="dimmer") {
			if(${'Dlevel'.$place}!=$detail||${'DT'.$place}<$update) {Dim(${'DI'.$place},$detail);printf("-> Switched");}
		} else if($action=="schakel") {
			if(${'S'.$place}!=$detail||${'ST'.$place}<$update) {Schakel(${'SI'.$place},$detail);printf("-> Switched");}
		} else if($action=="setpoint") {
			if(${'R'.$place}!=$detail||${'RT'.$place}<$update) {$mc->set('setpoint'.${'RI'.$place},2);Udevice(${'RI'.$place},0,$detail);printf("-> Switched");}
		}
	}
	printf("\n");
  }
}