UNPKG

homebridge-blaq

Version:

Control and view your garage door(s) remotely with real-time updates using Konnected's BlaQ hardware

77 lines 3.15 kB
import { BaseBlaQAccessory } from './base.js'; const LIGHT_PREFIX = 'light-'; export const label = 'Light'; /** * Platform Accessory * An instance of this class is created for each accessory your platform registers * Each accessory may expose multiple services of different service types. */ export class BlaQGarageLightAccessory extends BaseBlaQAccessory { lightbulbService; isOn; lightType = 'garage_light'; constructor(args) { super(args); this.lightbulbService = this.getOrAddService(this.platform.service.Lightbulb); // Set the service name. This is what is displayed as the name on the Home // app. We use what we stored in `accessory.context` in `discoverDevices`. this.lightbulbService.setCharacteristic(this.platform.characteristic.Name, this.accessory.context.device.displayName + ' ' + label); this.lightbulbService.getCharacteristic(this.platform.characteristic.On) .onGet(this.getPowerState.bind(this)) .onSet(this.changePowerState.bind(this)); this.logger.debug(`Initialized ${this.getSelfClassName()}!`); } getPowerState() { return this.isOn || false; } setPowerState(isOn) { this.isOn = isOn; this.lightbulbService.setCharacteristic(this.platform.characteristic.On, this.isOn); } async changePowerState(target) { const apiTarget = target ? 'turn_on' : 'turn_off'; if (target !== this.isOn) { await this.authFetch(`${this.apiBaseURL}/light/${this.lightType}/${apiTarget}`, { method: 'POST' }); } } handleStateEvent(stateEvent) { super.handleStateEvent(stateEvent); if (!this.synced) { return; } try { const stateInfo = JSON.parse(stateEvent.data); if (['light-garage_light', 'light-light'].includes(stateInfo.id)) { const buttonEvent = stateInfo; this.lightType = stateInfo.id.split(LIGHT_PREFIX).pop(); if (['OFF', 'ON'].includes(buttonEvent.state?.toUpperCase() || '')) { this.setPowerState(buttonEvent.state?.toUpperCase() === 'ON'); } } } catch (e) { this.logger.error('Cannot deserialize message:', stateEvent); this.logger.error('Deserialization yielded:', e); } } handleLogEvent(logEvent) { super.handleLogEvent(logEvent); if (!this.synced) { return; } try { const logStr = logEvent.data; const lowercaseLogStr = logStr.toLowerCase(); if (lowercaseLogStr.includes('light') && lowercaseLogStr.includes('state') && lowercaseLogStr.includes('on')) { this.setPowerState(true); } else if (lowercaseLogStr.includes('light') && lowercaseLogStr.includes('state') && lowercaseLogStr.includes('off')) { this.setPowerState(false); } } catch (e) { this.logger.error('Log parsing error:', e); } } } //# sourceMappingURL=garage-light.js.map