149 lines
4.2 KiB
JavaScript
149 lines
4.2 KiB
JavaScript
// Set Constants
|
|
const states = global.get('homeassistant.homeAssistant.states')
|
|
const textAllowed = states['input_boolean.laundry_notifications_text'].state
|
|
const ttsAllowed = states['input_boolean.laundry_notifications_tts'].state
|
|
const washerCycle = flow.get("washerCycle", "diskCon")
|
|
const washerFinished = flow.get("washerFinished", "diskCon")
|
|
const washerTimer = flow.get("washerTimer", "diskCon")
|
|
const washerDateTime = flow.get("washerDateTime", "diskCon")
|
|
const dryerCycle = flow.get("dryerCycle", "diskCon")
|
|
const dryerFinished = flow.get("dryerFinished", "diskCon")
|
|
const dryerTimer = flow.get("dryerTimer", "diskCon")
|
|
const dryerDateTime = flow.get("dryerDateTime", "diskCon")
|
|
const topic = msg.topic
|
|
const payload = msg.payload
|
|
|
|
// Init variables
|
|
let timerEntity = []
|
|
let boolFinished = []
|
|
let servFinished = {}
|
|
let dateTimeEntity = []
|
|
let servTimer = {}
|
|
let setCycle = []
|
|
let dateTime = []
|
|
let deviceName = {}
|
|
let notifyMsg = {}
|
|
let ttsMsg = {}
|
|
|
|
// Set entity IDs and states
|
|
if (topic === 'washer') {
|
|
setCycle = washerCycle
|
|
boolFinished = washerFinished
|
|
dateTimeEntity = washerDateTime
|
|
timerEntity = washerTimer
|
|
deviceName = 'washer'
|
|
} else if (topic === 'dryer') {
|
|
setCycle = dryerCycle
|
|
boolFinished = dryerFinished
|
|
dateTimeEntity = dryerDateTime
|
|
timerEntity = dryerTimer
|
|
deviceName = 'dryer'
|
|
}
|
|
|
|
// Get states from the entities
|
|
// @ts-ignore
|
|
let cycle = states[setCycle].state
|
|
// @ts-ignore
|
|
let timerState = states[timerEntity].state
|
|
// @ts-ignore
|
|
let finishedState = states[boolFinished].state
|
|
|
|
// Convert minutes into seconds for the timer
|
|
let timerDuration = cycle * 60
|
|
let notifyDuration = Math.round(cycle)
|
|
|
|
// Decide services
|
|
if (payload === 'start') {
|
|
servFinished = 'turn_off'
|
|
notifyMsg = 'The ' + deviceName + ' has been started for a ' + notifyDuration + ' minute cycle'
|
|
} else if (payload === 'finish') {
|
|
dateTime = msg.datetime
|
|
servFinished = 'turn_on'
|
|
notifyMsg = 'The ' + deviceName + ' has finished its cycle'
|
|
ttsMsg = 'The ' + deviceName + ' has finished its cycle. I repeat, the ' + deviceName + ' has finished its cycle.'
|
|
} else if (payload === 'cancel') {
|
|
notifyMsg = 'The ' + deviceName + ' cycle has been cancelled'
|
|
}
|
|
|
|
// Prepare message payloads
|
|
let sendTimerStart = {
|
|
"payload": {
|
|
"action": "timer.start",
|
|
"target": {
|
|
"entity_id": timerEntity
|
|
},
|
|
"data": {
|
|
"duration": timerDuration
|
|
}
|
|
}
|
|
}
|
|
|
|
let sendTimerCancel = {
|
|
"payload": {
|
|
"action": "timer.cancel",
|
|
"target": {
|
|
"entity_id": timerEntity
|
|
},
|
|
"data": {}
|
|
}
|
|
}
|
|
|
|
let sendBoolFinished = {
|
|
"payload": {
|
|
"action": "input_boolean." + servFinished,
|
|
"target": {
|
|
"entity_id": boolFinished
|
|
},
|
|
"data": {}
|
|
}
|
|
}
|
|
|
|
let sendDateTimeFinished = {
|
|
"payload": {
|
|
"action": "input_datetime.set_datetime",
|
|
"target": {
|
|
"entity_id": dateTimeEntity
|
|
},
|
|
"data": {
|
|
"datetime": dateTime
|
|
}
|
|
}
|
|
}
|
|
|
|
let sendTextNotify = {
|
|
"payload": {
|
|
"data": {
|
|
"type": "normal",
|
|
"who": "all",
|
|
"title": "Laundry Tracking",
|
|
"message": notifyMsg
|
|
}
|
|
}
|
|
}
|
|
|
|
let sendTTSNotify = {
|
|
"payload": ttsMsg,
|
|
"type": "alert",
|
|
"topic": "everywhere"
|
|
}
|
|
|
|
if (payload === 'start') {
|
|
node.send([sendBoolFinished,sendTimerStart,null,null])
|
|
node.status({fill:"green",shape:"dot",text:"Starting " + deviceName + " timer for " + notifyDuration + " minutes"})
|
|
node.log("Starting " + deviceName + " timer for " + notifyDuration + " minutes")
|
|
} else if (payload === 'finish') {
|
|
node.send([[sendBoolFinished,sendDateTimeFinished],null,null])
|
|
if (ttsAllowed === 'on') {
|
|
node.send([null,null,null,sendTTSNotify])
|
|
}
|
|
node.status({fill:"green",shape:"dot",text:"The " + deviceName + " has finished"})
|
|
node.log("The " + deviceName + " has finished")
|
|
} else if (payload === 'cancel') {
|
|
node.send([null,sendTimerCancel,null,null])
|
|
node.status({fill:"red",shape:"ring",text:"The " + deviceName + " has been cancelled"})
|
|
node.log("The " + deviceName + " has been cancelled")
|
|
}
|
|
|
|
if (textAllowed === 'on') {
|
|
node.send([null,null,sendTextNotify,null])
|
|
} |