UNPKG

homebridge-hatch-baby-rest

Version:
64 lines (63 loc) 2.42 kB
import { Product, } from "../shared/hatch-sleep-types.js"; import { distinctUntilChanged, map } from 'rxjs/operators'; import { IotDevice } from "./iot-device.js"; import { apiPath } from "./rest-client.js"; export class RestIot extends IotDevice { info; onIotClient; restClient; get model() { switch (this.info.product) { case Product.restoreIot: return 'Restore IoT'; case Product.riotPlus: return 'Rest+ 2nd Gen'; case Product.restBaby: return 'Hatch Baby'; case Product.restoreV4: return 'Restore 2'; case Product.restoreV5: return 'Restore 3'; case Product.riot: return 'Rest 2nd Gen'; default: return 'Rest 2nd Gen'; } } constructor(info, onIotClient, restClient) { super(info, onIotClient); this.info = info; this.onIotClient = onIotClient; this.restClient = restClient; } onSomeContentPlaying = this.onState.pipe(map((state) => state.current.playing !== 'none'), distinctUntilChanged()); onFirmwareVersion = this.onState.pipe(map((state) => state.deviceInfo.f)); setCurrent(playing, step, srId) { this.update({ current: { playing, step, srId, paused: false, // Must explicitly unpause or device ignores the command }, }); } async turnOnRoutine() { const routines = await this.fetchRoutines(); this.setCurrent('routine', 1, routines[0].id); } turnOff() { this.setCurrent('none', 0, 0); } async fetchRoutines() { const routinesPath = apiPath(`service/app/routine/v2/fetch?macAddress=${encodeURIComponent(this.info.macAddress)}`), allRoutines = await this.restClient.request({ url: routinesPath, method: 'GET', }), sortedRoutines = allRoutines.sort((a, b) => a.displayOrder - b.displayOrder), touchRingRoutines = sortedRoutines.filter((routine) => { return (routine.type === 'favorite' || // Before upgrade, only favorites were on touch ring routine.button0 // After upgrade, many routine types can be on touch ring but will have `button0: true` ); }); return touchRingRoutines; } }