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.
65 lines (64 loc) • 2.74 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WifiPointAccessoryHandler = void 0;
const accessory_handler_1 = require("./accessory_handler");
class WifiPointAccessoryHandler extends accessory_handler_1.AccessoryHandler {
constructor(platform, accessory, wifiPoint, residents) {
super(platform, accessory);
this.residents = [];
this.wifiPoint = wifiPoint;
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, this.wifiPoint.name, `wifi-point-${this.wifiPoint.name}`);
this.platform.log.debug(`Created new occupancy sensor service for wifi point: ${this.wifiPoint.name}`);
}
// Set up characteristics
service.setCharacteristic(this.platform.Characteristic.Name, `${this.wifiPoint.name} 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;
}
// Store previous state
const previousState = this.wifiPoint.hasResidents;
// Update wifi point presence based on residents
this.wifiPoint.updatePresence(this.residents);
// Update occupancy detected characteristic
service.updateCharacteristic(this.platform.Characteristic.OccupancyDetected, this.wifiPoint.hasResidents ? 1 : 0);
// Log status change only when it changes
if (previousState !== this.wifiPoint.hasResidents) {
const status = this.wifiPoint.hasResidents ? 'detected' : 'not detected';
this.platform.log.info(`${this.wifiPoint.name} presence ${status}`);
}
}
refresh() {
this.updatePresenceState();
}
getDisplayName() {
return `${this.wifiPoint.name} Presence`;
}
getStatusSummary() {
if (this.wifiPoint.hasResidents) {
const residentsAtLocation = this.residents
.filter(resident => resident.getDevicesAtAccessPoint(this.wifiPoint.mac || '').length > 0)
.map(resident => resident.name);
return `Present: ${residentsAtLocation.join(', ')}`;
}
else {
return 'No one present';
}
}
}
exports.WifiPointAccessoryHandler = WifiPointAccessoryHandler;