import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge' import { accessory } from './accessory' import { device } from './device' import { httpRequest, response } from './httpPromise' import { domoticaPlatform } from './platform' export class klikAanKlikUitAccessory extends accessory { private service: Service private state = { On: false, Brightness: 100 } static async discoverDevices(platform: domoticaPlatform): Promise { return new Promise((resolve, reject) => { let klikaanklikuitDevices: device[] = [] httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/klikaanklikuit') .then((response) => { let devices = JSON.parse(response.body) for (const device of devices) { if (device.enabled == "true") { let object: device = { type: "KlikAanKlikUit", typeId: device.id, uniqueId: "KlikAanKlikUit." + device.id, deviceName: device.name, switchable: "true", dimmable: device.dimmable, detailedType: "" } klikaanklikuitDevices.push(object) } } resolve(klikaanklikuitDevices) }) .catch((error) => { reject('klikAanKlikUitAccessory::discoverDevices Error ->' + error) }) }) } constructor( private readonly platform: domoticaPlatform, private readonly accessory: PlatformAccessory, ) { super("KlikAanKlikUit", accessory.context.device.typeId) this.accessory.getService(this.platform.Service.AccessoryInformation)! .setCharacteristic(this.platform.Characteristic.Manufacturer, 'KlikAanKlikUit') .setCharacteristic(this.platform.Characteristic.Model, 'AC-1000') .setCharacteristic(this.platform.Characteristic.SerialNumber, '01000' + accessory.context.device.typeId) this.service = this.accessory.getService(this.platform.Service.Lightbulb) || this.accessory.addService(this.platform.Service.Lightbulb) this.service.getCharacteristic(this.platform.Characteristic.On) .onSet(this.setOn.bind(this)) .onGet(this.getOn.bind(this)) if (accessory.context.device.dimmable != "false") { // WTF? this.service.getCharacteristic(this.platform.Characteristic.Brightness) .onSet(this.setBrightness.bind(this)) .onGet(this.getBrightness.bind(this)) } this.setName(accessory.context.device.deviceName) this.update() } setName(name) { this.service.setCharacteristic(this.platform.Characteristic.Name, name) } setValue(key, value) { if (key == "state" && value != "") { this.state.On = (value == "on") this.service.updateCharacteristic(this.platform.Characteristic.On, this.state.On) } else if (key == "dimLevel" && value!= "") { this.state.Brightness = value this.service.updateCharacteristic(this.platform.Characteristic.Brightness, this.state.Brightness) } } update() { const data = JSON.stringify([{ type: 'klikaanklikuit', query: 'state' }]) httpRequest("http://" + this.platform.config.hostname + ':' + this.platform.config.port + '/API/Status', data) .then((response) => { const devices = JSON.parse(response.body) for (const device of devices.klikaanklikuit) { if (device.id == this.accessory.context.device.typeId) { if (device.state == 'on') { this.state.On = true } else { this.state.On = false } this.service.updateCharacteristic(this.platform.Characteristic.On, this.state.On) if (device.dimmable != 'false') { // WTF? this.state.Brightness = device.dimLevel; this.service.updateCharacteristic(this.platform.Characteristic.Brightness, this.state.Brightness) } break } } }) .catch((error) => { this.platform.log.debug('klikAanKlikUitAccessory::update Error ->' + error) }) } async setOn(value: CharacteristicValue) { this.state.On = value as boolean // http://$domoticaIP/external/WebUpdate.php?data=KlikAanKlikUit/$id/$state/ httpRequest("http://" + this.platform.config.hostname + '/external/WebUpdate.php?data=KlikAanKlikUit/' + this.accessory.context.device.typeId + '/' + (this.state.On? '1': '0') + '/') .catch((error) => { this.platform.log.debug('klikAanKlikUitAccessory::setOn Error ->' + error) }) } getOn(): CharacteristicValue { return this.state.On } async setBrightness(value: CharacteristicValue) { this.state.Brightness = value as number // http://$domoticaIP/external/WebUpdate.php?data=KlikAanKlikUit/$id/$state/$dimLevel httpRequest("http://" + this.platform.config.hostname + '/external/WebUpdate.php?data=KlikAanKlikUit/' + this.accessory.context.device.typeId + '//' + this.state.Brightness) .catch((error) => { this.platform.log.debug('klikAanKlikUitAccessory::setBrightness Error ->' + error) }) } getBrightness(): CharacteristicValue { return this.state.Brightness } }