File Coverage

blib/lib/Device/WallyHome/Role/Creator.pm
Criterion Covered Total %
statement 61 62 98.3
branch 8 12 66.6
condition 11 27 40.7
subroutine 7 7 100.0
pod 0 3 0.0
total 87 111 78.3


line stmt bran cond sub pod time code
1             package Device::WallyHome::Role::Creator;
2 2     2   1343 use Moose::Role;
  2         3  
  2         20  
3 2     2   8552 use MooseX::AttributeShortcuts;
  2         3  
  2         16  
4              
5 2     2   5907 use Data::Dumper;
  2         12101  
  2         143  
6 2     2   950 use Module::Loader;
  2         24158  
  2         1218  
7              
8             our $VERSION = 0.01;
9              
10              
11             #== ATTRIBUTES =================================================================
12              
13             has 'callbackObject' => (
14             is => 'ro',
15             weak_ref => 1,
16             writer => '_callbackObject',
17             );
18              
19              
20             #== PUBLIC METHODS =============================================================
21              
22             sub instantiateObject {
23 10     10 0 21 my ($self, $class, $params) = @_;
24              
25 10   50     24 $params //= {};
26              
27 10         15 my $restApiRole = 'Device::WallyHome::Role::REST';
28              
29             # Dynamically load class module
30             eval {
31 10         48 (my $file = $class) =~ s/::/\//g;
32              
33 10         4414 require $file . '.pm';
34              
35 10         68 $class->import();
36              
37 10         58 1;
38 10 50       19 } or do {
39 0         0 die "Failed to dynamically load class module ($class): " . $@;
40             };
41              
42             # Pass along REST API information
43 10 100 66     62 if (
44             $self->does($restApiRole)
45             && $class->does($restApiRole)
46             ) {
47 2   33     1977 $params->{apiHostname} //= $self->apiHostname();
48 2   33     178 $params->{apiUseHttps} //= $self->apiUseHttps();
49 2   33     151 $params->{apiVersion} //= $self->apiVersion();
50 2   33     121 $params->{lastApiError} //= $self->lastApiError();
51 2   33     128 $params->{token} //= $self->token();
52 2   33     119 $params->{timeout} //= $self->timeout();
53             }
54              
55 10 50       1700 if ($self->_testModeIdentifier()) {
56 10         375 $params->{_testModeIdentifier} = $self->_testModeIdentifier();
57             }
58              
59             # Use ourself as the callback object
60 10         21 $params->{callbackObject} = $self;
61              
62 10         80 Module::Loader->new()->load($class);
63              
64 10         852 my $obj = $class->new(%$params);
65              
66 10 50       39 die "Failed to instantiate object ($class): " . Dumper($params) unless defined $obj;
67              
68 10         70 return $obj;
69             }
70              
71             sub loadPlaceFromApiResponseData {
72 1     1 0 1 my ($self, $placeData) = @_;
73              
74 1         2 my $initData = {};
75              
76             # Non-Boolean Attributes
77 1         3 foreach my $attribute (qw{
78             id
79             accountId
80             label
81             fullAddress
82             address
83             sensorIds
84             nestAdjustments
85             rapidResponseSupport
86             }) {
87 8         14 $initData->{$attribute} = $placeData->{$attribute};
88             }
89              
90             # Boolean Attributes
91 1         2 foreach my $attribute (qw{
92             suspended
93             buzzerEnabled
94             nestEnabled
95             }) {
96 3 100       45 $initData->{$attribute} = $placeData->{$attribute} ? 1 : 0;
97             }
98              
99 1         10 return $self->instantiateObject('Device::WallyHome::Place', $initData);
100             }
101              
102             sub loadSensorFromApiResponseData {
103 1     1 0 4 my ($self, $sensorData) = @_;
104              
105 1         6 my $initData = {};
106              
107             # Non-Boolean Attributes
108 1         4 foreach my $attribute (qw{
109             snid
110             paired
111             updated
112             signalStrength
113             recentSignalStrength
114             hardwareType
115             activities
116             }) {
117 7         21 $initData->{$attribute} = $sensorData->{$attribute};
118             }
119              
120             # Boolean Attributes
121 1         4 foreach my $attribute (qw{
122             offline
123             suspended
124             alarmed
125             }) {
126 3 50       38 $initData->{$attribute} = $sensorData->{$attribute} ? 1 : 0;
127             }
128              
129 1         13 $initData->{location} = $self->instantiateObject('Device::WallyHome::Sensor::Location', $sensorData->{location});
130              
131 1         4 $initData->{thresholdsByName} = {};
132              
133 1   50     1 foreach my $thresholdDataKey (keys %{ $sensorData->{thresholds} // {} }) {
  1         11  
134 2         7 my $thresholdHref = $sensorData->{thresholds}->{$thresholdDataKey};
135              
136             my $thresholdData = {
137             max => $thresholdHref->{max},
138             min => $thresholdHref->{min},
139 2         9 name => $thresholdDataKey,
140             };
141              
142 2         11 $initData->{thresholdsByName}->{$thresholdDataKey} = $self->instantiateObject('Device::WallyHome::Sensor::Threshold', $thresholdData);
143             }
144              
145 1         8 $initData->{statesByName} = {};
146              
147 1   50     2 foreach my $stateDataKey (keys %{ $sensorData->{state} // {} }) {
  1         11  
148 5         11 my $stateHref = $sensorData->{state}->{$stateDataKey};
149              
150             my $stateData = {
151             at => $stateHref->{at},
152             name => $stateDataKey,
153             value => $stateHref->{value},
154 5         16 };
155              
156 5         13 $initData->{statesByName}->{$stateDataKey} = $self->instantiateObject('Device::WallyHome::Sensor::State', $stateData);
157             }
158              
159 1         5 my $sensor = $self->instantiateObject('Device::WallyHome::Sensor', $initData);
160             }
161              
162             1;