Successfully controlling wiz bulbs
This commit is contained in:
parent
c3a6882413
commit
d73dead5d4
4
.prettierrc
Normal file
4
.prettierrc
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"tabWidth": 4,
|
||||||
|
"useTabs": false
|
||||||
|
}
|
35
.vscode/launch.json
vendored
35
.vscode/launch.json
vendored
@ -1,19 +1,20 @@
|
|||||||
{
|
{
|
||||||
// Use IntelliSense to learn about possible attributes.
|
// Use IntelliSense to learn about possible attributes.
|
||||||
// Hover to view descriptions of existing attributes.
|
// Hover to view descriptions of existing attributes.
|
||||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"type": "node",
|
"type": "node",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"name": "Launch Program",
|
"name": "Launch Program",
|
||||||
"preLaunchTask": "build",
|
"preLaunchTask": "build",
|
||||||
"program": "/Users/brandonwatson/.nvm/versions/node/v14.15.0/lib/node_modules/homebridge/bin/homebridge",
|
"program": "/Users/brandonwatson/.nvm/versions/node/v14.15.0/lib/node_modules/homebridge/bin/homebridge",
|
||||||
"env": {
|
"env": {
|
||||||
"HOMEBRIDGE_OPTS": "/Users/brandonwatson/.homebridge"
|
"HOMEBRIDGE_OPTS": "/Users/brandonwatson/.homebridge",
|
||||||
},
|
"LOG_LEVEL": "debug"
|
||||||
"sourceMaps": true
|
},
|
||||||
}
|
"sourceMaps": true
|
||||||
]
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
1950
package-lock.json
generated
1950
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -29,6 +29,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/node-cron": "^2.0.3",
|
"@types/node-cron": "^2.0.3",
|
||||||
"@types/suncalc": "^1.8.0",
|
"@types/suncalc": "^1.8.0",
|
||||||
|
"@watsonb8/wiz-lib": "^1.0.1-62427.0",
|
||||||
"node-cron": "^2.0.3",
|
"node-cron": "^2.0.3",
|
||||||
"node-hue-api": "^4.0.5",
|
"node-hue-api": "^4.0.5",
|
||||||
"suncalc": "^1.8.0"
|
"suncalc": "^1.8.0"
|
||||||
|
@ -8,6 +8,8 @@ import { IConfig } from "./models/iConfig";
|
|||||||
import { GetTimesResult, getTimes } from "suncalc";
|
import { GetTimesResult, getTimes } from "suncalc";
|
||||||
import HueError = require("node-hue-api/lib/HueError");
|
import HueError = require("node-hue-api/lib/HueError");
|
||||||
import cron from "node-cron";
|
import cron from "node-cron";
|
||||||
|
import { WizBulb } from "@watsonb8/wiz-lib/build/wizBulb";
|
||||||
|
import { colorTemperature2rgb, Pilot, RGB } from "@watsonb8/wiz-lib";
|
||||||
|
|
||||||
let Service: HAPNodeJS.Service;
|
let Service: HAPNodeJS.Service;
|
||||||
let Characteristic: HAPNodeJS.Characteristic;
|
let Characteristic: HAPNodeJS.Characteristic;
|
||||||
@ -16,339 +18,354 @@ const MINUTES_IN_MILLISECOND = 60000;
|
|||||||
const SECONDS_IN_HOUR = 3600;
|
const SECONDS_IN_HOUR = 3600;
|
||||||
|
|
||||||
export interface IFluxProps {
|
export interface IFluxProps {
|
||||||
api: any;
|
api: any;
|
||||||
log: any;
|
log: any;
|
||||||
homebridge: any;
|
homebridge: any;
|
||||||
hue: Api;
|
hue: Api;
|
||||||
config: IConfig;
|
wizBulbs: Array<WizBulb>;
|
||||||
|
config: IConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class FluxAccessory implements IAccessory {
|
export class FluxAccessory implements IAccessory {
|
||||||
private _api: any;
|
private _api: any;
|
||||||
private _homebridge: any;
|
private _homebridge: any;
|
||||||
private _log: any = {};
|
private _log: any = {};
|
||||||
private _config: IConfig;
|
private _config: IConfig;
|
||||||
private _isActive: boolean;
|
private _isActive: boolean;
|
||||||
|
|
||||||
//Service fields
|
//Service fields
|
||||||
private _switchService: HAPNodeJS.Service;
|
private _switchService: HAPNodeJS.Service;
|
||||||
private _infoService: HAPNodeJS.Service;
|
private _infoService: HAPNodeJS.Service;
|
||||||
|
|
||||||
private _hue: Api;
|
private _hue: Api;
|
||||||
|
|
||||||
private _lights: Array<Light> = [];
|
private _lights: Array<Light> = [];
|
||||||
|
private _wizLights: Array<WizBulb> = [];
|
||||||
|
|
||||||
private _times: GetTimesResult;
|
private _times: GetTimesResult;
|
||||||
|
|
||||||
constructor(props: IFluxProps) {
|
constructor(props: IFluxProps) {
|
||||||
//Assign class variables
|
//Assign class variables
|
||||||
this._log = props.log;
|
this._log = props.log;
|
||||||
this._api = props.api;
|
this._api = props.api;
|
||||||
this._config = props.config;
|
this._config = props.config;
|
||||||
Service = props.api.hap.Service;
|
Service = props.api.hap.Service;
|
||||||
Characteristic = props.api.hap.Characteristic;
|
Characteristic = props.api.hap.Characteristic;
|
||||||
this._homebridge = props.homebridge;
|
this._homebridge = props.homebridge;
|
||||||
this._isActive = false;
|
this._isActive = false;
|
||||||
|
this._wizLights = props.wizBulbs;
|
||||||
|
|
||||||
this._times = getTimes(
|
this._times = getTimes(
|
||||||
new Date(),
|
|
||||||
this._config.latitude,
|
|
||||||
this._config.longitude
|
|
||||||
);
|
|
||||||
|
|
||||||
//Schedule job to refresh times
|
|
||||||
cron
|
|
||||||
.schedule(
|
|
||||||
"0 12 * * *",
|
|
||||||
() => {
|
|
||||||
this._times = getTimes(
|
|
||||||
new Date(),
|
new Date(),
|
||||||
this._config.latitude,
|
this._config.latitude,
|
||||||
this._config.longitude
|
this._config.longitude
|
||||||
);
|
);
|
||||||
this._log("Updated sunset times");
|
|
||||||
},
|
|
||||||
{
|
|
||||||
scheduled: true,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
.start();
|
|
||||||
|
|
||||||
this._hue = props.hue;
|
//Schedule job to refresh times
|
||||||
this.name = this._config.name;
|
cron.schedule(
|
||||||
|
"0 12 * * *",
|
||||||
|
() => {
|
||||||
|
this._times = getTimes(
|
||||||
|
new Date(),
|
||||||
|
this._config.latitude,
|
||||||
|
this._config.longitude
|
||||||
|
);
|
||||||
|
this._log("Updated sunset times");
|
||||||
|
},
|
||||||
|
{
|
||||||
|
scheduled: true,
|
||||||
|
}
|
||||||
|
).start();
|
||||||
|
|
||||||
this.platformAccessory = new this._homebridge.platformAccessory(
|
this._hue = props.hue;
|
||||||
this.name,
|
this.name = this._config.name;
|
||||||
this.generateUUID(),
|
|
||||||
this._homebridge.hap.Accessory.Categories.SWITCH
|
|
||||||
);
|
|
||||||
|
|
||||||
//@ts-ignore
|
this.platformAccessory = new this._homebridge.platformAccessory(
|
||||||
this._infoService = new Service.AccessoryInformation();
|
this.name,
|
||||||
this._infoService.setCharacteristic(
|
this.generateUUID(),
|
||||||
Characteristic.Manufacturer,
|
this._homebridge.hap.Accessory.Categories.SWITCH
|
||||||
"Brandon Watson"
|
);
|
||||||
);
|
|
||||||
this._infoService.setCharacteristic(Characteristic.Model, "F.lux");
|
|
||||||
this._infoService.setCharacteristic(
|
|
||||||
Characteristic.SerialNumber,
|
|
||||||
"123-456-789"
|
|
||||||
);
|
|
||||||
|
|
||||||
this._switchService = new Service.Switch(this.name, "fluxService");
|
//@ts-ignore
|
||||||
|
this._infoService = new Service.AccessoryInformation();
|
||||||
|
this._infoService.setCharacteristic(
|
||||||
|
Characteristic.Manufacturer,
|
||||||
|
"Brandon Watson"
|
||||||
|
);
|
||||||
|
this._infoService.setCharacteristic(Characteristic.Model, "F.lux");
|
||||||
|
this._infoService.setCharacteristic(
|
||||||
|
Characteristic.SerialNumber,
|
||||||
|
"123-456-789"
|
||||||
|
);
|
||||||
|
|
||||||
this._switchService
|
this._switchService = new Service.Switch(this.name, "fluxService");
|
||||||
.getCharacteristic(Characteristic.On)
|
|
||||||
//@ts-ignore
|
|
||||||
.on("set", this.onSetEnabled)
|
|
||||||
//@ts-ignore
|
|
||||||
.on("get", this.onGetEnabled);
|
|
||||||
}
|
|
||||||
|
|
||||||
public name: string = "Flux";
|
this._switchService
|
||||||
|
.getCharacteristic(Characteristic.On)
|
||||||
public platformAccessory: any;
|
//@ts-ignore
|
||||||
|
.on("set", this.onSetEnabled)
|
||||||
/**
|
//@ts-ignore
|
||||||
* Handler for switch set event
|
.on("get", this.onGetEnabled);
|
||||||
* @param callback The callback function to call when complete
|
|
||||||
*/
|
|
||||||
private onSetEnabled = async (
|
|
||||||
activeState: boolean,
|
|
||||||
callback: (error?: Error | null | undefined) => void
|
|
||||||
) => {
|
|
||||||
if (activeState) {
|
|
||||||
this._times = getTimes(
|
|
||||||
new Date(),
|
|
||||||
this._config.latitude,
|
|
||||||
this._config.longitude
|
|
||||||
);
|
|
||||||
this.update();
|
|
||||||
} else {
|
|
||||||
this._isActive = false;
|
|
||||||
}
|
}
|
||||||
return callback();
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
public name: string = "Flux";
|
||||||
* Handler for switch get event
|
|
||||||
* @param callback The callback function to call when complete
|
|
||||||
*/
|
|
||||||
private onGetEnabled(
|
|
||||||
callback: (error: Error | null, value: boolean) => void
|
|
||||||
): void {
|
|
||||||
callback(null, this._isActive);
|
|
||||||
// return this._isActive;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
public platformAccessory: any;
|
||||||
* Called by homebridge to gather services.
|
|
||||||
*/
|
|
||||||
public getServices = (): Array<HAPNodeJS.Service> => {
|
|
||||||
return [this._infoService, this._switchService!];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Popuplates internal lights array using the configuration values
|
* Handler for switch set event
|
||||||
*/
|
* @param callback The callback function to call when complete
|
||||||
private getLights = async (): Promise<void> => {
|
*/
|
||||||
for (const value of this._config.lights) {
|
private onSetEnabled = async (
|
||||||
//@ts-ignore
|
activeState: boolean,
|
||||||
const light: Light = await this._hue.lights.getLightByName(value);
|
callback: (error?: Error | null | undefined) => void
|
||||||
this._lights.push(light);
|
) => {
|
||||||
}
|
if (activeState) {
|
||||||
};
|
this._times = getTimes(
|
||||||
|
new Date(),
|
||||||
private colorTempToRgb = (
|
this._config.latitude,
|
||||||
kelvin: number
|
this._config.longitude
|
||||||
): { red: number; green: number; blue: number } => {
|
);
|
||||||
var temp = kelvin / 100;
|
this.update();
|
||||||
var red, green, blue;
|
|
||||||
if (temp <= 66) {
|
|
||||||
red = 255;
|
|
||||||
green = temp;
|
|
||||||
green = 99.4708025861 * Math.log(green) - 161.1195681661;
|
|
||||||
|
|
||||||
if (temp <= 19) {
|
|
||||||
blue = 0;
|
|
||||||
} else {
|
|
||||||
blue = temp - 10;
|
|
||||||
blue = 138.5177312231 * Math.log(blue) - 305.0447927307;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
red = temp - 60;
|
|
||||||
red = 329.698727446 * Math.pow(red, -0.1332047592);
|
|
||||||
|
|
||||||
green = temp - 60;
|
|
||||||
green = 288.1221695283 * Math.pow(green, -0.0755148492);
|
|
||||||
|
|
||||||
blue = 255;
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
red: this.clamp(red, 0, 255),
|
|
||||||
green: this.clamp(green, 0, 255),
|
|
||||||
blue: this.clamp(blue, 0, 255),
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
private clamp(x: number, min: number, max: number) {
|
|
||||||
if (x < min) {
|
|
||||||
return min;
|
|
||||||
}
|
|
||||||
if (x > max) {
|
|
||||||
return max;
|
|
||||||
}
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
private isHueError = (object: any): object is HueError => {
|
|
||||||
return "_hueError" in object;
|
|
||||||
};
|
|
||||||
|
|
||||||
private setLights = async (state: LightState) => {
|
|
||||||
const promises: Array<Promise<unknown> | PromiseLike<unknown>> = [];
|
|
||||||
this._lights.map(async (light: Light) => {
|
|
||||||
try {
|
|
||||||
await this._hue.lights.setLightState(light.id, state);
|
|
||||||
} catch (err) {
|
|
||||||
if (
|
|
||||||
this.isHueError(err) &&
|
|
||||||
err.message ===
|
|
||||||
"parameter, xy, is not modifiable. Device is set to off."
|
|
||||||
) {
|
|
||||||
//Eat this
|
|
||||||
} else {
|
} else {
|
||||||
this._log(`Error while setting lights: ${err}`);
|
this._isActive = false;
|
||||||
}
|
}
|
||||||
}
|
return callback();
|
||||||
});
|
};
|
||||||
|
|
||||||
await Promise.all(promises);
|
/**
|
||||||
};
|
* Handler for switch get event
|
||||||
|
* @param callback The callback function to call when complete
|
||||||
/**
|
*/
|
||||||
* Helper function to generate a UUID
|
private onGetEnabled(
|
||||||
*/
|
callback: (error: Error | null, value: boolean) => void
|
||||||
private generateUUID(): string {
|
): void {
|
||||||
// Public Domain/MIT
|
callback(null, this._isActive);
|
||||||
var d = new Date().getTime();
|
// return this._isActive;
|
||||||
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);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets adjusted color temperature.
|
* Called by homebridge to gather services.
|
||||||
*/
|
*/
|
||||||
private getTempOffset = (
|
public getServices = (): Array<HAPNodeJS.Service> => {
|
||||||
startTemp: number,
|
return [this._infoService, this._switchService!];
|
||||||
endTemp: number,
|
};
|
||||||
startTime: Date,
|
|
||||||
endTime: Date
|
|
||||||
) => {
|
|
||||||
const now = this.getNow().getTime();
|
|
||||||
const percentComplete =
|
|
||||||
(now - startTime.getTime()) / (endTime.getTime() - startTime.getTime());
|
|
||||||
const tempRange = Math.abs(startTemp - endTemp);
|
|
||||||
const tempOffset = tempRange * percentComplete;
|
|
||||||
return startTemp - tempOffset;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current time. Use test time if present.
|
* Popuplates internal lights array using the configuration values
|
||||||
*/
|
*/
|
||||||
private getNow() {
|
private getLights = async (): Promise<void> => {
|
||||||
if (this._config.testNowDateString) {
|
for (const value of this._config.lights) {
|
||||||
return new Date(this._config.testNowDateString);
|
//@ts-ignore
|
||||||
} else {
|
const light: Light = await this._hue.lights.getLightByName(value);
|
||||||
return new Date();
|
this._lights.push(light);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private colorTempToRgb = (kelvin: number): RGB => {
|
||||||
|
var temp = kelvin / 100;
|
||||||
|
var red, green, blue;
|
||||||
|
if (temp <= 66) {
|
||||||
|
red = 255;
|
||||||
|
green = temp;
|
||||||
|
green = 99.4708025861 * Math.log(green) - 161.1195681661;
|
||||||
|
|
||||||
|
if (temp <= 19) {
|
||||||
|
blue = 0;
|
||||||
|
} else {
|
||||||
|
blue = temp - 10;
|
||||||
|
blue = 138.5177312231 * Math.log(blue) - 305.0447927307;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
red = temp - 60;
|
||||||
|
red = 329.698727446 * Math.pow(red, -0.1332047592);
|
||||||
|
|
||||||
|
green = temp - 60;
|
||||||
|
green = 288.1221695283 * Math.pow(green, -0.0755148492);
|
||||||
|
|
||||||
|
blue = 255;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
r: this.clamp(red, 0, 255),
|
||||||
|
g: this.clamp(green, 0, 255),
|
||||||
|
b: this.clamp(blue, 0, 255),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
private clamp(x: number, min: number, max: number) {
|
||||||
|
if (x < min) {
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
if (x > max) {
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
return x;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private update = async (): Promise<void> => {
|
private isHueError = (object: any): object is HueError => {
|
||||||
this._isActive = true;
|
return "_hueError" in object;
|
||||||
while (this._isActive) {
|
};
|
||||||
if (this._lights.length === 0) {
|
|
||||||
await this.getLights();
|
|
||||||
}
|
|
||||||
|
|
||||||
const now = this.getNow();
|
private setHueLights = async (state: LightState) => {
|
||||||
//Pad start time by an hour before sunset
|
const promises: Array<Promise<unknown> | PromiseLike<unknown>> = [];
|
||||||
const start = new Date(
|
this._lights.map(async (light: Light) => {
|
||||||
this._times.sunset.getTime() - 60 * MINUTES_IN_MILLISECOND
|
try {
|
||||||
);
|
await this._hue.lights.setLightState(light.id, state);
|
||||||
const sunsetStart = this._times.sunsetStart;
|
} catch (err) {
|
||||||
const sunsetEnd = new Date(
|
if (
|
||||||
this._times.sunset.getTime() + this._config.sunsetDuration
|
this.isHueError(err) &&
|
||||||
);
|
err.message ===
|
||||||
const nightStart = new Date(
|
"parameter, xy, is not modifiable. Device is set to off."
|
||||||
sunsetEnd.getTime() + 60 * MINUTES_IN_MILLISECOND
|
) {
|
||||||
);
|
//Eat this
|
||||||
const sunrise = new Date(
|
} else {
|
||||||
this._times.sunrise.getTime() + 1 * SECONDS_IN_DAY
|
this._log(`Error while setting lights: ${err}`);
|
||||||
);
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const startColorTemp = this._config.ceilingColorTemp
|
await Promise.all(promises);
|
||||||
? this._config.ceilingColorTemp
|
};
|
||||||
: 4000;
|
|
||||||
const sunsetColorTemp = this._config.sunsetColorTemp
|
|
||||||
? this._config.sunsetColorTemp
|
|
||||||
: 2800;
|
|
||||||
const floorColorTemp = this._config.floorColorTemp
|
|
||||||
? this._config.floorColorTemp
|
|
||||||
: 1900;
|
|
||||||
|
|
||||||
let newTemp = 0;
|
private setWizLights = async (rgb: RGB, fade: number): Promise<void> => {
|
||||||
|
await Promise.all(
|
||||||
if (start < now && now < sunsetStart) {
|
this._wizLights.map(async (bulb) => {
|
||||||
newTemp = this.getTempOffset(
|
const pilot = await bulb.get();
|
||||||
startColorTemp,
|
bulb.set(rgb, pilot?.dimming, fade);
|
||||||
sunsetColorTemp,
|
})
|
||||||
start,
|
|
||||||
sunsetStart
|
|
||||||
);
|
);
|
||||||
} else if (sunsetStart < now && now < sunsetEnd) {
|
return;
|
||||||
newTemp = this._config.sunsetColorTemp;
|
};
|
||||||
} else if (sunsetEnd < now && now < nightStart) {
|
|
||||||
newTemp = this.getTempOffset(
|
/**
|
||||||
sunsetColorTemp,
|
* Helper function to generate a UUID
|
||||||
floorColorTemp,
|
*/
|
||||||
sunsetEnd,
|
private generateUUID(): string {
|
||||||
nightStart
|
// 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);
|
||||||
|
}
|
||||||
);
|
);
|
||||||
} else if (nightStart < now && now < sunrise) {
|
|
||||||
newTemp = this._config.floorColorTemp;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Set lights
|
|
||||||
const rgb = this.colorTempToRgb(newTemp);
|
|
||||||
if (rgb && newTemp !== 0) {
|
|
||||||
const lightState = new LightState();
|
|
||||||
lightState
|
|
||||||
.transitionInMillis(
|
|
||||||
this._config.transition ? this._config.transition : 5000
|
|
||||||
)
|
|
||||||
.rgb(
|
|
||||||
rgb.red ? rgb.red : 0,
|
|
||||||
rgb.green ? rgb.green : 0,
|
|
||||||
rgb.blue ? rgb.blue : 0
|
|
||||||
);
|
|
||||||
await this.setLights(lightState);
|
|
||||||
this._log(`Adjusting light temp to ${newTemp}, ${JSON.stringify(rgb)}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
await Sleep(this._config.delay ? this._config.delay : 60000);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
/**
|
||||||
|
* Gets adjusted color temperature.
|
||||||
|
*/
|
||||||
|
private getTempOffset = (
|
||||||
|
startTemp: number,
|
||||||
|
endTemp: number,
|
||||||
|
startTime: Date,
|
||||||
|
endTime: Date
|
||||||
|
) => {
|
||||||
|
const now = this.getNow().getTime();
|
||||||
|
const percentComplete =
|
||||||
|
(now - startTime.getTime()) /
|
||||||
|
(endTime.getTime() - startTime.getTime());
|
||||||
|
const tempRange = Math.abs(startTemp - endTemp);
|
||||||
|
const tempOffset = tempRange * percentComplete;
|
||||||
|
return startTemp - tempOffset;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current time. Use test time if present.
|
||||||
|
*/
|
||||||
|
private getNow() {
|
||||||
|
if (this._config.testNowDateString) {
|
||||||
|
return new Date(this._config.testNowDateString);
|
||||||
|
} else {
|
||||||
|
return new Date();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private update = async (): Promise<void> => {
|
||||||
|
this._isActive = true;
|
||||||
|
while (this._isActive) {
|
||||||
|
if (this._lights.length === 0) {
|
||||||
|
await this.getLights();
|
||||||
|
}
|
||||||
|
|
||||||
|
const now = this.getNow();
|
||||||
|
//Pad start time by an hour before sunset
|
||||||
|
const start = new Date(
|
||||||
|
this._times.sunset.getTime() - 60 * MINUTES_IN_MILLISECOND
|
||||||
|
);
|
||||||
|
const sunsetStart = this._times.sunsetStart;
|
||||||
|
const sunsetEnd = new Date(
|
||||||
|
this._times.sunset.getTime() + this._config.sunsetDuration
|
||||||
|
);
|
||||||
|
const nightStart = new Date(
|
||||||
|
sunsetEnd.getTime() + 60 * MINUTES_IN_MILLISECOND
|
||||||
|
);
|
||||||
|
const sunrise = new Date(
|
||||||
|
this._times.sunrise.getTime() + 1 * SECONDS_IN_DAY
|
||||||
|
);
|
||||||
|
|
||||||
|
const startColorTemp = this._config.ceilingColorTemp
|
||||||
|
? this._config.ceilingColorTemp
|
||||||
|
: 4000;
|
||||||
|
const sunsetColorTemp = this._config.sunsetColorTemp
|
||||||
|
? this._config.sunsetColorTemp
|
||||||
|
: 2800;
|
||||||
|
const floorColorTemp = this._config.floorColorTemp
|
||||||
|
? this._config.floorColorTemp
|
||||||
|
: 1900;
|
||||||
|
|
||||||
|
let newTemp = 0;
|
||||||
|
|
||||||
|
if (start < now && now < sunsetStart) {
|
||||||
|
newTemp = this.getTempOffset(
|
||||||
|
startColorTemp,
|
||||||
|
sunsetColorTemp,
|
||||||
|
start,
|
||||||
|
sunsetStart
|
||||||
|
);
|
||||||
|
} else if (sunsetStart < now && now < sunsetEnd) {
|
||||||
|
newTemp = this._config.sunsetColorTemp;
|
||||||
|
} else if (sunsetEnd < now && now < nightStart) {
|
||||||
|
newTemp = this.getTempOffset(
|
||||||
|
sunsetColorTemp,
|
||||||
|
floorColorTemp,
|
||||||
|
sunsetEnd,
|
||||||
|
nightStart
|
||||||
|
);
|
||||||
|
} else if (nightStart < now && now < sunrise) {
|
||||||
|
newTemp = this._config.floorColorTemp;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Set lights
|
||||||
|
const hueRGB = this.colorTempToRgb(newTemp);
|
||||||
|
const wizRGB = colorTemperature2rgb(newTemp);
|
||||||
|
if (hueRGB && newTemp !== 0) {
|
||||||
|
const lightState = new LightState();
|
||||||
|
lightState
|
||||||
|
.transitionInMillis(
|
||||||
|
this._config.transition ? this._config.transition : 5000
|
||||||
|
)
|
||||||
|
.rgb(hueRGB.r ?? 0, hueRGB.g ?? 0, hueRGB.b ?? 0);
|
||||||
|
await this.setHueLights(lightState);
|
||||||
|
await this.setWizLights(
|
||||||
|
wizRGB,
|
||||||
|
this._config.transition ? this._config.transition / 1000 : 5
|
||||||
|
);
|
||||||
|
this._log(
|
||||||
|
`Adjusting light temp to ${newTemp}, ${JSON.stringify(
|
||||||
|
hueRGB
|
||||||
|
)}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await Sleep(this._config.delay ? this._config.delay : 60000);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
97
src/index.ts
97
src/index.ts
@ -1,28 +1,25 @@
|
|||||||
import { IConfig } from "./models/iConfig";
|
import { IConfig } 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";
|
||||||
import { IAccessory } from "./models/iAccessory";
|
import { IAccessory } from "./models/iAccessory";
|
||||||
import { FluxAccessory } from "./fluxAccessory";
|
import { FluxAccessory } from "./fluxAccessory";
|
||||||
|
import { WizBulb } from "@watsonb8/wiz-lib/build/wizBulb";
|
||||||
|
import discover from "@watsonb8/wiz-lib/build/discovery";
|
||||||
|
|
||||||
let Accessory: any;
|
let Accessory: any;
|
||||||
let Homebridge: any;
|
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-flux", "Flux", FluxPlatform, true);
|
||||||
'homebridge-flux',
|
}
|
||||||
'Flux',
|
|
||||||
FluxPlatform,
|
|
||||||
true
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
class FluxPlatform {
|
class FluxPlatform {
|
||||||
log: any = {};
|
log: any = {};
|
||||||
@ -31,50 +28,76 @@ class FluxPlatform {
|
|||||||
config: IConfig;
|
config: IConfig;
|
||||||
hue: Api | undefined;
|
hue: Api | undefined;
|
||||||
|
|
||||||
|
|
||||||
constructor(log: any, config: any, api: any) {
|
constructor(log: any, config: any, api: any) {
|
||||||
this.log = log;
|
this.log = log;
|
||||||
this.api = api;
|
this.api = api;
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.log('INFO - Registering Flux platform');
|
this.log("INFO - Registering Flux platform");
|
||||||
this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this));
|
this.api.on("didFinishLaunching", this.didFinishLaunching.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private connectWiz = async () => {
|
||||||
|
if (!this.config) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return await discover();
|
||||||
|
};
|
||||||
|
|
||||||
private connectHue = async () => {
|
private connectHue = async () => {
|
||||||
if (!this.config) {
|
if (!this.config) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.config.userName && this.config.clientKey) {
|
if (this.config.userName && this.config.clientKey) {
|
||||||
this.hue = await v3.api.createLocal(this.config.ipAddress).connect(this.config.userName, this.config.clientKey, undefined);
|
this.hue = await v3.api
|
||||||
|
.createLocal(this.config.ipAddress)
|
||||||
|
.connect(
|
||||||
|
this.config.userName,
|
||||||
|
this.config.clientKey,
|
||||||
|
undefined
|
||||||
|
);
|
||||||
this.log("Using existing connection info");
|
this.log("Using existing connection info");
|
||||||
} else {
|
} else {
|
||||||
const unauthenticatedApi = await v3.api.createLocal(this.config.ipAddress).connect(undefined, undefined, undefined);
|
const unauthenticatedApi = await v3.api
|
||||||
|
.createLocal(this.config.ipAddress)
|
||||||
|
.connect(undefined, undefined, undefined);
|
||||||
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("homebridge", "HueChase");
|
createdUser = await unauthenticatedApi.users.createUser(
|
||||||
|
"homebridge",
|
||||||
|
"HueChase"
|
||||||
|
);
|
||||||
|
|
||||||
this.hue = await v3.api.createLocal(this.config.ipAddress).connect(createdUser.username, createdUser.clientKey, undefined);
|
this.hue = await v3.api
|
||||||
|
.createLocal(this.config.ipAddress)
|
||||||
|
.connect(
|
||||||
|
createdUser.username,
|
||||||
|
createdUser.clientKey,
|
||||||
|
undefined
|
||||||
|
);
|
||||||
this.log("Connected to Hue Bridge");
|
this.log("Connected to Hue Bridge");
|
||||||
this.log(`UserName: ${createdUser.username}, ClientKey: ${createdUser.clientkey}`)
|
this.log(
|
||||||
|
`UserName: ${createdUser.username}, ClientKey: ${createdUser.clientkey}`
|
||||||
|
);
|
||||||
connected = true;
|
connected = true;
|
||||||
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
if (err.getHueErrorType() === 101) {
|
if (err.getHueErrorType() === 101) {
|
||||||
this.log('The Link button on the bridge was not pressed. Please press the Link button and try again.');
|
this.log(
|
||||||
|
"The Link button on the bridge was not pressed. Please press the Link button and try again."
|
||||||
|
);
|
||||||
Sleep(5000);
|
Sleep(5000);
|
||||||
} else {
|
} else {
|
||||||
this.log(`Unexpected Error: ${err.message}`);
|
this.log(`Unexpected Error: ${err.message}`);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for didFinishLaunching
|
* Handler for didFinishLaunching
|
||||||
@ -86,20 +109,26 @@ class FluxPlatform {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Called by homebridge to gather accessories.
|
* Called by homebridge to gather accessories.
|
||||||
* @param callback
|
* @param callback
|
||||||
*/
|
*/
|
||||||
public accessories = async (callback: (accessories: Array<IAccessory>) => void) => {
|
public accessories = async (
|
||||||
|
callback: (accessories: Array<IAccessory>) => void
|
||||||
|
) => {
|
||||||
//Connect to hue bridge
|
//Connect to hue bridge
|
||||||
await this.connectHue();
|
await this.connectHue();
|
||||||
|
const wizBulbs = await this.connectWiz();
|
||||||
|
|
||||||
this.accessoryList.push(new FluxAccessory({
|
this.accessoryList.push(
|
||||||
api: this.api,
|
new FluxAccessory({
|
||||||
log: this.log,
|
api: this.api,
|
||||||
homebridge: Homebridge,
|
log: this.log,
|
||||||
hue: this.hue!,
|
homebridge: Homebridge,
|
||||||
config: this.config
|
hue: this.hue!,
|
||||||
}));
|
wizBulbs: wizBulbs ?? [],
|
||||||
|
config: this.config,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
callback(this.accessoryList);
|
callback(this.accessoryList);
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user