Skip to content

Commit d62f2ff

Browse files
committed
* 0.11 - 2016/05/28 - owagner/hobbyquaker
- adapted to new 1.8.1+ API scheme of whitelist usernames being assigned by the bridge. The assigned username is stored using Java Preferences. The scheme should be compatible with already authorized hue2mqtt instances, which used a hardcoded username of "hue2mqttuser". - Hue API libraries updated to 0.11.2.
1 parent 675fd65 commit d62f2ff

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ The special group name 0 is also recognized and refers to the default group whic
7878
all lights connected to a bridge.
7979

8080

81+
Authentication
82+
--------------
83+
Like all applications connecting to a Hue bridge, hue2mqtt needs to be authenticated using push link
84+
at least once. The bridge will then assign a whitelist username (in fact a token) which is automatically
85+
used on subsequent connections. The token is stored using Java Preferences.
86+
87+
8188
Dependencies
8289
------------
8390
* Java 1.7 SE Runtime Environment: https://www.java.com/
@@ -90,6 +97,12 @@ Dependencies
9097

9198
History
9299
-------
100+
* 0.11 - 2016/05/28 - owagner/hobbyquaker
101+
- adapted to new 1.8.1+ API scheme of whitelist usernames being assigned by the bridge.
102+
The assigned username is stored using Java Preferences. The scheme should be compatible
103+
with already authorized hue2mqtt instances, which used a hardcoded username of "hue2mqttuser".
104+
- Hue API libraries updated to 0.11.2.
105+
93106
* 0.10 - 2016/02/23 - owagner
94107
- attempt to reconnect every 10s if bridge connection errors out
95108

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version = '0.10'
1+
version = '0.11'
22

33
apply plugin: 'java'
44
apply plugin: 'eclipse'

lib/huelocalsdk.jar

8.82 KB
Binary file not shown.

lib/huesdkresources.jar

16.2 KB
Binary file not shown.

src/main/java/com/tellerulam/hue2mqtt/HueHandler.java

+43-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.*;
44
import java.util.logging.Logger;
5+
import java.util.prefs.*;
56

67
import com.eclipsesource.json.*;
78
import com.philips.lighting.hue.listener.*;
@@ -13,6 +14,34 @@ public class HueHandler implements PHSDKListener
1314
private static PHHueSDK phHueSDK;
1415
private static HueHandler instance;
1516

17+
/*
18+
* Since Hue SDK 1.8.1, it is no longer possible to set your own whitelist username.
19+
* Instead, the bridge will assign you an username which you have to present on subsequent
20+
* connections.
21+
*
22+
* We use the Java Preference API to store the assigned username per bridge IP or hostname.
23+
*/
24+
25+
private static String usedBridgeIP;
26+
27+
private static String readUsername(String bridgeIp)
28+
{
29+
Preferences prefs;
30+
prefs = Preferences.userRoot().node("com.tellerulam.hue2mqtt-" + bridgeIp);
31+
String u = prefs.get("username", "huetomqttuser");
32+
L.info("Using whitelist username "+u+" for Bridge IP "+bridgeIp);
33+
return u;
34+
}
35+
36+
private static void saveUsername(String bridgeIp, String u)
37+
{
38+
Preferences prefs;
39+
prefs = Preferences.userRoot().node("com.tellerulam.hue2mqtt-" + bridgeIp);
40+
prefs.put("username", u);
41+
L.info("Saved whitelist username "+u+" for Bridge IP "+bridgeIp);
42+
}
43+
44+
1645
static void init()
1746
{
1847
instance=new HueHandler();
@@ -31,8 +60,9 @@ static void init()
3160
else
3261
{
3362
PHAccessPoint pap=new PHAccessPoint();
63+
usedBridgeIP=specifiedBridge;
3464
pap.setIpAddress(specifiedBridge);
35-
pap.setUsername("huetomqttuser");
65+
pap.setUsername(readUsername(specifiedBridge));
3666
instance.connect(pap);
3767
}
3868
}
@@ -64,8 +94,10 @@ public void onAccessPointsFound(List<PHAccessPoint> bridges)
6494
L.warning("Multiple bridges found. Will connect to the first bridge. This may not be what you want! Specify the bridge you want to connect to explicitely with hue.bridge=<name or ip>");
6595
}
6696
PHAccessPoint pap=bridges.get(0);
67-
pap.setUsername("huetomqttuser");
68-
L.info("Connecting to Hue bridge @ "+pap.getIpAddress());
97+
usedBridgeIP=pap.getIpAddress();
98+
String username=readUsername(usedBridgeIP);
99+
pap.setUsername(username);
100+
L.info("Connecting to Hue bridge @ "+pap.getIpAddress()+" with username "+username);
69101
connect(pap);
70102
}
71103

@@ -136,7 +168,13 @@ public void onConnectionResumed(PHBridge b)
136168
@Override
137169
public void onError(int e, String msg)
138170
{
139-
L.warning("Error in bridge connection. Code "+e+": "+msg);
171+
if(e==101)
172+
{
173+
/* This is "Authentication required", we silently ignore it here */
174+
return;
175+
}
176+
177+
L.warning("Error in bridge connection. Code "+e+": "+msg+"; will reconnect in 10s");
140178
/* Retry connection in 10s */
141179
Main.t.schedule(new TimerTask(){
142180
@Override
@@ -343,6 +381,7 @@ public void onBridgeConnected(PHBridge b, String name)
343381
{
344382
L.info("Successfully connected to Hue bridge as "+name);
345383
phHueSDK.setSelectedBridge(b);
384+
saveUsername(usedBridgeIP,name);
346385
phHueSDK.enableHeartbeat(b, PHHueSDK.HB_INTERVAL);
347386
MQTTHandler.setHueConnectionState(true);
348387
Main.t.schedule(new TimerTask(){

0 commit comments

Comments
 (0)