aboutsummaryrefslogtreecommitdiff
path: root/plugin.ts
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2021-08-10 20:12:17 +0200
committerlonkaars <loek@pipeframe.xyz>2021-08-10 20:12:17 +0200
commitefe195d6592d5c6cc64de71fc65f85522bc45dfa (patch)
tree513d0e6afda36198c78a6725dd824fef56cdd34b /plugin.ts
parentb8c9fc19e6e35679a1b9125396448987d7a6e8d0 (diff)
homebridge to homeassistant baby steps
Diffstat (limited to 'plugin.ts')
-rw-r--r--plugin.ts128
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,
- ];
- }
-}