forked from sktaylortrash/TCPLightingWebInterface-MQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqttdiscovery.php
131 lines (108 loc) · 3.93 KB
/
mqttdiscovery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
include "include.php";
require("phpMQTT/phpMQTT.php");
/*
*
* TCP Ligthing Web UI
*
*/
if( TOKEN != "" ){
if($ENABLE_MQTT == 0){
echo "MQTT is not Enabled";
} elseif($ENABLE_HA_DISCO == 0) {
echo "Home Assistant Discovery is not Enabled";
} else {
$mqtt = new phpMQTT($MQTTserver, $MQTTport, $MQTTpub_id);
//Get State of System Data
$CMD = "cmd=GWRBatch&data=<gwrcmds><gwrcmd><gcmd>RoomGetCarousel</gcmd><gdata><gip><version>1</version><token>".TOKEN."</token><fields>name,image,imageurl,control,power,product,class,realtype,status</fields></gip></gdata></gwrcmd></gwrcmds>&fmt=xml";
$result = getCurlReturn($CMD);
$array = xmlToArray($result);
//check if token is expired
if( !isset($array["gwrcmd"]) ){
echo '<p>GWR Command not returned, this likely indicates your token is expired, or invalid.<p>';
echo '<p>Remove token and try regenerating a new one.</p>';
//unlink old token file
if( file_exists("tcp.token") ){
if( unlink("tcp.token") ){
echo "<p>Successfully deleted expired token file</p>";
}
}
if(USE_TOKEN_FILE){
echo '<p>If you are continuously seeing this message, ensure the folder is writeable or that tcp.token is writeable</p>';
}
pageFooter();
exit;
}
if( isset( $array["gwrcmd"]["gdata"]["gip"]["room"] ) ){
$DATA = $array["gwrcmd"]["gdata"]["gip"]["room"];
}else{
echo "No Room Data";
pa( $array );
$DATA = array();
pageFooter();
exit;
}
$deviceCount = 0;
if( sizeof($DATA) > 0 ){
if ( isset( $DATA["rid"] ) ){ $DATA = array( $DATA ); }
foreach($DATA as $room){
$RoomName = str_replace(' ', '_', $room['name']);
if( isset($room['rid'] ) ){
$DEVICES = array();
if( ! is_array($room["device"]) ){
}else{
$device = (array)$room["device"];
if( isset($device["did"]) ){
//item is singular device
$DEVICES[] = $room["device"];
$deviceCount++;
}else{
for( $x = 0; $x < sizeof($device); $x++ ){
if( isset($device[$x]) && is_array($device[$x]) && ! empty($device[$x]) ){
$DEVICES[] = $device[$x];
$deviceCount++;
}
}
}
}
if( sizeof($DEVICES) > 0 ){
$unplugged = 0;
$roomBrightness = 0;
$roomDevices = 0;
if ($mqtt->connect(true, retain, $MQTTusername, $MQTTpassword)) {
foreach($DEVICES as $device){
$DeviceName = str_replace(' ', '_', $device['name']);
$DeviceCommand = $RoomName."/".$DeviceName."/".$device['did'];
$myObj->name = $device["name"];
$myObj->unique_id = $device['did'];
$myObj->command_topic = $MQTT_prefix."/".$DeviceCommand."/switch";
$myObj->state_topic = $MQTT_prefix."/".$DeviceCommand."/status";
$myObj->brightness_command_topic = $MQTT_prefix."/".$DeviceCommand."/brightness/set";
$myObj->brightness_state_topic = $MQTT_prefix."/".$DeviceCommand."/brightness";
$myObj->availability_topic = $MQTT_prefix."/".$DeviceCommand."/LWT";
$myObj->brightness_scale = 100;
$myObj->qos = 0;
$myObj->payload_on = "1";
$myObj->payload_off = "0";
$myObj->optimistic = "false";
$myJSON = json_encode($myObj, JSON_UNESCAPED_SLASHES);
$Topic = $HASSTopic_id."/light/".$device['did']."/config";
//echo $myJSON.'<br/>';
//echo $Topic.'<br/>';
$mqtt->publishnoretain($Topic, $myJSON);
}
$mqtt->close();
} else {
echo "Time out!\n";
}
}else{
echo 'No devices?';
pa( $room );
}
}
}
}
echo "Auto-Discovery Topics Published";
}
}
?>