UNPKG

homebridge-wyze-smart-home-justoy-forked

Version:

Wyze Smart Home plugin for Homebridge forked by justoy, which supports wyze battery cam pro

86 lines (72 loc) 2.14 kB
const { Service, Characteristic } = require("../types"); // Responses from the Wyze API can lag a little after a new value is set const UPDATE_THROTTLE_MS = 1000; module.exports = class WyzeAccessory { constructor(plugin, homeKitAccessory) { this.updating = false; this.lastTimestamp = null; this.plugin = plugin; this.homeKitAccessory = homeKitAccessory; } // Default Prop get display_name() { return this.homeKitAccessory.displayName; } get mac() { return this.homeKitAccessory.context.mac; } get product_type() { return this.homeKitAccessory.context.product_type; } get product_model() { return this.homeKitAccessory.context.product_model; } /** Determines whether this accessory matches the given Wyze device */ matches(device) { return this.mac === device.mac; } async update(device, timestamp) { const productType = device.product_type; switch (productType) { default: this.homeKitAccessory.context = { mac: device.mac, product_type: device.product_type, product_model: device.product_model, nickname: device.nickname, }; break; } this.homeKitAccessory .getService(Service.AccessoryInformation) .updateCharacteristic(Characteristic.Name, device.nickname) .updateCharacteristic(Characteristic.Manufacturer, "Wyze") .updateCharacteristic(Characteristic.Model, device.product_model) .updateCharacteristic(Characteristic.SerialNumber, device.mac) .updateCharacteristic( Characteristic.FirmwareRevision, device.firmware_ver ); if (this.shouldUpdateCharacteristics(timestamp)) { this.updateCharacteristics(device); } } shouldUpdateCharacteristics(timestamp) { if (this.updating) { return false; } if ( this.lastTimestamp && timestamp <= this.lastTimestamp + UPDATE_THROTTLE_MS ) { return false; } return true; } updateCharacteristics(device) { // } sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms * 1000)); } };