UNPKG

homebridge-eightsleep-pod

Version:
228 lines 10.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EightsleepPodPlatformAccessory = void 0; const eightsleep_1 = __importDefault(require("eightsleep")); const EightSleepPod_1 = __importDefault(require("./EightSleepPod")); var EightSleepAppApi_1 = require("eightsleep/dist/cjs/EightSleepAppApi"); Object.defineProperty(exports, "Sides", { enumerable: true, get: function () { return EightSleepAppApi_1.Sides; } }); const INACTIVE = 0; const IDLE = 1; const HEATING = 2; const COOLING = 3; const AUTO = 0; const HEAT = 1; const COOL = 2; /** * 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. */ class EightsleepPodPlatformAccessory { constructor(platform, accessory) { var _a, _b; this.platform = platform; this.accessory = accessory; this.targetHeatCool = HEAT; this.getActiveState = async (cb) => { try { this.platform.log.info('Get Active'); const side = this.accessory.context.side; const on = await this.eightSleepPod.isOn(side); this.platform.log.info('Get Active ->', on); cb(null, on ? 1 : 0); } catch (err) { this.platform.log.error('Get Active Error ->', err); cb(err); } }; this.setActiveState = async (active, cb) => { try { this.platform.log.info('Set Active', active); const side = this.accessory.context.side; if (active) { this.platform.log.info('Set Active ->', active); await this.eightSleepPod.turnOn(side); cb(null, 1); } else { this.platform.log.info('Set Active ->', active); await this.eightSleepPod.turnOff(side); cb(null, 0); } } catch (err) { this.platform.log.error('Set Active Error ->', err); cb(err); } }; this.getCurrentHeaterCoolerState = async (cb) => { try { this.platform.log.info('Get CurrentHeaterCoolerState'); const side = this.accessory.context.side; const status = await this.eightSleepPod.getStatus(side); if (status.currentActivity === 'off') { this.platform.log.info('Get CurrentHeaterCoolerState ->', INACTIVE); cb(null, INACTIVE); return; } if (status.currentTargetLevel === 0) { this.platform.log.info('Get CurrentHeaterCoolerState ->', status.currentTargetLevel, IDLE); cb(null, IDLE); } else if (status.currentTargetLevel > 0) { this.platform.log.info('Get CurrentHeaterCoolerState ->', status.currentTargetLevel, HEATING); cb(null, HEATING); } else { this.platform.log.info('Get CurrentHeaterCoolerState ->', status.currentTargetLevel, COOLING); cb(null, COOLING); } } catch (err) { this.platform.log.error('Get CurrentHeaterCoolerState Error ->', err); cb(err); } }; this.getTargetHeaterCoolerState = async (cb) => { try { this.platform.log.info('Get TargetHeaterCoolerState'); const side = this.accessory.context.side; const status = await this.eightSleepPod.getStatus(side); if (status.currentActivity === 'off') { this.platform.log.info('Get TargetHeaterCoolerState ->', AUTO); cb(null, AUTO); return; } if (status.currentTargetLevel > 0) { this.targetHeatCool = HEAT; } else if (status.currentTargetLevel < 0) { this.targetHeatCool = COOL; } this.platform.log.info('Get TargetHeaterCoolerState ->', this.targetHeatCool); cb(null, this.targetHeatCool); } catch (err) { this.platform.log.error('Get TargetHeaterCoolerState Error ->', err); cb(err); } }; this.setTargetHeaterCoolerState = async (state, cb) => { try { this.platform.log.info('Set TargetHeaterCoolerState', state); const side = this.accessory.context.side; const level = await this.eightSleepPod.getLevel(side); if (state === AUTO) { // ignore auto for now.. maybe make it revert to schedule? cb(null, this.targetHeatCool); return; } const targetHeatCool = state; if (level === 0) { this.targetHeatCool = targetHeatCool; cb(null, targetHeatCool); return; } const factor = targetHeatCool === HEAT ? 1 : -1; const nextLevel = (level * factor); this.platform.log.info('Set TargetHeaterCoolerState ->', state, targetHeatCool, nextLevel); if (level !== nextLevel) { await this.eightSleepPod.setLevel(side, nextLevel); } this.targetHeatCool = targetHeatCool; cb(null, targetHeatCool); } catch (err) { this.platform.log.error('Set TargetHeaterCoolerState Error ->', err); cb(err); } }; this.getRotationSpeed = async (cb) => { try { this.platform.log.info('Get RotationSpeed'); const side = this.accessory.context.side; let level = await this.eightSleepPod.getLevel(side); level = Math.abs(level); this.platform.log.info('Get RotationSpeed ->', level); cb(null, level); } catch (err) { this.platform.log.error('Get RotationSpeed Error ->', err); cb(err); } }; this.setRotationSpeed = async (rotationSpeed, cb) => { try { this.platform.log.info('Set RotationSpeed', rotationSpeed); const side = this.accessory.context.side; let level = (Math.round(rotationSpeed / 10) * 10); const factor = this.targetHeatCool === HEAT ? 1 : -1; level = (level * factor); this.platform.log.info('Set RotationSpeed ->', rotationSpeed, level); await this.eightSleepPod.setLevel(side, level); cb(null, Math.abs(level)); } catch (err) { this.platform.log.error('Set RotationSpeed Error ->', err); cb(err); } }; this.getCurrentTemperature = async (cb) => { try { this.platform.log.info('Get CurrentTemperature'); const temp = await this.eightSleepPod.getTemperature(); this.platform.log.info('Get CurrentTemperature ->', temp); cb(null, temp); } catch (err) { this.platform.log.error('Get CurrentTemperature Error ->', err); cb(err); } }; const device = this.accessory.context.device; const clientApi = new eightsleep_1.default(this.accessory.context.clientApiJSON || this.accessory.context.apiClientJSON); this.eightSleepPod = new EightSleepPod_1.default({ clientApi, deviceId: device.deviceId, }); // set accessory information this.accessory .getService(this.platform.Service.AccessoryInformation) .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Eightsleep') .setCharacteristic(this.platform.Characteristic.Model, ((_a = device.sensorInfo) === null || _a === void 0 ? void 0 : _a.skuName) || 'unknown') .setCharacteristic(this.platform.Characteristic.SerialNumber, ((_b = device.sensorInfo) === null || _b === void 0 ? void 0 : _b.serialNumber) || 'unknown'); // see https://developers.homebridge.io/#/service/HeaterCooler this.service = this.accessory.getService(this.platform.Service.HeaterCooler) || this.accessory.addService(this.platform.Service.HeaterCooler); // required Characteristics this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.context.displayName); this.service .getCharacteristic(this.platform.Characteristic.Active) .on('get', this.getActiveState) .on('set', this.setActiveState); this.service .getCharacteristic(this.platform.Characteristic.CurrentHeaterCoolerState) .on('get', this.getCurrentHeaterCoolerState); this.service .getCharacteristic(this.platform.Characteristic.TargetHeaterCoolerState) .on('get', this.getTargetHeaterCoolerState) .on('set', this.setTargetHeaterCoolerState); this.service .getCharacteristic(this.platform.Characteristic.CurrentTemperature) .on('get', this.getCurrentTemperature); // optional Characteristics this.service .getCharacteristic(this.platform.Characteristic.RotationSpeed) .on('get', this.getRotationSpeed) .on('set', this.setRotationSpeed); } } exports.EightsleepPodPlatformAccessory = EightsleepPodPlatformAccessory; //# sourceMappingURL=platformAccessory.js.map