UNPKG

@felixgeelhaar/govee-api-client

Version:

Enterprise-grade TypeScript client library for the Govee Developer REST API

106 lines 3.22 kB
export class DeviceState { constructor(deviceId, model, online, properties = {}) { this.validateDeviceId(deviceId); this.validateModel(model); this._deviceId = deviceId; this._model = model; this._online = online; this._properties = new Map(Object.entries(properties)); } validateDeviceId(deviceId) { if (!deviceId || typeof deviceId !== 'string' || deviceId.trim().length === 0) { throw new Error('Device ID must be a non-empty string'); } } validateModel(model) { if (!model || typeof model !== 'string' || model.trim().length === 0) { throw new Error('Model must be a non-empty string'); } } get deviceId() { return this._deviceId; } get model() { return this._model; } get online() { return this._online; } get properties() { return this._properties; } getProperty(key) { return this._properties.get(key); } hasProperty(key) { return this._properties.has(key); } getPowerState() { const powerState = this.getProperty('powerSwitch'); return powerState?.value; } getBrightness() { const brightnessState = this.getProperty('brightness'); return brightnessState?.value; } getColor() { const colorState = this.getProperty('color'); return colorState?.value; } getColorTemperature() { const colorTempState = this.getProperty('colorTem'); return colorTempState?.value; } isPoweredOn() { return this.getPowerState() === 'on'; } isPoweredOff() { return this.getPowerState() === 'off'; } isOnline() { return this._online; } isOffline() { return !this._online; } equals(other) { if (this._deviceId !== other._deviceId || this._model !== other._model || this._online !== other._online) { return false; } if (this._properties.size !== other._properties.size) { return false; } const thisEntries = Array.from(this._properties.entries()); for (const [key, value] of thisEntries) { const otherValue = other._properties.get(key); if (!otherValue || JSON.stringify(value) !== JSON.stringify(otherValue)) { return false; } } return true; } toString() { const status = this._online ? 'online' : 'offline'; const power = this.getPowerState() || 'unknown'; return `DeviceState(${this._deviceId}, ${status}, ${power})`; } toObject() { const properties = {}; const entries = Array.from(this._properties.entries()); for (const [key, value] of entries) { properties[key] = value; } return { deviceId: this._deviceId, model: this._model, online: this._online, properties, }; } static fromObject(obj) { return new DeviceState(obj.deviceId, obj.model, obj.online, obj.properties); } } //# sourceMappingURL=DeviceState.js.map