lib/Power/Outlet/TuyaAPI.pm

filter: Statementstmt 22.9% 14 / 61 Branchbran 0.0% 0 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 21.0% 4 / 19 Podpod 100% 8 / 8 totaltotal 20.9% 26 / 124 CC 19 SCAR 24.3
CC
19
Cov
14%
client_id
20.0
client_secret
20.0
deviceid
20.0
CRAP 11.4
« lib/Power/Outlet/Kauf.pm ↑ Summary lib/Power/Outlet/iBoot.pm »
1 package Power::Outlet::TuyaAPI;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base qw{Power::Outlet::Common::IP};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use WebService::Tuya::IoT::API 0.02; #device_information
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.54';
8  
9 =head1 NAME
10  
11 Power::Outlet::TuyaAPI - Control and query an outlet via the TuyaAPI.
12  
13 =head1 SYNOPSIS
14  
15 my $outlet = Power::Outlet::TuyaAPI->new(client_id=>"abc123", client_secret=>"cde234", deviceid=>"def345", switch=>"switch_1");
16 print $outlet->query, "\n";
17 print $outlet->on, "\n";
18 print $outlet->off, "\n";
19  
20 =head1 DESCRIPTION
21  
22 Power::Outlet::TuyaAPI is a package for controlling and querying an outlet via the TuyaAPI.
23  
24 This package is a wrapper around L<WebService::Tuya::IoT::API> please see that documentation for device configuration.
25  
26 =head1 USAGE
27  
28 use Power::Outlet::TuyaAPI;
29 my $relay = Power::Outlet::TuyaAPI->new(client_id=>"abc123", client_secret=>"cde234", deviceid=>"def345", switch=>"switch_1");
30 print $relay->on, "\n";
31  
32 =head1 CONSTRUCTOR
33  
34 =head2 new
35  
36 my $outlet = Power::Outlet->new(type=>"TuyaAPI", client_id=>"abc123", client_secret=>"cde234", deviceid=>"def345", switch=>"switch_1");
37 my $outlet = Power::Outlet::TuyaAPI->new(client_id=>"abc123", client_secret=>"cde234", deviceid=>"def345", switch=>"switch_1");
38  
39 =head1 PROPERTIES
40  
41 =head2 host
42  
43 default: openapi.tuyaus.com
44  
45 =cut
46  
47 0 ▶sub _host_default {undef}; #maps to America data center in WebService::Tuya::IoT::API
Subroutine _host_default: not called CC=1 SCAR=6.9
48 0 ▶sub _port_default {"443"}; #not used but 443 is right
Subroutine _port_default: not called CC=1 SCAR=6.9
49  
50 =head2 client_id
51  
52 The Client ID found on https://iot.tuya.com/ project overview page.
53  
54 =cut
55  
56 0 ▶sub client_id {
Subroutine client_id: not called CC=4 SCAR=30.0
Pod documented
57 0 my $self = shift;
58 0 ▶ $self->{'client_id'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
59 0 ▶ $self->{'client_id'} = $self->_client_id_default unless defined $self->{'client_id'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"client_id"} 0 0 0
60 0 ▶ die('Error: client_id required') unless defined $self->{'client_id'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"client_id"} 0 0 0
61 0 return $self->{'client_id'};
62 }
63  
64 0 ▶sub _client_id_default {undef};
Subroutine _client_id_default: not called CC=1 SCAR=6.9
65  
66 =head2 client_secret
67  
68 The Client Secret found on https://iot.tuya.com/ project overview page.
69  
70 =cut
71  
72 0 ▶sub client_secret {
Subroutine client_secret: not called CC=4 SCAR=30.0
Pod documented
73 0 my $self = shift;
74 0 ▶ $self->{'client_secret'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
75 0 ▶ $self->{'client_secret'} = $self->_client_secret_default unless defined $self->{'client_secret'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"client_secret"} 0 0 0
76 0 ▶ die('Error: client_secret required') unless defined $self->{'client_secret'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"client_secret"} 0 0 0
77 0 return $self->{'client_secret'};
78 }
79  
80 0 ▶sub _client_secret_default {undef};
Subroutine _client_secret_default: not called CC=1 SCAR=6.9
81  
82 =head2 deviceid
83  
84 The Device ID found on https://iot.tuya.com/ project devices page.
85  
86 =cut
87  
88 0 ▶sub deviceid {
Subroutine deviceid: not called CC=4 SCAR=30.0
Pod documented
89 0 my $self = shift;
90 0 ▶ $self->{'deviceid'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
91 0 ▶ $self->{'deviceid'} = $self->_deviceid_default unless defined $self->{'deviceid'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"deviceid"} 0 0 0
92 0 ▶ die('Error: deviceid required') unless defined $self->{'deviceid'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"deviceid"} 0 0 0
93 0 return $self->{'deviceid'};
94 }
95  
96 0 ▶sub _deviceid_default {undef};
Subroutine _deviceid_default: not called CC=1 SCAR=6.9
97  
98 =head2 relay
99  
100 The relay name or "code" for a particular relay on the device. Devices with a single relay this value will most likely be switch_1 but, for devices with multiple relays the first relay is normally switch_1 and subsequent relays should be labeled switch_2, etc.
101  
102 default: switch_1
103  
104 =cut
105  
106 0 ▶sub relay {
Subroutine relay: not called CC=3 SCAR=24.8
Pod documented
107 0 my $self = shift;
108 0 ▶ $self->{'relay'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
109 0 ▶ $self->{'relay'} = $self->_relay_default unless defined $self->{'relay'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"relay"} 0 0 0
110 0 return $self->{'relay'};
111 }
112  
113 0 ▶sub _relay_default {'switch_1'};
Subroutine _relay_default: not called CC=1 SCAR=6.9
114  
115 =head1 METHODS
116  
117 =head2 name
118  
119 Returns the name from the device information API
120  
121 Note: The name is cached for the life of the object.
122  
123 =cut
124  
125 0 ▶sub name {
Subroutine name: not called CC=2 SCAR=17.9
Pod documented
126 0 my $self = shift;
127 0 ▶ unless (exists $self->{'name'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"name"}) 0 0 0
128 0 my $response = $self->_WebService_Tuya_IoT_API->device_information($self->deviceid);
129 0 $self->{'name'} = $response->{'result'}->{'name'};
130 }
131 0 return $self->{'name'};
132 }
133  
134 =head2 query
135  
136 Sends an HTTP message to the API to query the current state of the device relay
137  
138 =cut
139  
140 0 ▶sub query {
Subroutine query: not called CC=2 SCAR=17.9
Pod documented
141 0 my $self = shift;
142 0 my $value = $self->_WebService_Tuya_IoT_API->device_status_code_value($self->deviceid, $self->relay); #isa JSON Boolean
143 0 ▶ return $value ? 'ON' : 'OFF';
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
144 }
145  
146 =head2 on
147  
148 Sends a message to the API to turn the device relay ON
149  
150 =cut
151  
152 0 ▶sub on {
Subroutine on: not called CC=2 SCAR=17.9
Pod documented
153 0 my $self = shift;
154 0 my $state_boolean = \1; #JSON true
155 0 my $response = $self->_WebService_Tuya_IoT_API->device_command_code_value($self->deviceid, $self->relay, $state_boolean);
156 0 ▶ return $response->{'success'} ? 'ON' : '';
Branch: 1 branch
BranchTrueFalseTotal
$response->{'success'} ? : 0 0 0
157 }
158  
159 =head2 off
160  
161 Sends a message to the API to turn the device relay OFF
162  
163 =cut
164  
165 0 ▶sub off {
Subroutine off: not called CC=2 SCAR=17.9
Pod documented
166 0 my $self = shift;
167 0 my $state_boolean = \0; #JSON false
168 0 my $response = $self->_WebService_Tuya_IoT_API->device_command_code_value($self->deviceid, $self->relay, $state_boolean);
169 0 ▶ return $response->{'success'} ? 'OFF' : '';
Branch: 1 branch
BranchTrueFalseTotal
$response->{'success'} ? : 0 0 0
170 }
171  
172 =head2 switch
173  
174 Sends a message to the API to toggle the device relay state
175  
176 =cut
177  
178 #see Power::Outlet::Common->switch
179  
180 =head2 cycle
181  
182 Sends messages to the device to cycle the device relay state
183  
184 =cut
185  
186 #see Power::Outlet::Common->cycle
187 # Note: switch in 10 seconds: $self->_WebService_Tuya_IoT_API->device_commands($self->deviceid, {code=>'countdown_1', value=>$self->cycle_duration});
188  
189 0 ▶sub _WebService_Tuya_IoT_API {
Subroutine _WebService_Tuya_IoT_API: not called CC=4 SCAR=30.0
190 0 my $self = shift;
191 0 ▶ unless ($self->{'_WebService_Tuya_IoT_API'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_WebService_Tuya_IoT_API"}) 0 0 0
192 0 ▶ my $client_id = $self->client_id or die("Error: client_id required");
Branch: 1 branch
BranchTrueFalseTotal
my $client_id = $self->client_id or die "Error: client_id required" 0 0 0
193 0 ▶ my $client_secret = $self->client_secret or die("Error: client_secret required");
Branch: 1 branch
BranchTrueFalseTotal
my $client_secret = $self->client_secret or die "Error: client_secret required" 0 0 0
194 0 $self->{'_WebService_Tuya_IoT_API'} = WebService::Tuya::IoT::API->new(client_id=>$client_id, client_secret=>$client_secret, http_hostname=>$self->host);
195 }
196 0 return $self->{'_WebService_Tuya_IoT_API'};
197 }
198  
199 =head1 BUGS
200  
201 Please log on RT and send an email to the author.
202  
203 =head1 SUPPORT
204  
205 DavisNetworks.com supports all Perl applications including this package.
206  
207 =head1 AUTHOR
208  
209 Michael R. Davis
210 CPAN ID: MRDVT
211 DavisNetworks.com
212  
213 =head1 COPYRIGHT
214  
215 Copyright (c) 2020 Michael R. Davis
216  
217 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
218  
219 The full text of the license can be found in the LICENSE file included with this module.
220  
221 =head1 SEE ALSO
222  
223 L<https://tasmota.github.io/docs/#/Commands>
224  
225 =cut
226  
227 1 1;
« lib/Power/Outlet/Kauf.pm ↑ Summary lib/Power/Outlet/iBoot.pm »