Commit Diff


commit - 25428cf5edb3a454955867e349a7b17a64c04692
commit + 7f68ca2a7a2aa783bb434e0134fb7cd25c3f6f79
blob - 7d13a61b9a398e25e06f1c92dd9c15c1be322b43
blob + cf8673832cbfbc4f1ec0a861ac9af64599ff089a
--- huectl.pl
+++ huectl.pl
@@ -20,13 +20,15 @@ GetOptions(
 	"id=i" => \(my $RESOURCE_ID),
 	"sensor=i" => \(my $SENSOR_ID),
 	"battery=i" => \(my $BATTERY),
+	"heat" => \(my $HEAT),
 	"action=s" => \(my $ACTION = "state"),
 	"verbose" => \(my $VERBOSE),
 	"debug" => \(my $DEBUG),
+	"pretty" => \(my $PRETTY),
 );
 
 my $USAGE = <<"END_USAGE";
-Usage: $0 bridge-name [-t type] [-i id] [-s sensor] [-b percent] [-a action] [-v] [-d]
+Usage: $0 bridge-name [-t type] [-i id] [-s sensor] [-b percent] [-a action] [-v] [-d] [-p]
 Options:
 bridge-name    as defined in [HOME]./hue.conf or [HOME]./.hue.conf or /etc/hue.conf
 -t | --type    [ lights | sensors | groups | all | trigger ] (default: lights)
@@ -34,8 +36,9 @@ bridge-name    as defined in [HOME]./hue.conf or [HOME
 -s | --sensor  sensor-id
 -b | --battery percent of battery level to report on, only relevant with sensors
 -a | --action  [ on | off | state | bright | relax | morning | dimmed | evening | nightlight ] (default: state)
--v | --verbose JSON output
--d | --debug   pretty JSON output 
+-v | --verbose  
+-d | --debug   JSON output
+-p | --pretty  pretty JSON output (can be a lot)
 
 Command examples:
 $0 bridge1
@@ -97,10 +100,11 @@ sub _return_error_with {
 
 sub _verify_response {
         my ($status, $content, $uri) = @_;
-        if ($status =~ /^2/ && $VERBOSE) {
-                print "URI: $uri\nHTTP RESPONSE: $status\nCONTENT: \n$content\n";
+        if ($status =~ /^2/) {
+                print "URI: $uri\nHTTP RESPONSE: $status\n" if ($VERBOSE || $DEBUG);
+		print "CONTENT: \n$content\n" if $DEBUG;
         }
-	say $json->ascii->pretty->encode(decode_json join '', $content) if $DEBUG;
+	say $json->ascii->pretty->encode(decode_json join '', $content) if $PRETTY;
         if ($status !~ /^2/ || $content =~ /error/) {
                 _return_error_with("URI: $uri\nHTTP RESPONSE: $status\nCONTENT: \n$content");
         }
@@ -191,6 +195,8 @@ sub sensors {
 	my ($resource) = @_;
 	my $sensor_objects = _get_json_for($resource);
 	my %sensor;
+	my $name;
+	my $temperature;
 	UNIQUEID:
 	for my $key (keys (%{$sensor_objects})) {
 		if ($sensor_objects->{$key}->{'uniqueid'} && ($sensor_objects->{$key}->{'uniqueid'} =~ /([a-fA-F0-9]{2}:?){8}/)) {
@@ -200,24 +206,39 @@ sub sensors {
 		}
 	}
 
-	if (! $BATTERY) {
+	if (! $BATTERY && ! $HEAT) {
 		printf "%4s %-34s %-8s %s (%s)\n", "ID", "Name", "State", "Type", $TYPE;
 		print "################################################################################\n";
 	}
 	for my $uniqueid (sort keys %sensor) {
 		for my $key (sort { $a <=> $b } @{$sensor{$uniqueid}}) {
-			if (! $BATTERY) {
-				if ($sensor_objects->{$key}->{'type'} =~ /ZLLSwitch|ZLLPresence/) {
-					printf "%-39s (%s%%)\n", $sensor_objects->{$key}->{'name'}, $sensor_objects->{$key}->{'config'}->{'battery'};
-				}
-				printf "%4d %-43s %s\n", $key, $sensor_objects->{$key}->{'productname'}, $sensor_objects->{$key}->{'type'};
-			} else {
+			if ($BATTERY) {
 				if ($sensor_objects->{$key}->{'type'} =~ /ZLLSwitch|ZLLPresence/) {
 					if ($sensor_objects->{$key}->{'config'}->{'battery'} < $BATTERY) {
 						printf "%-32s battery level %s%%\n", $sensor_objects->{$key}->{'name'}, $sensor_objects->{$key}->{'config'}->{'battery'};
 					}
 				}	
 			}
+			elsif ($HEAT) {
+				if ($sensor_objects->{$key}->{'type'} =~ /ZLLPresence/) {
+					$name = $sensor_objects->{$key}->{'name'};
+				}	
+				if ($sensor_objects->{$key}->{'type'} =~ /ZLLTemperature/) {
+					if ($sensor_objects->{$key}->{'state'}->{'temperature'}) {
+						$temperature = ($sensor_objects->{$key}->{'state'}->{'temperature'} / 100);
+						printf "%-32s - %.1fC", $name, $temperature;
+						print " (updated: " . unpack('@11 A8', $sensor_objects->{$key}->{'state'}->{'lastupdated'}) . " UTC)";
+						print " - sensor $key" if $VERBOSE;
+						say "";
+					}
+				}
+			}
+			else {
+				if ($sensor_objects->{$key}->{'type'} =~ /ZLLSwitch|ZLLPresence/) {
+					printf "%-39s (%s%%)\n", $sensor_objects->{$key}->{'name'}, $sensor_objects->{$key}->{'config'}->{'battery'};
+				}
+				printf "%4d %-43s %s\n", $key, $sensor_objects->{$key}->{'productname'}, $sensor_objects->{$key}->{'type'};
+			}
 		}
 	}
 }
@@ -238,6 +259,8 @@ sub groups {
 sub daylight_trigger {
 	my $light = _get_state_for("lights", $RESOURCE_ID);
 	my $sensor = _get_state_for("sensors", $SENSOR_ID);
+	say "Dark: $sensor->{'dark'}, Daylight: $sensor->{'daylight'}, Light On: $light->{'on'}" if ($VERBOSE || $DEBUG);
+	#print (f"Dark: {sensor_state['dark']}, Daylight: {sensor_state['daylight']}, Light On: {sensor_state['daylight']}")
 	if ($sensor->{'dark'} && ! $light->{'on'}) {
 		_change_state_for("lights", $RESOURCE_ID, $ACTION);
 	}