Skip to content

Commit

Permalink
Merge pull request #908 from DaveNeudoerffer/ha_fan
Browse files Browse the repository at this point in the history
HPlato: added fan support
  • Loading branch information
hplato authored Sep 8, 2024
2 parents d0c07c1 + 35fca51 commit 921e1ac
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions lib/HA_Item.pm
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Description:
- light: on, off and brightness
:rgb_color : for setting an RGB value
:effect : for setting a lighting effect
- fan: on, off and speed (%)
- cover: open,stop,close
:digital : for allowing granular setpoints
- lock: lock, unlock
Expand Down Expand Up @@ -114,7 +115,10 @@ Description:
- ha_call_service also exists on the HA_Server class
- this can be used to call services that are not entity services
eg. $hasrv->ha_call_service( 'notify.mobile_app_my_phone', {title=>'HA notification', message=>'test message'} );
- or add this to items.mht for controlling HA services, like an RF fan controlled by a broadlink item:
GENERIC, fan_light,
CODE, $fan_light -> tie_event('$hasrv->ha_call_service( "remote.rm4pro.send_command", {device => "Fan", command=> "Light"})');
CODE, $fan_light -> set_states ("on","off");
Discovery:
Expand Down Expand Up @@ -355,7 +359,6 @@ sub new {
$self->log( "creating HA Server $name on $address" );

&::MainLoop_pre_add_hook( \&HA_Server::check_for_data, 1, $self );
# &::Reload_pre_add_hook( \&HA_Server::disconnect, 1, $self );
&::Reload_post_add_hook( \&HA_Server::generate_voice_commands, 1, $self );

$HA_Server_List{$name} = $self;
Expand Down Expand Up @@ -1044,6 +1047,8 @@ sub new {
}
} elsif( $domain eq 'lock' ) {
$self->set_states( "unlocked", "locked" );
} elsif( $domain eq 'fan' ) {
# placeholder in case we need to do something. States are set dynamically when the object is set
} elsif( $domain eq 'climate' ) {
} elsif( $domain eq 'sensor' || $domain eq 'binary_sensor' ) {
} elsif( $domain eq 'select' || $domain eq 'input_select' ) {
Expand Down Expand Up @@ -1185,6 +1190,12 @@ sub process_ha_message {
$self->debug( 2, "Duplicate state $new_state->{state} ignored on $self->{object_name}" );
return;
}
if( $p_setby eq 'ha_server_init' ) {
if( defined $self->state() && lc( $self->state() ) eq lc( $new_state->{state} ) ) {
$self->debug( 2, "ha_server_init: Duplicate state $new_state->{state} ignored on $self->{object_name}" );
return;
}
}

if( $self->{domain} eq 'switch'
|| $self->{domain} eq 'lock'
Expand Down Expand Up @@ -1213,6 +1224,29 @@ sub process_ha_message {
if( $p_setby eq 'ha_server_init' ) {
$self->set_states( @{$new_state->{attributes}->{options}},"override=1" );
}
} elsif( $self->{domain} eq 'fan' ) {
my $level = $new_state->{state};
if( $new_state->{state} eq 'on' ){
$level = $new_state->{attributes}->{percentage} . "%" if( $new_state->{attributes}->{percentage} );
$level = "off" if ($level eq "0%");
}
$self->debug( 1, "fan event for $self->{object_name} set to $new_state->{state} ($level)" );
$self->SUPER::set( $level, $p_setby, $p_response );
if( $p_setby eq 'ha_server_init' ) {
#percentage_step gives the number of speed steps for the fan
if (defined $new_state->{attributes}->{percentage_step}) {
my @states = ();
push @states, "off";
for ($i = $new_state->{attributes}->{percentage_step}; $i < 100; $i = $i + $new_state->{attributes}->{percentage_step}) {
next if ($i >96);
push @states,int($i) . "%";
}
push @states, "100%","on";
$self->set_states( @states,"override=1" );
} else {
$self->set_states( "on", "off","override=1" );
}
}
} elsif( $self->{domain} eq 'light' ) {
if (lc $self->{subtype} eq "rgb_color") {
if( $new_state->{attributes} && ref $new_state->{attributes}->{$self->{subtype}} ) {
Expand Down Expand Up @@ -1244,7 +1278,7 @@ sub process_ha_message {
my $state;
foreach my $attrname (keys %{$new_state->{attributes}} ) {
if( $self->{subtype} eq $attrname ) {
$state = $new_state->{attributes}->{$attrname};
$state = $new_state->{attributes}->{$attrname};
}
}
#Check for duplicates again as the $new state is inside the attributes
Expand All @@ -1267,11 +1301,11 @@ sub process_ha_message {
}
if( $p_setby eq 'ha_server_init' ) {
if( $self->{subtype} eq 'hvac_mode' ) {
$self->set_states( @{$new_state->{attributes}->{hvac_modes}},"override=1" );
$self->set_states( @{$new_state->{attributes}->{hvac_modes}},"override=1" );
} elsif( $self->{subtype} eq 'fan_mode' ) {
$self->set_states( @{$new_state->{attributes}->{fan_modes}},"override=1" );
$self->set_states( @{$new_state->{attributes}->{fan_modes}},"override=1" );
} elsif( $self->{subtype} eq 'preset_mode' ) {
$self->set_states( @{$new_state->{attributes}->{preset_modes}},"override=1" );
$self->set_states( @{$new_state->{attributes}->{preset_modes}},"override=1" );
}
}
$self->SUPER::set( $state, $p_setby, $p_response );
Expand Down Expand Up @@ -1414,7 +1448,10 @@ sub ha_set_state {
} elsif (lc $self->{domain} eq 'cover') {
$service = 'set_cover_position';
$service_data->{position} = $numval;
} elsif ( lc $self->{domain} eq 'number' || lc $self->{domain} eq 'input_number' ) {
} elsif (lc $self->{domain} eq 'fan') {
$service = 'set_percentage';
$service_data->{percentage} = $numval;
} elsif ( lc $self->{domain} eq 'number' || lc $self->{domain} eq 'input_number' ) {
$service = 'set_value';
$service_data->{value} = $numval;
} else {
Expand Down

0 comments on commit 921e1ac

Please sign in to comment.