lib/Power/Outlet/Config.pm

filter: Statementstmt 94.2% 33 / 35 Branchbran 50.0% 4 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 7 / 8 Podpod 100% 4 / 4 totaltotal 87.2% 48 / 55 CC 6 SCAR 9.1
CC
6
Cov
86%
section
4.6
new
2.0
hash
2.0
CRAP 2.5
« lib/Power/Outlet/Common.pm ↑ Summary lib/Power/Outlet/Virtual.pm »
1 package Power::Outlet::Config;
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{Package::New Package::Role::ini};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Power::Outlet;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.54';
8 1 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 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
60 1 my $class = shift;
61 1 my $self = $class->SUPER::new(@_); #isa Power::Outlet::Config
62 1 ▶ die(sprintf(qq{Error: Package: $PACKAGE: Cannot Read Config File "%s".\n}, $self->ini_file)) unless -r $self->ini_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $self->ini_file 0 1 1
63 1 my $hash = $self->hash; #isa HASH
64 1 return Power::Outlet->new(%$hash); #isa Power::Outlet::XXX
65 }
66  
67 =head1 PROPERTIES
68  
69 =head2 section
70  
71 =cut
72  
73 1 ▶sub section {
Subroutine section: called CC=4 SCAR=15.2
Pod documented
74 1 my $self = shift;
75 1 ▶ $self->{'section'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
76 die(qq{Error: Package: $PACKAGE: Object property "section" required.\n})
77 1 ▶ unless $self->{'section'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"section"} 0 1 1
78 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 ▶ unless $self->ini->SectionExists($self->{'section'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ini->SectionExists($self->{"section"}) 0 1 1
80 1 return $self->{'section'};
81 }
82  
83 =head2 hash
84  
85 =cut
86  
87 1 ▶sub hash {
Subroutine hash: called CC=2 SCAR=6.9
Pod documented
88 1 my $self = shift;
89 1 my %hash = ();
90 1 my $section = $self->section;
91 1 my @parameters = $self->ini->Parameters($section);
92 1 foreach my $parameter (@parameters) {
93 3 my $value = $self->ini->val($section, $parameter, '');
94 3 $hash{$parameter} = $value;
95 }
96 1 return \%hash;
97 }
98  
99 =head1 OBJECT ACCESSORS
100  
101 =head2 ini
102  
103 Returns a L<Config::IniFiles> 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 ▶sub ini_file_default {"power-outlet.ini"};
Subroutine ini_file_default: not called CC=1 SCAR=6.9
Pod documented
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 1;
« lib/Power/Outlet/Common.pm ↑ Summary lib/Power/Outlet/Virtual.pm »