-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample-dim-light.php
More file actions
69 lines (59 loc) · 2.15 KB
/
Copy pathexample-dim-light.php
File metadata and controls
69 lines (59 loc) · 2.15 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
68
69
<?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
*/
use IKEA\Tradfri\Device\LightBulb;
require __DIR__ . '/init.php';
try {
echo '---------- IKEA Tradfri PHP API Example: ' . \basename(__FILE__) . \PHP_EOL;
/** @var \IKEA\Tradfri\Service\ServiceInterface $api */
$lights = $api->getLights();
$lights->sortByState();
if (false ===$lights->isEmpty()) {
$light = $lights->first();
\assert($light instanceof LightBulb);
echo '---------- IKEA Tradfri PHP API Example: ' . \basename(__FILE__) . \PHP_EOL;
echo '---------- Light Information' . \PHP_EOL;
echo ' ' . \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;
echo '---------- Check State' . \PHP_EOL;
if (!$light->isOn()) {
if ($light->switchOn()) {
echo 'switched on' . \PHP_EOL;
}
}
echo 'light is now ' . $light->getReadableState() . \PHP_EOL;
echo '---------- Dim light' . \PHP_EOL;
echo '- Brightness is: ' . $light->getBrightness() . '%' . \PHP_EOL;
echo '- Brightness to 75%' . \PHP_EOL;
if ($light->dim(75)) {
echo 'was set to 75%' . \PHP_EOL;
}
\sleep(2);
if ($light->dim(15)) {
echo 'was set to 15%' . \PHP_EOL;
}
\sleep(2);
if ($light->switchOff()) {
echo 'was set to 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;
}