File Coverage

blib/lib/Device/WallyHome/Sensor.pm
Criterion Covered Total %
statement 23 23 100.0
branch n/a
condition 2 4 50.0
subroutine 7 7 100.0
pod 2 2 100.0
total 34 36 94.4


line stmt bran cond sub pod time code
1             package Device::WallyHome::Sensor;
2 1     1   4 use Moose;
  1         2  
  1         8  
3 1     1   5791 use MooseX::AttributeShortcuts;
  1         2  
  1         10  
4 1     1   4263 use namespace::autoclean;
  1         5  
  1         18  
5              
6             our $VERSION = 0.01;
7              
8             with 'Device::WallyHome::Role::Creator';
9             with 'Device::WallyHome::Role::REST';
10             with 'Device::WallyHome::Role::Validator';
11              
12              
13             #== ATTRIBUTES =================================================================
14              
15             has 'snid' => (
16             is => 'ro',
17             isa => 'Str',
18             required => 1,
19             writer => '_snid',
20             );
21              
22             has 'offline' => (
23             is => 'ro',
24             isa => 'Int',
25             required => 1,
26             writer => '_offline',
27             );
28              
29             has 'paired' => (
30             is => 'ro',
31             isa => 'Str',
32             required => 1,
33             writer => '_paired',
34             );
35              
36             has 'updated' => (
37             is => 'ro',
38             isa => 'Str',
39             required => 1,
40             writer => '_updated',
41             );
42              
43             has 'alarmed' => (
44             is => 'ro',
45             isa => 'Str',
46             required => 1,
47             writer => '_alarmed',
48             );
49              
50             has 'signalStrength' => (
51             is => 'ro',
52             isa => 'Num',
53             required => 1,
54             writer => '_signalStrength',
55             );
56              
57             has 'recentSignalStrength' => (
58             is => 'ro',
59             isa => 'Num',
60             required => 1,
61             writer => '_recentSignalStrength',
62             );
63              
64             has 'hardwareType' => (
65             is => 'ro',
66             isa => 'Str',
67             required => 1,
68             writer => '_hardwareType',
69             );
70              
71             has 'location' => (
72             is => 'ro',
73             isa => 'Device::WallyHome::Sensor::Location',
74             required => 1,
75             writer => '_location',
76             );
77              
78             has 'thresholds' => (
79             is => 'lazy',
80             );
81              
82             has 'thresholdsByName' => (
83             is => 'ro',
84             isa => 'HashRef[Device::WallyHome::Sensor::Threshold]',
85             required => 1,
86             writer => '_thresholds',
87             );
88              
89             has 'states' => (
90             is => 'lazy',
91             );
92              
93             has 'statesByName' => (
94             is => 'ro',
95             isa => 'HashRef[Device::WallyHome::Sensor::State]',
96             required => 1,
97             writer => '_state',
98             );
99              
100             has 'activities' => (
101             is => 'ro',
102             isa => 'ArrayRef',
103             required => 1,
104             writer => '_activities',
105             );
106              
107              
108             #== ATTRIBUTE BUILDERS =========================================================
109              
110             sub _build_thresholds {
111 1     1   3 my ($self) = @_;
112              
113 1         3 return [map { $self->thresholdsByName()->{$_} } sort keys %{ $self->thresholdsByName() }];
  2         141  
  1         59  
114             }
115              
116             sub _build_states {
117 1     1   2 my ($self) = @_;
118              
119 1         2 return [map { $self->statesByName()->{$_} } sort keys %{ $self->statesByName() }];
  5         269  
  1         35  
120             }
121              
122             #== PUBLIC METHODS =============================================================
123              
124             sub threshold {
125 1     1 1 379 my ($self, $name) = @_;
126              
127 1         8 $self->_checkRequiredScalarParam($name, 'name');
128              
129 1   50     66 return $self->thresholdsByName->{$name} // undef;
130             }
131              
132             sub state {
133 1     1 1 357 my ($self, $name) = @_;
134              
135 1         8 $self->_checkRequiredScalarParam($name, 'name');
136              
137 1   50     63 return $self->statesByName->{$name} // undef;
138             }
139              
140              
141             __PACKAGE__->meta->make_immutable;
142              
143             1;
144              
145             __END__
146              
147             =pod
148              
149             =encoding utf8
150              
151             =head1 NAME
152              
153             Device::WallyHome::Sensor - WallyHome REST API Interface - Sensor
154              
155             =head1 SYNOPSIS
156              
157             # A Device::WallyHome::Sensor will always be instantiated from a parent Device::WallyHome::Place object.
158             use Device::WallyHome;
159              
160             my $wally = Device::WallyHome->new(
161             token => 'f4379e51-222f-4def-8ee1-edf0b15be3b8',
162             );
163              
164             my $place = $wally->getPlaceById('qyWIClYakQX8TQxtFv1ypN6c');
165              
166             my $sensor = $place->getSensorBySnid('90-7a-f1-ff-ff-ff');
167              
168             # Get sensor thresholds
169             my $temperatureMin = $sensor->threshold('TEMP')->min();
170             my $temperatureMax = $sensor->threshold('TEMP')->max();
171             my $relativeHumidityMin = $sensor->threshold('RH')->min();
172             my $relativeHumidityMax = $sensor->threshold('RH')->max();
173              
174             # Check sensor values
175             my $currentTemperature = $sensor->state('TEMP')->value();
176             my $currentRelativeHumidity = $sensor->state('RH')->value();
177             my $currentLeak = $sensor->state('LEAK')->value();
178              
179             =head1 DESCRIPTION
180              
181             B<Device::WallyHome::Sensor> represents a child class of the L<Device::WallyHome>
182             Perl5 interface for the WallyHome REST API.
183              
184             Device::WallyHome::Sensor objects are returned from various methods via
185             a parent L<Device::WallyHome::Place> object and are not intended to be instantiated
186             directly.
187              
188              
189             =head2 Methods
190              
191             =over
192              
193             =item B<thresholds>
194              
195             my $thresholds = $sensor->thresholds();
196              
197             Returns a list of all thresholds associated with the given sensor. Each
198             threshold returned is a L<Device::WallyHome::Threshold> object.
199              
200             =item B<threshold (thresholdAbbreviation)>
201              
202             my $threshold = $sensor->threshold('TEMP');
203              
204             Returns a single L<Device::WallyHome::Threshold> object matching the passed
205             threshold name. The threshold name must be a valid value from the following list:
206              
207             =over
208              
209             =item B<RH> Relative Humidity
210              
211             =item B<TEMP> Temperature
212              
213             =back
214              
215             =back
216              
217             =over
218              
219             =item B<states>
220              
221             my $states = $sensor->states();
222              
223             Returns a list of all states associated with the given sensor. Each
224             state returned is a L<Device::WallyHome::State> object.
225              
226             =item B<state (stateAbbreviation)>
227              
228             my $state = $sensor->state('TEMP');
229              
230             Returns a single L<Device::WallyHome::State> object matching the passed
231             state name. The state name must be a valid value from the following list:
232              
233             =over
234              
235             =item B<RH> Relative Humidity
236              
237             =item B<TEMP> Temperature
238              
239             =item B<LEAK> Water Leak
240              
241             =item B<SENSOR> Sensor
242              
243             =item B<COND> Condition
244              
245             =back
246              
247             =back
248              
249              
250             =head1 AUTHOR
251              
252             Chris Hamilton
253              
254              
255             =head1 COPYRIGHT AND LICENSE
256              
257             This software is copyright (c) 2016 by Chris Hamilton.
258              
259             This is free software; you can redistribute it and/or modify it under the
260             same terms as the Perl 5 programming language system itself.
261              
262              
263             =head1 BUG REPORTING, ENHANCEMENT/FEATURE REQUESTS
264              
265             Please report bugs or enhancement requests on GitHub directly at
266             L<https://github.com/cjhamil/Device-WallyHome/issues>
267              
268             =cut