Advertisement
Guest User

Untitled

a guest
Dec 4th, 2024
46
0
198 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.33 KB | None | 0 0
  1. use strict;
  2.  
  3. use constant false => 0;
  4. use constant true => 1;
  5.  
  6. # Format = MQTTINSTEON
  7. #
  8.  
  9.  
  10. my (%groups, %objects, %packages, %addresses);
  11.  
  12. sub read_table_init_MQTTINSTEON {
  13.  
  14. # reset known groups
  15. &::print_log("Initialized read_table_MQTTINSTEON.pl");
  16. # %groups=();
  17. %objects=();
  18. # %packages=();
  19. # %addresses=();
  20. }
  21.  
  22. sub read_table_MQTTINSTEON {
  23. #print_log "in read_table_MQTTINSTEON!";
  24. my ($record) = @_;
  25. my $record_raw = $record;
  26.  
  27. if($record =~ /^#/ or $record =~ /^\s*$/) {
  28. return;
  29. }
  30. $record =~ s/\s*#.*$//;
  31.  
  32. my ($code, $set_object, $state_object, $object, $light_dimable, $brightness, $object_off, $object_low, $object_med, $object_hi);
  33. my ($broker_label, $broker_host, $broker_port, $broker_filter, $broker_username, $broker_password, $object_mqtt_path, $object_common_name);
  34. my (@item_info) = split(',\s*', $record);
  35. my $type = uc shift @item_info;
  36.  
  37. #print_log "Looking for TYPE: $type";
  38.  
  39. if ( $type eq "BROKER" ) {
  40. $code = "\n#BROKER: $record_raw\n";
  41.  
  42. #BROKER, insteon, '127.0.0.1', 1883, 'insteon/#'
  43. ( $broker_label, $broker_host, $broker_port, $broker_filter, $broker_username, $broker_password ) = @item_info;
  44.  
  45. $broker_filter =~ s/\*/#/g;
  46. $broker_host = "'$broker_host'" unless ($broker_host =~ m/^'/) || ($broker_host =~ m/^\$/);
  47.  
  48. $code .= "\n";
  49. $code .= "\$MQTTobj_${broker_label} = new mqtt('mqtt_${broker_label}', $broker_host, $broker_port, '$broker_filter', '$broker_username', '$broker_password', 120);\n";
  50.  
  51. } elsif ( $type eq "CODE" ) {
  52. ($object) = "$record_raw" =~ /CODE,\s+(.*)/;
  53. $code = "$object\n";
  54. $object = '';
  55.  
  56. } elsif ( $type eq "SENSOR" ) {
  57. $code = "\n#SENSOR: $record_raw\n";
  58.  
  59. ( $broker_label, $state_object, $object_mqtt_path, $object_common_name ) = @item_info;
  60.  
  61. $object_mqtt_path = "sensors/${state_object}" unless $object_mqtt_path;
  62.  
  63. $code .= "\n";
  64.  
  65. $code .= "\$MQTT_insteon_${state_object}_state = new mqtt_Item(\$MQTTobj_${broker_label}, \"${object_mqtt_path}/state\", 1, 1);\n";
  66.  
  67. $code .= "if(\$state = state_now \$${state_object}) {\n";
  68. $code .= " print_log(\"[MQTT_SEND] ${state_object}: \$state\");\n";
  69. $code .= " set \$MQTT_insteon_${state_object}_state \$state;\n";
  70. $code .= "}\n";
  71.  
  72. $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;
  73.  
  74. } elsif ( $type eq "FAN" ) {
  75. $code = "\n#FAN: $record_raw\n";
  76.  
  77. ( $broker_label, $state_object, $object_off, $object_low, $object_med, $object_hi, $object_mqtt_path, $object_common_name ) = @item_info;
  78.  
  79. $object_mqtt_path = "fans/${state_object}" unless $object_mqtt_path;
  80.  
  81. $code .= "\n";
  82. $code .= "\$MQTT_insteon_${state_object}_set = new mqtt_Item(\$MQTTobj_${broker_label}, \"${object_mqtt_path}\");\n";
  83. $code .= "\$MQTT_insteon_${state_object}_state = new mqtt_Item(\$MQTTobj_${broker_label}, \"${object_mqtt_path}/state\", 1, 1);\n";
  84. $code .= "\$MQTT_insteon_${state_object}_percentage_set = new mqtt_Item(\$MQTTobj_${broker_label}, \"${object_mqtt_path}/percentage\");\n";
  85. $code .= "\$MQTT_insteon_${state_object}_percentage_state = new mqtt_Item(\$MQTTobj_${broker_label}, \"${object_mqtt_path}/percentage/state\", 1, 1);\n";
  86.  
  87. $code .= "if(my \$state = state_now \$MQTT_insteon_${state_object}_percentage_set) {\n";
  88. $code .= " print_log(\"[MQTT_RECV] ${state_object}: \$state\");\n";
  89. $code .= " set \$${object_hi} ON if \$state == 3;\n";
  90. $code .= " set \$${object_med} ON if \$state == 2;\n";
  91. $code .= " set \$${object_low} ON if \$state == 1;\n";
  92. $code .= " set \$${object_off} ON if \$state == OFF;\n";
  93. $code .= "}\n";
  94.  
  95. $code .= "if(my \$state = state_now \$MQTT_insteon_${state_object}_set) {\n";
  96. $code .= " print_log(\"[MQTT_RECV] ${state_object}: \$state\");\n";
  97. $code .= " set \$${object_med} \$state if \$state == ON;\n";
  98. $code .= " set \$${object_off} \$state if \$state == OFF;\n";
  99. $code .= "}\n";
  100.  
  101. $code .= "if(\$state = state_now \$${state_object}) {\n";
  102. $code .= " print_log(\"[MQTT] ${state_object} sate_now: \$state\");\n";
  103. $code .= " my \$brightness = \$state;\n";
  104. $code .= " \$brightness = '3' if \$brightness == ON;\n";
  105. $code .= " \$brightness = '3' if \$brightness == '100%';\n";
  106. $code .= " \$brightness = '2' if \$brightness == '60%';\n";
  107. $code .= " \$brightness = '1' if \$brightness == '30%';\n";
  108. $code .= " \$state = ON unless \$state == OFF;\n";
  109. $code .= " print_log(\"[MQTT_SEND] ${state_object}: \$state / \$brightness\");\n";
  110. $code .= " set \$MQTT_insteon_${state_object}_percentage_state \$brightness;\n";
  111. $code .= " set \$MQTT_insteon_${state_object}_state \$state;\n";
  112. $code .= "}\n";
  113.  
  114. $code .= "if(\$Startup || \$Reload) {mqtt_discovery_fan_send('fan', '', 'fans', '$state_object', '$object_common_name', 'Fan');}\n" if defined $object_common_name;
  115.  
  116. } elsif ( $type eq "LIGHT" ) {
  117. $code = "\n#LIGHT: $record_raw\n";
  118.  
  119. ( $broker_label, $set_object, $state_object, $light_dimable, $object_mqtt_path, $object_common_name ) = @item_info;
  120. #$light_dimable = false unless $light_dimable;
  121.  
  122. $object_mqtt_path = "lights/${set_object}" unless $object_mqtt_path;
  123.  
  124. $code .= "\n";
  125. $code .= "\$MQTT_insteon_${set_object}_set = new mqtt_Item(\$MQTTobj_${broker_label}, \"${object_mqtt_path}\");\n";
  126. $code .= "\$MQTT_insteon_${set_object}_state = new mqtt_Item(\$MQTTobj_${broker_label}, \"${object_mqtt_path}/state\", 1, 1);
  127. ";
  128. $code .= "if(my \$state = state_now \$MQTT_insteon_${set_object}_set) {\n";
  129. $code .= " print_log(\"[MQTT_RECV] ${set_object}: \$state\");\n";
  130. $code .= " set \$${set_object} \$state;\n";
  131. $code .= "}\n";
  132. # print_log "light_dimable: $light_dimable";
  133. if($light_dimable eq 'true') {
  134. $code .= "\$MQTT_insteon_${set_object}_brightness_set = new mqtt_Item(\$MQTTobj_${broker_label}, \"${object_mqtt_path}/brightness\");\n";
  135. $code .= "\$MQTT_insteon_${set_object}_brightness_state = new mqtt_Item(\$MQTTobj_${broker_label}, \"${object_mqtt_path}/brightness/state\", 1, 1);
  136. ";
  137. $code .= "if(my \$state = state_now \$MQTT_insteon_${set_object}_brightness_set) {\n";
  138. $code .= " print_log(\"[MQTT_RECV_brightness] ${set_object}: \$state\");\n";
  139. $code .= " \$state = '100' if \$state == ON;\n";
  140. # $code .= " \$state .= '%';\n";
  141. $code .= " print_log(\"[MQTT] Setting ${state_object}: \$state\");\n";
  142. # $code .= " set \$${set_object} \$state;\n";
  143. $code .= " set \$${state_object} \$state;\n";
  144. $code .= "}\n";
  145. }
  146. $code .= "if(\$state = state_now \$${state_object}) {\n";
  147. if($light_dimable eq 'true') {
  148. $code .= " print_log(\"[MQTT] ${state_object} sate_now: \$state\");\n";
  149. $code .= " \$state =~ s/%\$//;\n"; #Remove % from the end
  150. $code .= " my \$brightness = \$state;\n";
  151. $code .= " \$brightness = '100' if \$brightness == ON;\n";
  152. $code .= " \$state = ON unless \$state == OFF;\n";
  153. $code .= " print_log(\"[MQTT_SEND_brightness] ${state_object}: \$state / \$brightness\");\n";
  154. $code .= " set \$MQTT_insteon_${set_object}_state \$state;\n";
  155. $code .= " set \$MQTT_insteon_${set_object}_brightness_state \$brightness;\n";
  156. } else {
  157. $code .= " \$state = ON if \$state == '100%';\n";
  158. $code .= " print_log(\"[MQTT_SEND] ${state_object}: \$state\");\n";
  159. $code .= " set \$MQTT_insteon_${set_object}_state \$state;\n";
  160. }
  161. $code .= "}\n";
  162.  
  163. $code .= "if(\$Startup || \$Reload) {mqtt_discovery_switch_send('light', '', 'lights', '$state_object', '$object_common_name', 'Light', $light_dimable);}\n" if defined $object_common_name;;
  164.  
  165. } else {
  166. print "\nUnrecognized .mht entry: $record\n";
  167. return;
  168. }
  169.  
  170. return $code;
  171. }
  172.  
  173. 1;
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement