UNPKG

homebridge-hivehome-control

Version:

Allows control of Hive devices, including water heaters, through Homebridge.

67 lines 2.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HiveAccessory = void 0; const log_1 = require("../../util/log"); const hive_helpers_1 = require("../hive-helpers"); /** * Base class to implement functionality common to all Hive accessories. */ class HiveAccessory { constructor(platform, accessory, hiveSession) { this.platform = platform; this.accessory = accessory; this.hiveSession = hiveSession; this.currentState = {}; // Retrieve a reference to the hiveDevice from the accessory. this.hiveDevice = accessory.context.device; // Store a reference to the plugin configuration for later use. this.config = platform.config; // Begin monitoring the device for state changes. this.updateDeviceState(); setInterval(() => this.updateDeviceState(), HiveAccessory.kRefreshInterval); } // Adds a new service to the device, and returns it. addService(type, name, subType, configuredName) { // Convenience references to Characteristic and Service. const Characteristic = this.platform.Characteristic; // Add the new service, then add ConfiguredName as a new Characteristic. const newService = this.accessory.addService(type, name, subType); newService.addOptionalCharacteristic(Characteristic.ConfiguredName); newService.setCharacteristic(Characteristic.ConfiguredName, configuredName); return newService; } // Get the device power state and push to Homekit when it changes. async updateDeviceState() { // Update the hive device from the server. await (0, hive_helpers_1.updateHiveData)(this.hiveSession, this.hiveDevice); // Check whether the derived class' services have been initialized. if (!this.servicesReady()) { log_1.Log.debug('Services not yet initialized:', this.accessory.displayName); return; } // Retrieve the newly-updated device data. const lastState = Object.assign({}, this.currentState); await this.updateDeviceAndCurrentState(); // Check whether any attributes have changed. if (JSON.stringify(lastState) !== JSON.stringify(this.currentState)) { log_1.Log.debug(`Updating ${this.accessory.displayName}:`, this.currentState); this.updateHomekitState(); } } // Implemented by subclasses. Confirms that all accessory services are ready. servicesReady() { return false; } // Implemented by subclasses. Updates this.hiveDevice and this.currentState // based on the recently-updated Hive data. async updateDeviceAndCurrentState() { // TBI } // Implemented by subclasses. Updates Homekit with the current state. async updateHomekitState() { // TBI } } exports.HiveAccessory = HiveAccessory; HiveAccessory.kRefreshInterval = 5000; //# sourceMappingURL=hiveAccessory.js.map