Skip to content

Commit f881170

Browse files
committed
Update Logic to allow for Spaces in names
Strips spaces from Room, Device & Scene names
1 parent f192791 commit f881170

File tree

4 files changed

+42
-34
lines changed

4 files changed

+42
-34
lines changed

MQTTGenerator.php

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,23 @@
5555
if ( isset( $DATA["rid"] ) ){ $DATA = array( $DATA ); }
5656

5757
foreach($DATA as $room){
58+
$RoomName = str_replace(' ', '', $room['name']);
5859
if( isset($room['rid'] ) ){
5960
$DEVICES = array();
6061

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

7677
fwrite($file_handle, $data1);
@@ -115,18 +116,19 @@
115116
$roomDevices = 0;
116117
if ($mqtt->connect(true, NULL, $MQTTusername, $MQTTpassword)) {
117118
foreach($DEVICES as $device){
119+
$DeviceName = str_replace(' ', '', $device['name']);
118120
$file_handle = fopen('mqtt_sub.py', 'a') or die('Error opening file.');
119-
$data1 = "\n### ".$room['name']."-".$device['name']." \ndef on_message_".$room['name']."_".$device['name']."(client, userdata, msg):\n";
120-
$data2 = " if (msg.payload.decode() == '0' or msg.payload.decode() == '1'):\n print (\"".$room['name']." ".$device['name']."\" + msg.payload.decode())\n";
121+
$data1 = "\n### ".$RoomName."-".$DeviceName." \ndef on_message_".$RoomName."_".$DeviceName."(client, userdata, msg):\n";
122+
$data2 = " if (msg.payload.decode() == '0' or msg.payload.decode() == '1'):\n print (\"".$RoomName." ".$DeviceName."\" + msg.payload.decode())\n";
121123
$data3 = " r = requests.get('".LOCAL_URL."/api.php?fx=toggle&type=device&uid=".$device['did']."&val=' + msg.payload.decode())\n";
122124
$data4 = " r.json()\n";
123-
$data5 = " client.publish(\"".$MQTT_prefix."/".$room['name']."/".$device['name']."/".$device['did']."/status\", msg.payload.decode()";
125+
$data5 = " client.publish(\"".$MQTT_prefix."/".$RoomName."/".$DeviceName."/".$device['did']."/status\", msg.payload.decode()";
124126
$data6 = ", 0, True)\n";
125-
$data7 = "\ndef on_message_".$room['name']."_".$device['name']."_Bright(client, userdata, msg):\n";
126-
$data8 = " print (\"".$room['name']." ".$device['name']." Brightness \" + msg.payload.decode())\n";
127+
$data7 = "\ndef on_message_".$RoomName."_".$DeviceName."_Bright(client, userdata, msg):\n";
128+
$data8 = " print (\"".$RoomName." ".$DeviceName." Brightness \" + msg.payload.decode())\n";
127129
$data9 = " r = requests.get('".LOCAL_URL."/api.php?fx=dim&type=device&uid=".$device['did']."&val=' + msg.payload.decode())\n";
128130
$data10 = " r.json()\n\n";
129-
$data11 = " client.publish(\"".$MQTT_prefix."/".$room['name']."/".$device['name']."/".$device['did']."/brightness\", msg.payload.decode()";
131+
$data11 = " client.publish(\"".$MQTT_prefix."/".$RoomName."/".$DeviceName."/".$device['did']."/brightness\", msg.payload.decode()";
130132
$data12 = ", 0, True)\n";
131133

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

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

204207
foreach($DATA as $room){
208+
$RoomName = str_replace(' ', '', $room['name']);
205209
if( isset($room['rid'] ) ){
206210
$DEVICES = array();
207211

208212
if ($mqtt->connect(true, NULL, $MQTTusername, $MQTTpassword)) {
209213
foreach($DEVICES as $device){
210214
$file_handle = fopen('mqtt_sub.py', 'a') or die('Error opening file.');
211-
$data1 = "\n### ".$room['name']." Begin\n";
212-
$data2 = "client.message_callback_add('".$MQTT_prefix."/".$room['name']."/".$room['rid']."/switch', on_message_".$room['name'].")\n";
213-
$data3 = "client.message_callback_add('".$MQTT_prefix."/".$room['name']."/".$room['rid']."/brightness/set', on_message_".$room['name']."_Bright)\n";
215+
$data1 = "\n### ".$RoomName." Begin\n";
216+
$data2 = "client.message_callback_add('".$MQTT_prefix."/".$RoomName."/".$room['rid']."/switch', on_message_".$RoomName.")\n";
217+
$data3 = "client.message_callback_add('".$MQTT_prefix."/".$RoomName."/".$room['rid']."/brightness/set', on_message_".$RoomName."_Bright)\n";
214218
fwrite($file_handle, $data1);
215219
fwrite($file_handle, $data2);
216220
fwrite($file_handle, $data3);
@@ -245,10 +249,11 @@
245249
$roomDevices = 0;
246250
if ($mqtt->connect(true, NULL, $MQTTusername, $MQTTpassword)) {
247251
foreach($DEVICES as $device){
252+
$DeviceName = str_replace(' ', '', $device['name']);
248253
$file_handle = fopen('mqtt_sub.py', 'a') or die('Error opening file.');
249-
$data1 = "\n### ".$room['name']."-".$device['name']." Begin\n";
250-
$data2 = "client.message_callback_add('".$MQTT_prefix."/".$room['name']."/".$device['name']."/".$device['did']."/switch', on_message_".$room['name']."_".$device['name'].")\n";
251-
$data3 = "client.message_callback_add('".$MQTT_prefix."/".$room['name']."/".$device['name']."/".$device['did']."/brightness/set', on_message_".$room['name']."_".$device['name']."_Bright)\n";
254+
$data1 = "\n### ".$RoomName."-".$device['name']." Begin\n";
255+
$data2 = "client.message_callback_add('".$MQTT_prefix."/".$RoomName."/".$DeviceName."/".$device['did']."/switch', on_message_".$RoomName."_".$DeviceName.")\n";
256+
$data3 = "client.message_callback_add('".$MQTT_prefix."/".$RoomName."/".$DeviceName."/".$device['did']."/brightness/set', on_message_".$RoomName."_".$DeviceName."_Bright)\n";
252257
fwrite($file_handle, $data1);
253258
fwrite($file_handle, $data2);
254259
fwrite($file_handle, $data3);
@@ -277,11 +282,12 @@
277282
$scenes = $sarray["scene"];
278283
if( is_array($scenes) ){
279284
foreach($scenes as $scene){
285+
$SceneName = str_replace(' ', '', $scene['name']);
280286
// for($x = 0; $x < sizeof($scenes); $x++){
281287
if ($mqtt->connect(true, NULL, $MQTTusername, $MQTTpassword)) {
282288
$file_handle = fopen('mqtt_sub.py', 'a') or die('Error opening file.');
283289
$data1 = "\n### ".$scene['name']." Begin\n";
284-
$data2 = "client.message_callback_add('".$MQTT_prefix."/".$scene['name']."/".$scene['sid']."/switch', on_message_".$scene['name'].")\n";
290+
$data2 = "client.message_callback_add('".$MQTT_prefix."/".$SceneName."/".$scene['sid']."/switch', on_message_".$scene['name'].")\n";
285291
fwrite($file_handle, $data1);
286292
fwrite($file_handle, $data2);
287293
fclose($file_handle);

config.inc.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

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

88
define("USER_EMAIL", "[email protected]"); // I think this is so you dont have to regenerate tokens if you run this script elsewhere
99
define("USER_PASSWORD", "can-be-anything"); // can be anything
@@ -79,9 +79,9 @@
7979
$MQTTport = 1883; // Change as necessary
8080
$MQTTusername = "admin"; // Set your username
8181
$MQTTpassword = "password"; // set your password
82-
$MQTTsub_id = "tcp-subscriber"; // make sure this is unique for connecting to sever - you could use uniqid()
83-
$MQTTpub_id = "tcp-publisher"; // make sure this is unique for connecting to sever - you could use uniqid()
84-
$MQTT_prefix = "light"; // Topic prefix for lights
85-
$ENABLE_HA_DISCO = 1; // Enable MQTT Publishing of Home Assistant Discovery Topics
86-
$HASSTopic_id = "homeassistant"; // Topice prefix for Home Assistant Discovery Topics
82+
$MQTTsub_id = "tcp-subscriber"; // Make sure this is unique for connecting to server - you could use uniqid()
83+
$MQTTpub_id = "tcp-publisher"; // Make sure this is unique for connecting to server - you could use uniqid()
84+
$MQTT_prefix = "light"; // Topic prefix for lights - ie light/<room-name>/<light-name>/<UniqueBulbID>
85+
$ENABLE_HA_DISCO = 1; // Enable MQTT Publishing of Home Assistant Discovery Topics (1 = true, 0 = false)
86+
$HASSTopic_id = "homeassistant"; // Topic prefix for Home Assistant Discovery Topics - this must match with HASS
8787
?>

mqttdiscovery.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
if ( isset( $DATA["rid"] ) ){ $DATA = array( $DATA ); }
6363

6464
foreach($DATA as $room){
65-
65+
$RoomName = str_replace(' ', '', $room['name']);
6666
if( isset($room['rid'] ) ){
6767
$DEVICES = array();
6868

@@ -90,7 +90,8 @@
9090
$roomDevices = 0;
9191
if ($mqtt->connect(true, retain, $MQTTusername, $MQTTpassword)) {
9292
foreach($DEVICES as $device){
93-
$DeviceCommand = $room['name']."/".$device['name']."/".$device['did'];
93+
$DeviceName = str_replace(' ', '', $device['name']);
94+
$DeviceCommand = $RoomName."/".$DeviceName."/".$device['did'];
9495
$myObj->name = $device["name"];
9596
$myObj->command_topic = $MQTT_prefix."/".$DeviceCommand."/switch";
9697
$myObj->state_topic = $MQTT_prefix."/".$DeviceCommand."/status";

mqttstate.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
if ( isset( $DATA["rid"] ) ){ $DATA = array( $DATA ); }
6060

6161
foreach($DATA as $room){
62-
62+
$RoomName = str_replace(' ', '', $room['name']);
6363
if( isset($room['rid'] ) ){
6464
$DEVICES = array();
6565

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

0 commit comments

Comments
 (0)