Skip to content

Commit

Permalink
Update Logic to allow for Spaces in names
Browse files Browse the repository at this point in the history
Strips spaces from Room, Device & Scene names
  • Loading branch information
sktaylortrash committed Sep 11, 2019
1 parent f192791 commit f881170
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 34 deletions.
50 changes: 28 additions & 22 deletions MQTTGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,23 @@
if ( isset( $DATA["rid"] ) ){ $DATA = array( $DATA ); }

foreach($DATA as $room){
$RoomName = str_replace(' ', '', $room['name']);
if( isset($room['rid'] ) ){
$DEVICES = array();

if ($mqtt->connect(true, NULL, $MQTTusername, $MQTTpassword)) {
$file_handle = fopen('mqtt_sub.py', 'a') or die('Error opening file.');
$data1 = "\n### ".$room['name']." \ndef on_message_".$room['name']."(client, userdata, msg):\n";
$data2 = " if (msg.payload.decode() == '0' or msg.payload.decode() == '1'):\n print (\"".$room['name']."\" + msg.payload.decode())\n";
$data1 = "\n### ".$RoomName." \ndef on_message_".$RoomName."(client, userdata, msg):\n";
$data2 = " if (msg.payload.decode() == '0' or msg.payload.decode() == '1'):\n print (\"".$RoomName."\" + msg.payload.decode())\n";
$data3 = " r = requests.get('".LOCAL_URL."/api.php?fx=toggle&type=room&uid=".$room['rid']."&val=' + msg.payload.decode())\n";
$data4 = " r.json()\n";
$data5 = " client.publish(\"".$MQTT_prefix."/".$room['name']."/".$room['rid']."/status\", msg.payload.decode()";
$data5 = " client.publish(\"".$MQTT_prefix."/".$RoomName."/".$room['rid']."/status\", msg.payload.decode()";
$data6 = ", 0, True)\n";
$data7 = "\ndef on_message_".$room['name']."_Bright(client, userdata, msg):\n";
$data8 = " print (\"".$room['name']." Brightness \" + msg.payload.decode())\n";
$data7 = "\ndef on_message_".$RoomName."_Bright(client, userdata, msg):\n";
$data8 = " print (\"".$RoomName." Brightness \" + msg.payload.decode())\n";
$data9 = " r = requests.get('".LOCAL_URL."/api.php?fx=dim&type=room&uid=".$room['rid']."&val=' + msg.payload.decode())\n";
$data10 = " r.json()\n\n";
$data11 = " client.publish(\"".$MQTT_prefix."/".$room['name']."/".$room['rid']."/brightness\", msg.payload.decode()";
$data11 = " client.publish(\"".$MQTT_prefix."/".$RoomName."/".$room['rid']."/brightness\", msg.payload.decode()";
$data12 = ", 0, True)\n";

fwrite($file_handle, $data1);
Expand Down Expand Up @@ -115,18 +116,19 @@
$roomDevices = 0;
if ($mqtt->connect(true, NULL, $MQTTusername, $MQTTpassword)) {
foreach($DEVICES as $device){
$DeviceName = str_replace(' ', '', $device['name']);
$file_handle = fopen('mqtt_sub.py', 'a') or die('Error opening file.');
$data1 = "\n### ".$room['name']."-".$device['name']." \ndef on_message_".$room['name']."_".$device['name']."(client, userdata, msg):\n";
$data2 = " if (msg.payload.decode() == '0' or msg.payload.decode() == '1'):\n print (\"".$room['name']." ".$device['name']."\" + msg.payload.decode())\n";
$data1 = "\n### ".$RoomName."-".$DeviceName." \ndef on_message_".$RoomName."_".$DeviceName."(client, userdata, msg):\n";
$data2 = " if (msg.payload.decode() == '0' or msg.payload.decode() == '1'):\n print (\"".$RoomName." ".$DeviceName."\" + msg.payload.decode())\n";
$data3 = " r = requests.get('".LOCAL_URL."/api.php?fx=toggle&type=device&uid=".$device['did']."&val=' + msg.payload.decode())\n";
$data4 = " r.json()\n";
$data5 = " client.publish(\"".$MQTT_prefix."/".$room['name']."/".$device['name']."/".$device['did']."/status\", msg.payload.decode()";
$data5 = " client.publish(\"".$MQTT_prefix."/".$RoomName."/".$DeviceName."/".$device['did']."/status\", msg.payload.decode()";
$data6 = ", 0, True)\n";
$data7 = "\ndef on_message_".$room['name']."_".$device['name']."_Bright(client, userdata, msg):\n";
$data8 = " print (\"".$room['name']." ".$device['name']." Brightness \" + msg.payload.decode())\n";
$data7 = "\ndef on_message_".$RoomName."_".$DeviceName."_Bright(client, userdata, msg):\n";
$data8 = " print (\"".$RoomName." ".$DeviceName." Brightness \" + msg.payload.decode())\n";
$data9 = " r = requests.get('".LOCAL_URL."/api.php?fx=dim&type=device&uid=".$device['did']."&val=' + msg.payload.decode())\n";
$data10 = " r.json()\n\n";
$data11 = " client.publish(\"".$MQTT_prefix."/".$room['name']."/".$device['name']."/".$device['did']."/brightness\", msg.payload.decode()";
$data11 = " client.publish(\"".$MQTT_prefix."/".$RoomName."/".$DeviceName."/".$device['did']."/brightness\", msg.payload.decode()";
$data12 = ", 0, True)\n";

fwrite($file_handle, $data1);
Expand Down Expand Up @@ -163,14 +165,15 @@
$scenes = $sarray["scene"];
if( is_array($scenes) ){
foreach($scenes as $scene){
$SceneName = str_replace(' ', '', $scene['name']);
// for($x = 0; $x < sizeof($scenes); $x++){
if ($mqtt->connect(true, NULL, $MQTTusername, $MQTTpassword)) {
$file_handle = fopen('mqtt_sub.py', 'a') or die('Error opening file.');
$data1 = "\n### ".$scene['name']." \ndef on_message_".$scene['name']."(client, userdata, msg):\n";
$data2 = " if (msg.payload.decode() == '0' or msg.payload.decode() == '1'):\n print (\"".$scene['name']."\" + msg.payload.decode())\n";
$data1 = "\n### ".$SceneName." \ndef on_message_".$SceneName."(client, userdata, msg):\n";
$data2 = " if (msg.payload.decode() == '0' or msg.payload.decode() == '1'):\n print (\"".$SceneName."\" + msg.payload.decode())\n";
$data3 = " r = requests.get('".LOCAL_URL."/api.php?fx=scene&uid=".$scene['sid']."&type=' + msg.payload.decode())\n";
$data4 = " r.json()\n";
$data5 = " client.publish(\"".$MQTT_prefix."/".$scene['name']."/".$scene['sid']."/status\", msg.payload.decode()";
$data5 = " client.publish(\"".$MQTT_prefix."/".$SceneName."/".$scene['sid']."/status\", msg.payload.decode()";
$data6 = ", 0, True)\n";

fwrite($file_handle, $data1);
Expand Down Expand Up @@ -202,15 +205,16 @@
if ( isset( $DATA["rid"] ) ){ $DATA = array( $DATA ); }

foreach($DATA as $room){
$RoomName = str_replace(' ', '', $room['name']);
if( isset($room['rid'] ) ){
$DEVICES = array();

if ($mqtt->connect(true, NULL, $MQTTusername, $MQTTpassword)) {
foreach($DEVICES as $device){
$file_handle = fopen('mqtt_sub.py', 'a') or die('Error opening file.');
$data1 = "\n### ".$room['name']." Begin\n";
$data2 = "client.message_callback_add('".$MQTT_prefix."/".$room['name']."/".$room['rid']."/switch', on_message_".$room['name'].")\n";
$data3 = "client.message_callback_add('".$MQTT_prefix."/".$room['name']."/".$room['rid']."/brightness/set', on_message_".$room['name']."_Bright)\n";
$data1 = "\n### ".$RoomName." Begin\n";
$data2 = "client.message_callback_add('".$MQTT_prefix."/".$RoomName."/".$room['rid']."/switch', on_message_".$RoomName.")\n";
$data3 = "client.message_callback_add('".$MQTT_prefix."/".$RoomName."/".$room['rid']."/brightness/set', on_message_".$RoomName."_Bright)\n";
fwrite($file_handle, $data1);
fwrite($file_handle, $data2);
fwrite($file_handle, $data3);
Expand Down Expand Up @@ -245,10 +249,11 @@
$roomDevices = 0;
if ($mqtt->connect(true, NULL, $MQTTusername, $MQTTpassword)) {
foreach($DEVICES as $device){
$DeviceName = str_replace(' ', '', $device['name']);
$file_handle = fopen('mqtt_sub.py', 'a') or die('Error opening file.');
$data1 = "\n### ".$room['name']."-".$device['name']." Begin\n";
$data2 = "client.message_callback_add('".$MQTT_prefix."/".$room['name']."/".$device['name']."/".$device['did']."/switch', on_message_".$room['name']."_".$device['name'].")\n";
$data3 = "client.message_callback_add('".$MQTT_prefix."/".$room['name']."/".$device['name']."/".$device['did']."/brightness/set', on_message_".$room['name']."_".$device['name']."_Bright)\n";
$data1 = "\n### ".$RoomName."-".$device['name']." Begin\n";
$data2 = "client.message_callback_add('".$MQTT_prefix."/".$RoomName."/".$DeviceName."/".$device['did']."/switch', on_message_".$RoomName."_".$DeviceName.")\n";
$data3 = "client.message_callback_add('".$MQTT_prefix."/".$RoomName."/".$DeviceName."/".$device['did']."/brightness/set', on_message_".$RoomName."_".$DeviceName."_Bright)\n";
fwrite($file_handle, $data1);
fwrite($file_handle, $data2);
fwrite($file_handle, $data3);
Expand Down Expand Up @@ -277,11 +282,12 @@
$scenes = $sarray["scene"];
if( is_array($scenes) ){
foreach($scenes as $scene){
$SceneName = str_replace(' ', '', $scene['name']);
// for($x = 0; $x < sizeof($scenes); $x++){
if ($mqtt->connect(true, NULL, $MQTTusername, $MQTTpassword)) {
$file_handle = fopen('mqtt_sub.py', 'a') or die('Error opening file.');
$data1 = "\n### ".$scene['name']." Begin\n";
$data2 = "client.message_callback_add('".$MQTT_prefix."/".$scene['name']."/".$scene['sid']."/switch', on_message_".$scene['name'].")\n";
$data2 = "client.message_callback_add('".$MQTT_prefix."/".$SceneName."/".$scene['sid']."/switch', on_message_".$scene['name'].")\n";
fwrite($file_handle, $data1);
fwrite($file_handle, $data2);
fclose($file_handle);
Expand Down
12 changes: 6 additions & 6 deletions config.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

define("LIGTHING_BRIDGE_IP", "192.168.1.TCP"); // IP address of TCP Bridge/Gateway
define("LIGHTING_BRIDGE_PORT", "443"); // 443 for new firmware, 80 for legacy - If you don't know, leave it at 443
define("LOCAL_URL", "http://localhost"); // Address of your webserver running this - this is used in runSchedule to call the API
define("LOCAL_URL", "http://lighting.local"); // Address of your webserver running this - this is used in runSchedule to call the API

define("USER_EMAIL", "[email protected]"); // I think this is so you dont have to regenerate tokens if you run this script elsewhere
define("USER_PASSWORD", "can-be-anything"); // can be anything
Expand Down Expand Up @@ -79,9 +79,9 @@
$MQTTport = 1883; // Change as necessary
$MQTTusername = "admin"; // Set your username
$MQTTpassword = "password"; // set your password
$MQTTsub_id = "tcp-subscriber"; // make sure this is unique for connecting to sever - you could use uniqid()
$MQTTpub_id = "tcp-publisher"; // make sure this is unique for connecting to sever - you could use uniqid()
$MQTT_prefix = "light"; // Topic prefix for lights
$ENABLE_HA_DISCO = 1; // Enable MQTT Publishing of Home Assistant Discovery Topics
$HASSTopic_id = "homeassistant"; // Topice prefix for Home Assistant Discovery Topics
$MQTTsub_id = "tcp-subscriber"; // Make sure this is unique for connecting to server - you could use uniqid()
$MQTTpub_id = "tcp-publisher"; // Make sure this is unique for connecting to server - you could use uniqid()
$MQTT_prefix = "light"; // Topic prefix for lights - ie light/<room-name>/<light-name>/<UniqueBulbID>
$ENABLE_HA_DISCO = 1; // Enable MQTT Publishing of Home Assistant Discovery Topics (1 = true, 0 = false)
$HASSTopic_id = "homeassistant"; // Topic prefix for Home Assistant Discovery Topics - this must match with HASS
?>
5 changes: 3 additions & 2 deletions mqttdiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
if ( isset( $DATA["rid"] ) ){ $DATA = array( $DATA ); }

foreach($DATA as $room){

$RoomName = str_replace(' ', '', $room['name']);
if( isset($room['rid'] ) ){
$DEVICES = array();

Expand Down Expand Up @@ -90,7 +90,8 @@
$roomDevices = 0;
if ($mqtt->connect(true, retain, $MQTTusername, $MQTTpassword)) {
foreach($DEVICES as $device){
$DeviceCommand = $room['name']."/".$device['name']."/".$device['did'];
$DeviceName = str_replace(' ', '', $device['name']);
$DeviceCommand = $RoomName."/".$DeviceName."/".$device['did'];
$myObj->name = $device["name"];
$myObj->command_topic = $MQTT_prefix."/".$DeviceCommand."/switch";
$myObj->state_topic = $MQTT_prefix."/".$DeviceCommand."/status";
Expand Down
9 changes: 5 additions & 4 deletions mqttstate.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
if ( isset( $DATA["rid"] ) ){ $DATA = array( $DATA ); }

foreach($DATA as $room){

$RoomName = str_replace(' ', '', $room['name']);
if( isset($room['rid'] ) ){
$DEVICES = array();

Expand Down Expand Up @@ -87,9 +87,10 @@
$roomDevices = 0;
if ($mqtt->connect(true, retain, $MQTTusername, $MQTTpassword)) {
foreach($DEVICES as $device){
$mqtt->publish($MQTT_prefix.'/'.$room["name"].'/'.$device["name"].'/'.$device['did'].'/status', $device['state']);
$mqtt->publish($MQTT_prefix.'/'.$room["name"].'/'.$device["name"].'/'.$device['did'].'/brightness', $device['level']);
echo $device["name"].'- State: '.$device["state"].' Brightness:'.$device["level"].'<br>';
$DeviceName = str_replace(' ', '', $device['name']);
$mqtt->publish($MQTT_prefix.'/'.$RoomName.'/'.$DeviceName.'/'.$device['did'].'/status', $device['state']);
$mqtt->publish($MQTT_prefix.'/'.$RoomName.'/'.$DeviceName.'/'.$device['did'].'/brightness', $device['level']);
echo $DeviceName.'- State: '.$device["state"].' Brightness:'.$device["level"].'<br>';
}
$mqtt->close();
} else {
Expand Down

0 comments on commit f881170

Please sign in to comment.