| 1 | use v5.36; | ||
| 2 | |||
| 3 | package App::OpenHAP::Tasmota::Thermostat; | ||
| 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 | |||
| 15 | my $self = $class->SUPER::new( | ||
| 16 | %args, | ||
| 17 | model => 'Tasmota Thermostat', | ||
| 18 | manufacturer => 'OpenHAP', | ||
| 19 | serial => $args{serial} // 'TSTAT-001', | ||
| 20 | ); | ||
| 21 | |||
| 22 | # Sensor configuration (H5) | ||
| 23 | $self->{sensor_type} = $args{sensor_type}; # undef = auto-detect | ||
| 24 | $self->{sensor_index} = $args{sensor_index}; # For indexed sensors | ||
| 25 | |||
| 26 | # Current state | ||
| 27 | $self->{current_temp} = 20.0; | ||
| 28 | $self->{target_temp} = 20.0; | ||
| 29 | $self->{heating_state} = 0; # 0=Off, 1=Heat, 2=Cool | ||
| 30 | $self->{target_heating_state} = 0; | ||
| 31 | |||
| 32 | # Add the Thermostat service. One row per characteristic: | ||
| 33 | # type, iid, format, perms, and the extra arguments. | ||
| 34 | my $thermostat = Protocol::HAP::Service->new( | ||
| 35 | logger => $self->{logger}, | ||
| 36 | type => 'Thermostat', | ||
| 37 | iid => 10, | ||
| 38 | ); | ||
| 39 | |||
| 40 | my @rows = ( [ | ||
| 41 | 'CurrentHeatingCoolingState', 11, | ||
| 42 | 'uint8', [ 'pr', 'ev' ], | ||
| 43 | { value => \$self->{heating_state} }, | ||
| 44 | ], | ||
| 45 | [ | ||
| 46 | 'TargetHeatingCoolingState', | ||
| 47 | 12, 'uint8', | ||
| 48 | [ 'pr', 'pw', 'ev' ], | ||
| 49 | { | ||
| 50 | value => \$self->{target_heating_state}, | ||
| 51 | on_set => sub { $self->_set_target_state(@_) }, | ||
| 52 | }, | ||
| 53 | ], | ||
| 54 | [ | ||
| 55 | 'CurrentTemperature', | ||
| 56 | 13, 'float', | ||
| 57 | [ 'pr', 'ev' ], | ||
| 58 | { | ||
| 59 | unit => 'celsius', | ||
| 60 | value => \$self->{current_temp}, | ||
| 61 | min => -40, | ||
| 62 | max => 100, | ||
| 63 | }, | ||
| 64 | ], | ||
| 65 | [ | ||
| 66 | 'TargetTemperature', | ||
| 67 | 14, 'float', | ||
| 68 | [ 'pr', 'pw', 'ev' ], | ||
| 69 | { | ||
| 70 | unit => 'celsius', | ||
| 71 | value => \$self->{target_temp}, | ||
| 72 | min => 10, | ||
| 73 | max => 38, | ||
| 74 | step => 0.5, | ||
| 75 | on_set => sub { $self->_set_target_temp(@_) }, | ||
| 76 | }, | ||
| 77 | ], | ||
| 78 | [ | ||
| 79 | 'TemperatureDisplayUnits', 15, 'uint8', | ||
| 80 | [ 'pr', 'pw', 'ev' ], | ||
| 81 | { value => 0 }, # 0=Celsius, 1=Fahrenheit | ||
| 82 | ], | ||
| 83 | ); | ||
| 84 | |||
| 85 | for my $row (@rows) { | ||
| 86 | my ( $type, $iid, $format, $perms, $extra ) = @$row; | ||
| 87 | $thermostat->add_characteristic( | ||
| 88 | Protocol::HAP::Characteristic->new( | ||
| 89 | logger => $self->{logger}, | ||
| 90 | type => $type, | ||
| 91 | iid => $iid, | ||
| 92 | format => $format, | ||
| 93 | perms => $perms, | ||
| 94 | %$extra, | ||
| 95 | ) ); | ||
| 96 | } | ||
| 97 | |||
| 98 | $self->add_service($thermostat); | ||
| 99 | |||
| 100 | return $self; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub subscribe_mqtt ($self) | ||
| 104 | { | ||
| 105 | # Call the base class to set up the standard subscriptions | ||
| 106 | # (C1, C2, C3) | ||
| 107 | $self->SUPER::subscribe_mqtt(); | ||
| 108 | |||
| 109 | return unless $self->{mqtt_client}->is_connected(); | ||
| 110 | |||
| 111 | Fugu::Log->default->debug( | ||
| 112 | 'Thermostat %s subscribing to additional MQTT topics', | ||
| 113 | $self->{name} ); | ||
| 114 | |||
| 115 | # M2: The plain-text POWER response (SetOption4 support) | ||
| 116 | $self->_subscribe_plain_power( heating_state => 11 ); | ||
| 117 | |||
| 118 | # STATUS8 answers an active query; the spec recommends STATUS10 | ||
| 119 | $self->_subscribe_status_sns($_) for qw(STATUS8 STATUS10); | ||
| 120 | |||
| 121 | # Query the sensor status immediately | ||
| 122 | $self->query_status(10); | ||
| 123 | } | ||
| 124 | |||
| 125 | # Override the base method to process the sensor data from | ||
| 126 | # SENSOR messages and the STATUS8/STATUS10 responses (H4, H5) | ||
| 127 | sub _process_sensor_data ( $self, $data ) | ||
| 128 | { | ||
| 129 | my ($temp) = $self->_find_sensor_values($data); | ||
| 130 | return unless defined $temp; | ||
| 131 | |||
| 132 | # Convert the value to Celsius if necessary (H4) | ||
| 133 | $temp = $self->convert_temperature($temp); | ||
| 134 | |||
| 135 | Fugu::Log->default->debug( 'Thermostat %s temperature updated: %.1f°C', | ||
| 136 | $self->{name}, $temp ); | ||
| 137 | $self->{current_temp} = $temp; | ||
| 138 | $self->notify_change(13); | ||
| 139 | $self->_check_thermostat_logic(); | ||
| 140 | } | ||
| 141 | |||
| 142 | # Override the base method to process the power state updates | ||
| 143 | sub _on_power_update ( $self, $state ) | ||
| 144 | { | ||
| 145 | if ( $self->{heating_state} != $state ) { | ||
| 146 | $self->{heating_state} = $state; | ||
| 147 | Fugu::Log->default->debug( 'Thermostat %s heating updated: %s', | ||
| 148 | $self->{name}, $state ? 'ON' : 'OFF' ); | ||
| 149 | $self->notify_change(11); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | sub _set_target_temp ( $self, $temp ) | ||
| 154 | { | ||
| 155 | Fugu::Log->default->debug( | ||
| 156 | 'Thermostat %s target temperature set to %.1f°C', | ||
| 157 | $self->{name}, $temp ); | ||
| 158 | $self->{target_temp} = $temp; | ||
| 159 | $self->_check_thermostat_logic(); | ||
| 160 | } | ||
| 161 | |||
| 162 | sub _set_target_state ( $self, $state ) | ||
| 163 | { | ||
| 164 | Fugu::Log->default->debug( | ||
| 165 | 'Thermostat %s target heating state set to %d', | ||
| 166 | $self->{name}, $state ); | ||
| 167 | $self->{target_heating_state} = $state; | ||
| 168 | $self->_check_thermostat_logic(); | ||
| 169 | } | ||
| 170 | |||
| 171 | sub _check_thermostat_logic ($self) | ||
| 172 | { | ||
| 173 | # Simple bang-bang controller with 0.5°C hysteresis | ||
| 174 | my $hysteresis = 0.5; | ||
| 175 | my $current = $self->{current_temp}; | ||
| 176 | my $target = $self->{target_temp}; | ||
| 177 | |||
| 178 | if ( $self->{target_heating_state} == 0 ) { | ||
| 179 | |||
| 180 | # The target is OFF | ||
| 181 | $self->set_power(0) if $self->{heating_state}; | ||
| 182 | } | ||
| 183 | elsif ( $self->{target_heating_state} == 1 ) { | ||
| 184 | |||
| 185 | # The target is HEAT | ||
| 186 | if ( $current < $target - $hysteresis ) { | ||
| 187 | $self->set_power(1); | ||
| 188 | } | ||
| 189 | elsif ( $current > $target + $hysteresis ) { | ||
| 190 | $self->set_power(0); | ||
| 191 | } | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | 1; |