Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MQTT improvements #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion bin/max
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,10 @@ sub do_mqtt {
require Net::MQTT::Simple;
my $hostname = shift;
my $topic_prefix = shift;
my $username = shift;
my $password = shift;
our $mqtt ||= Net::MQTT::Simple->new($hostname);
$mqtt->login($username, $password) if $username and $password;

for my $room ($max->rooms) {
my @devices = $room->devices;
Expand All @@ -303,16 +306,25 @@ sub do_mqtt {
for my $device (@devices) {
my $r = $room->display_name;
my $d = $device->display_name;
s/\ /-/g for $r, $d;
s/\W+/-/g for $r, $d;
my $devtopic = join "/", $topic_prefix, $r, $d;
my $c = "°C";
utf8::encode($c);
$mqtt->retain("$devtopic/valve", $device->valve . " %")
if $device->has_valve;
$mqtt->retain("$devtopic/valve_perc", $device->valve)
if $device->has_valve;
$mqtt->retain("$devtopic/setpoint", $device->setpoint . " $c")
if $device->has_setpoint;
$mqtt->retain("$devtopic/setpoint_c", $device->setpoint)
if $device->has_setpoint;
$mqtt->retain("$devtopic/temperature", $device->temperature . " $c")
if $device->has_temperature;
$mqtt->retain("$devtopic/temperature_c", $device->temperature)
if $device->has_temperature;
while( my ($flag, $value) = each %{$device->{flags}} ) {
$mqtt->retain("$devtopic/flag_$flag", $value ? 1 : 0);
}
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions bin/max.pod
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,14 @@ Outputs a list of rooms with values from the room's devices.

Outputs a summary of device attributes, grouped by room.

=head3 mqtt <hostname> <topic_prefix>
=head3 mqtt <hostname> <topic_prefix> <username> <password>

Publishes device attributes to the given MQTT server. Currently does not
support any authentication or TLS. The I<topic_prefix> should not end in C</>,
as one is added by the program.
support TLS. The I<topic_prefix> should not end in C</>, as one is added by the
program.

Username and password are optional. If they are specified, you probably also
want to add the C<MQTT_SIMPLE_ALLOW_INSECURE_LOGIN=1> environment variable.

The messages are sent with the I<retain> flag enabled, and consist of the
numeric decimal value, a space, and the corresponding unit.
Expand Down