From e61ec0cc3c2d04d3b1f8d2e7c0cc978c44f85c1e Mon Sep 17 00:00:00 2001 From: Brandon Watson Date: Tue, 13 Sep 2022 10:12:34 -0500 Subject: [PATCH] Stability fixes Updating longitude type Only updating wiz bulb if pilot is not undefined Fixing issue where lights will turn off after receiving power Fixing bug where wiz lights would go dark instead of maintaining the current brightness --- config.schema.json | 17 +++++- package-lock.json | 14 ++--- package.json | 4 +- src/fluxAccessory.ts | 62 ++++++++++++++++--- src/index.ts | 135 ------------------------------------------ src/models/iConfig.ts | 4 +- src/platform.ts | 3 - 7 files changed, 81 insertions(+), 158 deletions(-) diff --git a/config.schema.json b/config.schema.json index 17a0ffb..5aa5318 100644 --- a/config.schema.json +++ b/config.schema.json @@ -32,9 +32,14 @@ }, "longitude": { "title": "Longitude", - "type": "string", + "type": "number", "required": true }, + "testNowDateString": { + "title": "Test Date Time", + "type": "string", + "required": false + }, "hueLights": { "title": "Hue Lights", "type": "array", @@ -51,6 +56,11 @@ "title": "Cron Schedule", "type": "string", "required": false + }, + "on": { + "title": "On", + "type": "boolean", + "required": false } } }, @@ -77,6 +87,11 @@ "title": "Cron Schedule", "type": "string", "required": false + }, + "on": { + "title": "On", + "type": "boolean", + "required": false } } }, diff --git a/package-lock.json b/package-lock.json index c8a35a0..15a1b37 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "dependencies": { "@types/node-cron": "^2.0.3", "@types/suncalc": "^1.8.0", - "@watsonb8/wiz-lib": "^1.0.1-62427.0", + "@watsonb8/wiz-lib": "^1.0.1-e1c84.0", "node-cron": "^2.0.3", "node-hue-api": "^4.0.5", "suncalc": "^1.8.0" @@ -119,9 +119,9 @@ "integrity": "sha512-XLD/llTSB6EBe3thkN+/I0L+yCTB6sjrcVovQdx2Cnl6N6bTzHmwe/J8mWnsXFgxLrj/emzdv8IR4evKYG2qxQ==" }, "node_modules/@watsonb8/wiz-lib": { - "version": "1.0.1-18e79.0", - "resolved": "http://10.44.1.6:4873/@watsonb8%2fwiz-lib/-/wiz-lib-1.0.1-18e79.0.tgz", - "integrity": "sha1-zJ6sEawCthW3Z9a02dEXFjnrOoo=", + "version": "1.0.1-e1c84.0", + "resolved": "http://10.44.1.6:4873/@watsonb8%2fwiz-lib/-/wiz-lib-1.0.1-e1c84.0.tgz", + "integrity": "sha1-3dEF3v2j/oS7ezEBGCfq8wIhBng=", "dependencies": { "dgram": "^1.0.1", "getmac": "^5.20.0", @@ -1967,9 +1967,9 @@ "integrity": "sha512-XLD/llTSB6EBe3thkN+/I0L+yCTB6sjrcVovQdx2Cnl6N6bTzHmwe/J8mWnsXFgxLrj/emzdv8IR4evKYG2qxQ==" }, "@watsonb8/wiz-lib": { - "version": "1.0.1-18e79.0", - "resolved": "http://10.44.1.6:4873/@watsonb8%2fwiz-lib/-/wiz-lib-1.0.1-18e79.0.tgz", - "integrity": "sha1-zJ6sEawCthW3Z9a02dEXFjnrOoo=", + "version": "1.0.1-e1c84.0", + "resolved": "http://10.44.1.6:4873/@watsonb8%2fwiz-lib/-/wiz-lib-1.0.1-e1c84.0.tgz", + "integrity": "sha1-3dEF3v2j/oS7ezEBGCfq8wIhBng=", "requires": { "dgram": "^1.0.1", "getmac": "^5.20.0", diff --git a/package.json b/package.json index 3e00975..a8d4697 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "dependencies": { "@types/node-cron": "^2.0.3", "@types/suncalc": "^1.8.0", - "@watsonb8/wiz-lib": "^1.0.1-62427.0", + "@watsonb8/wiz-lib": "^1.0.1-e1c84.0", "node-cron": "^2.0.3", "node-hue-api": "^4.0.5", "suncalc": "^1.8.0" @@ -44,4 +44,4 @@ "homebridge": "^1.5.0", "typescript": "^4.5.4" } -} \ No newline at end of file +} diff --git a/src/fluxAccessory.ts b/src/fluxAccessory.ts index bcc4c8c..ec1a6e5 100644 --- a/src/fluxAccessory.ts +++ b/src/fluxAccessory.ts @@ -130,6 +130,8 @@ export class FluxAccessory { .on("set", this.onSetEnabled) //@ts-ignore .on("get", this.onGetEnabled); + + // this.test(); } public name: string = "Flux"; @@ -237,7 +239,7 @@ export class FluxAccessory { const sunsetColorTemp = this._config.sunsetColorTemp ?? 2800; const floorColorTemp = this._config.floorColorTemp ?? 1900; - let newTemp = 0; + let newTemp = this._config.ceilingColorTemp; if (start < now && now < sunsetStart) { newTemp = this.getTempOffset( @@ -281,7 +283,10 @@ export class FluxAccessory { return cron.schedule( schedule, async () => { - await this.updateHueLight(light); + await this.updateHueLight( + light, + hueLightConfig?.on ?? false + ); this._platform.log.info("Updated hues"); }, { @@ -303,7 +308,10 @@ export class FluxAccessory { return cron.schedule( schedule, async () => { - await this.updateWizLight(wizBulb); + await this.updateWizLight( + wizBulb, + wizLightConfig?.on ?? false + ); this._platform.log.info("Updated hues"); }, { @@ -313,14 +321,29 @@ export class FluxAccessory { }); } - private updateWizLight = async (wizBulb: WizBulb): Promise => { - const pilot = await wizBulb.get(); - this._platform.log.info(`Adjusting wiz bulb: ${wizBulb.getMac()}`); - wizBulb.set(this._wizRGB, pilot?.state ? pilot.dimming : 0, this._fade); + private updateWizLight = async ( + wizBulb: WizBulb, + on: Boolean + ): Promise => { + let pilot; + try { + pilot = await wizBulb.get(); + } catch (err: any) { + this._platform.log.error(err.message); + } + if (pilot) { + this._platform.log.info(`Adjusting wiz bulb: ${wizBulb.getMac()}`); + await wizBulb.set( + this._wizRGB, + on ? 100 : pilot.dimming, + this._fade + ); + } }; private updateHueLight = async ( - hueLight: Light | undefined + hueLight: Light | undefined, + on: Boolean ): Promise => { if (!hueLight) { return; @@ -332,6 +355,10 @@ export class FluxAccessory { lightState .transitionInMillis(this._fade) .rgb(this._hueRGB.r ?? 0, this._hueRGB.g ?? 0, this._hueRGB.b ?? 0); + + if (on) { + lightState.brightness(100).on(true); + } try { await this._hue.lights.setLightState(hueLight.id, lightState); } catch (err) { @@ -354,4 +381,23 @@ export class FluxAccessory { private disable() { this._tasks.forEach((task) => task.stop()); } + + private test = async () => { + for (let i = 2500; i > 0; i--) { + this._platform.log.info(`i: ${i}`); + for (const wizBulb of this._wizLights) { + let pilot; + try { + pilot = await wizBulb.get(); + } catch (err: any) { + this._platform.log.error(err.message); + } + this._platform.log.info( + `Adjusting wiz bulb: ${wizBulb.getMac()}` + ); + wizBulb.set(colorTemperature2rgb(i), 100, this._fade); + } + await Sleep(100); + } + }; } diff --git a/src/index.ts b/src/index.ts index 07df7aa..b617276 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,138 +9,3 @@ import { Platform } from "./platform"; export = (api: API) => { api.registerPlatform(PLATFORM_NAME, Platform); }; - -// import { IConfig } from "./models/iConfig"; -// import { v3 } from "node-hue-api"; -// import LocalBootstrap = require("node-hue-api/lib/api/http/LocalBootstrap"); -// import Api = require("node-hue-api/lib/api/Api"); -// import { Sleep } from "./sleep"; -// import { IAccessory } from "./models/iAccessory"; -// import { FluxAccessory } from "./fluxAccessory"; -// import { WizBulb } from "@watsonb8/wiz-lib/build/wizBulb"; -// import discover from "@watsonb8/wiz-lib/build/discovery"; - -// let Accessory: any; -// let Homebridge: any; - -// /** -// * Main entry. -// * @param homebridge -// */ -// export default function (homebridge: any) { -// Homebridge = homebridge; -// Accessory = homebridge.platformAccessory; -// homebridge.registerPlatform("homebridge-flux", "Flux", FluxPlatform, true); -// } - -// class FluxPlatform { -// log: any = {}; -// api: any; -// accessoryList: Array = []; -// config: IConfig; -// hue: Api | undefined; - -// constructor(log: any, config: any, api: any) { -// this.log = log; -// this.api = api; -// this.config = config; -// this.log("INFO - Registering Flux platform"); -// this.api.on("didFinishLaunching", this.didFinishLaunching.bind(this)); -// } - -// private connectWiz = async () => { -// if (!this.config) { -// return; -// } - -// return await discover(); -// }; - -// private connectHue = async () => { -// if (!this.config) { -// return; -// } - -// 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.log("Using existing connection info"); -// } else { -// const unauthenticatedApi = await v3.api -// .createLocal(this.config.ipAddress) -// .connect(undefined, undefined, undefined); -// let createdUser; -// let connected = false; -// while (!connected) { -// try { -// this.log("Creating hue user. Push link button"); -// createdUser = await unauthenticatedApi.users.createUser( -// "homebridge", -// "HueChase" -// ); - -// this.hue = await v3.api -// .createLocal(this.config.ipAddress) -// .connect( -// createdUser.username, -// createdUser.clientKey, -// undefined -// ); -// this.log("Connected to Hue Bridge"); -// this.log( -// `UserName: ${createdUser.username}, ClientKey: ${createdUser.clientkey}` -// ); -// connected = true; -// } catch (err: any) { -// 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 -// * Happens after constructor -// */ -// private didFinishLaunching() { -// this.log(`INFO - Done registering Flux platform`); -// } - -// /** -// * Called by homebridge to gather accessories. -// * @param callback -// */ -// public accessories = async ( -// callback: (accessories: Array) => void -// ) => { -// //Connect to hue bridge -// await this.connectHue(); -// const wizBulbs = await this.connectWiz(); - -// this.accessoryList.push( -// new FluxAccessory({ -// api: this.api, -// log: this.log, -// homebridge: Homebridge, -// hue: this.hue!, -// wizBulbs: wizBulbs ?? [], -// config: this.config, -// }) -// ); - -// callback(this.accessoryList); -// }; -// } diff --git a/src/models/iConfig.ts b/src/models/iConfig.ts index 702adcb..378ad70 100644 --- a/src/models/iConfig.ts +++ b/src/models/iConfig.ts @@ -17,12 +17,12 @@ export interface IConfig { /** * The list of lights to affect */ - hueLights: Array<{ name: string; cron?: string }>; + hueLights: Array<{ name: string; cron?: string; on?: boolean }>; /** * The list of wiz lights to affect */ - wizLights: Array<{ ip: string; mac: string; cron?: string }>; + wizLights: Array<{ ip: string; mac: string; cron?: string; on?: boolean }>; wizDiscoveryEnabled: boolean; diff --git a/src/platform.ts b/src/platform.ts index 2f9292f..6c7d55d 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -2,13 +2,10 @@ import { Discover } from "@watsonb8/wiz-lib/build/discovery"; import { WizBulb } from "@watsonb8/wiz-lib/build/wizBulb"; import { API, - Characteristic, DynamicPlatformPlugin, Logger, - Logging, PlatformAccessory, PlatformConfig, - Service, UnknownContext, } from "homebridge"; import { v3 } from "node-hue-api";