File Coverage

blib/lib/Power/Outlet/TuyaAPI.pm
Criterion Covered Total %
statement 12 59 20.3
branch 0 36 0.0
condition n/a
subroutine 4 19 21.0
pod 8 8 100.0
total 24 122 19.6


line stmt bran cond sub pod time code
1             package Power::Outlet::TuyaAPI;
2 1     1   825 use strict;
  1         2  
  1         24  
3 1     1   5 use warnings;
  1         1  
  1         22  
4 1     1   5 use base qw{Power::Outlet::Common::IP};
  1         1  
  1         353  
5 1     1   425 use WebService::Tuya::IoT::API 0.02; #device_information
  1         52950  
  1         571  
6              
7             our $VERSION = '0.48';
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 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     0     sub _host_default {undef}; #maps to America data center in WebService::Tuya::IoT::API
48 0     0     sub _port_default {"443"}; #not used but 443 is right
49              
50             =head2 client_id
51              
52             The Client ID found on https://iot.tuya.com/ project overview page.
53              
54             =cut
55              
56             sub client_id {
57 0     0 1   my $self = shift;
58 0 0         $self->{'client_id'} = shift if @_;
59 0 0         $self->{'client_id'} = $self->_client_id_default unless defined $self->{'client_id'};
60 0 0         die('Error: client_id required') unless defined $self->{'client_id'};
61 0           return $self->{'client_id'};
62             }
63              
64 0     0     sub _client_id_default {undef};
65              
66             =head2 client_secret
67              
68             The Client Secret found on https://iot.tuya.com/ project overview page.
69              
70             =cut
71              
72             sub client_secret {
73 0     0 1   my $self = shift;
74 0 0         $self->{'client_secret'} = shift if @_;
75 0 0         $self->{'client_secret'} = $self->_client_secret_default unless defined $self->{'client_secret'};
76 0 0         die('Error: client_secret required') unless defined $self->{'client_secret'};
77 0           return $self->{'client_secret'};
78             }
79              
80 0     0     sub _client_secret_default {undef};
81              
82             =head2 deviceid
83              
84             The Device ID found on https://iot.tuya.com/ project devices page.
85              
86             =cut
87              
88             sub deviceid {
89 0     0 1   my $self = shift;
90 0 0         $self->{'deviceid'} = shift if @_;
91 0 0         $self->{'deviceid'} = $self->_deviceid_default unless defined $self->{'deviceid'};
92 0 0         die('Error: deviceid required') unless defined $self->{'deviceid'};
93 0           return $self->{'deviceid'};
94             }
95              
96 0     0     sub _deviceid_default {undef};
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             sub relay {
107 0     0 1   my $self = shift;
108 0 0         $self->{'relay'} = shift if @_;
109 0 0         $self->{'relay'} = $self->_relay_default unless defined $self->{'relay'};
110 0           return $self->{'relay'};
111             }
112              
113 0     0     sub _relay_default {'switch_1'};
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             sub name {
126 0     0 1   my $self = shift;
127 0 0         unless (exists $self->{'name'}) {
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             sub query {
141 0     0 1   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 0         return $value ? 'ON' : 'OFF';
144             }
145              
146             =head2 on
147              
148             Sends a message to the API to turn the device relay ON
149              
150             =cut
151              
152             sub on {
153 0     0 1   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 0         return $response->{'success'} ? 'ON' : '';
157             }
158              
159             =head2 off
160              
161             Sends a message to the API to turn the device relay OFF
162              
163             =cut
164              
165             sub off {
166 0     0 1   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 0         return $response->{'success'} ? 'OFF' : '';
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             sub _WebService_Tuya_IoT_API {
190 0     0     my $self = shift;
191 0 0         unless ($self->{'_WebService_Tuya_IoT_API'}) {
192 0 0         my $client_id = $self->client_id or die("Error: client_id required");
193 0 0         my $client_secret = $self->client_secret or die("Error: client_secret required");
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
224              
225             =cut
226              
227             1;