Files
HA-NerdFlows-Functions/lightfx/processing.js

358 lines
10 KiB
JavaScript

// Set constants
const states = global.get('homeassistant.homeAssistant.states')
const occupied = states["binary_sensor.basement_occupied"].state
const quiet = states["input_boolean.studio_quiet"].state
const stateTinaDesk = states["light.tina_desk_lights"].state
const stateLivingRoom = states["light.living_room_lights"].state
const stateDiningRoom = states["light.dining_room_lamp"].state
const stateStairwell = states["light.stairwell_led_strip"].state
const stateBasement = states["light.basement_studio_lights"].state
const adaptLivingRoom = states["switch.adaptive_lighting_living_room"].state
const adaptDiningRoom = states["switch.adaptive_lighting_dining_room_lamp"].state
const adaptBasement = states["switch.adaptive_lighting_basement_studio"].state
const nightLivingRoom = states["switch.adaptive_lighting_sleep_mode_living_room"].state
const nightDiningRoom = states["switch.adaptive_lighting_sleep_mode_dining_room_lamp"].state
const nightBasement = states["switch.adaptive_lighting_sleep_mode_basement_studio"].state
const currentSceneBasement = states["input_text.basement_studio_selected_scene"].state
const currentSceneLivingRoom = states["input_text.living_room_selected_scene"].state
const currentSceneTinaDesk = states["input_text.tina_desk_selected_scene"].state
const currentSceneDiningRoom = states["input_text.dining_room_lamp_selected_scene"].state
const effectBasement = msg.payload.event.basement
const effectFirstFloor = msg.payload.event.first_floor
const effectColor = msg.payload.event.color
const effectDivide = msg.payload.event.divide
const duration = msg.payload.event.duration
const delay = duration * 1000
const adaptiveSwitchFirstFloor = flow.get("adaptiveFirstFloor", "diskCon")
const adaptiveSwitchBasement = flow.get("adaptiveBasement", "diskCon")
const lightsTinaDesk = flow.get('lightsTinaDesk', 'diskCon')
const lightsBasement = flow.get('lightsBasement', 'diskCon')
const lightsLivingRoom = flow.get('lightsLivingRoom', 'diskCon')
const lightsDiningRoom = flow.get('lightsDiningRoom', 'diskCon')
const alertFirstFloor = flow.get('alertFirstFloor')
const alertBasement = flow.get('alertBasement')
// Set flow contexts to be used later in the reset node
flow.set("currentSceneBasement", currentSceneBasement)
flow.set("currentSceneLivingRoom", currentSceneLivingRoom)
flow.set("currentSceneTinaDesk", currentSceneTinaDesk)
flow.set('currentSceneDiningRoom', currentSceneDiningRoom)
flow.set("adaptLivingRoom", adaptLivingRoom)
flow.set("adaptBasement", adaptBasement)
flow.set("nightLivingRoom", nightLivingRoom)
flow.set("nightBasement", nightBasement)
flow.set('nightDiningRoom', nightDiningRoom)
flow.set("stateTinaDesk", stateTinaDesk)
flow.set("stateLivingRoom", stateLivingRoom)
flow.set("stateDiningRoom", stateDiningRoom)
flow.set("stateStairwell", stateStairwell)
flow.set("stateBasement", stateBasement)
// Define empty arrays for basement LED strip zones
let basement1 = []
let basement2 = []
let basementFull = []
// Define empty arrays for living room LED strip zones
let livingRoom1 = []
let livingRoom2 = []
let livingRoomFull = []
// Color names are set by default, however certain effects can override them below
let colorName1 = effectColor
let colorName2 = "white"
// Define empty object for LIFX effect settings
let lifxData = {}
let effectLifx = msg.payload.event.lifx
// Setup the proper zones to use for the effect
if (effectDivide === 'half') {
basement1 = flow.get("halfBasement1", "diskCon")
basement2 = flow.get("halfBasement2", "diskCon")
livingRoom1 = flow.get("halfLivingRoom1", "diskCon")
livingRoom2 = flow.get("halfLivingRoom2", "diskCon")
} else if (effectDivide === 'quarter') {
basement1 = flow.get("effectSegBasement1", "diskCon")
basement2 = flow.get("effectSegBasement2", "diskCon")
livingRoom1 = flow.get("effectSegLivingRoom1", "diskCon")
livingRoom2 = flow.get("effectSegLivingRoom2", "diskCon")
} else if (effectDivide === 'full') {
basementFull = flow.get("basementFull", "diskCon")
livingRoomFull = flow.get("livingRoomFull", "diskCon")
}
if (effectFirstFloor === true) {
flow.set('alertFirstFloor', true)
}
if (effectBasement === true) {
flow.set('alertBasement', true)
}
// Set/change colors if necessary
if (effectColor === 'police') {
colorName1 = 'red'
colorName2 = 'blue'
}
// Define parameters for different LIFX effects
if (effectLifx === 'move') {
lifxData = {
"speed": 0.5,
"direction": "right",
"power_on": false
}
} else if (effectLifx === 'ping') {
effectLifx = 'pulse'
colorName2 = colorName1
lifxData = {
"power_on": false,
"mode": "ping",
"period": 1,
"cycles": duration,
"color_name": "white",
"brightness": 255
}
} else if (effectLifx === 'blink') {
effectLifx = 'pulse'
colorName2 = colorName1
lifxData = {
"power_on": false,
"mode": "blink",
"period": 0.5,
"cycles": duration * 2,
"color_name": "white",
"brightness": 255
}
}
// Define messages
let sendBasementStripFX = {
"payload": {
"domain": "lifx",
"service": "effect_" + effectLifx,
"target": {
"entity_id": ["light.basement_led_strip_1"]
},
"data": lifxData
}
}
let sendBasementStrip1 = {
"payload": {
"domain": "lifx",
"service": "set_state",
"target": {
"entity_id": ["light.basement_led_strip_1"]
},
"data": {
"zones": basement1,
"color_name": colorName1,
"brightness": 255
}
}
}
let sendBasementStrip2 = {
"payload": {
"domain": "lifx",
"service": "set_state",
"target": {
"entity_id": ["light.basement_led_strip_1"]
},
"data": {
"zones": basement2,
"color_name": colorName2,
"brightness": 255,
"power": true
}
}
}
let sendBasementLights = {
"payload": {
"domain": "light",
"service": "turn_on",
"target": {
"entity_id": [
"light.basement_tall_lamp",
"light.basement_short_lamp",
"light.basement_stairwell"
]
},
"data": {
"color_name": colorName1,
"brightness": 255
}
}
}
let sendLivingRoomStripFX = {
"payload": {
"domain": "lifx",
"service": "effect_" + effectLifx,
"target": {
"entity_id": ["light.living_room_led_strip"]
},
"data": lifxData
}
}
let sendLivingRoomStrip1 = {
"payload": {
"domain": "lifx",
"service": "set_state",
"target": {
"entity_id": ["light.living_room_led_strip"]
},
"data": {
"zones": livingRoom1,
"color_name": colorName1,
"brightness": 255
}
}
}
let sendLivingRoomStrip2 = {
"payload": {
"domain": "lifx",
"service": "set_state",
"target": {
"entity_id": ["light.living_room_led_strip"]
},
"data": {
"zones": livingRoom2,
"color_name": colorName2,
"brightness": 255,
"power": true
}
}
}
let sendLivingRoomLights = {
"payload": {
"domain": "light",
"service": "turn_on",
"target": {
"entity_id": [
"light.living_room_color_1",
"light.living_room_color_2",
"light.living_room_color_3",
"light.stairwell_led_strip"
]
},
"data": {
"color_name": colorName1,
"brightness": 255
}
}
}
let sendTinaDeskLights = {
"payload": {
"domain": "light",
"service": "turn_on",
"target": {
"entity_id": ["light.tina_desk_lights"]
},
"data": {
"color_name": colorName1,
"brightness": 255
}
}
}
let sendDiningRoomLamp = {
"payload": {
"domain": "light",
"service": "turn_off",
"target": {
"entity_id": ["light.dining_room_lamp"]
},
"data": {}
}
}
let sendAdaptiveFirstFloor = {
"payload": {
"domain": "switch",
"service": "turn_off",
"target": {
"entity_id": adaptiveSwitchFirstFloor
},
"data": {}
}
}
let sendAdaptiveBasement = {
"payload": {
"domain": "switch",
"service": "turn_off",
"target": {
"entity_id": adaptiveSwitchBasement
},
"data": {}
}
}
let backupLivingRoom = {
"payload": {
"domain": "scene",
"service": "create",
"data": {
"scene_id": "living_room_alert_restore",
"snapshot_entities": lightsLivingRoom
}
}
}
let backupTinaDesk = {
"payload": {
"domain": "scene",
"service": "create",
"data": {
"scene_id": "tina_desk_alert_restore",
"snapshot_entities": lightsTinaDesk
}
}
}
let backupBasement = {
"payload": {
"domain": "scene",
"service": "create",
"data": {
"scene_id": "basement_alert_restore",
"snapshot_entities": lightsBasement
}
}
}
let sendReset = {
"delay": delay
}
// Send backup messages
if (stateLivingRoom === 'on' && alertFirstFloor === false) {
node.send([backupLivingRoom,null,null,null,null,null])
}
if (stateTinaDesk === 'on' && alertFirstFloor === false) {
node.send([backupTinaDesk,null,null,null,null,null])
}
if (stateBasement === 'on' && alertBasement === false) {
node.send([backupBasement,null,null,null,null,null])
}
// Send effect messages
if (effectBasement === true) {
node.send([null,null,null,null,[sendAdaptiveBasement,sendBasementLights,sendBasementStripFX,sendBasementStrip1,sendBasementStrip2],null])
}
if (effectFirstFloor === true) {
node.send([null,[sendAdaptiveFirstFloor,sendLivingRoomLights,sendLivingRoomStripFX,sendLivingRoomStrip1,sendLivingRoomStrip2],sendTinaDeskLights,sendDiningRoomLamp,null,null])
}
// Send the reset message to start the timer
node.send([null,null,null,null,null,sendReset])
node.status({fill:'green',shape:'dot',text:'Alert Sent'})