forked from bren1818/TCPLightingWebInterface
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmqttdiscovery.php
139 lines (116 loc) · 3.91 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
131
132
133
134
135
136
137
138
<?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, NULL, $MQTTusername, $MQTTpassword)) {
foreach($DEVICES as $device){
$DeviceName = str_replace(' ', '', $device['name']);
$DeviceCommand = $RoomName."/".$DeviceName."/".$device['did'];
$array = array(
"name" => $device["name"],
"unique_id" => $device['did'],
"command_topic" => $MQTT_prefix."/".$DeviceCommand."/switch",
"state_topic" => $MQTT_prefix."/".$DeviceCommand."/status",
"brightness_command_topic" => $MQTT_prefix."/".$DeviceCommand."/brightness/set",
"brightness_state_topic" => $MQTT_prefix."/".$DeviceCommand."/brightness",
"availability_topic" => $MQTT_prefix."/".$DeviceCommand."/LWT",
"brightness_scale" => 100,
"qos" => 0,
"payload_on" => "1",
"payload_off" => "0",
"optimistic" => "false",
"device" => array(
"ids" => $device['did'],
"name" => $device["name"],
"mf" => "TCP",
"mdl" => "TCP Bulb"
)
);
$myJSON = json_encode($array, JSON_UNESCAPED_SLASHES);
$Topic = $HASSTopic_id."/light/".$device['did']."/config";
//Uncomment the line below to see the formatted JSON being sent to the Home Assistant Discovery Topic
//echo '<pre>'.json_encode($array, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT).'</pre>';
$mqtt->publishnoretain($Topic, $myJSON);
}
$mqtt->close();
} else {
echo "Time out!\n";
}
}else{
echo 'No devices?';
pa( $room );
}
}
}
}
echo "Auto-Discovery Topics Published";
}
}
?>