TPLink/Kasa.pm

filter: Statementstmt 39.1% 27 / 69 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 77.7% 7 / 9 Podpod 100% 2 / 2 totaltotal 39.5% 36 / 91 CC 6 SCAR 21.7
CC
6
Cov
35%
resetToken
20.0
getDevices
12.0
BEGIN
1.0
CRAP 8.8
« TPLink/SmartHome/Direct.pm ↑ Summary TPLink/SmartHome.pm »
1 package Device::TPLink::Kasa;
2  
3 1 ▶use 5.008003;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use JSON; # The Kasa API is JSON based
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use LWP::JSON::Tiny; # Simplify working with JSON requests
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use UUID::Generator::PurePerl; # Used to generate UUIDs for Kasa if we aren't passed one...
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Device::TPLink::SmartHome::Kasa;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 has 'uuid' => (
13 is => 'rw',
14 isa => 'Str',
15 );
16  
17 1 has 'username' => (
18 is => 'rw',
19 isa => 'Str',
20 );
21  
22 1 has 'password' => (
23 is => 'rw',
24 isa => 'Str',
25 );
26  
27 1 has 'token' => (
28 is => 'rw',
29 isa => 'Str',
30 );
31  
32 =head1 NAME
33  
34 Device::TPLink::Kasa - Interact with TP-Link's Kasa cloud service API
35  
36 =head1 VERSION
37  
38 Version 0.02
39  
40 =cut
41  
42 1 our $VERSION = '0.02';
43  
44  
45 =head1 SYNOPSIS
46  
47 A small collection of helper methods to make working with Device::TPLink::SmartHome::Kasa easier.
48  
49 use Device::TPLink::Kasa;
50  
51 my $kasa = Device::TPLink::Kasa->new(
52 username => 'username', # username for Kasa cloud service
53 password => 'password', # password for Kasa cloud service
54 uuid => 'uuid', # Optional, v4 UUID. Will be auto generated if not set
55 );
56  
57 =head1 SUBROUTINES/METHODS
58  
59 =head2 resetToken
60  
61 Get a new token from the Kasa service
62
63 $kasa->resetToken();
64  
65 my $token = $kasa->token;
66  
67 =cut
68  
69 0 ▶sub resetToken {
Subroutine resetToken: not called CC=4 SCAR=30.0
Pod documented
70 0 my $self = shift;
71  
72 # If we don't already have a UUID set, generate a new one.
73 0 ▶ unless ($self->uuid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->uuid) 0 0 0
74 0 my $ug = UUID::Generator::PurePerl->new();
75 0 my $uuid = $ug->generate_v4();
76 0 $self->uuid($uuid->as_string());
77 }
78  
79 # If the username and/or password are not set, bail out with an error.
80 0 ▶ unless ($self->username && $self->password) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->username and $self->password) 0 0 0
Condition0%0/3
$self->username and $self->password
!ll&&!rl&&r
000
MC/DC0%0/2
$self->username and $self->password
$self->username $self->password
Truth table0%0/3
A and B
A= $self->usernameB= $self->password
ABresult
0X0
100
111
81 0 Carp::croak "Username and/or password not set!";
82 }
83  
84 # We have a username, password, and UUID - everything we need to get a new token
85 0 my $credentials = {
86 appType => 'Kasa_Android',
87 cloudUserName => $self->username,
88 cloudPassword => $self->password,
89 terminalUUID => $self->uuid
90 };
91  
92 0 my $request_object = {
93 method => 'login',
94 params => $credentials
95 };
96  
97 0 my $user_agent = LWP::UserAgent::JSON->new;
98 # Uncomment the next two lines if you need to debug...
99 #$user_agent->add_handler("request_send", sub { shift->dump; return });
100 #$user_agent->add_handler("response_done", sub { shift->dump; return });
101  
102 0 my $request = HTTP::Request::JSON->new(POST => "https://wap.tplinkcloud.com");
103 0 $request->header('Accept' => '*/*'); # Really, really annoying, but required by Kasa service...
104 0 $request->json_content( $request_object );
105  
106 0 my $response = $user_agent->request($request);
107 0 my $response_json_string = $response->content;
108 #print $response_json_string;
109  
110 0 my $json = JSON->new->allow_nonref;
111 0 $json = $json->pretty(1);
112  
113 0 my $response_perl_scalar = $json->decode( $response_json_string );
114 0 $self->token($response_perl_scalar->{result}{token});
115  
116 0 return 1;
117 }
118  
119 =head2 getDevices
120  
121 Returns an array of Device::TPLink::SmartHome::Kasa objects representing all devices associated with this account.
122  
123 use Device::TPLink::SmartHome::Kasa;
124  
125 my @devices = $kasa->getDevices();
126  
127 =cut
128  
129 0 ▶sub getDevices {
Subroutine getDevices: not called CC=3 SCAR=24.8
Pod documented
130 0 my $self = shift;
131  
132 # If we don't have a token, bail out with an error
133 0 ▶ unless ($self->token) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->token) 0 0 0
134 0 Carp::croak "No token found!";
135 }
136  
137 0 my $token = $self->token;
138 0 my $user_agent = LWP::UserAgent::JSON->new;
139 # Uncomment the next two lines if you need to debug...
140 #$user_agent->add_handler("request_send", sub { shift->dump; return });
141 #$user_agent->add_handler("response_done", sub { shift->dump; return });
142  
143 0 my $json = JSON->new->allow_nonref;
144 0 $json = $json->pretty(1);
145 0 my $request_object = { method => "getDeviceList" };
146 0 my $url = "https://wap.tplinkcloud.com?token=$token";
147 0 my $request = HTTP::Request::JSON->new(POST => $url);
148 0 $request->header('Accept' => '*/*');
149 0 $request->json_content( $request_object );
150 0 my $response = $user_agent->request($request);
151 0 my $response_json_string = $response->content;
152 0 my $response_perl_scalar = $json->decode( $response_json_string );
153  
154 0 my @devices = ();
155 0 for my $a (@{$response_perl_scalar->{result}{deviceList}}) {
156 0 my $device = Device::TPLink::SmartHome::Kasa->new($a);
157 0 $device->token($token);
158 0 push @devices, $device;
159 }
160  
161 0 return @devices;
162 }
163  
164 =head1 AUTHOR
165  
166 Verlin Henderson, C<< <verlin at gmail.com> >>
167  
168 =head1 BUGS / SUPPORT
169  
170 To report any bugs or feature requests, please use the github issue tracker: L<https://github.com/verlin/Device-TPLink/issues>
171  
172 =head1 ACKNOWLEDGEMENTS
173  
174  
175 =head1 LICENSE AND COPYRIGHT
176  
177 Copyright 2018 Verlin Henderson.
178  
179 This program is free software; you can redistribute it and/or modify it
180 under the terms of the the Artistic License (2.0). You may obtain a
181 copy of the full license at:
182  
183 L<http://www.perlfoundation.org/artistic_license_2_0>
184  
185 Any use, modification, and distribution of the Standard or Modified
186 Versions is governed by this Artistic License. By using, modifying or
187 distributing the Package, you accept this license. Do not use, modify,
188 or distribute the Package, if you do not accept this license.
189  
190 If your Modified Version has been derived from a Modified Version made
191 by someone other than you, you are nevertheless required to ensure that
192 your Modified Version complies with the requirements of this license.
193  
194 This license does not grant you the right to use any trademark, service
195 mark, tradename, or logo of the Copyright Holder.
196  
197 This license includes the non-exclusive, worldwide, free-of-charge
198 patent license to make, have made, use, offer to sell, sell, import and
199 otherwise transfer the Package with respect to any patent claims
200 licensable by the Copyright Holder that are necessarily infringed by the
201 Package. If you institute patent litigation (including a cross-claim or
202 counterclaim) against any party alleging that the Package constitutes
203 direct or contributory patent infringement, then this Artistic License
204 to you shall terminate on the date that such litigation is filed.
205  
206 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
207 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
208 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
209 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
210 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
211 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
212 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
213 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
214  
215  
216 =cut
217  
218 1 __PACKAGE__->meta->make_immutable;
219  
220 1 1; # End of Device::TPLink::Kasa
« TPLink/SmartHome/Direct.pm ↑ Summary TPLink/SmartHome.pm »