TPLink/SmartHome/Kasa.pm

filter: Statementstmt 40.0% 14 / 35 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 60.0% 3 / 5 Podpod 100% 1 / 1 totaltotal 43.9% 18 / 41 CC 1 SCAR 3.4
CC
1
Cov
40%
passthrough
2.0
__ANON__
2.0
BEGIN
1.0
CRAP 1.4
« TPLink/SmartHome.pm ↑ Summary TPLink.pm »
1 package Device::TPLink::SmartHome::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 LWP::JSON::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 extends 'Device::TPLink::SmartHome';
8  
9 1 has token => (
10 is => 'rw',
11 isa => 'Str',
12 );
13  
14 =head1 NAME
15  
16 Device::TPLink::SmartHome::Kasa - Use Perl to control TP-Link Smart Home devices using the Kasa cloud service
17  
18 =head1 VERSION
19  
20 Version 0.02
21  
22 =cut
23  
24 1 our $VERSION = '0.02';
25  
26  
27 =head1 SYNOPSIS
28  
29 Interacts with TP-Link smart home devices using the Kasa cloud service.
30  
31 =head1 SUBROUTINES/METHODS
32  
33 =head2 passthrough
34  
35 Passes an arbitrary object to the device after converting it to JSON and wrapping it in a Kasa request. Used internally to package commands and send them to the device.
36  
37 $HS100-> = Device::TPLink::SmartHome::Kasa->new();
38  
39 =cut
40  
41 0 ▶sub passthrough {
Subroutine passthrough: not called CC=1 SCAR=6.9
Pod documented
42 0 my $self = shift;
43 0 my ($token, $requestData) = @_;
44  
45 0 my $json = JSON->new->allow_nonref;
46 0 $json = $json->pretty(1);
47  
48 0 my $user_agent = LWP::UserAgent::JSON->new;
49 0 my $params = { deviceId => $self->deviceId, requestData => $json->encode($requestData) };
50 0 my $request_object = {
51 method => 'passthrough', params => $params
52 };
53 0 my $url = $self->appServerUrl . "?token=$token";
54 0 my $request = HTTP::Request::JSON->new(POST => $url);
55 0 $request->header('Accept' => '*/*');
56 0 $request->json_content( $request_object );
57  
58 0 my $response = $user_agent->request($request);
59 0 my $response_json_string = $response->content;
60 0 my $responseData = $json->decode( $response_json_string )->{result}{responseData};
61 0 my $responseDataScalar = $json->decode($responseData);
62 0 return $responseDataScalar;
63 }
64  
65  
66 =head2 function2
67  
68 =cut
69  
70 0 ▶around [qw(on off getSystemInfo reboot)] => sub { # Pass the command to the device
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 my $next = shift;
72 0 my $self = shift;
73 0 my $token = $self->token;
74 0 my $requestData = $next->($self);
75 0 return $self->passthrough($token, $requestData);
76 1 };
77  
78  
79 =head1 AUTHOR
80  
81 Verlin Henderson, C<< <verlin at gmail.com> >>
82  
83 =head1 BUGS / SUPPORT
84  
85 To report any bugs or feature requests, please use the github issue tracker: L<https://github.com/verlin/Device-TPLink/issues>
86  
87 =head1 ACKNOWLEDGEMENTS
88  
89  
90 =head1 LICENSE AND COPYRIGHT
91  
92 Copyright 2018 Verlin Henderson.
93  
94 This program is free software; you can redistribute it and/or modify it
95 under the terms of the the Artistic License (2.0). You may obtain a
96 copy of the full license at:
97  
98 L<http://www.perlfoundation.org/artistic_license_2_0>
99  
100 Any use, modification, and distribution of the Standard or Modified
101 Versions is governed by this Artistic License. By using, modifying or
102 distributing the Package, you accept this license. Do not use, modify,
103 or distribute the Package, if you do not accept this license.
104  
105 If your Modified Version has been derived from a Modified Version made
106 by someone other than you, you are nevertheless required to ensure that
107 your Modified Version complies with the requirements of this license.
108  
109 This license does not grant you the right to use any trademark, service
110 mark, tradename, or logo of the Copyright Holder.
111  
112 This license includes the non-exclusive, worldwide, free-of-charge
113 patent license to make, have made, use, offer to sell, sell, import and
114 otherwise transfer the Package with respect to any patent claims
115 licensable by the Copyright Holder that are necessarily infringed by the
116 Package. If you institute patent litigation (including a cross-claim or
117 counterclaim) against any party alleging that the Package constitutes
118 direct or contributory patent infringement, then this Artistic License
119 to you shall terminate on the date that such litigation is filed.
120  
121 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
122 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
123 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
124 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
125 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
126 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
127 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
128 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
129  
130  
131 =cut
132  
133 1 __PACKAGE__->meta->make_immutable;
134  
135 1 1; # End of Device::TPLink::SmartHome::Kasa
« TPLink/SmartHome.pm ↑ Summary TPLink.pm »