Skip to content

Commit

Permalink
Added ability to add discovery parms to mqtt items
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveNeudoerffer committed Nov 26, 2024
1 parent 3a53af8 commit df3f37c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
25 changes: 25 additions & 0 deletions lib/mqtt.pm
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ sub new {
# $self->set( 'on', $self );

&::Reload_post_add_hook( \&mqtt::generate_voice_commands, 1, $self );
&::Reload_post_add_hook( \&mqtt::create_discovery_data, 1, $self );

return $self;
}
Expand Down Expand Up @@ -1175,6 +1176,7 @@ sub list_retained_topics {
}

foreach my $interface ( @interface_list ) {
$interface->log( "Listing retained topics for: $interface->{instance}" );
for my $topic ( keys %{$interface->{retained_topics}} ) {
my $handled = $interface->{retained_topics}->{$topic};
$interface->log( "$$interface{instance} retained topic: ($handled) $topic" );
Expand Down Expand Up @@ -1222,6 +1224,29 @@ sub cleanup_discovery_topics {

# ------------------------------------------------------------------------------

=item C<(create_discovery_data())>
Create discovery messages for each discoverable item.
=cut

sub create_discovery_data {
my ($self) = @_;
my $obj;

if( !$self->isConnected ) {
$self->error( "Unable to publish discovery data -- $self->{instance} not connected" );
return 0;
}
$self->log( "Creating and publishing discovery data for all discoverable objects" );
for my $obj ( @{ $self->{objects} } ) {
if( $obj->can( 'create_discovery_message' ) ) {
$obj->create_discovery_message();
}
}
return 1;
}

=item C<(publish_discovery_data())>
Publish discovery messages for each discoverable item.
Expand Down
4 changes: 2 additions & 2 deletions lib/mqtt_discovery.pm
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ sub new { #### mqtt_DiscoveredItem
} else {
$mqtt_type = $disc_type;
}
} elsif( grep( /^${disc_type}$/, ('light', 'switch', 'binary_sensor', 'sensor', 'scene', 'select') ) ) {
} elsif( grep( /^${disc_type}$/, ('light', 'switch', 'binary_sensor', 'sensor', 'scene', 'select', 'text', 'number') ) ) {
$mqtt_type = $disc_type;
} else {
$disc_obj->debug( 1, "UNRECOGNIZED DISCOVERY TYPE: $disc_type" );
Expand Down Expand Up @@ -185,7 +185,7 @@ sub new { #### mqtt_DiscoveredItem
} else {
$obj_name .= $device_id;
}
$obj_name =~ s/ /_/g;
$obj_name =~ s/[^\w]/_/g;
$obj = ::get_object_by_name( $obj_name );
if( $obj ) {
$disc_obj->error( "Trying to create object that already exists: $obj_name" );
Expand Down
40 changes: 31 additions & 9 deletions lib/mqtt_items.pm
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,8 @@ sub decode_mqtt_payload {
$msg = $value;
} elsif( $$self{mqtt_type} eq 'text' ) {
$msg = $value;
} elsif( $$self{mqtt_type} eq 'number' ) {
$msg = $value;
} elsif( $$self{mqtt_type} eq 'cover' ) {
$msg = $value;
} else {
Expand Down Expand Up @@ -660,6 +662,7 @@ sub encode_mqtt_payload {
if( $self->{mqtt_type} eq 'sensor'
|| $self->{mqtt_type} eq 'select'
|| $self->{mqtt_type} eq 'text'
|| $self->{mqtt_type} eq 'number'
|| $self->{mqtt_type} eq 'cover'
) {
$payload = $setval;
Expand Down Expand Up @@ -984,6 +987,7 @@ package mqtt_LocalItem;
use strict;

use Data::Dumper;
use Hash::Merge;

@mqtt_LocalItem::ISA = ( 'mqtt_BaseItem' );

Expand All @@ -1003,7 +1007,7 @@ sub new { ### mqtt_LocalItem
$base_type = $local_object->{mqttlocalitem}->{base_type} if ((defined $local_object->{mqttlocalitem}->{base_type} ) and (!$type));
$device_class = $local_object->{mqttlocalitem}->{device_class} if ((defined $local_object->{mqttlocalitem}->{device_class} ) and (!$device_class));

if( !grep( /^$base_type$/, ('light','switch','binary_sensor', 'sensor', 'scene', 'select', 'text', 'cover' ) ) ) {
if( !grep( /^$base_type$/, ('light','switch','binary_sensor', 'sensor', 'scene', 'select', 'text', 'number', 'cover' ) ) ) {
$interface->error( "Invalid mqtt type '$type'" );
return;
}
Expand Down Expand Up @@ -1079,6 +1083,8 @@ sub new { ### mqtt_LocalItem
}
} elsif( $base_type eq 'text' ) {
$self->{disc_info}->{command_topic} = "$topic_prefix/set";
} elsif( $base_type eq 'number' ) {
$self->{disc_info}->{command_topic} = "$topic_prefix/set";
}

$self->{is_local} = 1;
Expand All @@ -1100,11 +1106,7 @@ sub new { ### mqtt_LocalItem
$self->{disc_info}->{unique_id} =~ s/ /_/g;
}

if( ref $self->{local_item}->{mqtt_device_info} ) {
$self->{disc_info}->{device} = $self->{local_item}->{mqtt_device_info};
}

$self->create_discovery_message();
# $self->create_discovery_message();


# my $d = Data::Dumper->new( [$self] );
Expand All @@ -1116,6 +1118,26 @@ sub new { ### mqtt_LocalItem
return $self;
}

sub add_discovery_info {
my ($self,$extra_disc_info) = @_;

my $merger = Hash::Merge->new( 'RIGHT_PRECEDENT' );
$self->{disc_info} = $merger->merge( $self->{disc_info}, $extra_disc_info );

# foreach my $key (keys %{$extra_disc_info}) {
# if( exists $extra_disc_info && ref $extra_disc_info->{$key} eq 'HASH' ) {
# if( !exists $self->{disc_info}->{$key} ) {
# $self->{disc_info}->{$key} = {};
# }
# for my $key2 (keys %{$extra_disc_info->{
#
# if( exists $self->{disc_info}->{$key} ) {
# $self->log( "Overriding $key in discovery info for: $self->{object_name}" );
# }
# $self->{disc_info}->{$key} = $extra_disc_info->{$key};
# }
}

=item C<receive_mqtt_message( topic, message, retained )>
Process received mqtt message
=cut
Expand All @@ -1138,7 +1160,7 @@ sub receive_mqtt_message {
$obj_name = $self->get_object_name();
}
if( $topic eq $self->{disc_info}->{state_topic} ) {
$self->debug( 1, "LocalItem $obj_name ignoring state topic message" );
$self->debug( 2, "LocalItem $obj_name ignoring state topic message" );
} elsif( $topic eq $self->{disc_info}->{command_topic} ) {
if( $retained ) {
# command messages should never be retained, but just in case...
Expand Down Expand Up @@ -1595,7 +1617,7 @@ sub new { ### mqtt_RemoteItem
$self->{disc_info}->{unique_id} = 'tasmota_' . $self->{mqtt_name};
$self->{disc_info}->{unique_id} =~ s/ /_/g;

$self->create_discovery_message();
# $self->create_discovery_message();

# We may need flags to deal with XML, JSON or Text
return $self;
Expand Down Expand Up @@ -1680,7 +1702,7 @@ sub new { ### mqtt_InstMqttItem
$self->{disc_info}->{unique_id} = $self->{mqtt_name};
$self->{disc_info}->{unique_id} =~ s/ /_/g;

$self->create_discovery_message();
# $self->create_discovery_message();


# $self->debug( 1, "InstMqttItem created: \n" . Dumper( $self ) );
Expand Down

0 comments on commit df3f37c

Please sign in to comment.