Skip to content
This repository has been archived by the owner on Jun 20, 2021. It is now read-only.

Commit

Permalink
Add color temperature and hsv to getColor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan-Langlais committed Apr 2, 2021
1 parent d741f7b commit eb1e4b1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Interfaces/YeePHPInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ public function getBrightness(): string;
/**
* Return the current light color
*
* @param string $type The type of color
* @return string
*/
public function getColor(): string;
public function getColor(string $type): string;

/**
* Return the current light name
Expand Down
53 changes: 50 additions & 3 deletions src/YeePHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class YeePHP implements YeePHPInterface
public const ALLOWED_PROPS = [
'bright',
'rgb',
'ct',
'hue',
'sat',
'name',
'power'
];
Expand Down Expand Up @@ -156,10 +159,23 @@ public function getBrightness(): string

/**
* @inheritDoc
* @throws Exception
*/
public function getColor(): string
public function getColor(string $type = 'rgb'): string
{
return dechex($this->getProp('rgb'));
if (!in_array($type, self::ALLOWED_COLOR_TYPES, true))
throw new Exception('Invalid color type ' . $type . ' available effects : ( ' . implode(" , ", self::ALLOWED_COLOR_TYPES) . ' )');

switch ($type) {
case 'ct':
return $this->getProp('ct');
case 'rgb':
return dechex($this->getProp('rgb'));
case 'hsv':
return $this->getProps(['hue', 'sat']);
default:
throw new Exception('Invalid color type !');
}
}

/**
Expand Down Expand Up @@ -188,7 +204,7 @@ public function setColor(int $color, array $params = []): self
if (!array_key_exists('type', $params))
$params['type'] = 'rgb';
if (!in_array($params['type'], self::ALLOWED_COLOR_TYPES, true))
throw new Exception('Invalid color type ' . $params['type'] . ' available effects : ' . implode(" ", self::ALLOWED_COLOR_TYPES));
throw new Exception('Invalid color type ' . $params['type'] . ' available effects : ( ' . implode(" , ", self::ALLOWED_COLOR_TYPES) . ' )');

$params = $this->checkColorValue($color, $params);
$params = $this->checkFadeParams($params);
Expand Down Expand Up @@ -324,6 +340,27 @@ protected function getProp(string $prop): string
return $res;
}

/**
* Get a certain prop value
*
* @param string $prop The prop name (refer to doc)
* @return string|null
* @throws Exception
*/
protected function getProps(array $props): string
{
if (!self::array_every(fn ($value) => in_array($value, self::ALLOWED_PROPS), $props))
throw new Exception('Invalid props supplied ' . $props);

$job = $this->createJobArray('get_prop', $props);
$res = $this->makeRequest($job);

if (!$res)
$res = '';

return $res;
}

/**
* Check if the light is online.
*
Expand Down Expand Up @@ -517,4 +554,14 @@ private function createColorJob(int $value, array $params)
throw new Exception('Invalid color type !');
}
}

private static function array_every(callable $callback, $arr)
{
foreach ($arr as $ele) {
if (!call_user_func($callback, $ele)) {
return false;
}
}
return true;
}
}

0 comments on commit eb1e4b1

Please sign in to comment.