13 Commits
1.0.0 ... 1.2.3

Author SHA1 Message Date
f8320319a4 Restricting build to internal lan
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is failing
2022-01-07 16:58:29 -05:00
779ffac227 Adding deployment steps
All checks were successful
continuous-integration/drone/push Build is passing
2022-01-07 16:56:05 -05:00
55c65dac84 Fixing issue where flux was unresponsive
All checks were successful
continuous-integration/drone/push Build is passing
2022-01-03 13:13:50 -05:00
8825a116fe Moving homebridge to dev deps
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-29 18:00:11 -05:00
a31ec7c282 Fixing publish step
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-29 17:43:55 -05:00
5e697021ed Bump version
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-29 17:42:48 -05:00
f935896cf2 Update homebridge version
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-23 15:34:34 -05:00
829e45b329 Fix build
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-23 15:32:41 -05:00
728d3a64a2 Update package versions
Some checks failed
continuous-integration/drone/push Build is failing
2021-12-22 21:06:48 -05:00
362a5e9d7c Rebuilding package lock
Some checks failed
continuous-integration/drone/push Build is failing
2021-12-22 21:03:10 -05:00
b5fe4a64b9 Updating registry
Some checks failed
continuous-integration/drone/push Build is failing
2021-12-22 20:58:24 -05:00
c80b01d3e2 Update drone.yml
Some checks reported errors
continuous-integration/drone Build was killed
2021-12-22 20:54:14 -05:00
87e6ff99f6 Adding typescript dev dep
Some checks failed
continuous-integration/drone/push Build is failing
2021-06-03 20:23:11 -04:00
7 changed files with 1361 additions and 797 deletions

View File

@ -2,40 +2,129 @@ kind: pipeline
type: docker type: docker
name: default name: default
clone: node:
disable: true lan: internal
steps: steps:
- name: clone
image: alpine/git
commands:
- git clone https://gitea.watsonlabs.net/watsonb8/homebridge-flux.git .
- git checkout $DRONE_COMMIT
- name: build - name: build
image: node image: node
commands: commands:
- npm install - npm install
- npm run build - npm run build
- name: publish - name: version
image: node
commands:
- export version=`node -p "require('./package.json').version"`
- export commit=`echo $DRONE_COMMIT | cut -c1-5`
- npm version prerelease --preid=$commit --git-tag-version=false --allow-same-version=true
when:
event:
exclude:
- tag
- pull_request
branch:
include:
- master
- name: publish pre
image: plugins/npm image: plugins/npm
settings: settings:
username: admin username:
from_secret: npm_username
password: password:
from_secret: npm_password from_secret: npm_password
email: brandon@watsonlabs.net email: b.watson@watsonlabs.net
registry: "http://linuxhost.me:4873/" registry: "http://10.44.1.6:4873/"
when:
event:
exclude:
- tag
- pull_request
branch:
include:
- master
- name: publish tagged version
image: plugins/npm
settings:
username:
from_secret: npm_username
password:
from_secret: npm_password
email: b.watson@watsonlabs.net
registry: "http://10.44.1.6:4873/"
when: when:
event: event:
- tag - tag
exclude:
- pull_request
notify: - name: remove old package
image: appleboy/drone-ssh
environment:
SSH_USER:
from_secret: ssh_user
settings:
host: homebridge.me
envs:
- SSH_USER
username:
from_secret: ssh_user
key:
from_secret: ssh_key
port: 22
script:
- rm -r /home/$SSH_USER/.npm-global/lib/node_modules/@watsonb8/homebridge-flux
when:
event:
- tag
exclude:
- pull_request
- name: deploy
image: appleboy/drone-ssh
settings:
host: homebridge.me
username:
from_secret: ssh_user
key:
from_secret: ssh_key
port: 22
script:
- npm install -g @watsonb8/homebridge-flux --registry http://10.44.1.6:4873
when:
event:
- tag
exclude:
- pull_request
- name: restart homebridge
image: appleboy/drone-ssh
settings:
host: homebridge.me
username:
from_secret: elevated_ssh_user
key:
from_secret: ssh_key
port: 22
script:
- systemctl restart homebridge
when:
event:
- tag
exclude:
- pull_request
- name: Notify
image: drillster/drone-email image: drillster/drone-email
host: smtp.watsonlabs.net settings:
host: 10.44.1.13
username: srvGitea username: srvGitea
password: password:
from_secret: smtp_password from_secret: smtp_password
from: drone@watsonlabs.net from: drone@watsonlabs.net
skip_verify: true
when: when:
status: [failure] status:
- failure

9
.vscode/launch.json vendored
View File

@ -8,9 +8,12 @@
"type": "node", "type": "node",
"request": "launch", "request": "launch",
"name": "Launch Program", "name": "Launch Program",
"preLaunchTask": "build and install", "preLaunchTask": "build",
"program": "/Users/brandonwatson/.npm-global/bin/homebridge", "program": "/Users/brandonwatson/.nvm/versions/node/v14.15.0/lib/node_modules/homebridge/bin/homebridge",
"sourceMaps": true, "env": {
"HOMEBRIDGE_OPTS": "/Users/brandonwatson/.homebridge"
},
"sourceMaps": true
} }
] ]
} }

1
.vscode/tasks.json vendored
View File

@ -6,6 +6,7 @@
{ {
"type": "npm", "type": "npm",
"script": "build", "script": "build",
"label": "build",
"problemMatcher": [] "problemMatcher": []
}, },
{ {

865
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,11 @@
{ {
"name": "homebridge-flux", "name": "@watsonb8/homebridge-flux",
"version": "1.0.0", "version": "1.1.3",
"description": "", "description": "",
"main": "bin/index.js", "main": "bin/index.js",
"publishConfig": {
"registry": "http://10.44.1.6:4873/"
},
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
@ -26,12 +29,13 @@
"dependencies": { "dependencies": {
"@types/node-cron": "^2.0.3", "@types/node-cron": "^2.0.3",
"@types/suncalc": "^1.8.0", "@types/suncalc": "^1.8.0",
"homebridge": "^0.4.53",
"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"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^13.11.1" "@types/node": "^13.11.1",
"homebridge": "^1.3.9",
"typescript": "^4.5.4"
} }
} }

View File

@ -19,7 +19,7 @@ export interface IFluxProps {
api: any; api: any;
log: any; log: any;
homebridge: any; homebridge: any;
hue: Api, hue: Api;
config: IConfig; config: IConfig;
} }
@ -50,35 +50,58 @@ export class FluxAccessory implements IAccessory {
this._homebridge = props.homebridge; this._homebridge = props.homebridge;
this._isActive = false; this._isActive = false;
this._times = getTimes(new Date(), this._config.latitude, this._config.longitude); this._times = getTimes(
new Date(),
this._config.latitude,
this._config.longitude
);
//Schedule job to refresh times //Schedule job to refresh times
cron.schedule("0 12 * * *", () => { cron
this._times = getTimes(new Date(), this._config.latitude, this._config.longitude); .schedule(
"0 12 * * *",
() => {
this._times = getTimes(
new Date(),
this._config.latitude,
this._config.longitude
);
this._log("Updated sunset times"); this._log("Updated sunset times");
}, { },
scheduled: true {
}).start(); scheduled: true,
}
)
.start();
this._hue = props.hue; this._hue = props.hue;
this.name = this._config.name; this.name = this._config.name;
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 //@ts-ignore
this._infoService = new Service.AccessoryInformation(); this._infoService = new Service.AccessoryInformation();
this._infoService.setCharacteristic(Characteristic.Manufacturer, "Brandon Watson") this._infoService.setCharacteristic(
this._infoService.setCharacteristic(Characteristic.Model, "F.lux") Characteristic.Manufacturer,
this._infoService.setCharacteristic(Characteristic.SerialNumber, "123-456-789"); "Brandon Watson"
);
this._infoService.setCharacteristic(Characteristic.Model, "F.lux");
this._infoService.setCharacteristic(
Characteristic.SerialNumber,
"123-456-789"
);
this._switchService = new Service.Switch( this._switchService = new Service.Switch(this.name, "fluxService");
this.name,
'fluxService'
)
this._switchService.getCharacteristic(Characteristic.On) this._switchService
.getCharacteristic(Characteristic.On)
//@ts-ignore //@ts-ignore
.on("set", this.onSetEnabled) .on("set", this.onSetEnabled)
//@ts-ignore
.on("get", this.onGetEnabled); .on("get", this.onGetEnabled);
} }
@ -90,31 +113,40 @@ export class FluxAccessory implements IAccessory {
* Handler for switch set event * Handler for switch set event
* @param callback The callback function to call when complete * @param callback The callback function to call when complete
*/ */
private onSetEnabled = async (activeState: boolean, callback: (error?: Error | null | undefined) => void) => { private onSetEnabled = async (
activeState: boolean,
callback: (error?: Error | null | undefined) => void
) => {
if (activeState) { if (activeState) {
this._times = getTimes(new Date(), this._config.latitude, this._config.longitude); this._times = getTimes(
new Date(),
this._config.latitude,
this._config.longitude
);
this.update(); this.update();
} else { } else {
this._isActive = false; this._isActive = false;
} }
return callback(); return callback();
} };
/** /**
* Handler for switch get event * Handler for switch get event
* @param callback The callback function to call when complete * @param callback The callback function to call when complete
*/ */
private onGetEnabled = (callback: (error: Error | null, value: boolean) => void) => { private onGetEnabled(
return callback(null, this._isActive); callback: (error: Error | null, value: boolean) => void
): void {
callback(null, this._isActive);
// return this._isActive;
} }
/** /**
* Called by homebridge to gather services. * Called by homebridge to gather services.
*/ */
public getServices = (): Array<HAPNodeJS.Service> => { public getServices = (): Array<HAPNodeJS.Service> => {
return [this._infoService, this._switchService!]; return [this._infoService, this._switchService!];
} };
/** /**
* Popuplates internal lights array using the configuration values * Popuplates internal lights array using the configuration values
@ -122,12 +154,14 @@ export class FluxAccessory implements IAccessory {
private getLights = async (): Promise<void> => { private getLights = async (): Promise<void> => {
for (const value of this._config.lights) { for (const value of this._config.lights) {
//@ts-ignore //@ts-ignore
const light: Light = await this._hue.lights.getLightByName(value) const light: Light = await this._hue.lights.getLightByName(value);
this._lights.push(light); this._lights.push(light);
} }
} };
private colorTempToRgb = (kelvin: number): { red: number, green: number, blue: number } => { private colorTempToRgb = (
kelvin: number
): { red: number; green: number; blue: number } => {
var temp = kelvin / 100; var temp = kelvin / 100;
var red, green, blue; var red, green, blue;
if (temp <= 66) { if (temp <= 66) {
@ -153,19 +187,23 @@ export class FluxAccessory implements IAccessory {
return { return {
red: this.clamp(red, 0, 255), red: this.clamp(red, 0, 255),
green: this.clamp(green, 0, 255), green: this.clamp(green, 0, 255),
blue: this.clamp(blue, 0, 255) blue: this.clamp(blue, 0, 255),
} };
} };
private clamp(x: number, min: number, max: number) { private clamp(x: number, min: number, max: number) {
if (x < min) { return min; } if (x < min) {
if (x > max) { return max; } return min;
}
if (x > max) {
return max;
}
return x; return x;
} }
private isHueError = (object: any): object is HueError => { private isHueError = (object: any): object is HueError => {
return '_hueError' in object; return "_hueError" in object;
} };
private setLights = async (state: LightState) => { private setLights = async (state: LightState) => {
const promises: Array<Promise<unknown> | PromiseLike<unknown>> = []; const promises: Array<Promise<unknown> | PromiseLike<unknown>> = [];
@ -173,7 +211,11 @@ export class FluxAccessory implements IAccessory {
try { try {
await this._hue.lights.setLightState(light.id, state); await this._hue.lights.setLightState(light.id, state);
} catch (err) { } catch (err) {
if (this.isHueError(err) && err.message === "parameter, xy, is not modifiable. Device is set to off.") { if (
this.isHueError(err) &&
err.message ===
"parameter, xy, is not modifiable. Device is set to off."
) {
//Eat this //Eat this
} else { } else {
this._log(`Error while setting lights: ${err}`); this._log(`Error while setting lights: ${err}`);
@ -181,35 +223,47 @@ export class FluxAccessory implements IAccessory {
} }
}); });
await Promise.all(promises); await Promise.all(promises);
} };
/** /**
* Helper function to generate a UUID * Helper function to generate a UUID
*/ */
private generateUUID(): string { // Public Domain/MIT private generateUUID(): string {
// Public Domain/MIT
var d = new Date().getTime(); var d = new Date().getTime();
if (typeof performance !== 'undefined' && typeof performance.now === 'function') { if (
typeof performance !== "undefined" &&
typeof performance.now === "function"
) {
d += performance.now(); //use high-precision timer if available d += performance.now(); //use high-precision timer if available
} }
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
/[xy]/g,
function (c) {
var r = (d + Math.random() * 16) % 16 | 0; var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16); d = Math.floor(d / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16); return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
}); }
);
} }
/** /**
* Gets adjusted color temperature. * Gets adjusted color temperature.
*/ */
private getTempOffset = (startTemp: number, endTemp: number, startTime: Date, endTime: Date) => { private getTempOffset = (
const now = this.getNow().getTime() startTemp: number,
const percentComplete = ((now - startTime.getTime()) / (endTime.getTime() - startTime.getTime())); 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 tempRange = Math.abs(startTemp - endTemp);
const tempOffset = tempRange * percentComplete const tempOffset = tempRange * percentComplete;
return startTemp - tempOffset; return startTemp - tempOffset;
} };
/** /**
* Get the current time. Use test time if present. * Get the current time. Use test time if present.
@ -231,25 +285,49 @@ export class FluxAccessory implements IAccessory {
const now = this.getNow(); const now = this.getNow();
//Pad start time by an hour before sunset //Pad start time by an hour before sunset
const start = new Date(this._times.sunset.getTime() - (60 * MINUTES_IN_MILLISECOND)); const start = new Date(
this._times.sunset.getTime() - 60 * MINUTES_IN_MILLISECOND
);
const sunsetStart = this._times.sunsetStart; const sunsetStart = this._times.sunsetStart;
const sunsetEnd = new Date(this._times.sunset.getTime() + this._config.sunsetDuration); const sunsetEnd = new Date(
const nightStart = new Date(sunsetEnd.getTime() + 60 * MINUTES_IN_MILLISECOND); this._times.sunset.getTime() + this._config.sunsetDuration
const sunrise = new Date(this._times.sunrise.getTime() + 1 * SECONDS_IN_DAY); );
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 startColorTemp = this._config.ceilingColorTemp
const sunsetColorTemp = this._config.sunsetColorTemp ? this._config.sunsetColorTemp : 2800; ? this._config.ceilingColorTemp
const floorColorTemp = this._config.floorColorTemp ? this._config.floorColorTemp : 1900; : 4000;
const sunsetColorTemp = this._config.sunsetColorTemp
? this._config.sunsetColorTemp
: 2800;
const floorColorTemp = this._config.floorColorTemp
? this._config.floorColorTemp
: 1900;
let newTemp = 0; let newTemp = 0;
if ((start < now) && (now < sunsetStart)) { if (start < now && now < sunsetStart) {
newTemp = this.getTempOffset(startColorTemp, sunsetColorTemp, start, sunsetStart); newTemp = this.getTempOffset(
} else if ((sunsetStart < now) && (now < sunsetEnd)) { startColorTemp,
sunsetColorTemp,
start,
sunsetStart
);
} else if (sunsetStart < now && now < sunsetEnd) {
newTemp = this._config.sunsetColorTemp; newTemp = this._config.sunsetColorTemp;
} else if ((sunsetEnd < now) && (now < nightStart)) { } else if (sunsetEnd < now && now < nightStart) {
newTemp = this.getTempOffset(sunsetColorTemp, floorColorTemp, sunsetEnd, nightStart); newTemp = this.getTempOffset(
} else if ((nightStart < now) && (now < sunrise)) { sunsetColorTemp,
floorColorTemp,
sunsetEnd,
nightStart
);
} else if (nightStart < now && now < sunrise) {
newTemp = this._config.floorColorTemp; newTemp = this._config.floorColorTemp;
} }
@ -258,13 +336,19 @@ export class FluxAccessory implements IAccessory {
if (rgb && newTemp !== 0) { if (rgb && newTemp !== 0) {
const lightState = new LightState(); const lightState = new LightState();
lightState lightState
.transitionInMillis(this._config.transition ? this._config.transition : 5000) .transitionInMillis(
.rgb(rgb.red ? rgb.red : 0, rgb.green ? rgb.green : 0, rgb.blue ? rgb.blue : 0); this._config.transition ? this._config.transition : 5000
await this.setLights(lightState) )
this._log(`Adjusting light temp to ${newTemp}, ${JSON.stringify(rgb)}`) .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); await Sleep(this._config.delay ? this._config.delay : 60000);
} }
} };
} }

View File

@ -62,7 +62,7 @@ class FluxPlatform {
this.log(`UserName: ${createdUser.username}, ClientKey: ${createdUser.clientkey}`) this.log(`UserName: ${createdUser.username}, ClientKey: ${createdUser.clientkey}`)
connected = true; connected = true;
} catch (err) { } 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);