Skip to content

Commit

Permalink
fingerbot support
Browse files Browse the repository at this point in the history
  • Loading branch information
talrhv authored Dec 14, 2021
1 parent 0b47496 commit 41c57e7
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions lib/push_accessory.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 41c57e7

Please sign in to comment.