| 1 | use v5.36; | ||
| 2 | |||
| 3 | package App::OpenHAP::Tasmota::Heater; | ||
| 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 Switch', | ||
| 17 | manufacturer => 'OpenHAP', | ||
| 18 | serial => $args{serial} // 'HEAT-001', | ||
| 19 | ); | ||
| 20 | |||
| 21 | $self->{power_state} = 0; | ||
| 22 | |||
| 23 | # Add the Switch/Outlet service | ||
| 24 | my $switch = Protocol::HAP::Service->new( | ||
| 25 | logger => $self->{logger}, | ||
| 26 | type => 'Switch', | ||
| 27 | iid => 10, | ||
| 28 | primary => 1, | ||
| 29 | ); | ||
| 30 | |||
| 31 | $switch->add_characteristic( | ||
| 32 | Protocol::HAP::Characteristic->new( | ||
| 33 | logger => $self->{logger}, | ||
| 34 | type => 'On', | ||
| 35 | iid => 11, | ||
| 36 | format => 'bool', | ||
| 37 | perms => [ 'pr', 'pw', 'ev' ], | ||
| 38 | value => \$self->{power_state}, | ||
| 39 | on_set => sub { $self->set_power(@_) }, | ||
| 40 | ) ); | ||
| 41 | |||
| 42 | $self->add_service($switch); | ||
| 43 | |||
| 44 | return $self; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub subscribe_mqtt ($self) | ||
| 48 | { | ||
| 49 | # Call the base class to set up the standard subscriptions | ||
| 50 | # (C1, C2, C3) | ||
| 51 | $self->SUPER::subscribe_mqtt(); | ||
| 52 | |||
| 53 | return unless $self->{mqtt_client}->is_connected(); | ||
| 54 | |||
| 55 | Fugu::Log->default->debug( | ||
| 56 | 'Heater %s subscribing to additional MQTT topics', | ||
| 57 | $self->{name} ); | ||
| 58 | |||
| 59 | # M2: The plain-text POWER response (SetOption4 support) | ||
| 60 | $self->_subscribe_plain_power( power_state => 11 ); | ||
| 61 | } | ||
| 62 | |||
| 63 | # Override _on_power_update to update the power state | ||
| 64 | sub _on_power_update ( $self, $state ) | ||
| 65 | { | ||
| 66 | if ( $self->{power_state} != $state ) { | ||
| 67 | $self->{power_state} = $state; | ||
| 68 | Fugu::Log->default->debug( 'Heater %s power updated: %s', | ||
| 69 | $self->{name}, $state ? 'ON' : 'OFF' ); | ||
| 70 | $self->notify_change(11); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | 1; |