UNPKG

@constructorfleet/ultimate-govee

Version:

Library for interacting with Govee devices written in Typescript.

232 lines 10 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HumidifierActiveState = exports.AutoModeState = exports.AutoModeStateName = exports.CustomModeState = exports.CustomModeStateName = exports.ManualModeState = exports.ManualModeStateName = void 0; const _ultimate_govee_common_1 = require("../../../../../common"); const states_1 = require("../../../states"); var HumidifierModes; (function (HumidifierModes) { HumidifierModes[HumidifierModes["MANUAL"] = 1] = "MANUAL"; HumidifierModes[HumidifierModes["PROGRAM"] = 2] = "PROGRAM"; HumidifierModes[HumidifierModes["AUTO"] = 3] = "AUTO"; })(HumidifierModes || (HumidifierModes = {})); exports.ManualModeStateName = 'manualMode'; class ManualModeState extends states_1.DeviceOpState { constructor(device, opType = _ultimate_govee_common_1.OpType.REPORT, identifier = [0x05, HumidifierModes.MANUAL]) { super({ opType, identifier }, device, exports.ManualModeStateName, undefined); this.stateToCommand = (nextState) => { if (nextState === undefined) { return undefined; } if (nextState < 0) { this.logger.warn('Next state is less than 0, adjusting to 0'); nextState = 0; } else if (nextState > 9) { this.logger.warn('Next state is greater than 9, adjusting to 9'); nextState = 9; } return { status: { op: { command: [[nextState]], }, }, command: { data: { command: [ (0, _ultimate_govee_common_1.asOpCode)(_ultimate_govee_common_1.OpType.COMMAND, ...this.identifier, nextState), ], }, }, }; }; } parseOpCommand(opCommand) { const command = opCommand.slice(1); this.stateValue.next(command[command.indexOf(0x00) - 1]); } } exports.ManualModeState = ManualModeState; exports.CustomModeStateName = 'customMode'; class CustomModeState extends states_1.DeviceOpState { constructor(device, opType = _ultimate_govee_common_1.OpType.REPORT, identifier = [0x05, HumidifierModes.PROGRAM]) { super({ opType, identifier }, device, exports.CustomModeStateName, undefined); this.customModes = {}; this.stateToCommand = (nextState) => { if (nextState === undefined) { this.logger.warn('Program not specified, ignoring command'); return; } const newProgram = { id: nextState?.id ?? this.customModes?.currentProgram?.id ?? 0, duration: nextState?.duration ?? this.customModes?.currentProgram?.duration ?? 0, remaining: nextState?.remaining ?? this.customModes?.currentProgram?.remaining ?? 100, mistLevel: nextState?.mistLevel ?? this.customModes?.currentProgram?.mistLevel ?? 0, }; const newPrograms = { ...(this.customModes.programs ?? { 0: { id: 0, duration: 100, remaining: newProgram.id > 0 ? 0 : 100, mistLevel: 0, }, 1: { id: 1, duration: 100, remaining: newProgram.id > 1 ? 0 : 100, mistLevel: 0, }, 2: { id: 2, duration: 32640, remaining: 32640, mistLevel: 0, }, }), [newProgram.id]: newProgram, }; return { command: { data: { command: [ (0, _ultimate_govee_common_1.asOpCode)(_ultimate_govee_common_1.OpType.COMMAND, this.identifier, // HumidifierModes.PROGRAM, newProgram.id, ...[0, 1, 2].reduce((commands, program) => { commands.push(...[ newPrograms[program].mistLevel, Math.floor(newPrograms[program].duration / 255), newPrograms[program].duration % 255, Math.floor(newPrograms[program].remaining / 255), newPrograms[program].remaining % 255, ]); return commands; }, [])), ], }, }, status: { op: { command: [ [ newProgram.id, ...[0, 1, 2].reduce((commands, program) => { commands.push(...[ newPrograms[program].mistLevel, Math.floor(newPrograms[program].duration / 255), newPrograms[program].duration % 255, undefined, undefined, ]); return commands; }, []), ], ], }, }, }; }; } parseOpCommand(opCommand) { const command = opCommand.slice(1); const value = { currentProgramId: command[0], programs: [0, 1, 2].map((i) => ({ id: i, mistLevel: command[1 + 5 * i], duration: command[2 + 5 * i] * 255 + command[3 + 5 * i], remaining: command[4 + 5 * i] * 255 + command[5 + 5 * i], })), }; this.customModes = { ...value, currentProgram: value.currentProgramId !== undefined ? value.programs[value.currentProgramId] : undefined, }; this.stateValue.next(this.customModes.currentProgram); } } exports.CustomModeState = CustomModeState; exports.AutoModeStateName = 'autoMode'; class AutoModeState extends states_1.DeviceOpState { constructor(device, humidityState, opType = 0xaa, identifier = [0x05, HumidifierModes.AUTO]) { super({ opType, identifier }, device, exports.AutoModeStateName, {}); this.humidityState = humidityState; this.stateToCommand = (nextState) => { if (nextState?.targetHumidity === undefined) { this.logger.warn('Target humidity not specified, ignoring state command'); return undefined; } if (this.humidityState?.value?.range) { const { range } = this.humidityState.value; if (nextState.targetHumidity < (range.min ?? 0)) { this.logger.warn(`Target humidity ${nextState.targetHumidity} is less than minimum ${range.min ?? 0}, adjusting to minimum`); nextState.targetHumidity = range.min ?? 0; } else if (nextState.targetHumidity > (range.max ?? 0)) { this.logger.warn(`Target humidity ${nextState.targetHumidity} is greater than maximum ${range.max ?? 100}, adjusting to maximum`); nextState.targetHumidity = range.max ?? 100; } } return { command: { data: { command: [(0, _ultimate_govee_common_1.asOpCode)(0x33, this.identifier, nextState.targetHumidity)], }, }, status: { op: { command: [[nextState.targetHumidity]], }, }, }; }; } parseOpCommand(opCommand) { this.stateValue.next({ targetHumidity: opCommand[0], }); } } exports.AutoModeState = AutoModeState; class HumidifierActiveState extends states_1.ModeState { constructor(device, states) { super(device, states.filter((state) => state !== undefined)); this.stateToCommand = () => { return undefined; }; this.activeIdentifier?.subscribe((identifier) => { if (identifier === undefined) { return; } const modeIdentifier = identifier[identifier.indexOf(0x00) - 1]; switch (modeIdentifier) { case HumidifierModes.MANUAL: this.stateValue.next(this.modes.find((mode) => mode.name === exports.ManualModeStateName)); break; case HumidifierModes.PROGRAM: this.stateValue.next(this.modes.find((mode) => mode.name === exports.CustomModeStateName)); break; case HumidifierModes.AUTO: this.stateValue.next(this.modes.find((mode) => mode.name === exports.AutoModeStateName)); break; default: break; } }); } setState(nextState) { if (nextState === undefined) { this.logger.warn('Next state is undefined, ignoring command'); return []; } return nextState.setState(nextState.value); } } exports.HumidifierActiveState = HumidifierActiveState; //# sourceMappingURL=humidifier.modes.js.map