Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Commit 2cac598

Browse files
committed
Add bridge presets to hue service
Removes previous discover feature of the Hue service because device presets allow for selection between multiple bridges in a nice gui. Uses the same N-UPnP discovery from the underlying Hue library as the discover feature did.
1 parent 861c5e6 commit 2cac598

File tree

2 files changed

+26
-41
lines changed

2 files changed

+26
-41
lines changed

nodecg-io-philipshue/extension/index.ts

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NodeCG } from "nodecg/types/server";
2-
import { Result, emptySuccess, success, error, ServiceBundle } from "nodecg-io-core";
2+
import { Result, emptySuccess, success, error, ServiceBundle, ObjectMap } from "nodecg-io-core";
33
import { v4 as ipv4 } from "is-ip";
44
import { v3 } from "node-hue-api";
55
// Only needed for type because of that it is "unused" but still needed
@@ -11,9 +11,8 @@ const deviceName = "nodecg-io";
1111
const name = "philipshue";
1212

1313
interface PhilipsHueServiceConfig {
14-
discover: boolean;
1514
ipAddr: string;
16-
port: number;
15+
port?: number;
1716
username?: string;
1817
apiKey?: string;
1918
}
@@ -25,20 +24,20 @@ module.exports = (nodecg: NodeCG) => {
2524
};
2625

2726
class PhilipsHueService extends ServiceBundle<PhilipsHueServiceConfig, PhilipsHueServiceClient> {
27+
presets = {};
28+
29+
constructor(nodecg: NodeCG, name: string, ...pathSegments: string[]) {
30+
super(nodecg, name, ...pathSegments);
31+
this.discoverBridges()
32+
.then((bridgePresets) => (this.presets = bridgePresets))
33+
.catch((err) => this.nodecg.log.error(`Failed to discover local bridges: ${err}`));
34+
}
35+
2836
async validateConfig(config: PhilipsHueServiceConfig): Promise<Result<void>> {
29-
const { discover, port, ipAddr } = config;
30-
31-
if (!config) {
32-
// config could not be found
33-
return error("No config found!");
34-
} else if (!discover) {
35-
// check the ip address if its there
36-
if (ipAddr && !ipv4(ipAddr)) {
37-
return error("Invalid IP address, can handle only IPv4 at the moment!");
38-
}
37+
const { port, ipAddr } = config;
3938

40-
// discover is not set but there is no ip address
41-
return error("Discover isn't true there is no IP address!");
39+
if (!ipv4(ipAddr)) {
40+
return error("Invalid IP address, can handle only IPv4 at the moment!");
4241
} else if (port && !(0 <= port && port <= 65535)) {
4342
// the port is there but the port is wrong
4443
return error("Your port is not between 0 and 65535!");
@@ -49,16 +48,6 @@ class PhilipsHueService extends ServiceBundle<PhilipsHueServiceConfig, PhilipsHu
4948
}
5049

5150
async createClient(config: PhilipsHueServiceConfig): Promise<Result<PhilipsHueServiceClient>> {
52-
if (config.discover) {
53-
const discIP = await this.discoverBridge();
54-
if (discIP) {
55-
config.ipAddr = discIP;
56-
config.discover = false;
57-
} else {
58-
return error("Could not discover your Hue Bridge, maybe try specifying a specific IP!");
59-
}
60-
}
61-
6251
const { port, username, apiKey, ipAddr } = config;
6352

6453
// check if there is one thing missing
@@ -97,15 +86,15 @@ class PhilipsHueService extends ServiceBundle<PhilipsHueServiceConfig, PhilipsHu
9786
// Not supported from the client
9887
}
9988

100-
private async discoverBridge() {
101-
const discoveryResults = await discovery.nupnpSearch();
89+
private async discoverBridges(): Promise<ObjectMap<PhilipsHueServiceConfig>> {
90+
const results: { ipaddress: string }[] = await discovery.nupnpSearch();
10291

103-
if (discoveryResults.length === 0) {
104-
this.nodecg.log.error("Failed to resolve any Hue Bridges");
105-
return null;
106-
} else {
107-
// Ignoring that you could have more than one Hue Bridge on a network as this is unlikely in 99.9% of users situations
108-
return discoveryResults[0].ipaddress as string;
109-
}
92+
return Object.fromEntries(
93+
results.map((bridge) => {
94+
const ipAddr = bridge.ipaddress;
95+
const config: PhilipsHueServiceConfig = { ipAddr };
96+
return [ipAddr, config];
97+
}),
98+
);
11099
}
111100
}

nodecg-io-philipshue/philipshue-schema.json

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@
33
"type": "object",
44
"additionalProperties": false,
55
"properties": {
6-
"discover": {
7-
"type": "boolean",
8-
"description": "If the extension should try to discover the Philips Hue Bridge. **IMPORTANT** ignores the specified IP address and errors if it could not find a bridge, will only be used on first run"
9-
},
106
"ipAddr": {
117
"type": "string",
12-
"description": "The IP address of the bridge that you want to connect to, not required/used if you specify discover as true"
8+
"description": "The IP address of the bridge that you want to connect to"
139
},
1410
"port": {
1511
"type": "number",
16-
"description": "The port that you want to use for connecting to your Hue bridge, not required/used if you specify discover as true"
12+
"description": "The port that you want to use for connecting to your Hue bridge"
1713
},
1814
"apiKey": {
1915
"type": "string",
@@ -24,5 +20,5 @@
2420
"description": "The username that you want to use/that will be generated on first startup"
2521
}
2622
},
27-
"required": ["discover"]
23+
"required": ["ipAddr"]
2824
}

0 commit comments

Comments
 (0)