blib/lib/App/OpenHAP/Tasmota/Sensor.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 78 CC 5 SCAR 17.6
CC
5
Cov
0%
_process_sensor_data
12.0
new
6.0
subscribe_mqtt
6.0
CRAP 5.8
1 use v5.36;
2  
3 package App::OpenHAP::Tasmota::Sensor;
4 our $VERSION = '0.1.0';
5  
6 use Fugu::Log;
7 require App::OpenHAP::Tasmota::Device;
8 our @ISA = qw(App::OpenHAP::Tasmota::Device);
9 use Protocol::HAP::Service;
10 use Protocol::HAP::Characteristic;
11  
12 sub new ( $class, %args )
13 {
14 my $self = $class->SUPER::new(
15 %args,
16 model => 'Tasmota Sensor',
17 manufacturer => 'OpenHAP',
18 serial => $args{serial} // 'SENS-001',
19 );
20  
21 $self->{sensor_type} = $args{sensor_type}; # undef = auto-detect
22 $self->{sensor_index} = $args{sensor_index}; # For indexed sensors
23 $self->{current_temp} = 20.0;
24 $self->{current_humidity} = undef;
25 $self->{has_humidity} = $args{has_humidity} // 0;
26  
27 # Add the Temperature Sensor service
28 my $temp_sensor = Protocol::HAP::Service->new(
29 logger => $self->{logger},
30 type => 'TemperatureSensor',
31 iid => 10,
32 primary => 1,
33 );
34  
35 $temp_sensor->add_characteristic(
36 Protocol::HAP::Characteristic->new(
37 logger => $self->{logger},
38 type => 'CurrentTemperature',
39 iid => 11,
40 format => 'float',
41 perms => [ 'pr', 'ev' ],
42 unit => 'celsius',
43 value => \$self->{current_temp},
44 min => -40,
45 max => 100,
46 ) );
47  
48 $self->add_service($temp_sensor);
49  
50 # Add the optional Humidity Sensor service (H5)
51 if ( $self->{has_humidity} ) {
52 $self->{current_humidity} = 50.0;
53  
54 my $humidity_sensor = Protocol::HAP::Service->new(
55 logger => $self->{logger},
56 type => 'HumiditySensor',
57 iid => 20,
58 );
59  
60 $humidity_sensor->add_characteristic(
61 Protocol::HAP::Characteristic->new(
62 logger => $self->{logger},
63 type => 'CurrentRelativeHumidity',
64 iid => 21,
65 format => 'float',
66 perms => [ 'pr', 'ev' ],
67 unit => 'percentage',
68 value => \$self->{current_humidity},
69 min => 0,
70 max => 100,
71 ) );
72  
73 $self->add_service($humidity_sensor);
74 }
75  
76 return $self;
77 }
78  
79 sub subscribe_mqtt ($self)
80 {
81 # Call the base class to set up the standard subscriptions
82 # (C1, C2, C3)
83 $self->SUPER::subscribe_mqtt();
84  
85 return unless $self->{mqtt_client}->is_connected();
86  
87 Fugu::Log->default->debug(
88 'Sensor %s subscribing to additional MQTT topics',
89 $self->{name} );
90  
91 # STATUS8 answers an active query; the spec recommends STATUS10
92 $self->_subscribe_status_sns($_) for qw(STATUS8 STATUS10);
93 }
94  
95 # Override the base method to process the sensor data from
96 # SENSOR messages
97 sub _process_sensor_data ( $self, $data )
98 {
99 my ( $temp, $humidity ) = $self->_find_sensor_values($data);
100  
101 if ( defined $temp ) {
102  
103 # Convert the value to Celsius if necessary (H4)
104 $temp = $self->convert_temperature($temp);
105  
106 Fugu::Log->default->debug(
107 'Sensor %s temperature updated: %.1f°C',
108 $self->{name}, $temp );
109 $self->{current_temp} = $temp;
110 $self->notify_change(11);
111 }
112  
113 if ( defined $humidity && $self->{has_humidity} ) {
114 Fugu::Log->default->debug( 'Sensor %s humidity updated: %.1f%%',
115 $self->{name}, $humidity );
116 $self->{current_humidity} = $humidity;
117 $self->notify_change(21);
118 }
119 }
120  
121 1;