Skip to content

Commit

Permalink
Merge pull request #4 from goodshort/development
Browse files Browse the repository at this point in the history
v0.4.0
  • Loading branch information
agoodshort authored Dec 4, 2021
2 parents ab02400 + f466754 commit b86130f
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 81 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.4.0] - 2021-12-03
### Added
- Supports multiple WLED devices
- Additional information in plugin **SETTINGS**

## [0.3.0] - 2021-12-03
### Added
- Get handler for Active Identifier characteristic
Expand Down
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,35 @@ Got some inspiration from [Homebridge Simple WLED](https://github.com/jstrausd/h

1. SSH or open a Terminal on your Homebridge host
2. Run `npm install -g homebridge-wled-preset`
3. Update your configuration file using the sample [below](#Configuration).
3. Update your configuration file using the sample [below](#configuration).

## Configuration

**At the moment, the plugin is unable to support multiple WLED accessories.**

Configuration sample:

```json
"platforms": [
{
"name": "Office WLED",
"ip": "192.168.1.30",
"presetsNb": 5,
"wleds": [
{
"name": "Office Led Strip",
"ip": "192.168.1.30",
"presetsNb": 5
},
{
"name": "Bedroom Led Strip",
"ip": "192.168.1.31",
"presetsNb": 2
}
],
"platform": "WledPreset"
}
]
```

## To-Do
- [ ] Get [verified](https://github.com/homebridge/verified)
- [ ] Support multiple WLED accessories
- [ ] Auto discovery of accessories
- [ ] Can use hostname
- [ ] Complete CHANGELOG.md and publish to version v0.3.0
- [ ] Add more details in the configuration section
- [ ] Add more details in the configuration (README, config.schema.json)
- [ ] Can we use the brightness slider?
50 changes: 29 additions & 21 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,38 @@
"pluginAlias": "WledPreset",
"pluginType": "platform",
"singular": true,
"headerDisplay": "TO-DO",
"footerDisplay": "TO-DO",
"headerDisplay": "Fill out required fields below and click \"SAVE\" to get started. See [README](https://github.com/goodshort/homebridge-wled-preset) for more information.",
"schema": {
"type": "object",
"properties": {
"name": {
"title": "Name",
"type": "string",
"placeholder": "Enter a name for your WLED...",
"default": "WLED",
"required": true
},
"ip": {
"title": "IP",
"type": "string",
"placeholder": "Enter WLED IP address...",
"required": true
},
"presetsNb": {
"title": "Number of presets configured in your WLED interface",
"type": "number",
"placeholder": "Enter amount of presets...",
"default": "5",
"required": true
"wleds": {
"type": "array",
"title": "WLEDs",
"items": {
"type": "object",
"title": "WLED device",
"properties": {
"name": {
"title": "Name",
"type": "string",
"default": "Led Strip",
"required": true
},
"ip": {
"title": "IP",
"type": "string",
"placeholder": "Enter IP...",
"required": true
},
"presetsNb": {
"title": "Number of presets configured",
"type": "number",
"placeholder": "Enter amount of presets",
"default": "5",
"required": true
}
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"displayName": "Homebridge WLED Preset",
"name": "homebridge-wled-preset",
"version": "0.3.0",
"version": "0.4.0",
"description": "A Homebridge plugin controlling WLED presets",
"license": "Apache-2.0",
"repository": {
Expand Down
56 changes: 32 additions & 24 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,27 @@ import { WledPresetAccessory } from './platformAccessory';
*/
export class WledPresetPlatform implements DynamicPlatformPlugin {
public readonly Service: typeof Service = this.api.hap.Service;
public readonly Characteristic: typeof Characteristic =
this.api.hap.Characteristic;
public readonly Characteristic: typeof Characteristic = this.api.hap.Characteristic;
private wleds: WledPresetAccessory[] = [];

// this is used to track restored cached accessories
public readonly accessories: PlatformAccessory[] = [];

constructor(
public readonly log: Logger,
public readonly config: PlatformConfig,
public readonly api: API,
) {
constructor(public readonly log: Logger, public readonly config: PlatformConfig, public readonly api: API) {

if (!this.config){
return;
}

if (!this.config.wleds) {
this.log.info('No WLEDs have been configured');
} else {
const wledsRecord = this.config.wleds as Record<string, Array<string>>;
for (const k in wledsRecord) {
this.log.info(wledsRecord[k]['name']);
}
}

this.log.debug('Finished initializing platform:', this.config.name);

// When this event is fired it means Homebridge has restored all cached accessories from disk.
Expand Down Expand Up @@ -59,23 +69,19 @@ export class WledPresetPlatform implements DynamicPlatformPlugin {
* must not be registered again to prevent "duplicate UUID" errors.
*/
discoverDevices() {
// EXAMPLE ONLY
// A real plugin you would discover accessories from the local network, cloud services
// or a user-defined array in the platform config.
const exampleDevices = [
{
displayName: this.config.name as string,
ip: this.config.ip as string,
presetsNb: this.config.presetsNb as number,
},
// {
// exampleUniqueId: 'EFGH',
// exampleDisplayName: 'Kitchen',
// },
];
const wledsRecord = this.config.wleds as Record<string, Array<string>>;
const wledDevices: { displayName: string; ip: string; presetsNb: number }[] = [];

for (const k in wledsRecord) {
wledDevices.push({
displayName: wledsRecord[k]['name'] as string,
ip: wledsRecord[k]['ip'] as string,
presetsNb: wledsRecord[k]['presetsNb'] as number,
});
}

// loop over the discovered devices and register each one if it has not already been registered
for (const device of exampleDevices) {
for (const device of wledDevices) {
// generate a unique id for the accessory this should be generated from
// something globally unique, but constant, for example, the device serial
// number or MAC address
Expand All @@ -97,13 +103,14 @@ export class WledPresetPlatform implements DynamicPlatformPlugin {

// if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
// existingAccessory.context.device = device;
// this.api.updatePlatformAccessories([existingAccessory]);
// this.api.updatePlatformAccessories([existingAccessory]); // To-Do: What does this do

// create the accessory handler for the restored accessory
// this is imported from `platformAccessory.ts`
new WledPresetAccessory(
this,
existingAccessory,
device.displayName,
device.ip,
device.presetsNb,
);
Expand Down Expand Up @@ -137,7 +144,7 @@ export class WledPresetPlatform implements DynamicPlatformPlugin {

// create the accessory handler for the newly create accessory
// this is imported from `platformAccessory.ts`
new WledPresetAccessory(this, accessory, device.ip, device.presetsNb);
new WledPresetAccessory(this, accessory, device.displayName, device.ip, device.presetsNb);

// link the accessory to your platform
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
Expand All @@ -147,3 +154,4 @@ export class WledPresetPlatform implements DynamicPlatformPlugin {
}
}
}

45 changes: 19 additions & 26 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class WledPresetAccessory {
constructor(
private readonly platform: WledPresetPlatform,
private readonly accessory: PlatformAccessory,
private readonly displayName: string,
private readonly ip: string,
private readonly presetsNb: number,
) {
Expand Down Expand Up @@ -59,7 +60,7 @@ export class WledPresetAccessory {
this.presetService.setCharacteristic(this.platform.Characteristic.ConfiguredName, accessory.context.device.displayName);

// Register Sleep Discovery Mode Characteristic
this.presetService.setCharacteristic(
this.presetService.setCharacteristic( // To-DO: Should I change the DISCOVERABLE?
this.platform.Characteristic.SleepDiscoveryMode, this.platform.Characteristic.SleepDiscoveryMode.NOT_DISCOVERABLE);

/* ------------------------------------------------------------------------------------------------------------------------------- */
Expand All @@ -70,7 +71,7 @@ export class WledPresetAccessory {
* The value cannot be higher than 250 based on documentation https://kno.wled.ge/interfaces/http-api/
*/
for (let i = 1; i <= this.presetsNb; i++) {
this.platform.log.debug('Looking for preset ' + i);
this.platform.log.debug(this.displayName + ': Looking for preset ' + i);
this.performRequestPreset({
host: this.ip,
path: '/win&PL=' + i,
Expand All @@ -83,7 +84,7 @@ export class WledPresetAccessory {

if (value === i) {
// TO-DO move to a method
this.platform.log.debug('Creating preset ' + i);
this.platform.log.debug(this.displayName + ': Creating preset ' + i);
const serviceName: string = 'p' + i;
const presetName: string = 'Preset ' + i;

Expand All @@ -97,12 +98,12 @@ export class WledPresetAccessory {
.setCharacteristic(this.platform.Characteristic.InputSourceType, this.platform.Characteristic.InputSourceType.HDMI);
this.presetService.addLinkedService(this['effectInputSource' + i]);
} else {
this.platform.log.debug('Preset ' + i + ' does not exists');
this.platform.log.debug(this.displayName + ': Preset ' + i + ' does not exists');
}
}
})
.catch((error) => {
this.platform.log.debug(error);
this.platform.log.debug(this.displayName + error);
});
}
}
Expand All @@ -123,18 +124,18 @@ export class WledPresetAccessory {
.then((response) => {
if (typeof response === 'string') {
if (response === '["0"]') {
this.platform.log.info('Turning off WLED');
this.platform.log.info(this.displayName + ': Turning off');
} else {
this.platform.log.info('Turning on WLED');
this.platform.log.info(this.displayName + ': Turning on');
}
this.platform.log.debug('Set on -> Sending GET request: ' + this.ip + '/win&T=' + value);
this.platform.log.debug('Set on -> response: ' + response);
this.platform.log.debug(this.displayName + ': Set on -> Sending GET request: ' + this.ip + '/win&T=' + value);
this.platform.log.debug(this.displayName + ': Set on -> response: ' + response);
callback(null);
}
})
.catch((error) => {
callback(error);
this.platform.log.debug(error);
this.platform.log.debug(this.displayName + error);
});
}

Expand All @@ -150,7 +151,7 @@ export class WledPresetAccessory {
})
.then((response) => {
if (typeof response === 'string') {
this.platform.log.debug('Get On -> Brightness:' + response);
this.platform.log.debug(this.displayName + ': Get On -> Brightness:' + response);
if (response === '["0"]') {
callback(null, 0);
} else {
Expand All @@ -160,7 +161,7 @@ export class WledPresetAccessory {
})
.catch((error) => {
callback(error);
this.platform.log.debug(error);
this.platform.log.debug(this.displayName + error);
});
}

Expand All @@ -178,13 +179,13 @@ export class WledPresetAccessory {
if (typeof response === 'string') {
const stringValue = response.replace(/\W/gi, '');
const answerValue: number = +stringValue;
this.platform.log.info('Preset is set to ' + answerValue.toString());
this.platform.log.info(this.displayName + ': Preset is set to ' + answerValue.toString());
callback(null, answerValue);
}
})
.catch((error) => {
callback(error);
this.platform.log.debug(error);
this.platform.log.debug(this.displayName + error);
});
}

Expand All @@ -202,23 +203,15 @@ export class WledPresetAccessory {
if (typeof response === 'string') {
const stringValue = response.replace(/\W/gi, '');
const answerValue: number = +stringValue;
this.platform.log.debug('Set Active Identifier -> Trying to set Preset to: ' + value.toString());
this.platform.log.debug('Set Active Identifier -> Sending GET request: ' + this.ip + '/win&PL=' + value);
this.platform.log.info('Preset set to ' + answerValue.toString());
// const keys = Object.keys(presets) as Array<string>;
// // const keys = Object.keys(presets);
// this.platform.log.debug(keys[1]);
// this.platform.log.debug(keys[0]);
// for (const k in presets) {
// const value = presets[k] as string;
// this.platform.log.debug(value);
// }
this.platform.log.debug(this.displayName + ': Set Active Identifier -> Trying to set Preset to: ' + value.toString());
this.platform.log.debug(this.displayName + ': Set Active Identifier -> Sending GET request: ' + this.ip + '/win&PL=' + value);
this.platform.log.info(this.displayName + ': Preset set to ' + answerValue.toString());
callback(null);
}
})
.catch((error) => {
callback(error);
this.platform.log.debug(error);
this.platform.log.debug(this.displayName + error);
});
}

Expand Down

0 comments on commit b86130f

Please sign in to comment.