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
This commit is contained in:
Brandon Watson 2022-09-13 10:12:34 -05:00
parent abb66eb26f
commit e61ec0cc3c
7 changed files with 81 additions and 158 deletions

View File

@ -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
}
}
},

14
package-lock.json generated
View File

@ -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",

View File

@ -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"
}
}
}

View File

@ -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<void> => {
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<void> => {
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<void> => {
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);
}
};
}

View File

@ -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<IAccessory> = [];
// 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<IAccessory>) => 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);
// };
// }

View File

@ -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;

View File

@ -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";