Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add human presence sensor #247

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const AirPurifierAccessory = require('./lib/air_purifier_accessory')
const WindowCoveringAccessory = require('./lib/window_covering_accessory')
const ContactSensorAccessory = require('./lib/contactsensor_accessory');
const LeakSensorAccessory = require('./lib/leak_sensor_accessory')
const HumanPresenceSensorAccessory = require('./lib/humanpresencesensor_accessory')

const LogUtil = require('./util/logutil')
const DataUtil = require('./util/datautil')
Expand Down Expand Up @@ -183,6 +184,11 @@ class TuyaPlatform {
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
this.deviceAccessories.set(uuid, deviceAccessory);
break;
case 'hps':
deviceAccessory = new HumanPresenceSensorAccessory(this, homebridgeAccessory, device);
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory);
this.deviceAccessories.set(uuid, deviceAccessory);
break;
default:
break;
}
Expand Down
78 changes: 78 additions & 0 deletions lib/humanpresencesensor_accessory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const BaseAccessory = require('./base_accessory')

let Accessory;
let Service;
let Characteristic;
let UUIDGen;

class HumanPresenceSensorAccessory extends BaseAccessory {
constructor(platform, homebridgeAccessory, deviceConfig) {

({ Accessory, Characteristic, Service } = platform.api.hap);
super(
platform,
homebridgeAccessory,
deviceConfig,
Accessory.Categories.SENSOR,
Service.OccupancySensor
);

this.statusArr = deviceConfig.status;
this.refreshAccessoryServiceIfNeed(this.statusArr, false);
}

//init Or refresh AccessoryService
refreshAccessoryServiceIfNeed(statusArr, isRefresh) {
this.isRefresh = isRefresh;
for (var statusMap of statusArr) {
if (statusMap.code === 'presence_state') {

this.sensorStatus = statusMap
const hbSensorState = this.tuyaParamToHomeBridge(Characteristic.OccupancyDetected, this.sensorStatus.value);
this.normalAsync(Characteristic.OccupancyDetected, hbSensorState)
}
}
}

tuyaParamToHomeBridge(name, param) {
switch (name) {
case Characteristic.OccupancyDetected:
let status
if (param === "presence") {
status = Characteristic.OccupancyDetected.OCCUPANCY_DETECTED
} else {
status = Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED
}
return status
}
}


normalAsync(name, hbValue) {
this.setCachedState(name, hbValue);
if (this.isRefresh) {
this.service
.getCharacteristic(name)
.updateValue(hbValue);
} else {
this.getAccessoryCharacteristic(name);
}
}

getAccessoryCharacteristic(name) {
//set Accessory service Characteristic
this.service.getCharacteristic(name)
.on('get', callback => {
if (this.hasValidCache()) {
callback(null, this.getCachedState(name));
}
});
}

//update device status
updateState(data) {
this.refreshAccessoryServiceIfNeed(data.status, true);
}
}

module.exports = HumanPresenceSensorAccessory;