Skip to content

Commit

Permalink
fix: clean message when color_temperature and color are both set (#617)
Browse files Browse the repository at this point in the history
* fix: clean message when color_temperature and color are both set

* fix: test cases
  • Loading branch information
philipparndt authored May 20, 2024
1 parent a1be1d9 commit 27967d1
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 20 deletions.
38 changes: 24 additions & 14 deletions app/lib/api/v2/hue-api-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) {
Expand Down
76 changes: 73 additions & 3 deletions app/lib/put/put-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -74,7 +74,6 @@ describe("PUT handler", () => {
expect(stopTestPutLights()).toStrictEqual([
{
dimming: { brightness: 50 },
color: deviceStubs.lightWithColor.color,
color_temperature: {
mirek: null,
mirek_schema: {
Expand All @@ -101,7 +100,6 @@ describe("PUT handler", () => {
expect(stopTestPutLights()).toStrictEqual([
{
dimming: { brightness: 50 },
color: deviceStubs.lightWithColor.color,
color_temperature: {
mirek: null,
mirek_schema: {
Expand Down Expand Up @@ -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]
}
})
})
})
})
4 changes: 1 addition & 3 deletions app/lib/put/put-handler.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
Expand Down

0 comments on commit 27967d1

Please sign in to comment.