homebridge-unifi-occupancy-lite
Version:
Lightweight UniFi occupancy sensor plugin for Homebridge using API tokens only. Works with UDM Pro/SE, UDM, UDR and other UniFi OS devices.
61 lines (60 loc) • 2.38 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GlobalPresenceAccessoryHandler = void 0;
const accessory_handler_1 = require("./accessory_handler");
class GlobalPresenceAccessoryHandler extends accessory_handler_1.AccessoryHandler {
constructor(platform, accessory, residents) {
super(platform, accessory);
this.residents = [];
this.residents = residents;
this.setupServices();
}
setupServices() {
// Get or create occupancy sensor service
let service = this.accessory.getService(this.platform.Service.OccupancySensor);
if (!service) {
service = this.accessory.addService(this.platform.Service.OccupancySensor, 'Global Presence', 'global-presence');
this.platform.log.debug('Created new occupancy sensor service for global presence');
}
// Set up characteristics
service.setCharacteristic(this.platform.Characteristic.Name, 'Global Presence');
// Set initial state
this.updatePresenceState();
}
updateResidents(residents) {
this.residents = residents;
this.updatePresenceState();
}
updatePresenceState() {
const service = this.accessory.getService(this.platform.Service.OccupancySensor);
if (!service) {
return;
}
// Check if any resident is home
const anyoneHome = this.residents.some(resident => resident.isHome);
// Update occupancy detected characteristic
service.updateCharacteristic(this.platform.Characteristic.OccupancyDetected, anyoneHome ? 1 : 0);
// Log status change
const status = anyoneHome ? 'detected' : 'not detected';
this.platform.log.debug(`Global presence ${status}`);
}
refresh() {
this.updatePresenceState();
}
getDisplayName() {
return 'Global Presence';
}
getStatusSummary() {
const anyoneHome = this.residents.some(resident => resident.isHome);
const homeResidents = this.residents
.filter(resident => resident.isHome)
.map(resident => resident.name);
if (anyoneHome) {
return `Home: ${homeResidents.join(', ')}`;
}
else {
return 'Nobody home';
}
}
}
exports.GlobalPresenceAccessoryHandler = GlobalPresenceAccessoryHandler;