UNPKG

homebridge-pushover-notification

Version:

Homebridge plugin to send push notifications through Pushover from HomeKit

95 lines 3.44 kB
import { wait } from './utils.js'; const PriorityMap = { emergency: 2, high: 1, normal: 0, low: -1, lowest: -2, }; export class SwitchAccessory { platform; accessory; pushoverClient; message; DefaultRetry = 300; // 5 minutes DefaultExpire = 1800; // 30 minutes log; service; onCharacteristic; isOn = false; inCooldown = false; constructor(platform, accessory, pushoverClient, message) { this.platform = platform; this.accessory = accessory; this.pushoverClient = pushoverClient; this.message = message; this.log = platform.log; // Set accessory information const infoService = this.accessory.getService(this.platform.Service.AccessoryInformation); if (infoService) { infoService .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Homebridge Pushover Notification') .setCharacteristic(this.platform.Characteristic.Model, 'Notification Switch') .setCharacteristic(this.platform.Characteristic.SerialNumber, 'N/A'); } this.service = this.accessory.getService(this.platform.Service.Switch) || this.accessory.addService(this.platform.Service.Switch); this.service.setCharacteristic(this.platform.Characteristic.Name, this.message.name); this.onCharacteristic = this.service.getCharacteristic(this.platform.Characteristic.On); // Register handlers for the On/Off Characteristic this.onCharacteristic .onSet(this.setOn.bind(this)) .onGet(this.getOn.bind(this)); } async setOn(value) { this.log.debug('setOn:', value); this.isOn = value; this.onCharacteristic.updateValue(this.isOn); if (this.inCooldown) { this.log.info('Still in cooldown:', this.message.name); await wait(250); this.resetSwitch(); return; } if (this.isOn) { this.triggerCooldown(this.message.cooldownTime); await this.sendMessage(); this.resetSwitch(); } } async getOn() { return this.isOn; } async sendMessage() { const request = { title: this.message.title, message: this.message.message, priority: this.mapPriority(this.message.priority), sound: this.message.sound, }; if (this.message.priority === 'emergency') { request.retry = this.message.retry ?? this.DefaultRetry; request.expire = this.message.expire ?? this.DefaultExpire; } await this.pushoverClient.sendMessage(request); this.log.info('Message sent:', this.message.name); } async triggerCooldown(cooldownTime = 0) { if (cooldownTime <= 0) { return; } this.log.debug('Entering cooldown:', this.message.name); this.inCooldown = true; await wait(cooldownTime); this.inCooldown = false; this.log.debug('Exited cooldown:', this.message.name); } resetSwitch() { this.log.debug('Resetting switch:', this.message.name); this.isOn = false; this.onCharacteristic.updateValue(this.isOn); } mapPriority(priority) { return priority ? PriorityMap[priority.toLowerCase()] : undefined; } } //# sourceMappingURL=switch-accessory.js.map