/
Group.php
479 lines (421 loc) · 9.69 KB
/
Group.php
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<?php
/**
* Phue: Philips Hue PHP Client
*
* @author Michael Squires <sqmk@php.net>
* @copyright Copyright (c) 2012 Michael K. Squires
* @license http://github.com/sqmk/Phue/wiki/License
*/
namespace Phue;
use Phue\Command\DeleteGroup;
use Phue\Command\SetLightState;
use Phue\Command\SetGroupAttributes;
use Phue\Command\SetGroupState;
use Phue\Helper\ColorConversion;
/**
* Group object
*/
class Group implements LightInterface
{
/**
* Id
*
* @var int
*/
protected $id;
/**
* Group attributes
*
* @var \stdClass
*/
protected $attributes;
/**
* Phue client
*
* @var Client
*/
protected $client;
/**
* Construct a Phue Group object
*
* @param int $id
* Id
* @param \stdClass $attributes
* Group attributes
* @param Client $client
* Phue client
*/
public function __construct($id, \stdClass $attributes, Client $client)
{
$this->id = (int) $id;
$this->attributes = $attributes;
$this->client = $client;
}
/**
* Get group Id
*
* @return int Group id
*/
public function getId()
{
return $this->id;
}
/**
* Get assigned name of Group
*
* @return string Name of Group
*/
public function getName()
{
return $this->attributes->name;
}
/**
* Get type
*
* @return string Group type
*/
public function getType()
{
return $this->attributes->type;
}
/**
* Set name of group
*
* @param string $name
*
* @return self This object
*/
public function setName($name)
{
$x = new SetGroupAttributes($this);
$y = $x->name((string) $name);
$this->client->sendCommand($y);
$this->attributes->name = (string) $name;
return $this;
}
/**
* Get light ids
*
* @return array List of light ids
*/
public function getLightIds()
{
return $this->attributes->lights;
}
/**
* Set lights
*
* @param array $lights
* Light ids or Light objects
*
* @return self This object
*/
public function setLights(array $lights)
{
$lightIds = array();
foreach ($lights as $light) {
$lightIds[] = (string) $light;
}
$x = new SetGroupAttributes($this);
$y = $x->lights($lightIds);
$this->client->sendCommand($y);
$this->attributes->lights = $lightIds;
return $this;
}
/**
* Is the group on?
*
* @return bool True if on, false if not
*/
public function isOn()
{
return (bool) $this->attributes->action->on;
}
/**
* Set group lights on/off
*
* @param bool $flag
* True for on, false for off
*
* @return self This object
*/
public function setOn($flag = true)
{
$x = new SetGroupState($this);
$y = $x->on((bool) $flag);
$this->client->sendCommand($y);
$this->attributes->action->on = (bool) $flag;
return $this;
}
/**
* Get alert
*
* @return string Alert mode
*/
public function getAlert()
{
return $this->attributes->action->alert;
}
/**
* Set light alert
*
* @param string $mode
* Alert mode
*
* @return self This object
*/
public function setAlert($mode = SetLightState::ALERT_LONG_SELECT)
{
$x = new SetGroupState($this);
$y = $x->alert($mode);
$this->client->sendCommand($y);
$this->attributes->action->alert = $mode;
return $this;
}
/**
* Get brightness
*
* @return int Brightness level
*/
public function getBrightness()
{
return $this->attributes->action->bri;
}
/**
* Set brightness
*
* @param int $level
* Brightness level
*
* @return self This object
*/
public function setBrightness($level = SetLightState::BRIGHTNESS_MAX)
{
$x = new SetGroupState($this);
$y = $x->brightness((int) $level);
$this->client->sendCommand($y);
$this->attributes->action->bri = (int) $level;
return $this;
}
/**
* Get hue
*
* @return int Hue value
*/
public function getHue()
{
return $this->attributes->action->hue;
}
/**
* Set hue
*
* @param int $value
* Group value
*
* @return self This object
*/
public function setHue($value)
{
$x = new SetGroupState($this);
$y = $x->hue((int) $value);
$this->client->sendCommand($y);
// Change both hue and color mode state
$this->attributes->action->hue = (int) $value;
$this->attributes->action->colormode = 'hs';
return $this;
}
/**
* Get saturation
*
* @return int Saturation value
*/
public function getSaturation()
{
return $this->attributes->action->sat;
}
/**
* Set saturation
*
* @param int $value
* Saturation value
*
* @return self This object
*/
public function setSaturation($value)
{
$x = new SetGroupState($this);
$y = $x->saturation((int) $value);
$this->client->sendCommand($y);
// Change both saturation and color mode state
$this->attributes->action->sat = (int) $value;
$this->attributes->action->colormode = 'hs';
return $this;
}
/**
* Get XY
*
* @return array X, Y key/value
*/
public function getXY()
{
// return [
// 'x' => $this->attributes->action->xy[0],
// 'y' => $this->attributes->action->xy[1],
// ];
return array(
'x' => $this->attributes->action->xy[0],
'y' => $this->attributes->action->xy[1]
);
}
/**
* Set XY
*
* @param float $x
* X value
* @param float $y
* Y value
*
* @return self This object
*/
public function setXY($x, $y)
{
$_x = new SetGroupState($this);
$_y = $_x->xy((float) $x, (float) $y);
$this->client->sendCommand($_y);
// Change both internal xy and colormode state
$this->attributes->action->xy = array(
$x,
$y
);
$this->attributes->action->colormode = 'xy';
return $this;
}
/**
* Get calculated RGB
*
* @return array red, green, blue key/value
*/
public function getRGB()
{
$xy = $this->getXY();
$bri = $this->getBrightness();
$rgb = ColorConversion::convertXYToRGB($xy['x'], $xy['y'], $bri);
return $rgb;
}
/**
* Set XY and brightness calculated from RGB
*
* @param int $red Red value
* @param int $green Green value
* @param int $blue Blue value
*
* @return self This object
*/
public function setRGB($red, $green, $blue)
{
$x = new SetGroupState($this);
$y = $x->rgb((int) $red, (int) $green, (int) $blue);
$this->client->sendCommand($y);
// Change internal xy, brightness and colormode state
$xy = ColorConversion::convertRGBToXY($red, $green, $blue);
$this->attributes->action->xy = array(
$xy['x'],
$xy['y']
);
$this->attributes->action->bri = $xy['bri'];
$this->attributes->action->colormode = 'xy';
return $this;
}
/**
* Get Color temperature
*
* @return int Color temperature value
*/
public function getColorTemp()
{
return $this->attributes->action->ct;
}
/**
* Set Color temperature
*
* @param int $value
* Color temperature value
*
* @return self This object
*/
public function setColorTemp($value)
{
$x = new SetGroupState($this);
$y = $x->colorTemp((int) $value);
$this->client->sendCommand($y);
// Change both internal color temp and colormode state
$this->attributes->action->ct = (int) $value;
$this->attributes->action->colormode = 'ct';
return $this;
}
/**
* Get effect mode
*
* @return string effect mode
*/
public function getEffect()
{
return $this->attributes->action->effect;
}
/**
* Set effect
*
* @param string $mode
* Effect mode
*
* @return self This object
*/
public function setEffect($mode = SetLightState::EFFECT_NONE)
{
$x = new SetGroupState($this);
$y = $x->effect($mode);
$this->client->sendCommand($y);
$this->attributes->action->effect = $mode;
return $this;
}
/**
* Get color mode of group
*
* @return string Color mode
*/
public function getColorMode()
{
return $this->attributes->action->colormode;
}
/**
* Set scene on group
*
* @param mixed $scene
* Scene id or Scene object
*
* @return self This object
*/
public function setScene($scene)
{
$x = new SetGroupState($this);
$y = $x->scene((string) $scene);
$this->client->sendCommand($y);
return $this;
}
/**
* Delete group
*/
public function delete()
{
$this->client->sendCommand((new DeleteGroup($this)));
}
/**
* __toString
*
* @return string Group Id
*/
public function __toString()
{
return (string) $this->getId();
}
}