File Coverage

blib/lib/Power/Outlet.pm
Criterion Covered Total %
statement 38 38 100.0
branch 2 4 50.0
condition 1 3 33.3
subroutine 10 10 100.0
pod 1 1 100.0
total 52 56 92.8


line stmt bran cond sub pod time code
1             package Power::Outlet;
2 5     5   223728 use strict;
  5         26  
  5         168  
3 5     5   33 use warnings;
  5         11  
  5         1009  
4              
5             our $VERSION = '0.47';
6              
7             =head1 NAME
8              
9             Power::Outlet - Control and query network attached power outlets
10              
11             =head1 SYNOPSIS
12              
13             Command Line
14              
15             power-outlet Config ON section "My Section"
16             power-outlet iBoot ON host mylamp
17             power-outlet Hue ON host mybridge id 1 username myuser
18             power-outlet Shelly ON host myshelly
19             power-outlet SonoffDiy ON host mysonoff
20             power-outlet Tasmota ON host mytasmota
21             power-outlet WeMo ON host mywemo
22              
23             Perl Object API
24              
25             my $outlet=Power::Outlet->new( #sane defaults from manufactures spec
26             type => "iBoot",
27             host => "mylamp",
28             );
29             print $outlet->query, "\n";
30             print $outlet->on, "\n";
31             print $outlet->off, "\n";
32              
33             =head1 DESCRIPTION
34              
35             Power::Outlet is a package for controlling and querying network attached power outlets. Individual hardware drivers in this name space must provide a common object interface for the controlling and querying of an outlet. Common methods that every network attached power outlet must know are on, off, query, switch and cycle. Optional methods might be implemented in some drivers like amps and volts.
36              
37             =head2 SCOPE
38              
39             The current scope of these packages is network attached power outlets. I started with iBoot and iBootBar since I had the hardware. Hardware configuration is beyond the scope of this group of packages as most power outlets have functional web based or command line configuration tools.
40              
41             =head2 Home Assistant
42              
43             Integration with Home Assistant L can be accomplished by configuring a Command Line Switch.
44              
45             switch:
46             - platform: command_line
47             switches:
48             ibootbar_1:
49             command_on: /usr/bin/power-outlet iBootBar ON host mybar.local outlet 1
50             command_off: /usr/bin/power-outlet iBootBar OFF host mybar.local outlet 1
51             command_state: /usr/bin/power-outlet iBootBar QUERY host mybar.local outlet 1 | /bin/grep -q ON
52             friendly_name: My iBootBar Outlet 1
53              
54             See L
55              
56             =head2 Node Red
57              
58             Integration with Node Red L can be accomplished with the included JSON web API power-outlet-json.cgi. The power-outlet-json.cgi script is a layer on top of L where the "name" parameter maps to the section in the /etc/power-outlet.ini INI file.
59              
60             To access all of these devices use an http request node with a URL https://127.0.0.1/cgi-bin/power-outlet-json.cgi?name={{topic}};action={{payload}} then simply set the topic to the INI section and the action to either ON or OFF.
61              
62             =head1 USAGE
63              
64             The Perl one liner
65              
66             perl -MPower::Outlet -e 'print Power::Outlet->new(type=>"Tasmota", host=>shift)->switch, "\n"' myhost
67              
68             The included command line script
69              
70             power-outlet Shelly ON host myshelly
71              
72             =head1 CONSTRUCTOR
73              
74             =head2 new
75              
76             my $outlet = Power::Outlet->new(type=>"WeMo", host=>"mywemo");
77              
78             =cut
79              
80             sub new {
81 13     13 1 10886 my $this = shift;
82 13   33     77 my $base = ref($this) || $this;
83 13         49 my %data = @_;
84 13 50       41 my $type = $data{"type"} or die("Error: the type parameter is required.");
85 13         38 my $class = join("::", $base, $type);
86 13         28 local $@;
87 5     5   523 eval "use $class";
  5     3   16  
  5     1   119  
  3     1   22  
  3     1   7  
  3     1   49  
  1     1   6  
  1         3  
  1         27  
  1         7  
  1         2  
  1         28  
  1         20  
  1         2  
  1         20  
  1         8  
  1         3  
  1         21  
  1         7  
  1         2  
  1         26  
  13         933  
88 13         46 my $error = $@;
89 13 50       46 die(qq{Errot: Cannot find package "$class" for outlet type "$type"\n}) if $error;
90 13         89 my $outlet = $class->new(%data);
91 13         308 return $outlet;
92             }
93              
94             =head1 BUGS
95              
96             Please open an issue on github
97              
98             =head1 SUPPORT
99              
100             DavisNetworks.com supports all Perl applications including this package.
101              
102             =head1 AUTHOR
103              
104             Michael R. Davis
105             CPAN ID: MRDVT
106             DavisNetworks.com
107              
108             =head1 COPYRIGHT
109              
110             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
111              
112             The full text of the license can be found in the LICENSE file included with this module.
113              
114             =head1 SEE ALSO
115              
116             L, L, L, L
117              
118             =cut
119              
120             1;