diff --git a/app/lib/api/v2/hue-api-v2.ts b/app/lib/api/v2/hue-api-v2.ts index 408f2512..469c14e0 100644 --- a/app/lib/api/v2/hue-api-v2.ts +++ b/app/lib/api/v2/hue-api-v2.ts @@ -53,22 +53,42 @@ type PutLight = { color?: LightColorData } +const EMPTY = JSON.stringify({}) + +export const cleanMessage = (message: any) => { + const result = { ...message } + + if (JSON.stringify(result.color_temperature) === EMPTY) { + delete result.color_temperature + } + + if (JSON.stringify(result.color) === EMPTY) { + delete result.color + } + + if (result.color_temperature && result.color) { + delete result.color + } + + return result +} + export const putLightResource = async (resource: Light) => { - return putLight(resource, { + return putLight(resource, cleanMessage({ dimming: resource.dimming, on: resource.on, color_temperature: resource.color_temperature, color: resource.color - }) + })) } export const putGroupedLightResource = async (resource: GroupedLight) => { - return putLight(resource, { + return putLight(resource, cleanMessage({ dimming: resource.dimming, on: resource.on, color_temperature: resource.color_temperature, color: resource.color - }) + })) } const putLightLocked = async (resource: HueIdentifiable, message: PutLight) => { @@ -107,18 +127,8 @@ export const stopTestPutLights = () => { return result } -export const validatePutLights = (message: any, info: string) => { - if (message.color && message.color_temperature && !Object.is(message.color, {}) && !Object.is(message.color_temperature, {})) { - log.warn(`${info} Both color and color_temperature set, put is likely to fail`, { - message - }) - } -} - const lock = new AsyncLock({ timeout: 5000 }) export const putLight = async (light: HueIdentifiable, message: PutLight) => { - validatePutLights(message, "putLight") - const [resolveResult, promise] = resolvable() lock.acquire("put", async (done) => { if (testPutLights) { diff --git a/app/lib/put/put-handler.test.ts b/app/lib/put/put-handler.test.ts index dc6b6098..1f36e4b0 100644 --- a/app/lib/put/put-handler.test.ts +++ b/app/lib/put/put-handler.test.ts @@ -6,7 +6,7 @@ import { putMessage } from "./put-handler" import { TestLogger } from "../logger.test" import { expectedForNotifyRestore } from "./effects/light-effect-handler-stubs" import { afterEach, beforeAll, beforeEach, describe, expect, test, vi } from "vitest" -import { startTestPutLights, stopTestPutLights } from "../api/v2/hue-api-v2" +import { cleanMessage, startTestPutLights, stopTestPutLights } from "../api/v2/hue-api-v2" vi.spyOn(api, "loadTypedById").mockReturnValue(Promise.resolve(deviceStubs.lightWithColor)) @@ -74,7 +74,6 @@ describe("PUT handler", () => { expect(stopTestPutLights()).toStrictEqual([ { dimming: { brightness: 50 }, - color: deviceStubs.lightWithColor.color, color_temperature: { mirek: null, mirek_schema: { @@ -101,7 +100,6 @@ describe("PUT handler", () => { expect(stopTestPutLights()).toStrictEqual([ { dimming: { brightness: 50 }, - color: deviceStubs.lightWithColor.color, color_temperature: { mirek: null, mirek_schema: { @@ -145,4 +143,76 @@ describe("PUT handler", () => { } ]) }) + + describe("Clean message", () => { + test("Both set", async () => { + const cleaned = cleanMessage({ + color_temperature: { + mirek: 250 + }, + color: { + xy: [0.5, 0.5] + } + }) + expect(cleaned).toStrictEqual({ + color_temperature: { + mirek: 250 + } + }) + }) + + test("Temperature set", async () => { + const cleaned = cleanMessage({ + color_temperature: { + mirek: 250 + } + }) + expect(cleaned).toStrictEqual({ + color_temperature: { + mirek: 250 + } + }) + }) + + test("Temperature set and color empty", async () => { + const cleaned = cleanMessage({ + color_temperature: { + mirek: 250 + }, + color: {} + }) + expect(cleaned).toStrictEqual({ + color_temperature: { + mirek: 250 + } + }) + }) + + test("Color set", async () => { + const cleaned = cleanMessage({ + color: { + xy: [0.5, 0.5] + } + }) + expect(cleaned).toStrictEqual({ + color: { + xy: [0.5, 0.5] + } + }) + }) + + test("Color set and temperature empty", async () => { + const cleaned = cleanMessage({ + color: { + xy: [0.5, 0.5] + }, + color_temperature: {} + }) + expect(cleaned).toStrictEqual({ + color: { + xy: [0.5, 0.5] + } + }) + }) + }) }) diff --git a/app/lib/put/put-handler.ts b/app/lib/put/put-handler.ts index 9b9190b2..e13b922f 100644 --- a/app/lib/put/put-handler.ts +++ b/app/lib/put/put-handler.ts @@ -1,7 +1,7 @@ import { HueIdentifiable } from "../api/v2/types/general" import { isLight } from "../api/v2/types/light" import { isEffectMessage, LightEffectMessage, LightMessage, toGroupedLight, toLight } from "../messages/light-message" -import { putGroupedLightResource, putLightResource, validatePutLights } from "../api/v2/hue-api-v2" +import { putGroupedLightResource, putLightResource } from "../api/v2/hue-api-v2" import { log } from "../logger" import { applyEffect } from "./effects/light-effect-handler" import { isGroupedLight } from "../api/v2/types/grouped-light" @@ -17,8 +17,6 @@ export const putMessage = async (resource: HueIdentifiable, message: Buffer) => else { const newResource = toLight(resource, lightMsg) - validatePutLights(newResource, "putMessage") - // resource will be updated by the Hue SSE API try { await putLightResource(newResource)