UNPKG

homebridge-eightsleep-pod

Version:
109 lines 4.68 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const EightSleepAppApi_1 = require("eightsleep/dist/cjs/EightSleepAppApi"); const baseerr_1 = __importDefault(require("baseerr")); const memoize_concurrent_1 = __importDefault(require("memoize-concurrent")); class MapWithClear extends Map { async clear() { Array.from(this.keys()).forEach((key) => this.delete(key)); } } class EightSleepPod { constructor({ clientApi, deviceId }) { this.getDeviceStatus = memoize_concurrent_1.default(() => this.clientApi.getAppApiClient().getDeviceStatus(this.deviceId)); this.getStatus = async (side = EightSleepAppApi_1.Sides.SOLO) => { const deviceStatus = await this.getDeviceStatus(); const sideStatus = deviceStatus[side]; if (!sideStatus) { throw new baseerr_1.default('invalid deviceStatus', { deviceStatus, side }); } return sideStatus; }; this.turnOn = memoize_concurrent_1.default(async (side = EightSleepAppApi_1.Sides.SOLO) => { return await this.clientApi .getAppApiClient() .setDeviceSideOn(this.deviceId, side); }, { cacheKey: (args) => { const cache = this.setOnOffCache; const key = (args[0] || EightSleepAppApi_1.Sides.SOLO); const cached = cache.get(key); cache.clear(); if (cached != null) { cache.set(key, cached); } return key; }, cache: this.setOnOffCache, }); this.turnOff = memoize_concurrent_1.default(async (side = EightSleepAppApi_1.Sides.SOLO) => { return await this.clientApi .getAppApiClient() .setDeviceSideOff(this.deviceId, side); }, { cacheKey: (args) => { const cache = this.setOnOffCache; const key = (args[0] || EightSleepAppApi_1.Sides.SOLO); const cached = cache.get(key); cache.clear(); if (cached != null) { cache.set(key, cached); } return key; }, cache: this.setOnOffCache, }); this.setLevel = memoize_concurrent_1.default(async (side = EightSleepAppApi_1.Sides.SOLO, level) => { return await this.clientApi .getAppApiClient() .setDeviceSideLevel(this.deviceId, side, level); }, { cacheKey: ([side, level]) => { const cache = this.setLevelCache; const key = `${side || EightSleepAppApi_1.Sides.SOLO}::${level}`; const cached = cache.get(key); cache.clear(); if (cached != null) { cache.set(key, cached); } return key; }, cache: this.setLevelCache, }); this.getTemperature = memoize_concurrent_1.default(async () => { var _a, _b; const hourAgo = (() => { const d = new Date(); d.setHours(d.getHours() - 1); return d; })(); const json = await this.clientApi .getAppApiClient() .json(`v1/devices/${this.deviceId}/metrics/ambient?granularity=minute&from=${hourAgo.toISOString()}&scope=humidity&scope=roomTemperature`, 200); const timeseries = (_b = (_a = json.metrics) === null || _a === void 0 ? void 0 : _a.roomTemperature) === null || _b === void 0 ? void 0 : _b.timeseries; if (timeseries === null || timeseries === void 0 ? void 0 : timeseries.length) { return timeseries[timeseries.length - 1].value || 0; } return 0; }, { maxAge: 3600, }); this.clientApi = clientApi; this.deviceId = deviceId; this.setLevelCache = new MapWithClear(); this.setOnOffCache = new MapWithClear(); } async isOn(side = EightSleepAppApi_1.Sides.SOLO) { const sideStatus = await this.getStatus(side); return sideStatus.currentActivity !== 'off'; } async getLevel(side = EightSleepAppApi_1.Sides.SOLO) { const sideStatus = await this.getStatus(side); return sideStatus.currentTargetLevel || 0; } } exports.default = EightSleepPod; //# sourceMappingURL=EightSleepPod.js.map