File Coverage

blib/lib/Power/Outlet/Kauf.pm
Criterion Covered Total %
statement 10 31 32.2
branch 0 8 0.0
condition n/a
subroutine 4 9 44.4
pod 3 3 100.0
total 17 51 33.3


line stmt bran cond sub pod time code
1             package Power::Outlet::Kauf;
2 1     1   82716 use strict;
  1         2  
  1         50  
3 1     1   6 use warnings;
  1         2  
  1         66  
4 1     1   6 use base qw{Power::Outlet::Common::IP::HTTP::JSON};
  1         15  
  1         595  
5              
6             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 and L
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     1   4 sub _port_default {'80'};
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             sub _name_default {
91 0     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             sub query {
102 0     0 1   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             sub on {
113 0     0 1   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             sub off {
124 0     0 1   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             sub _call {
151 0     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 0         if ($method eq 'GET') { #get status/state
    0          
158 0           my $hash = $self->json_request($method, $url); #isa HASH
159 0 0         die('Error: Method _call failed to return expected JSON format') unless ref($hash) eq 'HASH';
160 0           return $hash;
161             } elsif ($method eq 'POST') { #set state
162 0           my $response = $self->http_client->request($method, $url);
163 0 0         return $response->{"status"} eq '200' ? $state_success : 'Unknown';
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;