lib/Power/Outlet/Kauf.pm

filter: Statementstmt 36.3% 12 / 33 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 44.4% 4 / 9 Podpod 100% 3 / 3 totaltotal 35.8% 19 / 53 CC 5 SCAR 25.2
CC
5
Cov
29%
_call
30.0
_name_default
2.0
query
2.0
CRAP 12.5
« lib/Power/Outlet/Common/IP/SNMP.pm ↑ Summary lib/Power/Outlet/TuyaAPI.pm »
1 package Power::Outlet::Kauf;
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::HTTP::JSON};
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.54';
7  
8 =head1 NAME
9  
10 Power::Outlet::Kauf - Control and query a Kauf Plug with HTTP REST API
11  
12 =head1 SYNOPSIS
13  
14 my $outlet = Power::Outlet::Kauf->new(host=>"my_kauf_plug");
15 print $outlet->query, "\n";
16 print $outlet->on, "\n";
17 print $outlet->off, "\n";
18 print $outlet->switch, "\n";
19 print $outlet->cycle, "\n";
20  
21 =head1 DESCRIPTION
22  
23 Power::Outlet::Kauf is a package for controlling and querying a Kauf Plug.
24  
25 From: L<https://github.com/KaufHA/common/> and L<https://github.com/hightowe/control_outdoor_lighting-pl>
26  
27 Commands can be executed via web (HTTP) requests, for example:
28  
29 POST http://10.10.10.39/switch/kauf_plug/turn_off => status 200
30 POST http://10.10.10.39/switch/kauf_plug/turn_on => status 200
31 GET http://10.10.10.39/switch/kauf_plug => {"id":"plug name","value":true,"state":"ON"}
32  
33 =head1 USAGE
34  
35 use Power::Outlet::Kauf;
36 my $outlet = Power::Outlet::Kauf->new(host=>"sw-kitchen");
37 print $outlet->on, "\n";
38  
39 Command Line
40  
41 $ power-outlet Kauf ON host sw-kitchen
42  
43 Command Line (from settings)
44  
45 $ cat /etc/power-outlet.ini
46  
47 [Kitchen]
48 type=Kauf
49 name=Kitchen
50 host=sw-kitchen
51 groups=Inside Lights
52 groups=Main Floor
53  
54 $ power-outlet Config ON section Kitchen
55 $ curl http://127.0.0.1/cgi-bin/power-outlet-json.cgi?name=Kitchen;action=ON
56  
57 =head1 CONSTRUCTOR
58  
59 =head2 new
60  
61 my $outlet = Power::Outlet->new(type=>"Kauf", host=>"my_kauf_plug");
62 my $outlet = Power::Outlet::Kauf->new(host=>"my_kauf_plug");
63  
64 =head1 PROPERTIES
65  
66 =head2 host
67  
68 Sets and returns the hostname or IP address.
69  
70 =head2 port
71  
72 Sets and returns the port number.
73  
74 Default: 80
75  
76 =cut
77  
78 1 ▶sub _port_default {'80'};
Subroutine _port_default: called CC=1 SCAR=0.0
79  
80 #override Power::Outlet::Common::IP->_port_default
81  
82 =head1 METHODS
83  
84 =head2 name
85  
86 =cut
87  
88 #from Power::Outlet::Common
89  
90 0 ▶sub _name_default {
Subroutine _name_default: not called CC=1 SCAR=6.9
91 0 my $self = shift;
92 0 return $self->_call(GET => '/switch/kauf_plug')->{'id'};
93 }
94  
95 =head2 query
96  
97 Sends an HTTP message to the device to query the current state
98  
99 =cut
100  
101 0 ▶sub query {
Subroutine query: not called CC=1 SCAR=6.9
Pod documented
102 0 my $self = shift;
103 0 return $self->_call(GET => '/switch/kauf_plug')->{'state'}; #ON|OFF
104 }
105  
106 =head2 on
107  
108 Sends a message to the device to Turn Power ON
109  
110 =cut
111  
112 0 ▶sub on {
Subroutine on: not called CC=1 SCAR=6.9
Pod documented
113 0 my $self = shift;
114 0 return $self->_call(POST => '/switch/kauf_plug/turn_on', 'ON');
115 }
116  
117 =head2 off
118  
119 Sends a message to the device to Turn Power OFF
120  
121 =cut
122  
123 0 ▶sub off {
Subroutine off: not called CC=1 SCAR=6.9
Pod documented
124 0 my $self = shift;
125 0 return $self->_call(POST => '/switch/kauf_plug/turn_off', 'OFF');
126 }
127  
128 =head2 switch
129  
130 Sends a message to the device to toggle the power
131  
132 =cut
133  
134 #from Power::Outlet::Common->switch
135  
136 =head2 cycle
137  
138 Sends messages to the device to Cycle Power (ON-OFF-ON or OFF-ON-OFF).
139  
140 =cut
141  
142 #from Power::Outlet::Common->cycle
143  
144 =head2 cycle_duration
145  
146 Default; 10 seconds (floating point number)
147  
148 =cut
149  
150 0 ▶sub _call {
Subroutine _call: not called CC=5 SCAR=34.0
151 0 my $self = shift;
152 0 my $method = shift; #POST returns ON|OFF, GET returns {}
153 0 my $path = shift; #e.g., /switch/kauf_plug/turn_on
154 0 my $state_success = shift; #ON|OFF - Only used for GET requests
155 0 $self->http_path($path); #from Power::Outlet::Common::IP::HTTP
156 0 my $url = $self->url(undef); #isa URI from Power::Outlet::Common::IP::HTTP
157 0 ▶ if ($method eq 'GET') { #get status/state
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'GET') { } 0 0 0
elsif ($method eq 'POST') { } 0 0 0
158 0 my $hash = $self->json_request($method, $url); #isa HASH
159 0 ▶ die('Error: Method _call failed to return expected JSON format') unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 0 0
160 0 return $hash;
161 } elsif ($method eq 'POST') { #set state
162 0 my $response = $self->http_client->request($method, $url);
163 0 ▶ return $response->{"status"} eq '200' ? $state_success : 'Unknown';
Branch: 1 branch
BranchTrueFalseTotal
$response->{'status'} eq '200' ? : 0 0 0
164 } else {
165 0 die("Error: Invalid HTTP Method. Expected GET or POST");
166 }
167 }
168  
169 =head1 COPYRIGHT & LICENSE
170  
171 Copyright (c) 2025 Michael R. Davis
172  
173 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
174  
175 The full text of the license can be found in the LICENSE file included with this module.
176  
177 =head1 SEE ALSO
178  
179 =cut
180  
181 1 1;
« lib/Power/Outlet/Common/IP/SNMP.pm ↑ Summary lib/Power/Outlet/TuyaAPI.pm »