File Coverage

blib/lib/Power/Outlet/Config.pm
Criterion Covered Total %
statement 30 32 93.7
branch 4 8 50.0
condition n/a
subroutine 7 8 87.5
pod 4 4 100.0
total 45 52 86.5


line stmt bran cond sub pod time code
1             package Power::Outlet::Config;
2 1     1   108007 use strict;
  1         14  
  1         29  
3 1     1   4 use warnings;
  1         2  
  1         30  
4 1     1   5 use base qw{Package::New Package::Role::ini};
  1         5  
  1         496  
5 1     1   56286 use Power::Outlet;
  1         3  
  1         391  
6              
7             our $VERSION = '0.47';
8             our $PACKAGE = __PACKAGE__;
9              
10             =head1 NAME
11              
12             Power::Outlet::Config - Control and query a Power::Outlet device from Configuration file
13              
14             =head1 SYNOPSIS
15              
16             my $outlet = Power::Outlet::Config->new(section=>"My Section");
17             print $outlet->query, "\n";
18             print $outlet->on, "\n";
19             print $outlet->off, "\n";
20              
21             =head1 DESCRIPTION
22              
23             Power::Outlet::Config is a package for controlling and querying Power::Outlet devices registered in an INI file.
24              
25             =head1 USAGE
26              
27             Configuration
28              
29             /etc/power-outliet.ini
30             [My Tasmota]
31             type=Tasmota
32             host=light-hostname
33             relay=POWER
34              
35             [My SonoffDiy]
36             type=SonoffDiy
37             host=switch=hostname
38              
39             Script
40              
41             use Power::Outlet::Config;
42             my $outlet = Power::Outlet::Config->new(section=>"My Section");
43             print $outlet->on, "\n";
44              
45             Command Line
46              
47             /usr/bin/power-outlet Config ON section "My Tasmota"
48             /usr/bin/power-outlet Config ON section "My Section" ini_file ./my.ini
49              
50             =head1 CONSTRUCTOR
51              
52             =head2 new
53              
54             my $outlet = Power::Outlet->new(type=>"Config", section=>"My Section");
55             my $outlet = Power::Outlet::Config->new(section=>"My Section");
56              
57             =cut
58              
59             sub new {
60 1     1 1 128 my $class = shift;
61 1         12 my $self = $class->SUPER::new(@_); #isa Power::Outlet::Config
62 1 50       33 die(sprintf(qq{Error: Package: $PACKAGE: Cannot Read Config File "%s".\n}, $self->ini_file)) unless -r $self->ini_file;
63 1         41 my $hash = $self->hash; #isa HASH
64 1         7 return Power::Outlet->new(%$hash); #isa Power::Outlet::XXX
65             }
66              
67             =head1 PROPERTIES
68              
69             =head2 section
70              
71             =cut
72              
73             sub section {
74 1     1 1 2 my $self = shift;
75 1 50       4 $self->{'section'} = shift if @_;
76             die(qq{Error: Package: $PACKAGE: Object property "section" required.\n})
77 1 50       4 unless $self->{'section'};
78 0         0 die(sprintf(qq{Error: Package: $PACKAGE: Section "%s" does not exist in file "%s". Expected one of %s.\n}, $self->{'section'}, $self->ini_file, join(", ", map {qq{"$_"}} $self->ini->Sections)))
79 1 50       7 unless $self->ini->SectionExists($self->{'section'});
80 1         2044 return $self->{'section'};
81             }
82              
83             =head2 hash
84              
85             =cut
86              
87             sub hash {
88 1     1 1 3 my $self = shift;
89 1         3 my %hash = ();
90 1         3 my $section = $self->section;
91 1         4 my @parameters = $self->ini->Parameters($section);
92 1         61 foreach my $parameter (@parameters) {
93 3         20 my $value = $self->ini->val($section, $parameter, '');
94 3         92 $hash{$parameter} = $value;
95             }
96 1         4 return \%hash;
97             }
98              
99             =head1 OBJECT ACCESSORS
100              
101             =head2 ini
102              
103             Returns a L for the power-outlet.ini file.
104              
105             =head2 ini_file
106              
107             Default: /etc/power-outlet.ini or C:\Windows\power-outlet.ini
108              
109             =cut
110              
111             =head2 ini_file_default
112              
113             Default: power-outlet.ini
114              
115             =cut
116              
117 0     0 1   sub ini_file_default {"power-outlet.ini"};
118              
119             =head1 BUGS
120              
121             Please log on RT and send an email to the author.
122              
123             =head1 SUPPORT
124              
125             DavisNetworks.com supports all Perl applications including this package.
126              
127             =head1 AUTHOR
128              
129             Michael R. Davis
130             CPAN ID: MRDVT
131             DavisNetworks.com
132              
133             =head1 COPYRIGHT
134              
135             Copyright (c) 2020 Michael R. Davis
136              
137             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
138              
139             The full text of the license can be found in the LICENSE file included with this module.
140              
141             =head1 SEE ALSO
142              
143             =cut
144              
145             1;