Skip to content

Commit

Permalink
Fix color and temperature mapping for home assistant, plus refactorin…
Browse files Browse the repository at this point in the history
…g to improve readability.
  • Loading branch information
enforcebeautifulcode committed Feb 9, 2024
1 parent ae84184 commit 60b485c
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion examples/alexa_to_homeassistant.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"type": "function",
"z": "affffc88a48e1001",
"name": "convert",
"func": "const directiveToService = {\n TurnOff: \"turn_off\",\n TurnOn: \"turn_on\",\n SetBrightness: \"turn_on\",\n SetColorTemperature: \"turn_on\",\n SetColor: \"turn_on\"\n}\n\nconst {domain, entityId} = msg.metadata ?? {}\n\nconst service = directiveToService[msg.payload.directive]\n\nnode.status({\n fill: service == 'turn_on' ? 'yellow' : 'grey',\n shape: 'dot',\n text: msg.payload.directive\n})\n\nreturn {\n payload: {\n domain,\n service,\n data: {\n entity_id: domain + \".\" + entityId,\n ...(service === \"turn_on\" & msg.payload.brightness !== undefined && { brightness_pct: msg.payload.brightness}),\n ...(service === \"turn_on\" & msg.payload.color_xy !== undefined && msg.payload.lightMode === \"hsb\" && { color_xy: msg.payload.color_xy }),\n ...(service === \"turn_on\" & msg.payload.colorTemperatureInKelvin !== undefined & msg.payload.lightMode === \"temp\" && { color_temp: msg.payload.colorTemperatureInKelvin}) }\n }\n}",
"func": "// Mapping of directives to Home Assistant services\nconst directiveToServiceMapping = {\n TurnOff: \"turn_off\",\n TurnOn: \"turn_on\",\n SetBrightness: \"turn_on\",\n SetColorTemperature: \"turn_on\",\n SetColor: \"turn_on\",\n AdjustBrightness: \"turn_on\"\n};\n\n// Extract metadata from the message, defaulting to an empty object\nconst { domain, entityId } = msg.metadata ?? {};\nconst service = directiveToServiceMapping[msg.payload.directive];\n\n// Set the node status visually in the Node-RED flow\nupdateNodeStatus(service, msg.payload.directive);\n\n// Generate payload data based on the service and message\nconst payloadData = generatePayloadData();\n\n// Construct and return the full payload\nreturn constructPayload(domain, service, payloadData);\n\n// Function definitions below\n\nfunction updateNodeStatus(service, directive) {\n node.status({\n fill: service === 'turn_on' ? 'yellow' : 'grey',\n shape: 'dot',\n text: directive\n });\n}\n\nfunction generatePayloadData() {\n const data = { entity_id: `${domain}.${entityId}` };\n\n if (service === \"turn_on\") {\n Object.assign(data, handleTurnOnConditions());\n }\n\n return data;\n}\n\nfunction handleTurnOnConditions() {\n const data = {};\n if (hasBrightness()) {\n data.brightness_pct = msg.payload.brightness;\n }\n if (isColorModeHSB()) {\n data.xy_color = msg.payload.color_xy;\n }\n if (isTemperatureMode()) {\n data.color_temp = convertKelvinToMireds(msg.payload.colorTemperatureInKelvin);\n }\n return data;\n}\n\nfunction hasBrightness() {\n return msg.payload.brightness !== undefined;\n}\n\nfunction isColorModeHSB() {\n return msg.payload.color_xy !== undefined && msg.payload.lightMode === \"hsb\";\n}\n\nfunction isTemperatureMode() {\n return msg.payload.colorTemperatureInKelvin !== undefined && msg.payload.lightMode === \"temp\";\n}\n\nfunction convertKelvinToMireds(kelvin) {\n // Converts Kelvin to Mireds to comply with Home Assistant's expected units\n return Math.round(1000000 / kelvin);\n}\n\nfunction constructPayload(domain, service, data) {\n return {\n payload: {\n domain,\n service,\n data\n }\n };\n}\n",
"outputs": 1,
"timeout": "",
"noerr": 0,
Expand Down

0 comments on commit 60b485c

Please sign in to comment.