From 41c57e7e2a16236197fc3faa160eeaf314384529 Mon Sep 17 00:00:00 2001 From: talrhv <34038314+talrhv@users.noreply.github.com> Date: Tue, 14 Dec 2021 20:18:51 +0200 Subject: [PATCH] fingerbot support --- lib/push_accessory.js | 107 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 lib/push_accessory.js diff --git a/lib/push_accessory.js b/lib/push_accessory.js new file mode 100644 index 00000000..82c58c42 --- /dev/null +++ b/lib/push_accessory.js @@ -0,0 +1,107 @@ +const BaseAccessory = require('./base_accessory') + +let Accessory; +let Service; +let Characteristic; +let UUIDGen; + +class PushAccessory extends BaseAccessory { + constructor(platform, homebridgeAccessory, deviceConfig, deviceData) { + ({ Accessory, Characteristic, Service } = platform.api.hap); + super( + platform, + homebridgeAccessory, + deviceConfig, + Accessory.Categories.SWITCH, + Service.Switch, + deviceData.subType + ); + this.statusArr = deviceConfig.status; + this.subTypeArr = deviceData.subType; + + this.refreshAccessoryServiceIfNeed(this.statusArr, false); + } + + //init Or refresh AccessoryService + refreshAccessoryServiceIfNeed(statusArr, isRefresh) { + this.isRefresh = isRefresh; + if (!statusArr) { + return; + } + + for (var subType of this.subTypeArr) { + var status = statusArr.find(item => item.code === subType) + if (status != undefined && status.value === true){ + status.value = false; + } + if (!status) { + continue; + } + var value = status.value + let service + if (this.subTypeArr.length == 1) { + service = this.service; + this.switchValue = status; + }else{ + service = this.homebridgeAccessory.getService(subType); + } + this.setCachedState(service.displayName, value); + if (this.isRefresh) { + service + .getCharacteristic(Characteristic.On) + .updateValue(value); + } else { + this.getAccessoryCharacteristic(service, Characteristic.On); + } + } + } + + getAccessoryCharacteristic(service, name) { + //set Accessory service Characteristic + service.getCharacteristic(name) + .on('get', callback => { + if (this.hasValidCache()) { + callback(null, this.getCachedState(service.displayName)); + } + }) + .on('set', (value, callback) => { + var param = this.getSendParam(service.displayName, value) + this.platform.tuyaOpenApi.sendCommand(this.deviceId, param).then(() => { + this.setCachedState(service.displayName, value); + callback(); + }).catch((error) => { + this.log.error('[SET][%s] Characteristic.Brightness Error: %s', this.homebridgeAccessory.displayName, error); + this.invalidateCache(); + callback(error); + }); + }); + } + + //get Command SendData + getSendParam(name, value) { + var code; + var value; + const isOn = value ? true : false; + if (this.subTypeArr.length == 1) { + code = this.switchValue.code; + }else{ + code = name; + } + value = isOn; + return { + "commands": [ + { + "code": code, + "value": value + } + ] + }; + } + + //update device status + updateState(data) { + this.refreshAccessoryServiceIfNeed(data.status, true); + } +} + +module.exports = PushAccessory; \ No newline at end of file