blib/lib/Protocol/HAP/Characteristic.pm

filter: Statementstmt 95.6% 44 / 46 Branchbran 86.6% 26 / 30 Conditioncond 57.1% 8 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 7 totaltotal 76.3% 87 / 114 CC 22 SCAR 16.5
CC
22
Cov
87%
new
8.8
to_json
6.0
json_value
5.2
CRAP 5.2
« blib/lib/App/OpenHAP/Tasmota/Heater.pm ↑ Summary lib/Protocol/HAP/HTTP.pm »
1 4 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Protocol::HAP::Characteristic;
4 4 our $VERSION = '0.1.0';
5  
6 4 ▶use Protocol::HAP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # HAP Characteristic Type UUIDs
9 4 our %CHAR_TYPES = (
10  
11 # Accessory Information
12 'Identify' => '00000014-0000-1000-8000-0026BB765291',
13 'Manufacturer' => '00000020-0000-1000-8000-0026BB765291',
14 'Model' => '00000021-0000-1000-8000-0026BB765291',
15 'Name' => '00000023-0000-1000-8000-0026BB765291',
16 'SerialNumber' => '00000030-0000-1000-8000-0026BB765291',
17 'FirmwareRevision' => '00000052-0000-1000-8000-0026BB765291',
18  
19 # Thermostat
20 'CurrentHeatingCoolingState' => '0000000F-0000-1000-8000-0026BB765291',
21 'TargetHeatingCoolingState' => '00000033-0000-1000-8000-0026BB765291',
22 'CurrentTemperature' => '00000011-0000-1000-8000-0026BB765291',
23 'TargetTemperature' => '00000035-0000-1000-8000-0026BB765291',
24 'TemperatureDisplayUnits' => '00000036-0000-1000-8000-0026BB765291',
25  
26 # Switch/Outlet
27 'On' => '00000025-0000-1000-8000-0026BB765291',
28 'OutletInUse' => '00000026-0000-1000-8000-0026BB765291',
29  
30 # Lightbulb
31 'Brightness' => '00000008-0000-1000-8000-0026BB765291',
32 'Hue' => '00000013-0000-1000-8000-0026BB765291',
33 'Saturation' => '0000002F-0000-1000-8000-0026BB765291',
34 'ColorTemperature' => '000000CE-0000-1000-8000-0026BB765291',
35  
36 # Sensors
37 'CurrentRelativeHumidity' => '00000010-0000-1000-8000-0026BB765291',
38  
39 # Protocol information
40 'Version' => '00000037-0000-1000-8000-0026BB765291',
41 );
42  
43 191 ▶sub new ( $class, %args )
Subroutine new: called CC=7 SCAR=21.8
Pod undocumented
44 {
45  
46 191 ▶ my $type = $args{type} // die "Characteristic type required";
Condition50%1/2
$args{"type"} // die("Characteristic type required")
l!l
1910
MC/DC0%0/1
$args{"type"} // die("Characteristic type required")
$args{"type"}
Truth table50%1/2
A
A= $args{"type"}
Aresult
11
00
47 191 ▶ my $uuid = $CHAR_TYPES{$type} // $type;
Condition66%2/3
$Protocol::HAP::Characteristic::CHAR_TYPES{$type} // $type
l!l&&r!l&&!r
19010
MC/DC0%0/2
$Protocol::HAP::Characteristic::CHAR_TYPES{$type} // $type
$Protocol::HAP::Characteristic::CHAR_TYPES{$type} $type
Truth table66%2/3
A // B
A= $Protocol::HAP::Characteristic::CHAR_TYPES{$type}B= $type
ABresult
1X1
011
000
48  
49 my $self = bless {
50 type => $uuid,
51 iid => $args{iid} // die('Instance ID required'),
52 format => $args{format} // 'string',
53 perms => $args{perms} // ['pr'],
54 logger => $args{logger} // Protocol::HAP->null_logger,
55  
56 # Value: a scalar reference makes the value mutable
57 value => $args{value},
58  
59 # Optional metadata
60 unit => $args{unit},
61 min => $args{min},
62 max => $args{max},
63 step => $args{step},
64  
65 # Callbacks
66 on_get => $args{on_get},
67 on_set => $args{on_set},
68  
69 # Event notifications
70 191 ▶ event_enabled => 0,
Condition55%5/9
$args{"iid"} // die("Instance ID required")
l!l
1910
$args{"format"} // "string"
l!l
1910
$args{"perms"} // ["pr"]
l!l
1910
$args{"logger"} // "Protocol::HAP"->null_logger
l!l&&r!l&&!r
159320
MC/DC0%0/1
$args{"iid"} // die("Instance ID required")
$args{"iid"}
MC/DC0%0/1
$args{"format"} // "string"
$args{"format"}
MC/DC0%0/1
$args{"perms"} // ["pr"]
$args{"perms"}
MC/DC0%0/2
$args{"logger"} // "Protocol::HAP"->null_logger
$args{"logger"} "Protocol::HAP"->null_logger
Truth table50%1/2
A
A= $args{"iid"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"format"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"perms"}
Aresult
11
00
Truth table66%2/3
A // B
A= $args{"logger"}B= "Protocol::HAP"->null_logger
ABresult
1X1
011
000
71 }, $class;
72  
73 191 return $self;
74 }
75  
76 24 ▶sub get_value ($self)
Subroutine get_value: called CC=3 SCAR=11.2
Pod undocumented
77 {
78  
79 # Use the custom getter if the characteristic has one
80 24 ▶ if ( $self->{on_get} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"on_get"}) 0 24 24
81 0 return $self->{on_get}->();
82 }
83  
84 # If the value is a reference, dereference it
85 24 ▶ if ( ref $self->{value} eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"value"} eq "SCALAR") 3 21 24
86 3 return ${ $self->{value} };
87 }
88  
89 21 return $self->{value};
90 }
91  
92 4 ▶sub set_value ( $self, $value )
Subroutine set_value: called CC=4 SCAR=13.9
Pod undocumented
93 {
94 $self->{logger}->debug( 'Setting characteristic IID=%d to value: %s',
95 4 ▶ $self->{iid}, defined $value ? $value : 'undef' );
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 4 0 4
96  
97 # Use the custom setter if the characteristic has one
98 4 ▶ if ( $self->{on_set} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"on_set"}) 1 3 4
99 1 $self->{on_set}->($value);
100 }
101  
102 # Update the value
103 4 ▶ if ( ref $self->{value} eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'value'} eq 'SCALAR') { } 2 2 4
104 2 ${ $self->{value} } = $value;
105 }
106 else {
107 2 $self->{value} = $value;
108 }
109 }
110  
111 4 ▶sub enable_events ( $self, $enabled )
Subroutine enable_events: called CC=2 SCAR=6.9
Pod undocumented
112 {
113 $self->{logger}->debug(
114 'Events %s for characteristic IID=%d',
115 $enabled ? 'enabled' : 'disabled',
116 4 ▶ $self->{iid} );
Branch: 1 branch
BranchTrueFalseTotal
$enabled ? : 3 1 4
117 4 $self->{event_enabled} = $enabled;
118 }
119  
120 3 ▶sub events_enabled ($self)
Subroutine events_enabled: called CC=1 SCAR=0.0
Pod undocumented
121 {
122 3 return $self->{event_enabled};
123 }
124  
125 26 ▶sub to_json ($self)
Subroutine to_json: called CC=6 SCAR=17.9
Pod undocumented
126 {
127  
128 my $json = {
129 type => Protocol::HAP::uuid_to_short( $self->{type} ),
130 iid => $self->{iid},
131 format => $self->{format},
132 perms => $self->{perms},
133 26 };
134  
135 # Add the optional metadata
136 26 ▶ $json->{unit} = $self->{unit} if defined $self->{unit};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"unit"} 1 25 26
137 26 ▶ $json->{minValue} = $self->{min} if defined $self->{min};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"min"} 1 25 26
138 26 ▶ $json->{maxValue} = $self->{max} if defined $self->{max};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"max"} 1 25 26
139 26 ▶ $json->{minStep} = $self->{step} if defined $self->{step};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"step"} 1 25 26
140  
141 # Add the value if the characteristic is readable
142 26 ▶ if ( grep { $_ eq 'pr' } @{ $self->{perms} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "pr";} @{$$self{"perms"};}) 23 3 26
143 23 $json->{value} = $self->json_value;
144 }
145  
146 26 return $json;
147 }
148  
149 # json_value() - Convert the current value to its JSON type. The
150 # characteristic format selects the type (HAP-HTTP.md ยง15.1).
151 23 ▶sub json_value ($self)
Subroutine json_value: called CC=5 SCAR=16.4
Pod undocumented
152 {
153 23 my $value = $self->get_value();
154  
155 23 ▶ if ( $self->{format} eq 'bool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"format"} eq "bool") 3 20 23
156 3 ▶ return $value ? \1 : \0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 3 0 3
157 }
158 20 ▶ if ( $self->{format} =~ /^(uint|int)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"format"} =~ /^(uint|int)/u) 0 20 20
159 0 return $value + 0;
160 }
161 20 ▶ if ( $self->{format} eq 'float' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"format"} eq "float") 2 18 20
162 2 return $value + 0.0;
163 }
164 18 return $value;
165 }
166  
167 4 1;
« blib/lib/App/OpenHAP/Tasmota/Heater.pm ↑ Summary lib/Protocol/HAP/HTTP.pm »