Created
November 17, 2019 16:45
-
-
Save amityweb/0b39b891f9dc95cc6ccdb8ebe2175264 to your computer and use it in GitHub Desktop.
Set Hive Heating Maximum Temperature using PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Thanks to http://www.smartofthehome.com/2016/05/hive-rest-api-v6/ | |
// Set maxTemp below | |
// Set your username and password | |
// To get the list of all devices to get the thermostat ID, run this script with deviceList as an argument after it | |
/* Set Your Variabes */ | |
$maxTemp = 19.5; // Or 19 or 20 or whatever! | |
$username = 'your-hive-username'; | |
$password = 'your-hive-password'; | |
$thermostatId = 'your-thermostat-id'; | |
// Hive API URL | |
$service_url = 'https://api-prod.bgchprod.info:443/omnia'; | |
/* Create API Endpoint URLs */ | |
$loginurl = $service_url.'/auth/sessions'; | |
$deviceListurl = $service_url.'/nodes'; // Get Device List | |
$deviceurl = $service_url.'/nodes/'.$thermostatId; // Get device ID from Device List Response for Thermostat | |
/* Add Standard CURL Headers */ | |
$headers = array( | |
'Content-Type: application/vnd.alertme.zoo-6.1+json', | |
'Accept: application/vnd.alertme.zoo-6.1+json', | |
'X-Omnia-Client: Hive Web Dashboard' | |
); | |
/* Authenticate to get Session ID */ | |
$ch = curl_init(); | |
$post_data = json_encode( | |
array( "sessions" => | |
array( | |
array( | |
"username" => $username, | |
"password" => $password, | |
"caller" => 'WEB' | |
) | |
) | |
) | |
); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_URL, $loginurl); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); | |
$response = json_decode(curl_exec($ch)); | |
curl_close($ch); | |
/* Get Session ID */ | |
$session_id = $response->sessions[0]->sessionId; | |
/* Add session ID to headers */ | |
$headers[] = 'X-Omnia-Access-Token: '.$session_id; // Set Session ID from Auth | |
// Get Device List | |
if(isset($argv[1]) && $argv[1] == 'deviceList') | |
{ | |
/* Get Device List via CURL from API */ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_URL, $deviceListurl); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$response = json_decode(curl_exec($ch)); | |
curl_close($ch); | |
echo '<pre>'; | |
print_r($response); | |
echo '</pre>'; | |
die(); | |
} | |
else | |
{ | |
/* Get Current Set Temperature via CURL from API */ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_URL, $deviceurl); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$response = json_decode(curl_exec($ch)); | |
curl_close($ch); | |
$currentTemp = $response->nodes[0]->attributes->targetHeatTemperature->reportedValue; | |
echo 'Current Temp is '.$currentTemp."\n"; | |
/* If Current Set Temp id higher than th max... */ | |
if($currentTemp > $maxTemp) | |
{ | |
/* Create post data to set temp to max. */ | |
$post_data = json_encode( | |
array( "nodes" => | |
array( | |
array( | |
'attributes' => array( | |
"targetHeatTemperature" => array( | |
"targetValue" => $maxTemp | |
) | |
) | |
) | |
) | |
) | |
); | |
/* Set Temp via CURL to API */ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_URL, $deviceurl); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); | |
$response = json_decode(curl_exec($ch)); | |
curl_close($ch); | |
echo 'Change to '.$maxTemp . "\n"; | |
} | |
else | |
{ | |
echo 'No change' . "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment