UNPKG

homebridge-hivehome-control

Version:

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

84 lines 4.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HiveHotWaterAccessory = void 0; const log_1 = require("../../util/log"); const hive_api_1 = require("../hive-api"); const hive_helpers_1 = require("../hive-helpers"); const hiveAccessory_1 = require("./hiveAccessory"); /** * An instance of this class is created for each Hot Water accessory. Exposes * both the Manual and Boost services for the Hot Water device. */ class HiveHotWaterAccessory extends hiveAccessory_1.HiveAccessory { constructor(platform, accessory, hiveSession) { // Initialize the base class first, then initialize the services. super(platform, accessory, hiveSession); this.platform = platform; this.accessory = accessory; this.hiveSession = hiveSession; this.kManualName = 'Manual'; this.kBoostName = 'Boost'; // Convenience references to Characteristic and Service. const Characteristic = this.platform.Characteristic; const Service = this.platform.Service; // // Create one switch for each service offered by this hot water device. // const manualConName = `${this.accessory.displayName} ${this.kManualName}`; this.manualService = this.accessory.getService(this.kManualName) || this.addService(Service.Switch, this.kManualName, this.kManualName, manualConName); const boostConName = `${this.accessory.displayName} ${this.kBoostName}`; this.boostService = this.accessory.getService(this.kBoostName) || this.addService(Service.Switch, this.kBoostName, this.kBoostName, boostConName); // // Register handlers for all dynamic characteristics. // this.manualService.getCharacteristic(Characteristic.On) .onGet(() => this.currentState[this.kManualName] === hive_api_1.HeatingMode.kOn) .onSet((active) => this.setDeviceState(this.kManualName, active ? hive_api_1.HeatingMode.kOn : hive_api_1.HeatingMode.kOff)); this.boostService.getCharacteristic(Characteristic.On) .onGet(() => this.currentState[this.kBoostName] === hive_api_1.HeatingMode.kOn) .onSet((active) => this.setDeviceState(this.kBoostName, active ? hive_api_1.HeatingMode.kOn : hive_api_1.HeatingMode.kOff)); } // Return true if both services have been initialized. servicesReady() { return !!(this.boostService && this.manualService); } // Get the device power state and push to Homekit when it changes. async updateDeviceAndCurrentState() { // Retrieve the newly-updated device data... this.hiveDevice = await this.hiveSession.hotwater.getWaterHeater(this.hiveDevice); // ... and use it to populate the current state. this.currentState[this.kBoostName] = await this.hiveSession.hotwater.getBoost(this.hiveDevice); this.currentState[this.kManualName] = await this.hiveSession.hotwater.getMode(this.hiveDevice); } // Push the current state to Homekit. async updateHomekitState() { this.boostService.updateCharacteristic(this.platform.Characteristic.On, this.currentState[this.kBoostName] === hive_api_1.HeatingMode.kOn); this.manualService.updateCharacteristic(this.platform.Characteristic.On, this.currentState[this.kManualName] === hive_api_1.HeatingMode.kOn); } // Apply the state requested by Homekit. async setDeviceState(serviceName, newState) { switch (serviceName) { case this.kBoostName: if (newState === hive_api_1.HeatingMode.kOn) { await this.hiveSession.hotwater.setBoostOn(this.hiveDevice, this.config.hotWaterBoostMins); log_1.Log.info(`Enabled ${this.accessory.displayName} Boost for ${this.config.hotWaterBoostMins} minutes`); } else { await this.hiveSession.hotwater.setBoostOff(this.hiveDevice); log_1.Log.info(`Turned off ${this.accessory.displayName} Boost`); } break; case this.kManualName: await this.hiveSession.hotwater.setMode(this.hiveDevice, (0, hive_helpers_1.translateModeForRequest)(newState)); log_1.Log.info(`Set ${this.accessory.displayName} mode:`, newState); } this.currentState[serviceName] = newState; } } exports.HiveHotWaterAccessory = HiveHotWaterAccessory; //# sourceMappingURL=hiveHotWaterAccessory.js.map