Updating drone config
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Brandon Watson 2021-12-23 15:58:25 -05:00
parent 1c56844be2
commit 3564a34502
4 changed files with 396 additions and 324 deletions

View File

@ -2,40 +2,69 @@ kind: pipeline
type: docker type: docker
name: default name: default
clone:
disable: true
steps: steps:
- name: clone
image: alpine/git
commands:
- git clone https://gitea.watsonlabs.net/watsonb8/homebridge-hue-chase.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
settings:
username:
from_secret: npm_username
password:
from_secret: npm_password
email: brandon@watsonlabs.net
registry: "http://10.44.1.6:4873/"
when:
event:
exclude:
- tag
- pull_request
branch:
include:
- master
- name: publish tagged version
image: plugins/npm image: plugins/npm
settings: settings:
username: admin username: admin
password: password:
from_secret: npm_password from_secret: npm_password
email: brandon@watsonlabs.net email: brandon@watsonlabs.net
registry: "http://linuxhost.me:4873/" registry: "http://10.44.1.6:4873/"
when: when:
event: event:
- tag - tag
exclude:
- pull_request
notify: - 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

View File

@ -1,8 +1,11 @@
{ {
"name": "homebridge-hue-chase", "name": "@watsonb8/homebridge-hue-chase",
"version": "1.1.0", "version": "1.1.0",
"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": "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"

View File

@ -14,7 +14,7 @@ export interface IChaseProps {
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 {
@ -49,29 +49,40 @@ export class Chase implements IAccessory {
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 //@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, "Hue Chase") Characteristic.Manufacturer,
this._infoService.setCharacteristic(Characteristic.SerialNumber, "123-456-789"); "Brandon Watson"
);
this._infoService.setCharacteristic(Characteristic.Model, "Hue Chase");
this._infoService.setCharacteristic(
Characteristic.SerialNumber,
"123-456-789"
);
this._lightbulbService = new Service.Lightbulb( this._lightbulbService = new Service.Lightbulb(
this.name, this.name,
'lightbulbService' "lightbulbService"
) );
this._lightbulbService.getCharacteristic(Characteristic.On) this._lightbulbService
.getCharacteristic(Characteristic.On)
//@ts-ignore //@ts-ignore
.on("set", this.onPowerSet) .on("set", this.onPowerSet)
.on("get", this.onPowerGet); .on("get", this.onPowerGet);
this._lightbulbService.getCharacteristic(Characteristic.Brightness) this._lightbulbService
.getCharacteristic(Characteristic.Brightness)
//@ts-ignore //@ts-ignore
.on("set", this.onBrightnessSet) .on("set", this.onBrightnessSet)
.on("get", this.onBrightnessGet); .on("get", this.onBrightnessGet);
} }
/** /**
@ -86,55 +97,61 @@ export class Chase implements IAccessory {
*/ */
public getServices = (): Array<HAPNodeJS.Service> => { public getServices = (): Array<HAPNodeJS.Service> => {
return [this._infoService, this._lightbulbService!]; return [this._infoService, this._lightbulbService!];
} };
/** /**
* 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 onPowerSet = async (activeState: boolean, callback: (error?: Error | null | undefined) => void) => { private onPowerSet = async (
activeState: boolean,
callback: (error?: Error | null | undefined) => void
) => {
if (this._isActive != activeState) { if (this._isActive != activeState) {
activeState ? this.chase() : this.stop(); activeState ? this.chase() : this.stop();
} }
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 onPowerGet = (callback: (error: Error | null, value: boolean) => void) => { private onPowerGet = () => {
return callback(null, this._isActive); return this._isActive;
} };
private onBrightnessSet = async (newValue: number, callback: (error?: Error | null | undefined) => void) => { private onBrightnessSet = async (
newValue: number,
callback: (error?: Error | null | undefined) => void
) => {
this._brightness = newValue; this._brightness = newValue;
const lightState = new LightState(); const lightState = new LightState();
lightState lightState.on(true).brightness(this._brightness);
.on(true)
.brightness(this._brightness)
await this.setLights(lightState); await this.setLights(lightState);
return callback(); return callback();
} };
private onBrightnessGet = (callback: (eror: Error | null, value: number) => void) => { private onBrightnessGet = () => {
return callback(null, this._brightness); return this._brightness;
} };
/** /**
* Popuplates internal lights array using the configuration values * Popuplates internal lights array using the configuration values
*/ */
private getLights = async () => { private getLights = async () => {
//Get lights //Get lights
const lightPromises: Array<Promise<void>> = this._sequence.lights.map(async (value: string) => { const lightPromises: Array<Promise<void>> = this._sequence.lights.map(
async (value: string) => {
//@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);
});
await Promise.all(lightPromises);
} }
);
await Promise.all(lightPromises);
};
/** /**
* Execute chase sequence * Execute chase sequence
@ -147,7 +164,7 @@ export class Chase implements IAccessory {
let idx = 0; let idx = 0;
this._isActive = true; this._isActive = true;
while (this._isActive) { while (this._isActive) {
const rgb = this.hexToRgb(this._sequence.colors[idx]) const rgb = this.hexToRgb(this._sequence.colors[idx]);
lightState lightState
.on(true) .on(true)
.brightness(this._brightness) .brightness(this._brightness)
@ -156,7 +173,6 @@ export class Chase implements IAccessory {
await this.setLights(lightState); await this.setLights(lightState);
await Sleep(this._sequence.transitionTime); await Sleep(this._sequence.transitionTime);
if (idx == this._sequence.colors.length) { if (idx == this._sequence.colors.length) {
idx = 0; idx = 0;
@ -164,22 +180,21 @@ export class Chase implements IAccessory {
idx++; idx++;
} }
} }
} };
/** /**
* Stop chase sequence * Stop chase sequence
*/ */
private stopAndSetOff = async () => { private stopAndSetOff = async () => {
this._isActive = false; this._isActive = false;
const lightState: LightState = new LightState() const lightState: LightState = new LightState().off();
.off();
await this.setLights(lightState); await this.setLights(lightState);
} };
private stop = () => { private stop = () => {
this._isActive = false; this._isActive = false;
} };
private setLights = async (state: LightState) => { private setLights = async (state: LightState) => {
try { try {
@ -189,35 +204,46 @@ export class Chase implements IAccessory {
}) })
); );
} catch (err) { } catch (err) {
this._log(`Error while setting brightness: ${err}`) this._log(`Error while setting brightness: ${err}`);
}
} }
};
/** /**
* Helper function to convert a hex string to rgb * Helper function to convert a hex string to rgb
* @param hex hex string starting with "#" * @param hex hex string starting with "#"
*/ */
private hexToRgb = (hex: string): { red: number, green: number, blue: number } | null => { 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); var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? { return result
? {
red: parseInt(result[1], 16), red: parseInt(result[1], 16),
green: parseInt(result[2], 16), green: parseInt(result[2], 16),
blue: parseInt(result[3], 16) blue: parseInt(result[3], 16),
} : null;
} }
: null;
};
/** /**
* 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);
}); }
);
} }
} }

View File

@ -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";
@ -16,12 +16,12 @@ 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 = {};
@ -30,13 +30,12 @@ class HueChasePlatform {
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 Hue Chase platform'); this.log("INFO - Registering Hue Chase platform");
this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this)); this.api.on("didFinishLaunching", this.didFinishLaunching.bind(this));
} }
private connectHue = async () => { private connectHue = async () => {
@ -45,35 +44,46 @@ class HueChasePlatform {
} }
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, null); this.hue = await v3.api
.createLocal(this.config.ipAddress)
.connect(this.config.userName, this.config.clientKey, null);
this.log("Using existing connection info"); this.log("Using existing connection info");
} else { } else {
const unauthenticatedApi = await v3.api.createLocal(this.config.ipAddress).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("homebridge", "HueChase"); createdUser = await unauthenticatedApi.users.createUser(
"homebridge",
"HueChase"
);
this.hue = await v3.api.createLocal(this.config.ipAddress).connect(createdUser.username, createdUser.clientKey, null); this.hue = await v3.api
.createLocal(this.config.ipAddress)
.connect(createdUser.username, createdUser.clientKey, null);
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) {
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
@ -87,20 +97,24 @@ class HueChasePlatform {
* 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 (
callback: (accessories: Array<Chase>) => void
) => {
//Connect to hue bridge //Connect to hue bridge
await this.connectHue(); await this.connectHue();
this.config.sequences.forEach((sequence: ISequence) => { this.config.sequences.forEach((sequence: ISequence) => {
this.chaseList.push(new Chase({ this.chaseList.push(
new Chase({
name: sequence.name, name: sequence.name,
api: this.api, api: this.api,
log: this.log, log: this.log,
homebridge: Homebridge, homebridge: Homebridge,
sequence: sequence, sequence: sequence,
hue: this.hue!, hue: this.hue!,
}))
}) })
);
});
callback(this.chaseList); callback(this.chaseList);
} };
} }