UNPKG

@koush/ring-client-api

Version:

Unofficial API for Ring doorbells, cameras, security alarm system and smart lighting

62 lines (61 loc) 2.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Lock = void 0; const base_device_accessory_1 = require("./base-device-accessory"); const operators_1 = require("rxjs/operators"); const hap_1 = require("./hap"); function getCurrentState({ locked }) { const { Characteristic: { LockCurrentState: State }, } = hap_1.hap; switch (locked) { case 'unlocked': return State.UNSECURED; case 'locked': return State.SECURED; case 'jammed': return State.JAMMED; default: return State.UNKNOWN; } } class Lock extends base_device_accessory_1.BaseDeviceAccessory { constructor(device, accessory, logger, config) { super(); this.device = device; this.accessory = accessory; this.logger = logger; this.config = config; const { Characteristic, Service } = hap_1.hap; this.device.onData .pipe((0, operators_1.distinctUntilChanged)((a, b) => a.locked === b.locked)) .subscribe((data) => { this.targetState = this.getTargetState(data); }); this.registerCharacteristic({ characteristicType: Characteristic.LockCurrentState, serviceType: Service.LockMechanism, getValue: (data) => { const state = getCurrentState(data); if (state === this.targetState) { this.targetState = undefined; } return state; }, }); this.registerCharacteristic({ characteristicType: Characteristic.LockTargetState, serviceType: Service.LockMechanism, getValue: (data) => this.getTargetState(data), setValue: (value) => this.setTargetState(value), }); } setTargetState(state) { const { Characteristic: { LockTargetState: State }, } = hap_1.hap, command = state === State.SECURED ? 'lock' : 'unlock'; this.targetState = state === getCurrentState(this.device.data) ? undefined : state; return this.device.sendCommand(`lock.${command}`); } getTargetState(data) { return this.targetState || getCurrentState(data); } } exports.Lock = Lock;