node-red-contrib-virtual-smart-home
Version:
Do stuff in Node-RED with Amazon Alexa - no custom hardware, no separate accounts, no hassle.
107 lines (106 loc) • 4.59 kB
JSON
[
{
"id": "affffc88a48e1001",
"type": "subflow",
"name": "Alexa -> Home Assistant",
"info": "",
"category": "",
"in": [{ "x": 60, "y": 80, "wires": [{ "id": "2565add7dc2c886d" }] }],
"out": [],
"env": [],
"meta": {},
"color": "#039be5",
"icon": "font-awesome/fa-home",
"status": {
"x": 320,
"y": 160,
"wires": [{ "id": "2e59c216b911bfdf", "port": 0 }]
}
},
{
"id": "2565add7dc2c886d",
"type": "function",
"z": "affffc88a48e1001",
"name": "convert",
"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,
"initialize": "",
"finalize": "",
"libs": [],
"x": 200,
"y": 80,
"wires": [["d6bd4dac36a284ea"]]
},
{
"id": "d6bd4dac36a284ea",
"type": "api-call-service",
"z": "affffc88a48e1001",
"name": "",
"server": "",
"version": 5,
"debugenabled": false,
"domain": "",
"service": "",
"areaId": [],
"deviceId": [],
"entityId": [],
"data": "",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 370,
"y": 80,
"wires": [[]]
},
{
"id": "2e59c216b911bfdf",
"type": "status",
"z": "affffc88a48e1001",
"name": "",
"scope": ["2565add7dc2c886d"],
"x": 200,
"y": 160,
"wires": [[]]
},
{
"id": "9fe9a12665bce27b",
"type": "comment",
"z": "affffc88a48e1001",
"name": "READ ME!",
"info": "# Please note\n\nYou must have the package `node-red-contrib-home-assistant-websocket` installed in order for this subflow to work!",
"x": 380,
"y": 40,
"wires": []
},
{
"id": "1ecc03b0.52929c",
"type": "vsh-virtual-device",
"z": "3156dd60.366d12",
"name": "Kitchen Light",
"topic": "",
"metadata": "{\"domain\":\"light\",\"entityId\":\"kitchen\"}",
"connection": "",
"template": "SWITCH",
"retrievable": false,
"passthrough": false,
"diff": false,
"filter": false,
"filterTopic": false,
"x": 111,
"y": 280,
"wires": [["f057c4815e3be504"]]
},
{
"id": "f057c4815e3be504",
"type": "subflow:affffc88a48e1001",
"z": "3156dd60.366d12",
"name": "",
"x": 430,
"y": 280,
"wires": []
}
]