-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample-group-switch.php
More file actions
67 lines (56 loc) · 2.17 KB
/
Copy pathexample-group-switch.php
File metadata and controls
67 lines (56 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
declare(strict_types=1);
/**
* Copyright (c) 2025-2026 Benjamin Fahl
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/WebProject-xyz/ikea-tradfri-php
*/
require __DIR__ . '/init.php';
try {
echo '---------- IKEA Tradfri PHP API Example: ' . \basename(__FILE__) . \PHP_EOL;
/** @var \IKEA\Tradfri\Service\ServiceInterface $api */
$groups = $api->getGroups();
if ($groups->isEmpty() === false) {
$group= $groups->first();
echo '---------- Group Information' . \PHP_EOL;
echo '- ID: ' . $group->getId() . \PHP_EOL;
echo '- Name: ' . $group->getName() . \PHP_EOL;
$lights = $group->getLights();
if (false === $lights->isEmpty()) {
$light = $lights->first();
$lights->forAll(static function ($key, $light) {
/** @var IKEA\Tradfri\Device\LightBulb $light */
echo '---------- Light Information' . \PHP_EOL;
echo '- ID: ' . $light->getId() . \PHP_EOL;
echo '- Name: ' . $light->getName() . \PHP_EOL;
echo '- Manufacturer: ' . $light->getManufacturer() . \PHP_EOL;
echo '- Version: ' . $light->getVersion() . \PHP_EOL;
echo '- State is: ' . $light->getReadableState() . \PHP_EOL;
echo '- Brightness ' . $light->getBrightness() . '%' . \PHP_EOL;
echo ' ' . \PHP_EOL;
return true;
});
}
echo '---------- Switch Group' . \PHP_EOL;
echo 'group is: ' . ($group->isOn() ? 'on' : 'off') . \PHP_EOL;
if ($group->isOn()) {
if ($group->off()) {
echo 'switched off' . \PHP_EOL;
}
} else {
if ($group->switchOn()) {
echo 'switched on' . \PHP_EOL;
}
}
echo 'group is now ' . ($group->isOn() ? 'on' : 'off') . \PHP_EOL;
return true;
}
} catch (Exception $e) {
echo \PHP_EOL . '---------- Error';
echo \PHP_EOL . $e->getMessage() . \PHP_EOL . \PHP_EOL;
echo $e->getTraceAsString();
exit;
}