From f758c36508e29211963c7cbe6daf2663ad91f2c3 Mon Sep 17 00:00:00 2001 From: Tony Stork Date: Thu, 19 Oct 2023 19:06:51 -0400 Subject: [PATCH] Most of the effect and restore flow is now working --- lightfx/processing-start.js | 13 ++ lightfx/processing.js | 90 +++++++---- lightfx/reset.js | 303 ++++++++++++++++++++++++++++++++++-- 3 files changed, 362 insertions(+), 44 deletions(-) diff --git a/lightfx/processing-start.js b/lightfx/processing-start.js index 85b0529..5a2401f 100644 --- a/lightfx/processing-start.js +++ b/lightfx/processing-start.js @@ -44,6 +44,13 @@ const adaptiveBasement = [ "switch.adaptive_lighting_basement_studio" ] +// Define light arrays for backup scenes +const lightsTinaDesk = ['light.tina_desk_strip','light.tina_lamp_top','light.tina_lamp_side'] +const lightsBasement = ['light.basement_tall_lamp','light.basement_short_lamp','light.basement_stairwell','light.basement_led_strip_1'] +const lightsLivingRoom = ['light.living_room_color_1','light.living_room_color_2','light.living_room_color_3','light.living_room_led_strip'] +const lightsDiningRoom = ['light.dining_room_lamp'] +const lightsStairwell = ['light.stairwell_led_strip'] + // Write flow variables flow.set("halfLivingRoom1", halfLivingRoom1, "diskCon") flow.set("halfLivingRoom2", halfLivingRoom2, "diskCon") @@ -73,3 +80,9 @@ flow.set("basementFull", basementFull, "diskCon") flow.set("adaptiveFirstFloor", adaptiveFirstFloor, "diskCon") flow.set("adaptiveBasement", adaptiveBasement, "diskCon") + +flow.set('lightsTinaDesk', lightsTinaDesk, 'diskCon') +flow.set('lightsBasement', lightsBasement, 'diskCon') +flow.set('lightsLivingRoom', lightsLivingRoom, 'diskCon') +flow.set('lightsDiningRoom', lightsDiningRoom, 'diskCon') +flow.set('lightsStairwell', lightsStairwell, 'diskCon') diff --git a/lightfx/processing.js b/lightfx/processing.js index 9fd4bf1..08a50b4 100644 --- a/lightfx/processing.js +++ b/lightfx/processing.js @@ -8,12 +8,15 @@ 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 effectType = msg.payload.event.type @@ -22,24 +25,27 @@ 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') // 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 backup scenes -let firstFloorLights = [] -let basementLights = [] // Define empty arrays for basement LED strip zones let basement1 = [] let basement2 = [] @@ -51,28 +57,8 @@ let livingRoomFull = [] // Color names are set by default, however certain effects can override them below let colorName1 = effectType let colorName2 = "white" - -// Get lights to add to the backup scenes -//! We need to change this from one entire first floor scene to individual room scenes -if (stateTinaDesk === 'on') { - firstFloorLights.push('light.tina_desk_strip','light.tina_lamp_top','light.tina_lamp_side') -} - -if (stateLivingRoom === 'on') { - firstFloorLights.push('light.living_room_color_1','light.living_room_color_2','light.living_room_color_3','light.living_room_led_strip') -} - -if (stateDiningRoom === 'on') { - firstFloorLights.push('light.dining_room_lamp') -} - -if (stateStairwell === 'on') { - firstFloorLights.push('light.stairwell_led_strip') -} - -if (stateBasement === 'on') { - basementLights.push('light.basement_tall_lamp','light.basement_short_lamp','light.basement_stairwell','light.basement_led_strip_1') -} +// Define empty array for input texts +let inputTexts = [] // Setup the proper zones to use for the effect if (effectDivide === 'half') { @@ -90,6 +76,14 @@ if (effectDivide === 'half') { livingRoomFull = flow.get("livingRoomFull", "diskCon") } +if (effectFirstFloor === true) { + inputTexts.push('input_text.living_room_selected_scene','input_text.tina_desk_selected_scene') +} + +if (effectBasement === true) { + inputTexts.push('input_text.basement_studio_selected_scene') +} + // Set/change colors if necessary if (effectType === 'police') { colorName1 = 'red' @@ -97,6 +91,17 @@ if (effectType === 'police') { } // Set message payloads +let sendInputTexts = { + "payload": "input_text", + "service": "set_value", + "target": { + "entity_id": inputTexts + }, + "data": { + "value": "Alert" + } +} + let sendBasementStripFX = { "payload": { "domain": "lifx", @@ -215,7 +220,8 @@ let sendLivingRoomLights = { "entity_id": [ "light.living_room_color_1", "light.living_room_color_2", - "light.living_room_color_3" + "light.living_room_color_3", + "light.stairwell_led_strip" ] }, "data": { @@ -272,13 +278,24 @@ let sendAdaptiveBasement = { } } -let backupFirstFloor = { +let backupLivingRoom = { "payload": { "domain": "scene", "service": "create", "data": { - "scene_id": "first_floor_alert_restore", - "snapshot_entities": firstFloorLights + "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 } } } @@ -289,7 +306,7 @@ let backupBasement = { "service": "create", "data": { "scene_id": "basement_alert_restore", - "snapshot_entities": basementLights + "snapshot_entities": lightsBasement } } } @@ -298,21 +315,26 @@ let sendReset = { "delay": delay } +// Send backup messages if (stateLivingRoom === 'on' && currentSceneLivingRoom != 'Alert') { - node.send([[backupFirstFloor],null,null,null,null,null]) + node.send([backupLivingRoom,null,null,null,null,null]) +} + +if (stateTinaDesk === 'on' && currentSceneTinaDesk != 'Alert') { + node.send([backupTinaDesk,null,null,null,null,null]) } if (stateBasement === 'on' && currentSceneBasement != 'Alert') { - node.send([[backupBasement],null,null,null,null,null]) + node.send([backupBasement,null,null,null,null,null]) } // Send effect messages if (effectBasement === true) { - node.send([null,null,null,null,[sendBasementStripFX,sendBasementLights,sendBasementStrip1,sendBasementStrip2],null]) + node.send([sendAdaptiveBasement,null,null,null,[sendBasementStripFX,sendBasementLights,sendBasementStrip1,sendBasementStrip2],null]) } if (effectFirstFloor === true) { - node.send([null,[sendLivingRoomStripFX,sendLivingRoomLights,sendLivingRoomStrip1,sendLivingRoomStrip2],sendTinaDeskLights,sendDiningRoomLamp,null,null]) + node.send([sendAdaptiveFirstFloor,[sendLivingRoomStripFX,sendLivingRoomLights,sendLivingRoomStrip1,sendLivingRoomStrip2],sendTinaDeskLights,sendDiningRoomLamp,null,null]) } // Send the reset message to start the timer diff --git a/lightfx/reset.js b/lightfx/reset.js index 32b08be..354579d 100644 --- a/lightfx/reset.js +++ b/lightfx/reset.js @@ -3,30 +3,313 @@ const states = global.get('homeassistant.homeAssistant.states') const currentSceneBasement = flow.get('currentSceneBasement') const currentSceneLivingRoom = flow.get('currentSceneLivingRoom') const currentSceneTinaDesk = flow.get('currentSceneTinaDesk') +const currentSceneDiningRoom = flow.get('currentSceneDiningRoom') const adaptLivingRoom = flow.get('adaptLivingRoom') const adaptBasement = flow.get('adaptBasement') const nightLivingRoom = flow.get('nightLivingRoom') const nightBasement = flow.get('nightBasement') +const nightDiningRoom = flow.get('nightDiningRoom') const stateTinaDesk = flow.get('stateTinaDesk') const stateLivingRoom = flow.get('stateLivingRoom') const stateDiningRoom = flow.get('stateDiningRoom') const stateBasement = flow.get('stateBasement') +const stateStairwell = flow.get('stateStairwell') const scenesLivingRoom = states["input_select.living_room_scenes"].attributes.options const scenesBasement = states["input_select.basement_studio_scenes"].attributes.options const scenesTinaDesk = states["input_select.tina_desk_scenes"].attributes.options - +const scenesDiningRoom = states["input_select.dining_room_lamp_scenes"].attributes.options const definedLivingRoom = scenesLivingRoom.includes(currentSceneLivingRoom) const definedBasement = scenesBasement.includes(currentSceneBasement) const definedTinaDesk = scenesTinaDesk.includes(currentSceneTinaDesk) +const definedDiningRoom = scenesDiningRoom.includes(currentSceneDiningRoom) -let sendDebug = { - "scenesLivingRoom": scenesLivingRoom, - "scenesBasement": scenesBasement, - "scenesTinaDesk": scenesTinaDesk, - "definedLivingRoom": definedLivingRoom, - "definedBasement": definedBasement, - "definedTinaDesk": definedTinaDesk +// Define variables +let setSceneLivingRoom = [] +let setSceneTinaDesk = [] +let setSceneBasement = [] +let setSceneDiningRoom = [] +let setServiceLivingRoom = [] +let setServiceTinaDesk = [] +let setServiceDiningRoom = [] +let setServiceBasement = [] +let restoreLivingRoom = {} +let restoreTinaDesk = {} +let restoreBasement = {} +let inputTexts = [] + +// Decide what to restore for each room +if (stateLivingRoom === 'on') { + setServiceLivingRoom = 'turn_on' + if (definedLivingRoom === true) { + restoreLivingRoom = 'defined' + setSceneLivingRoom = currentSceneLivingRoom + } else if (nightLivingRoom === 'on') { + restoreLivingRoom = 'night' + setSceneLivingRoom = 'Adaptive' + } else if (adaptLivingRoom === 'on') { + restoreLivingRoom = 'adaptive' + setSceneLivingRoom = 'Adaptive' + } else { + restoreLivingRoom = 'backup' + setSceneLivingRoom = 'living_room_alert_restore' + inputTexts.push('input_text.living_room_selected_scene') + } +} else { + restoreLivingRoom = 'off' + setServiceLivingRoom = 'turn_off' + inputTexts.push('input_text.living_room_selected_scene') } -node.send([sendDebug,null,null,null]) -node.status({fill:"green",shape:"dot",text:"Success"}) \ No newline at end of file +if (stateTinaDesk === 'on') { + setServiceTinaDesk = 'turn_on' + if (definedTinaDesk === true) { + restoreTinaDesk = 'defined' + setSceneTinaDesk = currentSceneTinaDesk + } else { + restoreTinaDesk = 'adaptive' + setSceneTinaDesk = 'Reset' + } +} else { + restoreTinaDesk = 'off' + setServiceTinaDesk = 'turn_off' + inputTexts.push('input_text.tina_desk_selected_scene') +} + +if (stateDiningRoom === 'on') { + setServiceDiningRoom = 'turn_on' + if (nightDiningRoom === 'on') { + setSceneDiningRoom = 'Nightlight' + } else { + setSceneDiningRoom = 'Adaptive' + } +} else { + setServiceDiningRoom = 'turn_off' + inputTexts.push('input_text.dining_room_lamp_selected_scene') +} + +if (stateBasement === 'on') { + setServiceBasement = 'turn_on' + if (definedBasement === true) { + restoreBasement = 'defined' + setSceneBasement = currentSceneBasement + } else if (nightBasement === 'on') { + restoreBasement = 'night' + setSceneBasement = 'Adaptive' + } else if (adaptBasement === 'on') { + restoreBasement = 'adaptive' + setSceneBasement = 'Adaptive' + } else { + restoreBasement = 'backup' + setSceneBasement = 'basement_alert_restore' + inputTexts.push('input_text.basement_studio_selected_scene') + } +} else { + restoreBasement = 'off' + setServiceBasement = 'turn_off' + inputTexts.push('input_text.basement_studio_selected_scene') +} + +// Define messages +let sendInputTexts = { + "payload": { + "domain": "input_text", + "service": "set_value", + "target": { + "entity_id": inputTexts + }, + "data": "Adaptive" + } +} + +let sendSceneLivingRoom = { + "payload": { + "domain": "input_select", + "service": "select_option", + "target": { + "entity_id": ["input_select.living_room_scenes"] + }, + "data": { + "option": setSceneLivingRoom + } + } +} + +let sendSceneTinaDesk = { + "payload": { + "domain": "input_select", + "service": "select_option", + "target": { + "entity_id": ["input_select.tina_desk_scenes"] + }, + "data": { + "option": setSceneTinaDesk + } + } +} + +let sendSceneBasement = { + "payload": { + "domain": "input_select", + "service": "select_option", + "target": { + "entity_id": ["input_select.basement_studio_scenes"] + }, + "data": { + "option": setSceneBasement + } + } +} + +let sendSceneDiningRoomLamp = { + "payload": { + "domain": "input_select", + "service": "select_option", + "target": { + "entity_id": ["input_select.dining_room_lamp_scenes"] + }, + "data": { + "option": setSceneDiningRoom + } + } +} + + +let sendNightLivingRoom = { + "payload": { + "domain": "switch", + "service": "turn_on", + "target": { + "entity_id": ["switch.adaptive_lighting_sleep_mode_living_room"] + }, + "data": {} + } +} + +let sendNightTinaDesk = { + "payload": { + "domain": "switch", + "service": "turn_on", + "target": { + "entity_id": ["switch.adaptive_lighting_sleep_mode_tina_desk"] + }, + "data": {} + } +} + +let sendNightBasement = { + "payload": { + "domain": "switch", + "service": "turn_on", + "target": { + "entity_id": ["switch.adaptive_lighting_sleep_mode_basement_studio"] + }, + "data": {} + } +} + +let sendStateLivingRoom = { + "payload": { + "domain": "light", + "service": setServiceLivingRoom, + "target": { + "entity_id": ["light.living_room_lights"] + }, + "data": {} + } +} + +let sendStateTinaDesk = { + "payload": { + "domain": "light", + "service": setServiceTinaDesk, + "target": { + "entity_id": ["light.tina_desk_lights"] + }, + "data": {} + } +} + +let sendStateDiningRoom = { + "payload": { + "domain": "light", + "service": setServiceDiningRoom, + "target": { + "entity_id": ["light.dining_room_lamp"] + }, + "data": {} + } +} + +let sendRestoreLivingRoom = { + "payload": { + "domain": "scene", + "service": "turn_on", + "target": { + "entity_id": setSceneLivingRoom + }, + "data": {} + } +} + +let sendRestoreTinaDesk = { + "payload": { + "domain": "scene", + "service": "turn_on", + "target": { + "entity_id": setSceneTinaDesk + }, + "data": {} + } +} + +let sendStairwellScript = { + "payload": { + "domain": "script", + "service": "stairwell_led_strip", + "data": {} + } +} + +let sendStairwellOff = { + "payload": { + "domain": "light", + "service": "turn_off", + "target": { + "entity_id": ["light.stairwell_led_strip"] + }, + "data": {} + } +} + +// Send messages +if (restoreLivingRoom === 'backup') { + node.send([sendRestoreLivingRoom,null,null,null,null]) +} else { + node.send([sendSceneLivingRoom,null,null,null,null]) +} + +if (restoreTinaDesk === 'backup') { + node.send([null,sendRestoreTinaDesk,null,null,null]) +} else { + node.send([null,sendSceneTinaDesk,null,null,null]) +} + +if (stateDiningRoom === 'off') { + node.send([null,null,sendStateDiningRoom,null,null]) +} else { + node.send([null,null,sendSceneDiningRoomLamp,null,null]) +} + +if (restoreBasement === 'backup') { + node.send([null,null,null,restoreBasement,null]) +} else { + node.send([null,null,null,sendSceneBasement,null]) +} + +if (stateStairwell === 'on') { + node.send([sendStairwellScript,null,null,null,null]) +} else { + node.send([sendStairwellOff,null,null,null,null]) +} + +node.send([null,null,null,null,sendInputTexts]) \ No newline at end of file