Using lightbulb service instead of switch service
This commit is contained in:
parent
1d89355658
commit
756bc3c2b6
82
src/chase.ts
82
src/chase.ts
@ -23,7 +23,7 @@ export class Chase implements IAccessory {
|
||||
private _log: any = {};
|
||||
|
||||
//Service fields
|
||||
private _switchService: HAPNodeJS.Service;
|
||||
private _lightbulbService: HAPNodeJS.Service;
|
||||
private _infoService: HAPNodeJS.Service;
|
||||
|
||||
private _sequence: ISequence;
|
||||
@ -33,6 +33,8 @@ export class Chase implements IAccessory {
|
||||
|
||||
private _lights: Array<Light> = [];
|
||||
|
||||
private _brightness: number = 0;
|
||||
|
||||
constructor(props: IChaseProps) {
|
||||
//Assign class variables
|
||||
this._log = props.log;
|
||||
@ -51,21 +53,25 @@ export class Chase implements IAccessory {
|
||||
|
||||
//@ts-ignore
|
||||
this._infoService = new Service.AccessoryInformation();
|
||||
this._infoService.setCharacteristic(Characteristic.Manufacturer, "The Watson Project")
|
||||
this._infoService.setCharacteristic(Characteristic.Manufacturer, "Brandon Watson")
|
||||
this._infoService.setCharacteristic(Characteristic.Model, "Hue Chase")
|
||||
this._infoService.setCharacteristic(Characteristic.SerialNumber, "123-456-789");
|
||||
|
||||
this._switchService = new Service.Switch(
|
||||
this._lightbulbService = new Service.Lightbulb(
|
||||
this.name,
|
||||
'switchService'
|
||||
'lightbulbService'
|
||||
)
|
||||
|
||||
this._switchService.getCharacteristic(Characteristic.On)
|
||||
//@ts-ignore
|
||||
.on("set", this.onSwitchSet)
|
||||
.on("get", this.onSwitchGet);
|
||||
|
||||
this.getLights();
|
||||
this._lightbulbService.getCharacteristic(Characteristic.On)
|
||||
//@ts-ignore
|
||||
.on("set", this.onPowerSet)
|
||||
.on("get", this.onPowerGet);
|
||||
|
||||
this._lightbulbService.getCharacteristic(Characteristic.Brightness)
|
||||
//@ts-ignore
|
||||
.on("set", this.onBrightnessSet)
|
||||
.on("get", this.onBrightnessGet);
|
||||
|
||||
}
|
||||
|
||||
@ -80,15 +86,17 @@ export class Chase implements IAccessory {
|
||||
* Called by homebridge to gather services.
|
||||
*/
|
||||
public getServices = (): Array<HAPNodeJS.Service> => {
|
||||
return [this._switchService!];
|
||||
return [this._infoService, this._lightbulbService!];
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for switch set event
|
||||
* @param callback The callback function to call when complete
|
||||
*/
|
||||
private onSwitchSet = (activeState: boolean, callback: (error?: Error | null | undefined) => void) => {
|
||||
activeState ? this.chase() : this.stop();
|
||||
private onPowerSet = async (activeState: boolean, callback: (error?: Error | null | undefined) => void) => {
|
||||
if (this._isActive != activeState) {
|
||||
activeState ? this.chase() : this.stopAndSetOff();
|
||||
}
|
||||
return callback();
|
||||
}
|
||||
|
||||
@ -96,10 +104,26 @@ export class Chase implements IAccessory {
|
||||
* Handler for switch get event
|
||||
* @param callback The callback function to call when complete
|
||||
*/
|
||||
private onSwitchGet = (callback: (error: Error | null, value: boolean) => void) => {
|
||||
private onPowerGet = (callback: (error: Error | null, value: boolean) => void) => {
|
||||
return callback(null, this._isActive);
|
||||
}
|
||||
|
||||
private onBrightnessSet = async (newValue: number, callback: (error?: Error | null | undefined) => void) => {
|
||||
this._brightness = newValue;
|
||||
const lightState = new LightState();
|
||||
lightState
|
||||
.on(true)
|
||||
.brightness(this._brightness)
|
||||
|
||||
await this.setLights(lightState);
|
||||
|
||||
return callback();
|
||||
}
|
||||
|
||||
private onBrightnessGet = (callback: (eror: Error | null, value: number) => void) => {
|
||||
return callback(null, this._brightness);
|
||||
}
|
||||
|
||||
/**
|
||||
* Popuplates internal lights array using the configuration values
|
||||
*/
|
||||
@ -127,19 +151,11 @@ export class Chase implements IAccessory {
|
||||
const rgb = this.hexToRgb(this._sequence.colors[idx])
|
||||
lightState
|
||||
.on(true)
|
||||
.brightness(this._sequence.intensity)
|
||||
.brightness(this._brightness)
|
||||
.transitionInMillis(this._sequence.transitionTime)
|
||||
.rgb(rgb?.red, rgb?.green, rgb?.blue);
|
||||
|
||||
try {
|
||||
await Promise.all(
|
||||
this._lights.map(async (value: Light) => {
|
||||
await this._hue.lights.setLightState(value.id, lightState);
|
||||
})
|
||||
);
|
||||
} catch (err) {
|
||||
this._log(`Error while changing lights: ${err}`)
|
||||
}
|
||||
await this.setLights(lightState);
|
||||
|
||||
|
||||
await Sleep(this._sequence.transitionTime);
|
||||
@ -154,10 +170,30 @@ export class Chase implements IAccessory {
|
||||
/**
|
||||
* Stop chase sequence
|
||||
*/
|
||||
private stopAndSetOff = async () => {
|
||||
this._isActive = false;
|
||||
const lightState: LightState = new LightState()
|
||||
.off();
|
||||
|
||||
await this.setLights(lightState);
|
||||
}
|
||||
|
||||
private stop = () => {
|
||||
this._isActive = false;
|
||||
}
|
||||
|
||||
private setLights = async (state: LightState) => {
|
||||
try {
|
||||
await Promise.all(
|
||||
this._lights.map(async (value: Light) => {
|
||||
await this._hue.lights.setLightState(value.id, state);
|
||||
})
|
||||
);
|
||||
} catch (err) {
|
||||
this._log(`Error while setting brightness: ${err}`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to convert a hex string to rgb
|
||||
* @param hex hex string starting with "#"
|
||||
|
@ -1,7 +1,6 @@
|
||||
export interface ISequence {
|
||||
name: string;
|
||||
transitionTime: number;
|
||||
intensity?: number;
|
||||
matchAllLights?: boolean;
|
||||
colors: Array<string>;
|
||||
lights: Array<string>;
|
||||
|
Loading…
Reference in New Issue
Block a user