UNPKG

homebridge-blaq

Version:

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

86 lines 3.54 kB
import { BaseBlaQAccessory } from './base.js'; export const label = 'Learn/Pair Mode'; /** * 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 BlaQGarageLearnModeAccessory extends BaseBlaQAccessory { switchService; isOn; constructor(args) { super(args); this.switchService = this.getOrAddService(this.platform.service.Switch); // 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.switchService.setCharacteristic(this.platform.characteristic.Name, this.accessory.context.device.displayName + ' ' + label); this.switchService.getCharacteristic(this.platform.characteristic.On) .onGet(this.getIsOn.bind(this)) .onSet(this.changeIsOn.bind(this)); this.logger.debug(`Initialized ${this.getSelfClassName()}!`); } getIsOn() { return this.isOn || false; } setIsOn(isOn) { this.isOn = isOn; this.switchService.setCharacteristic(this.platform.characteristic.On, this.isOn); } async changeIsOn(target) { const apiTarget = target ? 'turn_on' : 'turn_off'; if (target !== this.isOn) { // only call the API when target = true (button on) await this.authFetch(`${this.apiBaseURL}/switch/learn/${apiTarget}`, { method: 'POST' }); } } handleStateEvent(stateEvent) { super.handleStateEvent(stateEvent); if (!this.synced) { return; } try { const stateInfo = JSON.parse(stateEvent.data); if (['switch-learn'].includes(stateInfo.id)) { const buttonEvent = stateInfo; if (['OFF', 'ON'].includes(buttonEvent.state?.toUpperCase() || '')) { this.setIsOn(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(); const learnTurningOn = lowercaseLogStr.includes('learn') && lowercaseLogStr.includes('turning') && lowercaseLogStr.includes('on'); const learnStateOn = lowercaseLogStr.includes('learn') && lowercaseLogStr.includes('state') && lowercaseLogStr.includes('on'); const learnTurningOff = lowercaseLogStr.includes('learn') && lowercaseLogStr.includes('turning') && lowercaseLogStr.includes('off'); const learnStateOff = lowercaseLogStr.includes('learn') && lowercaseLogStr.includes('state') && lowercaseLogStr.includes('off'); if (learnTurningOn || learnStateOn) { this.setIsOn(true); } else if (learnTurningOff || learnStateOff) { this.setIsOn(false); } } catch (e) { this.logger.error('Log parsing error:', e); } } } //# sourceMappingURL=garage-learn-mode.js.map