Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use strict;
- use constant false => 0;
- use constant true => 1;
- # Format = MQTTINSTEON
- #
- my (%groups, %objects, %packages, %addresses);
- sub read_table_init_MQTTINSTEON {
- # reset known groups
- &::print_log("Initialized read_table_MQTTINSTEON.pl");
- # %groups=();
- %objects=();
- # %packages=();
- # %addresses=();
- }
- sub read_table_MQTTINSTEON {
- #print_log "in read_table_MQTTINSTEON!";
- my ($record) = @_;
- my $record_raw = $record;
- if($record =~ /^#/ or $record =~ /^\s*$/) {
- return;
- }
- $record =~ s/\s*#.*$//;
- my ($code, $set_object, $state_object, $object, $light_dimable, $brightness, $object_off, $object_low, $object_med, $object_hi);
- my ($broker_label, $broker_host, $broker_port, $broker_filter, $broker_username, $broker_password, $object_mqtt_path, $object_common_name);
- my (@item_info) = split(',\s*', $record);
- my $type = uc shift @item_info;
- #print_log "Looking for TYPE: $type";
- if ( $type eq "BROKER" ) {
- $code = "\n#BROKER: $record_raw\n";
- #BROKER, insteon, '127.0.0.1', 1883, 'insteon/#'
- ( $broker_label, $broker_host, $broker_port, $broker_filter, $broker_username, $broker_password ) = @item_info;
- $broker_filter =~ s/\*/#/g;
- $broker_host = "'$broker_host'" unless ($broker_host =~ m/^'/) || ($broker_host =~ m/^\$/);
- $code .= "\n";
- $code .= "\$MQTTobj_${broker_label} = new mqtt('mqtt_${broker_label}', $broker_host, $broker_port, '$broker_filter', '$broker_username', '$broker_password', 120);\n";
- } elsif ( $type eq "CODE" ) {
- ($object) = "$record_raw" =~ /CODE,\s+(.*)/;
- $code = "$object\n";
- $object = '';
- } elsif ( $type eq "SENSOR" ) {
- $code = "\n#SENSOR: $record_raw\n";
- ( $broker_label, $state_object, $object_mqtt_path, $object_common_name ) = @item_info;
- $object_mqtt_path = "sensors/${state_object}" unless $object_mqtt_path;
- $code .= "\n";
- $code .= "\$MQTT_insteon_${state_object}_state = new mqtt_Item(\$MQTTobj_${broker_label}, \"${object_mqtt_path}/state\", 1, 1);\n";
- $code .= "if(\$state = state_now \$${state_object}) {\n";
- $code .= " print_log(\"[MQTT_SEND] ${state_object}: \$state\");\n";
- $code .= " set \$MQTT_insteon_${state_object}_state \$state;\n";
- $code .= "}\n";
- $code .= "if(\$Startup || \$Reload) {mqtt_discovery_sensor_send('binary_sensor', 'Sensor', 'Sensors', '$object_mqtt_path', '$object_common_name', 'Sensor');}\n" if defined $object_common_name;
- } elsif ( $type eq "FAN" ) {
- $code = "\n#FAN: $record_raw\n";
- ( $broker_label, $state_object, $object_off, $object_low, $object_med, $object_hi, $object_mqtt_path, $object_common_name ) = @item_info;
- $object_mqtt_path = "fans/${state_object}" unless $object_mqtt_path;
- $code .= "\n";
- $code .= "\$MQTT_insteon_${state_object}_set = new mqtt_Item(\$MQTTobj_${broker_label}, \"${object_mqtt_path}\");\n";
- $code .= "\$MQTT_insteon_${state_object}_state = new mqtt_Item(\$MQTTobj_${broker_label}, \"${object_mqtt_path}/state\", 1, 1);\n";
- $code .= "\$MQTT_insteon_${state_object}_percentage_set = new mqtt_Item(\$MQTTobj_${broker_label}, \"${object_mqtt_path}/percentage\");\n";
- $code .= "\$MQTT_insteon_${state_object}_percentage_state = new mqtt_Item(\$MQTTobj_${broker_label}, \"${object_mqtt_path}/percentage/state\", 1, 1);\n";
- $code .= "if(my \$state = state_now \$MQTT_insteon_${state_object}_percentage_set) {\n";
- $code .= " print_log(\"[MQTT_RECV] ${state_object}: \$state\");\n";
- $code .= " set \$${object_hi} ON if \$state == 3;\n";
- $code .= " set \$${object_med} ON if \$state == 2;\n";
- $code .= " set \$${object_low} ON if \$state == 1;\n";
- $code .= " set \$${object_off} ON if \$state == OFF;\n";
- $code .= "}\n";
- $code .= "if(my \$state = state_now \$MQTT_insteon_${state_object}_set) {\n";
- $code .= " print_log(\"[MQTT_RECV] ${state_object}: \$state\");\n";
- $code .= " set \$${object_med} \$state if \$state == ON;\n";
- $code .= " set \$${object_off} \$state if \$state == OFF;\n";
- $code .= "}\n";
- $code .= "if(\$state = state_now \$${state_object}) {\n";
- $code .= " print_log(\"[MQTT] ${state_object} sate_now: \$state\");\n";
- $code .= " my \$brightness = \$state;\n";
- $code .= " \$brightness = '3' if \$brightness == ON;\n";
- $code .= " \$brightness = '3' if \$brightness == '100%';\n";
- $code .= " \$brightness = '2' if \$brightness == '60%';\n";
- $code .= " \$brightness = '1' if \$brightness == '30%';\n";
- $code .= " \$state = ON unless \$state == OFF;\n";
- $code .= " print_log(\"[MQTT_SEND] ${state_object}: \$state / \$brightness\");\n";
- $code .= " set \$MQTT_insteon_${state_object}_percentage_state \$brightness;\n";
- $code .= " set \$MQTT_insteon_${state_object}_state \$state;\n";
- $code .= "}\n";
- $code .= "if(\$Startup || \$Reload) {mqtt_discovery_fan_send('fan', '', 'fans', '$state_object', '$object_common_name', 'Fan');}\n" if defined $object_common_name;
- } elsif ( $type eq "LIGHT" ) {
- $code = "\n#LIGHT: $record_raw\n";
- ( $broker_label, $set_object, $state_object, $light_dimable, $object_mqtt_path, $object_common_name ) = @item_info;
- #$light_dimable = false unless $light_dimable;
- $object_mqtt_path = "lights/${set_object}" unless $object_mqtt_path;
- $code .= "\n";
- $code .= "\$MQTT_insteon_${set_object}_set = new mqtt_Item(\$MQTTobj_${broker_label}, \"${object_mqtt_path}\");\n";
- $code .= "\$MQTT_insteon_${set_object}_state = new mqtt_Item(\$MQTTobj_${broker_label}, \"${object_mqtt_path}/state\", 1, 1);
- ";
- $code .= "if(my \$state = state_now \$MQTT_insteon_${set_object}_set) {\n";
- $code .= " print_log(\"[MQTT_RECV] ${set_object}: \$state\");\n";
- $code .= " set \$${set_object} \$state;\n";
- $code .= "}\n";
- # print_log "light_dimable: $light_dimable";
- if($light_dimable eq 'true') {
- $code .= "\$MQTT_insteon_${set_object}_brightness_set = new mqtt_Item(\$MQTTobj_${broker_label}, \"${object_mqtt_path}/brightness\");\n";
- $code .= "\$MQTT_insteon_${set_object}_brightness_state = new mqtt_Item(\$MQTTobj_${broker_label}, \"${object_mqtt_path}/brightness/state\", 1, 1);
- ";
- $code .= "if(my \$state = state_now \$MQTT_insteon_${set_object}_brightness_set) {\n";
- $code .= " print_log(\"[MQTT_RECV_brightness] ${set_object}: \$state\");\n";
- $code .= " \$state = '100' if \$state == ON;\n";
- # $code .= " \$state .= '%';\n";
- $code .= " print_log(\"[MQTT] Setting ${state_object}: \$state\");\n";
- # $code .= " set \$${set_object} \$state;\n";
- $code .= " set \$${state_object} \$state;\n";
- $code .= "}\n";
- }
- $code .= "if(\$state = state_now \$${state_object}) {\n";
- if($light_dimable eq 'true') {
- $code .= " print_log(\"[MQTT] ${state_object} sate_now: \$state\");\n";
- $code .= " \$state =~ s/%\$//;\n"; #Remove % from the end
- $code .= " my \$brightness = \$state;\n";
- $code .= " \$brightness = '100' if \$brightness == ON;\n";
- $code .= " \$state = ON unless \$state == OFF;\n";
- $code .= " print_log(\"[MQTT_SEND_brightness] ${state_object}: \$state / \$brightness\");\n";
- $code .= " set \$MQTT_insteon_${set_object}_state \$state;\n";
- $code .= " set \$MQTT_insteon_${set_object}_brightness_state \$brightness;\n";
- } else {
- $code .= " \$state = ON if \$state == '100%';\n";
- $code .= " print_log(\"[MQTT_SEND] ${state_object}: \$state\");\n";
- $code .= " set \$MQTT_insteon_${set_object}_state \$state;\n";
- }
- $code .= "}\n";
- $code .= "if(\$Startup || \$Reload) {mqtt_discovery_switch_send('light', '', 'lights', '$state_object', '$object_common_name', 'Light', $light_dimable);}\n" if defined $object_common_name;;
- } else {
- print "\nUnrecognized .mht entry: $record\n";
- return;
- }
- return $code;
- }
- 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement