UNPKG

homebridge-hatch-baby-rest

Version:
72 lines (71 loc) 3.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RestPlus = void 0; const hatch_sleep_types_1 = require("../shared/hatch-sleep-types"); const operators_1 = require("rxjs/operators"); const colors_1 = require("../shared/colors"); const iot_device_1 = require("./iot-device"); class RestPlus extends iot_device_1.IotDevice { constructor() { super(...arguments); this.model = 'Rest+'; this.audioTracks = hatch_sleep_types_1.audioTracks; this.onVolume = this.onState.pipe((0, operators_1.map)((state) => (0, iot_device_1.convertToPercentage)(state.a.v)), (0, operators_1.distinctUntilChanged)()); this.onAudioTrack = this.onState.pipe((0, operators_1.map)((state) => state.a.t), (0, operators_1.distinctUntilChanged)()); this.onAudioPlaying = this.onAudioTrack.pipe((0, operators_1.map)((track) => track !== 0 /* AudioTrack.None */), (0, operators_1.distinctUntilChanged)()); this.onIsPowered = this.onState.pipe((0, operators_1.map)((state) => state.isPowered), (0, operators_1.distinctUntilChanged)()); this.onBrightness = this.onState.pipe((0, operators_1.map)(({ c }) => { if (c.r === 0 && c.g === 0 && c.b === 0 && !c.R && !c.W) { // when "no" color is selected in Rest app, i (intensity) doesn't get set to 0, but everything else does return 0; } return (0, iot_device_1.convertToPercentage)(c.i); }), (0, operators_1.distinctUntilChanged)()); this.onHsb = this.onState.pipe((0, operators_1.map)((state) => (0, colors_1.rgbToHsb)(state.c, iot_device_1.MAX_IOT_VALUE))); this.onHue = this.onHsb.pipe((0, operators_1.map)(({ h }) => h), (0, operators_1.distinctUntilChanged)()); this.onSaturation = this.onHsb.pipe((0, operators_1.map)(({ s }) => s), (0, operators_1.distinctUntilChanged)()); this.onBatteryLevel = this.onState.pipe((0, operators_1.map)((state) => state.deviceInfo.b), (0, operators_1.distinctUntilChanged)()); this.onFirmwareVersion = this.onState.pipe((0, operators_1.map)((state) => state.deviceInfo.f)); } setVolume(percentage) { this.update({ a: { v: (0, iot_device_1.convertFromPercentage)(percentage), }, }); } setAudioTrack(audioTrack) { this.update({ a: { t: audioTrack, }, }); } setAudioPlaying(playing) { if (!playing) { return this.setAudioTrack(0 /* AudioTrack.None */); } // do nothing for other audio tracks. They will be handed to `setAudioTrack` directly } setColor(color) { this.update({ c: color, }); } setHsb({ h, s, b }) { // NOTE: lights assume 100% brightness in color calculations const rgb = (0, colors_1.hsbToRgb)({ h, s, b: 100 }, iot_device_1.MAX_IOT_VALUE); this.setColor({ ...rgb, R: false, W: false, i: (0, iot_device_1.convertFromPercentage)(b), }); } setPower(on) { this.update({ isPowered: on, }); } } exports.RestPlus = RestPlus;