import { isTruly, isFunction, rgb2hsv } from '@lumiastream/lumia-rgb-utils'; interface KasaLightValues { on_off?: boolean; brightness?: number; hue?: number; saturation?: number; color_temp?: number; mode?: string; transition_period?: number; [key: string]: any; } class SuperState { protected _values: KasaLightValues = {}; // In Milliseconds // 0.0 – infinity seconds transition = (value: number) => { this._values['transition_period'] = value; return this; }; duration = this.transition; getValues = (): KasaLightValues => { return this._values; }; } export class KasaLightState extends SuperState { constructor(values?) { super(); if (values) this.create(values); } create = (values?): this => { if (values) { Object.keys(values).forEach((value) => { let fn; if (this.hasOwnProperty(value)) { fn = this[value]; if (isFunction(fn) && isTruly(values[value])) { fn.apply(this, [values[value]]); } } }); } return this; }; on = (value = true): this => { this._values['on_off'] = value; return this; }; turnOn = this.on; off = (): this => { this.on(false); return this; }; turnOff = this.off; mode = (value = 'normal'): this => { if (value === 'normal') { this._values['color_temp'] = 0; } this._values['mode'] = value; return this; }; // 0 - 360 hue = (value: number): this => { this._values['hue'] = value; this._values['color_temp'] = 0; return this; }; // Bri 0-100 bri = (value: number): this => { this._values['brightness'] = value; return this; }; brightness = this.bri; sat = (value: number): this => { this._values['saturation'] = value; return this; }; saturation = this.sat; // Color Temperature 2500 - 9000 in kelvin // Will override hue if passed temp = (value: number): this => { this._values['color_temp'] = value; return this; }; colorTemperature = this.temp; // Can take in an array or object hsv = (value: [number, number, number] | { h: number; s: number; v: number }) => { let convertedHsv: Array = []; if (Array.isArray(value)) { convertedHsv = value; } else { convertedHsv = [value.h, value.s, value.v]; } this.hue(convertedHsv[0]); this.sat(convertedHsv[1]); this.bri(convertedHsv[2]); return this; }; // Can take in an array or object rgb = (value: [number, number, number] | { r: number; g: number; b: number }): this => { let hsv; if (Array.isArray(value)) { hsv = rgb2hsv(value); } else { hsv = rgb2hsv([value.r, value.g, value.b]); } return this.hsv(hsv); }; // Can take in an object will first check for ct and if it exists and the light will worki with it it'll use color temp color = (value: { ct?: number; r?: number; g?: number; b?: number }): this => { if (Array.isArray(value)) { value = { r: value[0], g: value[1], b: value[2] }; } if (value.ct) { return this.temp(value.ct); } else { return this.rgb({ r: value.r!, g: value.g!, b: value.b! }); } }; } export class SceneState extends SuperState { ignore = (value: string) => { this._values['ignore'] = value; }; overrides = (state: KasaLightState) => { this._values['overrides'] = state; }; }