File Coverage

blib/lib/Device/Yeelight/Light.pm
Criterion Covered Total %
statement 23 71 32.3
branch 0 20 0.0
condition 0 3 0.0
subroutine 8 16 50.0
pod 4 5 80.0
total 35 115 30.4


line stmt bran cond sub pod time code
1             package Device::Yeelight::Light;
2              
3 1     1   21 use 5.026;
  1         4  
4 1     1   6 use utf8;
  1         2  
  1         16  
5 1     1   23 use strict;
  1         2  
  1         18  
6 1     1   4 use warnings;
  1         2  
  1         46  
7              
8 1     1   6 use Carp;
  1         2  
  1         64  
9 1     1   729 use JSON;
  1         12986  
  1         6  
10 1     1   115 use IO::Socket;
  1         2  
  1         6  
11              
12             =encoding utf8
13             =head1 NAME
14              
15             Device::Yeelight::Light - WiFi Smart LED Light
16              
17             =head1 SYNOPSIS
18              
19             This module provides base class for Yeelight smart device
20              
21             =head1 SUBROUTINES/METHODS
22              
23             =head2 new
24              
25             Creates new Yeelight light device.
26              
27             =cut
28              
29             sub new {
30 0     0 1   my $class = shift;
31 0           my $data = {
32             id => undef,
33             location => 'yeelight://',
34             support => [],
35             @_,
36             _socket => undef,
37             _next_command_id => 1,
38             };
39 0           return bless( $data, $class );
40             }
41              
42             sub DESTROY {
43 0     0     my $self = shift;
44 0 0         $self->{_socket}->close if defined $self->{_socket};
45             }
46              
47             =head2 is_supported
48              
49             Checks if method is supported by the device.
50              
51             =cut
52              
53             sub is_supported {
54 0     0 1   my $self = shift;
55 0           my ($method) = @_;
56              
57 0 0         unless ( grep { $method =~ m/::$_$/ } @{ $self->{support} } ) {
  0            
  0            
58 0           carp "$method is not supported by this device";
59             }
60              
61 0           return 1;
62             }
63              
64             sub AUTOLOAD {
65 0     0     my $self = shift;
66 0           our $AUTOLOAD;
67 0 0         return unless $self->is_supported($AUTOLOAD);
68 0           $self->call( $AUTOLOAD, @_ );
69 0           return $self;
70             }
71              
72             sub command_id {
73 0     0 0   my $self = shift;
74 0           $self->{_next_command_id}++;
75             }
76              
77             =head2 connection
78              
79             Create and return socket connected to the device.
80              
81             =cut
82              
83             sub connection {
84 0     0 1   my $self = shift;
85             return $self->{_socket}
86 0 0 0       if defined $self->{_socket} and $self->{_socket}->connected;
87 0           my ( $addr, $port ) = $self->{location} =~ m#yeelight://([^:]*):(\d*)#;
88 0 0         $self->{_socket} = IO::Socket::INET->new(
89             PeerAddr => $addr,
90             PeerPort => $port,
91             ReuseAddr => 1,
92             Proto => 'tcp',
93             ) or croak $!;
94 0           return $self->{_socket};
95             }
96              
97             =head2 call
98              
99             Sends command to device.
100              
101             =cut
102              
103             sub call {
104 0     0 1   my $self = shift;
105 0           my ( $cmd, @params ) = @_;
106 0           my $id = $self->command_id;
107              
108 0           my $package = __PACKAGE__;
109 0           my $json = encode_json(
110             {
111             id => $id,
112             method => $cmd =~ s/^${package}:://r,
113             params => \@params,
114             }
115             );
116 0 0         $self->connection->send("$json\r\n") or croak $!;
117              
118 0           my $buffer;
119 0           while ( not $self->connection->recv( $buffer, 4096 ) ) {
120 0           my $response = decode_json($buffer);
121              
122             # Ignore notification messages
123 0 0         next if not defined $response->{id};
124             carp "Received response to unkown command $response->{id}"
125 0 0         if $response->{id} != $id;
126 0 0         if ( defined $response->{error} ) {
127 0           carp
128             "$response->{error}->{message} (code $response->{error}->{code})";
129 0           return;
130             }
131 0           return @{ $response->{result} };
  0            
132             }
133             }
134              
135             =head1 Yeelight API CALLS
136              
137             =cut
138              
139             use subs
140 1     1   1584 qw/get_prop set_ct_abx set_rgb set_hsv set_bright set_power toggle set_default start_cf stop_cf set_scene cron_add cron_get cron_del set_adjust set_music set_name bg_set_rgb bg_set_hsv bg_set_ct_abx bg_start_cf bg_stop_cf bg_set_scene bg_set_default bg_set_power bg_set_bright bg_set_adjust bg_toggle dev_toggle adjust_bright adjust_ct adjust_color bg_adjust_bright bg_adjust_ct/;
  1         25  
  1         6  
141              
142             =head2 get_prop
143              
144             This method is used to retrieve current property of smart LED.
145              
146             =cut
147              
148             sub get_prop {
149 0     0     my $self = shift;
150 0           my (@properties) = @_;
151 0 0         return unless $self->is_supported( ( caller(0) )[3] );
152 0           my @res = $self->call( 'get_prop', @_ );
153 0           my $props;
154 0           for my $i ( 0 .. $#properties ) {
155 0           $props->{ $properties[$i] } = $res[$i];
156             }
157 0           return $props;
158             }
159              
160             =head2 set_ct_abx
161              
162             This method is used to change the color temperature of a smart LED.
163              
164             =head4 Parameters
165              
166             =over
167              
168             =item ct_value
169              
170             Target color temperature. The type is integer and range is 1700 ~ 6500 (k).
171              
172             =item effect
173              
174             Support two values: I and I. If effect is I, then the
175             color temperature will be changed directly to target value, under this case,
176             the third parameter I is ignored. If effect is I, then the
177             color temperature will be changed to target value in a gradual fashion, under
178             this case, the total time of gradual change is specified in third parameter
179             I.
180              
181             =item duration
182              
183             Specifies the total time of the gradual changing. The unit is milliseconds. The
184             minimum support duration is 30 milliseconds.
185              
186             =back
187              
188             =head2 set_rgb
189              
190             This method is used to change the color of a smart LED.
191              
192             =head4 Parameters
193              
194             =over
195              
196             =item rgb_value
197              
198             Target color, whose type is integer. It should be expressed in decimal integer
199             ranges from 0 to 16777215 (hex: 0xFFFFFF).
200              
201             =item effect
202              
203             Refer to C method.
204              
205             =item duration
206              
207             Refer to C method.
208              
209             =back
210              
211             =head2 set_hsv
212              
213             This method is used to change the color of a smart LED.
214              
215             =head4 Parameters
216              
217             =over
218              
219             =item hue
220              
221             Target hue value, whose type is integer. It should be expressed in decimal
222             integer ranges from 0 to 359.
223              
224             =item sat
225              
226             Target saturation value whose type is integer. It's range is 0 to 100.
227              
228             =item effect
229              
230             Refer to C method.
231              
232             =item duration
233              
234             Refer to C method.
235              
236             =back
237              
238             =head2 set_bright
239              
240             This method is used to change the brightness of a smart LED.
241              
242             =head4 Parameters
243              
244             =over
245              
246             =item brightness
247              
248             Target brightness. The type is integer and ranges from 1 to 100. The brightness
249             is a percentage instead of a absolute value. 100 means maximum brightness while
250             1 means the minimum brightness.
251              
252             =item effect
253              
254             Refer to C method.
255              
256             =item duration
257              
258             Refer to C method.
259              
260             =back
261              
262             =head2 set_power
263              
264             This method is used to switch on or off the smart LED (software
265             managed on/off).
266              
267             =head4 Parameters
268              
269             =over
270              
271             =item power
272              
273             Can only be I or I. I means turn on the smart LED, I means turn
274             off the smart LED.
275              
276             =item effect
277              
278             Refer to C method.
279              
280             =item duration
281              
282             Refer to C method.
283              
284             =item mode
285             (optional parameter)
286              
287             =over
288              
289             =item 0
290             Normal turn on operation (default value)
291              
292             =item 1
293             Turn on and switch to CT mode.
294              
295             =item 2
296             Turn on and switch to RGB mode.
297              
298             =item 3
299             Turn on and switch to HSV mode.
300              
301             =item 4
302             Turn on and switch to color flow mode.
303              
304             =item 5
305             Turn on and switch to Night light mode. (Ceiling light only).
306              
307             =back
308              
309             =back
310              
311             =head2 toggle
312              
313             This method is used to toggle the smart LED.
314              
315             =head2 set_default
316              
317             This method is used to save current state of smart LED in persistent memory. So
318             if user powers off and then powers on the smart LED again (hard power reset),
319             the smart LED will show last saved state.
320              
321             =head2 start_cf
322              
323             This method is used to start a color flow. Color flow is a series of smart LED
324             visible state changing. It can be brightness changing, color changing or color
325             temperature changing. This is the most powerful command.
326              
327             =head4 Parameters
328              
329             =over
330              
331             =item count
332              
333             Total number of visible state changing before color flow stopped. 0 means
334             infinite loop on the state changing.
335              
336             =item action
337              
338             The action taken after the flow is stopped.
339              
340             =over
341              
342             =item 0
343             means smart LED recover to the state before the color flow started.
344              
345             =item 1
346             means smart LED stay at the state when the flow is stopped.
347              
348             =item 2
349             means turn off the smart LED after the flow is stopped.
350              
351             =back
352              
353             =item flow_expression
354              
355             The expression of the state changing series.
356              
357             =over
358              
359             =item Duration
360              
361             Gradual change time or sleep time, in milliseconds, minimum value 50.
362              
363             =item Mode
364              
365             =over
366              
367             =item 1
368             color
369              
370             =item 2
371             color temperature
372              
373             =item 7
374             sleep
375              
376             =back
377              
378             =item Value
379              
380             =over
381              
382             =item
383             RGB value when mode is 1,
384              
385             =item
386             CT value when mode is 2,
387              
388             =item
389             Ignored when mode is 7
390              
391             =back
392              
393             =item Brightness
394              
395             Brightness value, -1 or 1 ~ 100. Ignored when mode is 7. When this value is
396             -1, brightness in this tuple is ignored (only color or CT change takes effec
397              
398             =back
399              
400             =back
401              
402             =head2 stop_cf
403              
404             This method is used to stop a running color flow.
405              
406             =head2 set_scene
407              
408             This method is used to set the smart LED directly to specified state. If the
409             smart LED is off, then it will turn on the smart LED firstly and then apply the
410             specified command.
411              
412             =head4 Parameters
413              
414             =over
415              
416             =item class
417              
418             =over
419              
420             =item I
421             means change the smart LED to specified color and brightness
422              
423             =item I
424             means change the smart LED to specified color and brightness
425              
426             =item I
427             means change the smart LED to specified ct and brightness
428              
429             =item I
430             means start a color flow in specified fashion
431              
432             =item I
433             means turn on the smart LED to specified brightness and start a sleep timer to
434             turn off the light after the specified minutes
435              
436             =back
437              
438             =item val1
439              
440             =item val2
441              
442             =item val3
443              
444             =back
445              
446             =head2 cron_add
447              
448             This method is used to start a timer job on the smart LED.
449              
450             =head4 Parameters
451              
452             =over
453              
454             =item type
455              
456             Currently can only be 0 (means power off).
457              
458             =item value
459              
460             Length of the timer (in minutes).
461              
462             =back
463              
464             =head2 cron_get
465              
466             This method is used to retrieve the setting of the current cron job of the
467             specified type.
468              
469             =head4 Parameters
470              
471             =over
472              
473             =item type
474              
475             The type of the cron job (currently only support 0).
476              
477             =back
478              
479             =head2 cron_del
480              
481             This method is used to stop the specified cron job.
482              
483             =head4 Parameters
484              
485             =over
486              
487             =item type
488              
489             The type of the cron job (currently only support 0).
490              
491             =back
492              
493             =head2 set_adjust
494              
495             This method is used to change brightness, CT or color of a smart LED without
496             knowing the current value, it's main used by controllers.
497              
498             =head4 Parameters
499              
500             =over
501              
502             =item action
503              
504             The direction of the adjustment, the valid value can be:
505              
506             =over
507              
508             =item I
509             increase the specified property
510              
511             =item I
512             decrease the specified property
513              
514             =item I
515             increase the specified property, after it reaches the max value, go back to
516             minimum value
517              
518             =back
519              
520             =item prop
521              
522             The property to adjust. The valid value can be:
523              
524             =over
525              
526             =item I
527             adjust brightness
528              
529             =item I
530             adjust color temperature
531              
532             =item I
533             adjust color.
534              
535             (When C is I, the C can only be I, otherwise, it
536             will be deemed as invalid request.)
537              
538             =back
539              
540             =back
541              
542             =head2 set_music
543              
544             This method is used to start or stop music mode on a device. Under music mode,
545             no property will be reported and no message quota is checked.
546              
547             =head4 Parameters
548              
549             =over
550              
551             =item action
552              
553             The action of C command. The valid value can be:
554              
555             =over
556              
557             =item 0
558             turn off music mode
559              
560             =item 1
561             turn on music mode
562              
563             =back
564              
565             =item host
566              
567             The IP address of the music server.
568              
569             =item port
570              
571             The TCP port music application is listening on.
572              
573             =back
574              
575             =head2 set_name
576              
577             This method is used to name the device. The name will be stored on the device
578             and reported in discovering response. User can also read the name through
579             C method.
580              
581             =head4 Parameters
582              
583             =over
584              
585             =item name
586              
587             The name of the device.
588              
589             =back
590              
591             =head2 adjust_bright
592              
593             This method is used to adjust the brightness by specified percentage within
594             specified duration.
595              
596             =head4 Parameters
597              
598             =over
599              
600             =item percentage
601              
602             The percentage to be adjusted. The range is: -100 ~ 100
603              
604             =item duration
605              
606             Refer to "set_ct_abx" method.
607              
608             =back
609              
610             =head2 adjust_ct
611              
612             This method is used to adjust the color temperature by specified percentage
613             within specified duration.
614              
615             =head4 Parameters
616              
617             =over
618              
619             =item percentage
620              
621             The percentage to be adjusted. The range is: -100 ~ 100
622              
623             =item duration
624              
625             Refer to "set_ct_abx" method.
626              
627             =back
628              
629             =head2 adjust_color
630              
631             This method is used to adjust the color within specified duration.
632              
633             =head4 Parameters
634              
635             =over
636              
637             =item percentage
638              
639             The percentage to be adjusted. The range is: -100 ~ 100
640              
641             =item duration
642              
643             Refer to "set_ct_abx" method.
644              
645             =back
646              
647             =head2 bg_set_xxx / bg_toggle
648              
649             These methods are used to control background light, for each command detail,
650             refer to set_xxx command.
651              
652             Refer to C command.
653              
654             =head2 bg_adjust_xxx
655              
656             This method is used to adjust background light by specified percentage within
657             specified duration.
658              
659             Refer to C, C, C.
660              
661             =head2 dev_toggle
662              
663             This method is used to toggle the main light and background light at the same
664             time.
665              
666             =head1 AUTHOR
667              
668             Jan Baier, C<< >>
669              
670             =head1 SEE ALSO
671              
672             This API is described in the Yeeling WiFi Light Inter-Operation Specification.
673              
674             =head1 LICENSE AND COPYRIGHT
675              
676             Copyright 2019 Jan Baier.
677              
678             This program is free software; you can redistribute it and/or modify it
679             under the terms of either: the GNU General Public License as published
680             by the Free Software Foundation; or the Artistic License.
681              
682             See L for more information.
683              
684             =cut
685              
686             1; # End of Device::Yeelight