Created
October 2, 2020 21:28
-
-
Save FirePanther/a6f6467a58af2f6169ddf40d6e5aed72 to your computer and use it in GitHub Desktop.
This file contains 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 | |
// @author: su.at (made for myself) | |
class Hue { | |
private static $ip = '___ADD-HUE-IP-ADDRESS-HERE___'; | |
private static $user = '___ADD-USER-ID-HERE___'; | |
// private methods | |
private static function request($method, $api, $body) { | |
$url = self::$user.'/'.$api; | |
$options = [ | |
'http' => [ | |
'header' => "Content-type: application/x-www-form-urlencoded\r\n", | |
'method' => $method, | |
'content' => $body | |
] | |
]; | |
$context = stream_context_create($options); | |
return @json_decode(file_get_contents('http://'.self::$ip.'/api/'.$url, false, $context), true); | |
} | |
private static function get($api, $body = '') { | |
return self::request('GET', $api, $body); | |
} | |
private static function put($api, $body = '') { | |
return self::request('PUT', $api, $body); | |
} | |
private static function post($api, $body = '') { | |
return self::request('POST', $api, $body); | |
} | |
private static function delete($api, $body = '') { | |
return self::request('DELETE', $api, $body); | |
} | |
// public methods | |
public static function getLightId($light) { | |
if (isset($light) && !is_numeric($light)) { | |
$lLight = strtolower($light); | |
$lights = self::getLights(); | |
foreach ($lights as $i => $l) { | |
if (strtolower($l['name']) == $lLight) { | |
return $i; | |
} | |
} | |
} | |
return $light; | |
} | |
public static function getGroupId($group) { | |
if (isset($group) && !is_numeric($group)) { | |
$lGroup = mb_strtolower($group); | |
$groups = self::getGroups(); | |
foreach ($groups as $i => $g) { | |
if (mb_strtolower($g['name']) == $lGroup) { | |
return $i; | |
} | |
} | |
} | |
return $group; | |
} | |
public static function getLights($light = null) { | |
$light = self::getLightId($light); | |
return self::get('lights'.($light ? "/$light" : '')); | |
} | |
public static function getGroups($group = null) { | |
if (!is_numeric($group)) $group = self::$groups[$group]; | |
return self::get('groups'.($group ? "/$group" : '')); | |
} | |
public static function setLight($light, $state) { | |
$light = self::getLightId($light); | |
$key = 'on'; | |
if ($state == 'toggle') { | |
$key = $state; | |
$state = true; | |
} | |
if (strtolower($state) === 'off') $state = false; | |
return self::put("lights/$light/state", json_encode([ | |
$key => $state ? true : false | |
])); | |
} | |
public static function setGroup($group, $state) { | |
$group = self::getGroupId($group); | |
if ($state == 'toggle') { | |
$groupData = self::get("groups/$group"); | |
if ($groupData['state']['any_on']) $state = false; | |
else $state = true; | |
} | |
if (strtolower($state) === 'off') $state = false; | |
return self::put("groups/$group/action", json_encode([ | |
'on' => $state ? true : false | |
])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment