lib/Power/Outlet/Hue.pm

filter: Statementstmt 18.1% 14 / 77 Branchbran 0.0% 0 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 22.2% 4 / 18 Podpod 100% 6 / 6 totaltotal 15.8% 24 / 151 CC 26 SCAR 38.8
CC
26
Cov
11%
_call
110.0
query
90.0
id
12.0
CRAP 48.2
« lib/Power/Outlet/MQTT.pm ↑ Summary lib/Power/Outlet/Common/IP/HTTP/JSON.pm »
1 package Power::Outlet::Hue;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Data::Dumper qw{Dumper};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use base qw{Power::Outlet::Common::IP::HTTP::JSON};
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.54';
8  
9 =head1 NAME
10  
11 Power::Outlet::Hue - Control and query a Philips Hue light
12  
13 =head1 SYNOPSIS
14  
15 my $outlet=Power::Outlet::Hue->new(host => "mybridge", id=>1, username=>"myuser");
16 print $outlet->query, "\n";
17 print $outlet->on, "\n";
18 print $outlet->off, "\n";
19  
20 =head1 DESCRIPTION
21  
22 Power::Outlet::Hue is a package for controlling and querying a light on a Philips Hue network attached bridge.
23  
24 =head1 USAGE
25  
26 use Power::Outlet::Hue;
27 my $lamp=Power::Outlet::Hue->new(host=>"mybridge", id=>1, username=>"myuser");
28 print $lamp->on, "\n";
29  
30 =head1 CONSTRUCTOR
31  
32 =head2 new
33  
34 my $outlet=Power::Outlet->new(type=>"Hue", host=>"mybridge", id=>1);
35 my $outlet=Power::Outlet::Hue->new(host=>"mybridge", id=>1);
36  
37 =head1 PROPERTIES
38  
39 =head2 id
40  
41 ID for the particular light as configured in the Philips Hue Bridge
42  
43 Default: 1
44  
45 =cut
46  
47 0 ▶sub id {
Subroutine id: not called CC=3 SCAR=24.8
Pod documented
48 0 my $self = shift;
49 0 ▶ $self->{"id"} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
50 0 ▶ $self->{"id"} = $self->_id_default unless defined $self->{"id"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"id"} 0 0 0
51 0 return $self->{"id"};
52 }
53  
54 0 ▶sub _id_default {1};
Subroutine _id_default: not called CC=1 SCAR=6.9
55  
56 =head2 resource
57  
58 Resource for the particular object as presented on the Philips Hue Bridge
59  
60 Default: lights
61  
62 Currently supported Resources from L<https://developers.meethue.com/documentation/core-concepts>
63  
64 lights - resource which contains all the light resources
65 groups - resource which contains all the groups
66 config - resource which contains all the configuration items
67 schedules - which contains all the schedules
68 scenes - which contains all the scenes
69 sensors - which contains all the sensors
70 rules - which contains all the rules
71  
72 =cut
73  
74 0 ▶sub resource {
Subroutine resource: not called CC=3 SCAR=24.8
Pod documented
75 0 my $self = shift;
76 0 ▶ $self->{"resource"} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
77 0 ▶ $self->{"resource"} = $self->_resource_default unless defined $self->{"resource"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"resource"} 0 0 0
78 0 return $self->{"resource"};
79 }
80  
81 0 ▶sub _resource_default {'lights'};
Subroutine _resource_default: not called CC=1 SCAR=6.9
82  
83 =head2 host
84  
85 Sets and returns the hostname or IP address.
86  
87 Default: mybridge
88  
89 =cut
90  
91 0 ▶sub _host_default {"mybridge"};
Subroutine _host_default: not called CC=1 SCAR=6.9
92  
93 =head2 port
94  
95 Sets and returns the port number.
96  
97 Default: 80
98  
99 =cut
100  
101 0 ▶sub _port_default {"80"};
Subroutine _port_default: not called CC=1 SCAR=6.9
102  
103 =head2 username
104  
105 Sets and returns the username used for authentication with the Hue Bridge
106  
107 Default: newdeveloper (Hue Emulator default)
108  
109 =cut
110  
111 0 ▶sub username {
Subroutine username: not called CC=3 SCAR=24.8
Pod documented
112 0 my $self = shift;
113 0 ▶ $self->{"username"} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
114 0 ▶ $self->{"username"} = $self->_username_default unless defined $self->{"username"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"username"} 0 0 0
115 0 return $self->{"username"};
116 }
117  
118 0 ▶sub _username_default {"newdeveloper"};
Subroutine _username_default: not called CC=1 SCAR=6.9
119  
120 =head2 name
121  
122 Returns the configured friendly name for the device
123  
124 =cut
125  
126 0 ▶sub _name_default { #overloaded _name_default so the name will be cached for the life of this object
Subroutine _name_default: not called CC=1 SCAR=6.9
127 0 my $self = shift;
128 0 my $url = $self->url; #isa URI from Power::Outlet::Common::IP::HTTP
129 0 $url->path($self->_path);
130 0 my $res = $self->json_request(GET => $url); #isa perl structure
131 0 return $res->{"name"}; #isa string
132 }
133  
134 =head1 METHODS
135  
136 =cut
137  
138 #head2 _path
139  
140 #Builds the URL path
141  
142 #cut
143  
144 0 ▶sub _path {
Subroutine _path: not called CC=3 SCAR=24.8
145 0 my $self = shift;
146 0 my $state = shift;
147 0 ▶ my @state = defined($state) ? ($state) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $state ? : 0 0 0
148 0 ▶ my @resource = defined($self->resource) ? ($self->resource) : (); #support undef resource just in case needed
Branch: 1 branch
BranchTrueFalseTotal
defined $self->resource ? : 0 0 0
149 0 return join('/', '', 'api', $self->username, @resource, $self->id, @state);
150 }
151  
152 =head2 query
153  
154 Sends an HTTP message to the device to query the current state
155  
156 =cut
157  
158 #Response: {"identifier":null,"state":{"on":true,"bri":254,"hue":4444,"sat":254,"xy":[0.0,0.0],"ct":0,"alert":"none","effect":"none","colormode":"hs","reachable":true,"transitionTime":null},"type":"Extended color light","name":"Hue Lamp 1","modelid":"LCT001","swversion":"65003148","pointsymbol":{"1":"none","2":"none","3":"none","4":"none","5":"none","6":"none","7":"none","8":"none"}}
159 #Response: [{"error":{"address":"/","description":"unauthorized user","type":"1"}}]
160 #Response: [{"error":{"address":"/lights/333","description":"resource, /lights/333, not available","type":"3"}}]
161  
162  
163 0 ▶sub query {
Subroutine query: not called CC=9 SCAR=45.0
Pod documented
164 0 my $self = shift;
165 0 ▶ if (defined wantarray) { #scalar and list context
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 0 0 0
166  
167 #url configuration
168 0 my $url = $self->url; #isa URI from Power::Outlet::Common::IP::HTTP
169 0 $url->path($self->_path);
170  
171 #web request
172 0 my $res = $self->json_request(GET => $url); #isa perl structure
173  
174 #Response is an ARRAY on error and a HASH on success
175 0 ▶ if (ref($res) eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $res eq 'HASH') { } 0 0 0
elsif (ref $res eq 'ARRAY') { } 0 0 0
176 0 ▶ die("Error: (query) state does not exists") unless exists $res->{"state"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $res->{"state"} 0 0 0
177 0 ▶ die("Error: (query) state is not a hash") unless ref($res->{"state"}) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res->{"state"} eq "HASH" 0 0 0
178 0 ▶ die("Error: (query) state does not provide on property") unless exists $res->{"state"}->{"on"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $res->{"state"}{"on"} 0 0 0
179 0 my $state = $res->{"state"}->{"on"}; #isa boolean true/false
180 0 ▶ return $state ? "ON" : "OFF";
Branch: 1 branch
BranchTrueFalseTotal
$state ? : 0 0 0
181 } elsif (ref($res) eq "ARRAY") {
182 0 my $hash = shift(@$res);
183 0 ▶ die(sprintf(qq{Error: (query) "%s"}, $hash->{"error"}->{"description"})) if exists $hash->{"error"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{"error"} 0 0 0
184 0 die(sprintf("Error: (query) Unkown Error: URL: %s\n\n%s", $url, Dumper($res)));
185 } else {
186 0 die(sprintf("Error: (query) Unkown Error: URL: %s\n\n%s", $url, Dumper($res)));
187 }
188 } else { #void context
189 0 return;
190 }
191 }
192  
193 =head2 on
194  
195 Sends a message to the device to Turn Power ON
196  
197 =cut
198  
199 #Response: [{"success":{"/lights/1/state/on":true}}]
200 #Response: [{"error":{"address":"/","description":"unauthorized user","type":"1"}}]
201 #Response: [{"error":{"address":"/lights/333","description":"resource, /lights/333, not available","type":"3"}}]
202  
203 0 ▶sub on {
Subroutine on: not called CC=1 SCAR=6.9
Pod documented
204 0 my $self = shift;
205 0 return $self->_call("on");
206 }
207  
208 =head2 off
209  
210 Sends a message to the device to Turn Power OFF
211  
212 =cut
213  
214 0 ▶sub off {
Subroutine off: not called CC=1 SCAR=6.9
Pod documented
215 0 my $self = shift;
216 0 return $self->_call("off");
217 }
218  
219 0 ▶sub _call {
Subroutine _call: not called CC=10 SCAR=47.0
220 0 my $self = shift;
221 0 ▶ my $input = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $input = shift() or die 0 0 0
222 0 ▶ my $boolean = $input eq "on" ? \1 : #JSON true
Branch: 2 branches
BranchTrueFalseTotal
$input eq 'on' ? : 0 0 0
$input eq 'off' ? : 0 0 0
223 $input eq "off" ? \0 : #JSON false
224 die("Error: (_call) syntax _call('on'||'off')");
225  
226 #url configuration
227 0 my $url = $self->url; #isa URI from Power::Outlet::Common::IP::HTTP
228 0 $url->path($self->_path('state'));
229  
230 #web request
231 0 my $array = $self->json_request(PUT => $url, {on=>$boolean}); #isa perl structure
232  
233 #error handling
234 0 ▶ die("Error: ($input) failed to return expected JSON format") unless ref($array) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $array eq "ARRAY" 0 0 0
235 0 my $hash = shift(@$array);
236 0 ▶ die("Error: ($input) Failed to return expected JSON format") unless ref($hash) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 0 0
237 0 ▶ die(sprintf(qq{Error: ($input) "%s"}, $hash->{"error"}->{"description"})) if exists $hash->{"error"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{"error"} 0 0 0
238 0 ▶ die(sprintf("Error: ($input) Unkown Error: URL: %s\n\n%s", $url, Dumper($array))) unless exists $hash->{"success"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{"success"} 0 0 0
239 0 my $success = $hash->{"success"};
240 #state normalization
241 0 my $key = sprintf("/lights/%s/state/on", $self->id);
242 0 ▶ die("Error: ($input) Unkown success state") unless exists $success->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $success->{$key} 0 0 0
243 0 my $state = $success->{$key};
244 0 ▶ return $state ? "ON" : "OFF";
Branch: 1 branch
BranchTrueFalseTotal
$state ? : 0 0 0
245 }
246  
247 =head2 switch
248  
249 Queries the device for the current status and then requests the opposite.
250  
251 =cut
252  
253 #see Power::Outlet::Common->switch
254  
255 =head2 cycle
256  
257 Sends messages to the device to Cycle Power (ON-OFF-ON or OFF-ON-OFF).
258  
259 =cut
260  
261 #see Power::Outlet::Common->cycle
262  
263 =head1 BUGS
264  
265 Please log on RT and send an email to the author.
266  
267 =head1 SUPPORT
268  
269 DavisNetworks.com supports all Perl applications including this package.
270  
271 =head1 AUTHOR
272  
273 Michael R. Davis
274 CPAN ID: MRDVT
275 DavisNetworks.com
276  
277 Thanks to Mathias Neerup manee12 at student.sdu.dk - L<https://rt.cpan.org/Ticket/Display.html?id=123965>
278  
279 =head1 COPYRIGHT
280  
281 Copyright (c) 2018 Michael R. Davis
282  
283 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
284  
285 The full text of the license can be found in the LICENSE file included with this module.
286  
287 =head1 SEE ALSO
288  
289 L<http://www.developers.meethue.com/philips-hue-api>, L<http://steveyo.github.io/Hue-Emulator/>, L<https://home-assistant.io/components/emulated_hue/>
290  
291 =cut
292  
293 2 1;
« lib/Power/Outlet/MQTT.pm ↑ Summary lib/Power/Outlet/Common/IP/HTTP/JSON.pm »