Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
fa0f248011 | |||
0bf85afc20 | |||
|
32715c5092 | ||
|
0541680671 | ||
|
53506757bf | ||
|
2fcf0c89bf | ||
|
8bf06215af | ||
|
22daeb8875 | ||
|
3564a34502 | ||
|
1c56844be2 | ||
|
b65306e3d5 | ||
|
fb63f3cf52 |
81
.gitea/workflows/ci.yaml
Normal file
81
.gitea/workflows/ci.yaml
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
name: Build homebridge-hue-chase
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
name: Build
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: 18
|
||||||
|
- run: |
|
||||||
|
npm ci
|
||||||
|
npm run build
|
||||||
|
|
||||||
|
version:
|
||||||
|
name: Version
|
||||||
|
outputs:
|
||||||
|
version: ${{ steps.get_version.outputs.version }}
|
||||||
|
commit: ${{ steps.get_version.outputs.commit }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: 18
|
||||||
|
- id: get_version
|
||||||
|
name: Set Version
|
||||||
|
run: |
|
||||||
|
export version=`node -p "require('./package.json').version"`
|
||||||
|
export commit=`echo $GITHUB_SHA | cut -c1-5`
|
||||||
|
echo "version=$version" >> "$GITHUB_OUTPUT"
|
||||||
|
echo "commit=$commit" >> "$GITHUB_OUTPUT"
|
||||||
|
|
||||||
|
publish_tagged:
|
||||||
|
name: Publish Latest
|
||||||
|
needs:
|
||||||
|
- build
|
||||||
|
- version
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: actions/setup-node@v4
|
||||||
|
- run: npm config set @watsonb8:registry https://gitea.watsonlabs.net/api/packages/watsonb8/npm/
|
||||||
|
- name: Publish
|
||||||
|
env:
|
||||||
|
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
||||||
|
COMMIT: ${{ needs.version.outputs.commit }}
|
||||||
|
run: |
|
||||||
|
npm config set -- '//gitea.watsonlabs.net/api/packages/watsonb8/npm/:_authToken' "$NPM_TOKEN"
|
||||||
|
npm version prerelease --preid="$COMMIT" --git-tag-version=false --allow-same-version=true
|
||||||
|
npm publish
|
||||||
|
|
||||||
|
deploy:
|
||||||
|
runs-on:
|
||||||
|
- ubuntu-latest
|
||||||
|
- internal
|
||||||
|
name: Deploy
|
||||||
|
needs: publish_tagged
|
||||||
|
steps:
|
||||||
|
- name: Set up SSH key
|
||||||
|
run: |
|
||||||
|
mkdir -p ~/.ssh
|
||||||
|
echo "${{ secrets.ELEVATED_HOMEBRIDGE_SSH_KEY }}" > ~/.ssh/id_rsa
|
||||||
|
chmod 600 ~/.ssh/id_rsa
|
||||||
|
ssh-keyscan -p 22 homebridge.me >> ~/.ssh/known_hosts
|
||||||
|
sudo apt update
|
||||||
|
sudo apt install sshpass
|
||||||
|
|
||||||
|
- name: Remove old Package
|
||||||
|
run: |
|
||||||
|
sshpass -p '${{ secrets.ELEVATED_HOMEBRIDGE_PASSWORD }}' ssh -v -o StrictHostKeyChecking=no ${{ secrets.ELEVATED_HOMEBRIDGE_USER }}@${{ secrets.HOMEBRIDGE_HOST }} <<'ENDSSH'
|
||||||
|
rm -r /home/${{ secrets.HOMEBRIDGE_USER }}/.npm-global/lib/node_modules/@watsonb8/homebridge-hue-chase
|
||||||
|
ENDSSH
|
||||||
|
- name: Deploy
|
||||||
|
run: |
|
||||||
|
sshpass -p '${{ secrets.ELEVATED_HOMEBRIDGE_PASSWORD }}' ssh -v -o StrictHostKeyChecking=no ${{ secrets.ELEVATED_HOMEBRIDGE_USER }}@${{ secrets.HOMEBRIDGE_HOST }} <<'ENDSSH'
|
||||||
|
npm install -g @watsonb8/homebridge-hue-chase
|
||||||
|
ENDSSH
|
56
README.md
56
README.md
@ -0,0 +1,56 @@
|
|||||||
|
# homebridge-hue-chase
|
||||||
|
|
||||||
|
Homebridge-hue-chase is a good way to add a little spice to your home lighting set up. This simple plugin will cycle the colors of any given hue lights.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
1. Clone the repository by running `git clone ssh://git@thebword.ddns.net:3122/watsonb8/homebridge-hue-chase.git`
|
||||||
|
2. Run `npm install` to install required modules
|
||||||
|
3. Run `npm run build` to build the module
|
||||||
|
4. Run `npm link` or install globally to link this instance to your global homebridge instance
|
||||||
|
|
||||||
|
> NOTE: Upon starting this plugin for the first time, you will be asked to press the sync button on your hue bridge
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
```
|
||||||
|
{
|
||||||
|
"platform": "HueChase",
|
||||||
|
"ipAddress": "example.com",
|
||||||
|
"userName": "",
|
||||||
|
"clientKey": "",
|
||||||
|
"sequences": [
|
||||||
|
{
|
||||||
|
"name": "Play Sequence",
|
||||||
|
"transitionTime": 60000,
|
||||||
|
"matchAllLights": false,
|
||||||
|
"colors": ["#00e456", "#21adea", "#f14cfc", "#8c4cfc", "#e40098"],
|
||||||
|
"lights": ["Play left", "Play right"]
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Platform
|
||||||
|
|
||||||
|
- `ipAddress`: The ipaddres or host name of the hue hub
|
||||||
|
|
||||||
|
- `userName`: The userName to use when authenticating with the hue hub
|
||||||
|
|
||||||
|
> NOTE: This will be visible in the console upon first run of the platform
|
||||||
|
|
||||||
|
- `clientKey`: This clientKey to use when authenticating with the hue hub
|
||||||
|
|
||||||
|
> NOTE: This will be visible in the console upon first run of the platform
|
||||||
|
|
||||||
|
- `sequences`: A list of sequence objects
|
||||||
|
|
||||||
|
#### sequences
|
||||||
|
|
||||||
|
- `name`: The name of the sequence. This will be the label of the enable switch
|
||||||
|
|
||||||
|
- `transitionTime`: The amount of time in milliseconds it will take to transition from one color to the next
|
||||||
|
|
||||||
|
- `colors`: A list hex colors
|
||||||
|
|
||||||
|
- `lights`: The list of lights to use in the sequence
|
1178
package-lock.json
generated
1178
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
16
package.json
16
package.json
@ -1,10 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "homebridge-hue-chase",
|
"name": "@watsonb8/homebridge-hue-chase",
|
||||||
"version": "1.0.0",
|
"version": "1.1.4",
|
||||||
"description": "A Phillips Hue add on for creating chase sequences.",
|
"description": "A Phillips Hue add on for creating chase sequences.",
|
||||||
"main": "bin/index.js",
|
"main": "bin/index.js",
|
||||||
|
"publishConfig": {
|
||||||
|
"registry": "https://gitea.watsonlabs.net"
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc --build",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
@ -28,7 +31,10 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/node": "^13.1.2",
|
"@types/node": "^13.1.2",
|
||||||
"@types/node-hue-api": "^2.3.0",
|
"@types/node-hue-api": "^2.3.0",
|
||||||
"homebridge": "^0.4.50",
|
|
||||||
"node-hue-api": "^4.0.0"
|
"node-hue-api": "^4.0.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"homebridge": "^0.4.53",
|
||||||
|
"typescript": "^4.5.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
413
src/chase.ts
413
src/chase.ts
@ -9,216 +9,247 @@ let Service: HAPNodeJS.Service;
|
|||||||
let Characteristic: HAPNodeJS.Characteristic;
|
let Characteristic: HAPNodeJS.Characteristic;
|
||||||
|
|
||||||
export interface IChaseProps {
|
export interface IChaseProps {
|
||||||
name: string;
|
name: string;
|
||||||
api: any;
|
api: any;
|
||||||
log: any;
|
log: any;
|
||||||
homebridge: any;
|
homebridge: any;
|
||||||
sequence: ISequence;
|
sequence: ISequence;
|
||||||
hue: Api
|
hue: Api;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Chase implements IAccessory {
|
export class Chase implements IAccessory {
|
||||||
private _api: any;
|
private _api: any;
|
||||||
private _homebridge: any;
|
private _homebridge: any;
|
||||||
private _log: any = {};
|
private _log: any = {};
|
||||||
|
|
||||||
//Service fields
|
//Service fields
|
||||||
private _lightbulbService: HAPNodeJS.Service;
|
private _lightbulbService: HAPNodeJS.Service;
|
||||||
private _infoService: HAPNodeJS.Service;
|
private _infoService: HAPNodeJS.Service;
|
||||||
|
|
||||||
private _sequence: ISequence;
|
private _sequence: ISequence;
|
||||||
|
|
||||||
private _isActive: boolean;
|
private _isActive: boolean;
|
||||||
private _hue: Api;
|
private _hue: Api;
|
||||||
|
|
||||||
private _lights: Array<Light> = [];
|
private _lights: Array<Light> = [];
|
||||||
|
|
||||||
private _brightness: number = 0;
|
private _brightness: number = 0;
|
||||||
|
|
||||||
constructor(props: IChaseProps) {
|
constructor(props: IChaseProps) {
|
||||||
//Assign class variables
|
//Assign class variables
|
||||||
this._log = props.log;
|
this._log = props.log;
|
||||||
this._api = props.api;
|
this._api = props.api;
|
||||||
Service = props.api.hap.Service;
|
Service = props.api.hap.Service;
|
||||||
Characteristic = props.api.hap.Characteristic;
|
Characteristic = props.api.hap.Characteristic;
|
||||||
this.name = props.name;
|
this.name = props.name;
|
||||||
this._homebridge = props.homebridge;
|
this._homebridge = props.homebridge;
|
||||||
|
|
||||||
this._sequence = props.sequence;
|
this._sequence = props.sequence;
|
||||||
|
|
||||||
this._isActive = false;
|
this._isActive = false;
|
||||||
this._hue = props.hue;
|
this._hue = props.hue;
|
||||||
|
|
||||||
this.platformAccessory = new this._homebridge.platformAccessory(this.name, this.generateUUID(), this._homebridge.hap.Accessory.Categories.SWITCH);
|
this.platformAccessory = new this._homebridge.platformAccessory(
|
||||||
|
this.name,
|
||||||
|
this.generateUUID(),
|
||||||
|
this._homebridge.hap.Accessory.Categories.SWITCH
|
||||||
|
);
|
||||||
|
|
||||||
|
//@ts-ignore
|
||||||
|
this._infoService = new Service.AccessoryInformation();
|
||||||
|
this._infoService.setCharacteristic(
|
||||||
|
Characteristic.Manufacturer,
|
||||||
|
"Brandon Watson"
|
||||||
|
);
|
||||||
|
this._infoService.setCharacteristic(Characteristic.Model, "Hue Chase");
|
||||||
|
this._infoService.setCharacteristic(
|
||||||
|
Characteristic.SerialNumber,
|
||||||
|
"123-456-789"
|
||||||
|
);
|
||||||
|
|
||||||
|
this._lightbulbService = new Service.Lightbulb(
|
||||||
|
this.name,
|
||||||
|
"lightbulbService"
|
||||||
|
);
|
||||||
|
|
||||||
|
this._lightbulbService
|
||||||
|
.getCharacteristic(Characteristic.On)
|
||||||
|
//@ts-ignore
|
||||||
|
.on("set", this.onPowerSet)
|
||||||
|
//@ts-ignore
|
||||||
|
.on("get", this.onPowerGet);
|
||||||
|
|
||||||
|
this._lightbulbService
|
||||||
|
.getCharacteristic(Characteristic.Brightness)
|
||||||
|
//@ts-ignore
|
||||||
|
.on("set", this.onBrightnessSet)
|
||||||
|
//@ts-ignore
|
||||||
|
.on("get", this.onBrightnessGet);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Required by homebridge.
|
||||||
|
*/
|
||||||
|
public name: string;
|
||||||
|
|
||||||
|
public platformAccessory: any;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called by homebridge to gather services.
|
||||||
|
*/
|
||||||
|
public getServices = (): Array<HAPNodeJS.Service> => {
|
||||||
|
return [this._infoService, this._lightbulbService!];
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for switch set event
|
||||||
|
* @param callback The callback function to call when complete
|
||||||
|
*/
|
||||||
|
private onPowerSet = async (
|
||||||
|
activeState: boolean,
|
||||||
|
callback: (error?: Error | null | undefined) => void
|
||||||
|
) => {
|
||||||
|
if (this._isActive != activeState) {
|
||||||
|
activeState ? this.chase() : this.stop();
|
||||||
|
}
|
||||||
|
return callback();
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for switch get event
|
||||||
|
* @param callback The callback function to call when complete
|
||||||
|
*/
|
||||||
|
private onPowerGet = (
|
||||||
|
callback: (error: Error | null, value: boolean) => void
|
||||||
|
) => {
|
||||||
|
return callback(null, this._isActive);
|
||||||
|
};
|
||||||
|
|
||||||
|
private onBrightnessSet = async (
|
||||||
|
newValue: number,
|
||||||
|
callback: (error?: Error | null | undefined) => void
|
||||||
|
) => {
|
||||||
|
this._brightness = newValue;
|
||||||
|
const lightState = new LightState();
|
||||||
|
lightState.on(true).brightness(this._brightness);
|
||||||
|
|
||||||
|
await this.setLights(lightState);
|
||||||
|
|
||||||
|
return callback();
|
||||||
|
};
|
||||||
|
|
||||||
|
private onBrightnessGet = (
|
||||||
|
callback: (error: Error | null, value: number) => void
|
||||||
|
) => {
|
||||||
|
return callback(null, this._brightness);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Popuplates internal lights array using the configuration values
|
||||||
|
*/
|
||||||
|
private getLights = async () => {
|
||||||
|
//Get lights
|
||||||
|
const lightPromises: Array<Promise<void>> = this._sequence.lights.map(
|
||||||
|
async (value: string) => {
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
this._infoService = new Service.AccessoryInformation();
|
const light: Light = await this._hue.lights.getLightByName(value);
|
||||||
this._infoService.setCharacteristic(Characteristic.Manufacturer, "Brandon Watson")
|
this._lights.push(light);
|
||||||
this._infoService.setCharacteristic(Characteristic.Model, "Hue Chase")
|
}
|
||||||
this._infoService.setCharacteristic(Characteristic.SerialNumber, "123-456-789");
|
);
|
||||||
|
await Promise.all(lightPromises);
|
||||||
this._lightbulbService = new Service.Lightbulb(
|
};
|
||||||
this.name,
|
|
||||||
'lightbulbService'
|
|
||||||
)
|
|
||||||
|
|
||||||
this.getLights();
|
|
||||||
this._lightbulbService.getCharacteristic(Characteristic.On)
|
|
||||||
//@ts-ignore
|
|
||||||
.on("set", this.onPowerSet)
|
|
||||||
.on("get", this.onPowerGet);
|
|
||||||
|
|
||||||
this._lightbulbService.getCharacteristic(Characteristic.Brightness)
|
|
||||||
//@ts-ignore
|
|
||||||
.on("set", this.onBrightnessSet)
|
|
||||||
.on("get", this.onBrightnessGet);
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute chase sequence
|
||||||
|
*/
|
||||||
|
private chase = async () => {
|
||||||
|
if (this._lights.length === 0) {
|
||||||
|
await this.getLights();
|
||||||
}
|
}
|
||||||
|
const lightState = new LightState();
|
||||||
|
let idx = 0;
|
||||||
|
this._isActive = true;
|
||||||
|
while (this._isActive) {
|
||||||
|
const rgb = this.hexToRgb(this._sequence.colors[idx]);
|
||||||
|
lightState
|
||||||
|
.on(true)
|
||||||
|
.brightness(this._brightness)
|
||||||
|
.transitionInMillis(this._sequence.transitionTime)
|
||||||
|
.rgb(rgb?.red, rgb?.green, rgb?.blue);
|
||||||
|
|
||||||
/**
|
await this.setLights(lightState);
|
||||||
* Required by homebridge.
|
|
||||||
*/
|
|
||||||
public name: string;
|
|
||||||
|
|
||||||
public platformAccessory: any;
|
await Sleep(this._sequence.transitionTime);
|
||||||
|
if (idx == this._sequence.colors.length) {
|
||||||
/**
|
idx = 0;
|
||||||
* Called by homebridge to gather services.
|
} else {
|
||||||
*/
|
idx++;
|
||||||
public getServices = (): Array<HAPNodeJS.Service> => {
|
}
|
||||||
return [this._infoService, this._lightbulbService!];
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for switch set event
|
* Stop chase sequence
|
||||||
* @param callback The callback function to call when complete
|
*/
|
||||||
*/
|
private stopAndSetOff = async () => {
|
||||||
private onPowerSet = async (activeState: boolean, callback: (error?: Error | null | undefined) => void) => {
|
this._isActive = false;
|
||||||
if (this._isActive != activeState) {
|
const lightState: LightState = new LightState().off();
|
||||||
activeState ? this.chase() : this.stopAndSetOff();
|
|
||||||
|
await this.setLights(lightState);
|
||||||
|
};
|
||||||
|
|
||||||
|
private stop = () => {
|
||||||
|
this._isActive = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
private setLights = async (state: LightState) => {
|
||||||
|
try {
|
||||||
|
await Promise.all(
|
||||||
|
this._lights.map(async (value: Light) => {
|
||||||
|
await this._hue.lights.setLightState(value.id, state);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
this._log(`Error while setting brightness: ${err}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to convert a hex string to rgb
|
||||||
|
* @param hex hex string starting with "#"
|
||||||
|
*/
|
||||||
|
private hexToRgb = (
|
||||||
|
hex: string
|
||||||
|
): { red: number; green: number; blue: number } | null => {
|
||||||
|
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
||||||
|
return result
|
||||||
|
? {
|
||||||
|
red: parseInt(result[1], 16),
|
||||||
|
green: parseInt(result[2], 16),
|
||||||
|
blue: parseInt(result[3], 16),
|
||||||
}
|
}
|
||||||
return callback();
|
: null;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to generate a UUID
|
||||||
|
*/
|
||||||
|
private generateUUID(): string {
|
||||||
|
// Public Domain/MIT
|
||||||
|
var d = new Date().getTime();
|
||||||
|
if (
|
||||||
|
typeof performance !== "undefined" &&
|
||||||
|
typeof performance.now === "function"
|
||||||
|
) {
|
||||||
|
d += performance.now(); //use high-precision timer if available
|
||||||
}
|
}
|
||||||
|
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
|
||||||
/**
|
/[xy]/g,
|
||||||
* Handler for switch get event
|
function (c) {
|
||||||
* @param callback The callback function to call when complete
|
var r = (d + Math.random() * 16) % 16 | 0;
|
||||||
*/
|
d = Math.floor(d / 16);
|
||||||
private onPowerGet = (callback: (error: Error | null, value: boolean) => void) => {
|
return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
|
||||||
return callback(null, this._isActive);
|
}
|
||||||
}
|
);
|
||||||
|
}
|
||||||
private onBrightnessSet = async (newValue: number, callback: (error?: Error | null | undefined) => void) => {
|
}
|
||||||
this._brightness = newValue;
|
|
||||||
const lightState = new LightState();
|
|
||||||
lightState
|
|
||||||
.on(true)
|
|
||||||
.brightness(this._brightness)
|
|
||||||
|
|
||||||
await this.setLights(lightState);
|
|
||||||
|
|
||||||
return callback();
|
|
||||||
}
|
|
||||||
|
|
||||||
private onBrightnessGet = (callback: (eror: Error | null, value: number) => void) => {
|
|
||||||
return callback(null, this._brightness);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Popuplates internal lights array using the configuration values
|
|
||||||
*/
|
|
||||||
private getLights = async () => {
|
|
||||||
//Get lights
|
|
||||||
const lightPromises: Array<Promise<void>> = this._sequence.lights.map(async (value: string) => {
|
|
||||||
//@ts-ignore
|
|
||||||
const light: Light = await this._hue.lights.getLightByName(value)
|
|
||||||
this._lights.push(light);
|
|
||||||
});
|
|
||||||
await Promise.all(lightPromises);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute chase sequence
|
|
||||||
*/
|
|
||||||
private chase = async () => {
|
|
||||||
if (this._lights.length === 0) {
|
|
||||||
await this.getLights();
|
|
||||||
}
|
|
||||||
const lightState = new LightState();
|
|
||||||
let idx = 0;
|
|
||||||
this._isActive = true;
|
|
||||||
while (this._isActive) {
|
|
||||||
const rgb = this.hexToRgb(this._sequence.colors[idx])
|
|
||||||
lightState
|
|
||||||
.on(true)
|
|
||||||
.brightness(this._brightness)
|
|
||||||
.transitionInMillis(this._sequence.transitionTime)
|
|
||||||
.rgb(rgb?.red, rgb?.green, rgb?.blue);
|
|
||||||
|
|
||||||
await this.setLights(lightState);
|
|
||||||
|
|
||||||
|
|
||||||
await Sleep(this._sequence.transitionTime);
|
|
||||||
if (idx == this._sequence.colors.length) {
|
|
||||||
idx = 0;
|
|
||||||
} else {
|
|
||||||
idx++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop chase sequence
|
|
||||||
*/
|
|
||||||
private stopAndSetOff = async () => {
|
|
||||||
this._isActive = false;
|
|
||||||
const lightState: LightState = new LightState()
|
|
||||||
.off();
|
|
||||||
|
|
||||||
await this.setLights(lightState);
|
|
||||||
}
|
|
||||||
|
|
||||||
private stop = () => {
|
|
||||||
this._isActive = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private setLights = async (state: LightState) => {
|
|
||||||
try {
|
|
||||||
await Promise.all(
|
|
||||||
this._lights.map(async (value: Light) => {
|
|
||||||
await this._hue.lights.setLightState(value.id, state);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
} catch (err) {
|
|
||||||
this._log(`Error while setting brightness: ${err}`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper function to convert a hex string to rgb
|
|
||||||
* @param hex hex string starting with "#"
|
|
||||||
*/
|
|
||||||
private hexToRgb = (hex: string): { red: number, green: number, blue: number } | null => {
|
|
||||||
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
|
||||||
return result ? {
|
|
||||||
red: parseInt(result[1], 16),
|
|
||||||
green: parseInt(result[2], 16),
|
|
||||||
blue: parseInt(result[3], 16)
|
|
||||||
} : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper function to generate a UUID
|
|
||||||
*/
|
|
||||||
private generateUUID(): string { // Public Domain/MIT
|
|
||||||
var d = new Date().getTime();
|
|
||||||
if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
|
|
||||||
d += performance.now(); //use high-precision timer if available
|
|
||||||
}
|
|
||||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
||||||
var r = (d + Math.random() * 16) % 16 | 0;
|
|
||||||
d = Math.floor(d / 16);
|
|
||||||
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
176
src/index.ts
176
src/index.ts
@ -1,6 +1,6 @@
|
|||||||
import { Chase } from "./chase";
|
import { Chase } from "./chase";
|
||||||
import { IConfig, ISequence } from "./models/iConfig";
|
import { IConfig, ISequence } from "./models/iConfig";
|
||||||
import { v3 } from 'node-hue-api';
|
import { v3 } from "node-hue-api";
|
||||||
import LocalBootstrap = require("node-hue-api/lib/api/http/LocalBootstrap");
|
import LocalBootstrap = require("node-hue-api/lib/api/http/LocalBootstrap");
|
||||||
import Api = require("node-hue-api/lib/api/Api");
|
import Api = require("node-hue-api/lib/api/Api");
|
||||||
import { Sleep } from "./sleep";
|
import { Sleep } from "./sleep";
|
||||||
@ -10,97 +10,111 @@ let Homebridge: any;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Main entry.
|
* Main entry.
|
||||||
* @param homebridge
|
* @param homebridge
|
||||||
*/
|
*/
|
||||||
export default function (homebridge: any) {
|
export default function (homebridge: any) {
|
||||||
Homebridge = homebridge;
|
Homebridge = homebridge;
|
||||||
Accessory = homebridge.platformAccessory;
|
Accessory = homebridge.platformAccessory;
|
||||||
homebridge.registerPlatform(
|
homebridge.registerPlatform(
|
||||||
'homebridge-hue-chase',
|
"homebridge-hue-chase",
|
||||||
'HueChase',
|
"HueChase",
|
||||||
HueChasePlatform,
|
HueChasePlatform,
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
};
|
}
|
||||||
|
|
||||||
class HueChasePlatform {
|
class HueChasePlatform {
|
||||||
log: any = {};
|
log: any = {};
|
||||||
api: any;
|
api: any;
|
||||||
chaseList: Array<Chase> = [];
|
chaseList: Array<Chase> = [];
|
||||||
config: IConfig;
|
config: IConfig;
|
||||||
hue: Api | undefined;
|
hue: Api | undefined;
|
||||||
|
|
||||||
|
constructor(log: any, config: any, api: any) {
|
||||||
|
this.log = log;
|
||||||
|
this.api = api;
|
||||||
|
this.config = config;
|
||||||
|
this.log("INFO - Registering Hue Chase platform");
|
||||||
|
this.api.on("didFinishLaunching", this.didFinishLaunching.bind(this));
|
||||||
|
}
|
||||||
|
|
||||||
constructor(log: any, config: any, api: any) {
|
private connectHue = async () => {
|
||||||
this.log = log;
|
if (!this.config) {
|
||||||
this.api = api;
|
return;
|
||||||
this.config = config;
|
|
||||||
this.log('INFO - Registering Hue Chase platform');
|
|
||||||
this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private connectHue = async () => {
|
if (this.config.userName && this.config.clientKey) {
|
||||||
if (!this.config) {
|
this.hue = await v3.api
|
||||||
return;
|
.createLocal(this.config.ipAddress)
|
||||||
}
|
.connect(this.config.userName, this.config.clientKey, null);
|
||||||
|
this.log("Using existing connection info");
|
||||||
if (this.config.userName && this.config.clientKey) {
|
} else {
|
||||||
this.hue = await v3.api.createLocal(this.config.ipAddress).connect(this.config.userName, this.config.clientKey, null);
|
const unauthenticatedApi = await v3.api
|
||||||
this.log("Using existing connection info");
|
.createLocal(this.config.ipAddress)
|
||||||
} else {
|
.connect(null, null, null);
|
||||||
const unauthenticatedApi = await v3.api.createLocal(this.config.ipAddress).connect(null, null, null);
|
let createdUser;
|
||||||
let createdUser;
|
let connected = false;
|
||||||
let connected = false
|
while (!connected) {
|
||||||
while (!connected) {
|
try {
|
||||||
try {
|
this.log("Creating hue user. Push link button");
|
||||||
this.log("Creating hue user. Push link button")
|
createdUser = await unauthenticatedApi.users.createUser(
|
||||||
createdUser = await unauthenticatedApi.users.createUser("homebridge", "HueChase");
|
"homebridge",
|
||||||
|
"HueChase"
|
||||||
this.hue = await v3.api.createLocal(this.config.ipAddress).connect(createdUser.username, createdUser.clientKey, null);
|
);
|
||||||
this.log("Connected to Hue Bridge");
|
|
||||||
this.log(`UserName: ${createdUser.username}, ClientKey: ${createdUser.clientkey}`)
|
this.hue = await v3.api
|
||||||
connected = true;
|
.createLocal(this.config.ipAddress)
|
||||||
|
.connect(createdUser.username, createdUser.clientKey, null);
|
||||||
} catch (err) {
|
this.log("Connected to Hue Bridge");
|
||||||
if (err.getHueErrorType() === 101) {
|
this.log(
|
||||||
this.log('The Link button on the bridge was not pressed. Please press the Link button and try again.');
|
`UserName: ${createdUser.username}, ClientKey: ${createdUser.clientkey}`
|
||||||
Sleep(5000);
|
);
|
||||||
} else {
|
connected = true;
|
||||||
this.log(`Unexpected Error: ${err.message}`);
|
} catch (err: any) {
|
||||||
break;
|
if (err.getHueErrorType() === 101) {
|
||||||
}
|
this.log(
|
||||||
|
"The Link button on the bridge was not pressed. Please press the Link button and try again."
|
||||||
}
|
);
|
||||||
}
|
Sleep(5000);
|
||||||
|
} else {
|
||||||
|
this.log(`Unexpected Error: ${err.message}`);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for didFinishLaunching
|
* Handler for didFinishLaunching
|
||||||
* Happens after constructor
|
* Happens after constructor
|
||||||
*/
|
*/
|
||||||
private didFinishLaunching() {
|
private didFinishLaunching() {
|
||||||
this.log(`INFO - Done registering Hue Chase platform`);
|
this.log(`INFO - Done registering Hue Chase platform`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called by homebridge to gather accessories.
|
* Called by homebridge to gather accessories.
|
||||||
* @param callback
|
* @param callback
|
||||||
*/
|
*/
|
||||||
public accessories = async (callback: (accessories: Array<Chase>) => void) => {
|
public accessories = async (
|
||||||
//Connect to hue bridge
|
callback: (accessories: Array<Chase>) => void
|
||||||
await this.connectHue();
|
) => {
|
||||||
|
//Connect to hue bridge
|
||||||
|
await this.connectHue();
|
||||||
|
|
||||||
this.config.sequences.forEach((sequence: ISequence) => {
|
this.config.sequences.forEach((sequence: ISequence) => {
|
||||||
this.chaseList.push(new Chase({
|
this.chaseList.push(
|
||||||
name: sequence.name,
|
new Chase({
|
||||||
api: this.api,
|
name: sequence.name,
|
||||||
log: this.log,
|
api: this.api,
|
||||||
homebridge: Homebridge,
|
log: this.log,
|
||||||
sequence: sequence,
|
homebridge: Homebridge,
|
||||||
hue: this.hue!,
|
sequence: sequence,
|
||||||
}))
|
hue: this.hue!,
|
||||||
})
|
})
|
||||||
callback(this.chaseList);
|
);
|
||||||
}
|
});
|
||||||
}
|
callback(this.chaseList);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user