diff options
author | Loek Le Blansch <32883851+lonkaars@users.noreply.github.com> | 2021-08-13 14:03:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-13 14:03:33 +0200 |
commit | f1872e3b036e679d8e6b5f8e08c31da18caaa37a (patch) | |
tree | d5ff7c3656868780a4f29461eb3bbec535ec3cfa /plugin.ts | |
parent | b8c9fc19e6e35679a1b9125396448987d7a6e8d0 (diff) | |
parent | 886630c94bd76b5bec9aa5b2eeb8a6da7d6a0cc2 (diff) |
Merge pull request #1 from lonkaars/homeassistant
homebridge to homeassistant
Diffstat (limited to 'plugin.ts')
-rw-r--r-- | plugin.ts | 128 |
1 files changed, 0 insertions, 128 deletions
diff --git a/plugin.ts b/plugin.ts deleted file mode 100644 index 9b2fa10..0000000 --- a/plugin.ts +++ /dev/null @@ -1,128 +0,0 @@ -import { AccessoryConfig, AccessoryPlugin, API, Logger, Service } from 'homebridge'; - -import Lamp from './lamp'; -const Color = require('color'); - -export default class BekenBridge implements AccessoryPlugin { - private lamp: Lamp; - private infoService: Service; - - private whiteBulbService: Service; - private RGBBulbService: Service; - - private state: { - on: boolean; - brightness: number; - lamp: 'white' | 'rgb'; - saturation: number; - hue: number; - }; - - public name: string; - - constructor( - public readonly log: Logger, - public readonly config: AccessoryConfig, - public readonly api: API, - ) { - this.name = config.name; - this.lamp = new Lamp(config.address, log); - - this.state = { - on: false, - brightness: 100, - lamp: 'white', - saturation: 0, - hue: 0, - }; - - this.infoService = new this.api.hap.Service.AccessoryInformation() - .setCharacteristic(this.api.hap.Characteristic.Manufacturer, 'Beken') - .setCharacteristic(this.api.hap.Characteristic.Model, 'Beken LED'); - - this.whiteBulbService = new this.api.hap.Service.Lightbulb('White', 'normal'); - this.RGBBulbService = new this.api.hap.Service.Lightbulb('RGB', 'rgb'); - - this.registerWhiteBulbServices(); - this.registerRGBBulbServices(); - } - - registerWhiteBulbServices() { - var done = () => { - this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.On).updateValue(false); - this.state.lamp = 'white'; - this.updateLamp(); - }; - this.whiteBulbService.getCharacteristic(this.api.hap.Characteristic.On) - .onGet(() => this.state.on && this.state.lamp == 'white') - .onSet((on: boolean) => { - this.state.on = on; - done(); - }); - this.whiteBulbService.getCharacteristic(this.api.hap.Characteristic.Brightness) - .onGet(() => this.state.brightness) - .onSet((brt: number) => { - this.state.brightness = brt; - done(); - }); - } - - registerRGBBulbServices() { - var done = () => { - this.whiteBulbService.getCharacteristic(this.api.hap.Characteristic.On).updateValue(false); - this.state.lamp = 'rgb'; - this.updateLamp(); - }; - this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.On) - .onGet(() => this.state.on && this.state.lamp == 'rgb') - .onSet((on: boolean) => { - this.state.on = on; - done(); - }); - this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.Brightness) - .onGet(() => this.state.brightness) - .onSet((brt: number) => { - this.state.brightness = brt; - done(); - }); - this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.Hue) - .onGet(() => this.state.hue) - .onSet((hue: number) => { - this.state.hue = hue; - done(); - }); - this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.Saturation) - .onGet(() => this.state.saturation) - .onSet((sat: number) => { - this.state.saturation = sat; - done(); - }); - } - - updateLamp() { - if (!this.state.on) { - this.lamp.color = [0, 0, 0, 0]; - return; - } - switch (this.state.lamp) { - case 'rgb': { - var rgb = Color({ h: this.state.hue, s: this.state.saturation, v: this.state.brightness }); - this.lamp.color = [rgb.red(), rgb.green(), rgb.blue(), 0]; - break; - } - case 'white': { - var value = Math.floor(this.state.brightness / 100 * 255); - this.lamp.color = [0, 0, 0, value]; - break; - } - } - } - - getServices() { - return [ - this.infoService, - this.whiteBulbService, - this.RGBBulbService, - ]; - } -} |