2 * Copyright 2016 SmartThings
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11 * for the specific language governing permissions and limitations under the License.
15 * Author: Steve Vlaminck
18 * https://s3.amazonaws.com/smartapp-icons/HealthAndWellness/App-SleepyTime.png
19 * https://s3.amazonaws.com/smartapp-icons/HealthAndWellness/App-SleepyTime%402x.png
20 * Gentle Wake Up turns on your lights slowly, allowing you to wake up more
21 * naturally. Once your lights have reached full brightness, optionally turn on
22 * more things, or send yourself a text for a more gentle nudge into the waking
23 * world (you may want to set your normal alarm as a backup plan).
27 name: "Gentle Wake Up",
28 namespace: "smartthings",
29 author: "SmartThings",
30 description: "Dim your lights up slowly, allowing you to wake up more naturally.",
31 category: "Health & Wellness",
32 iconUrl: "https://s3.amazonaws.com/smartapp-icons/HealthAndWellness/App-SleepyTime.png",
33 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/HealthAndWellness/App-SleepyTime@2x.png"
37 page(name: "rootPage")
38 page(name: "numbersPage")
39 page(name: "schedulingPage")
40 page(name: "completionPage")
41 page(name: "controllerExplanationPage")
42 //page(name: "unsupportedDevicesPage")
46 dynamicPage(name: "rootPage", title: "", install: true, uninstall: true) {
48 section("What to dim") {
49 input(name: "dimmers", type: "capability.switchLevel", title: "Dimmers", description: null, multiple: true, required: true, submitOnChange: true)
51 /*if (dimmersContainUnsupportedDevices()) {
52 href(name: "toUnsupportedDevicesPage", page: "unsupportedDevicesPage", title: "Some of your selected dimmers don't seem to be supported", description: "Tap here to fix it", required: true)
54 href(name: "toNumbersPage", page: "numbersPage", title: "Duration & Direction", state: "complete")
60 section("Gentle Wake Up Has A Controller") {
61 href(title: "Learn how to control Gentle Wake Up", page: "controllerExplanationPage", description: null)
64 section("Rules For Dimming") {
65 href(name: "toSchedulingPage", page: "schedulingPage", title: "Automation")
66 input(name: "manualOverride", type: "enum", options: ["Cancel dimming","Jump to the end"], title: "When one of the dimmers is manually turned off…", description: "dimming will continue", required: false, multiple: false)
67 href(name: "toCompletionPage", title: "Completion Actions", page: "completionPage")
72 label(title: "Label This SmartApp", required: false, defaultValue: "", description: "Highly recommended", submitOnChange: true)
78 def unsupportedDevicesPage() {
80 def unsupportedDimmers = dimmers.findAll { !hasSetLevelCommand(it) }
82 dynamicPage(name: "unsupportedDevicesPage") {
83 if (unsupportedDimmers) {
84 section("These devices do not support the setLevel command") {
85 unsupportedDimmers.each {
86 paragraph deviceLabel(it)
90 paragraph "If you think there is a mistake here, please contact support."
94 paragraph "You're all set. You can hit the back button, now. Thanks for cleaning up your settings :)"
100 def controllerExplanationPage() {
101 dynamicPage(name: "controllerExplanationPage", title: "How To Control Gentle Wake Up") {
103 section("With other SmartApps", hideable: true, hidden: false) {
104 paragraph "When this SmartApp is installed, it will create a controller device which you can use in other SmartApps for even more customizable automation!"
105 paragraph "The controller acts like a switch so any SmartApp that can control a switch can control Gentle Wake Up, too!"
106 paragraph "Routines and 'Smart Lighting' are great ways to automate Gentle Wake Up."
109 section("More about the controller", hideable: true, hidden: true) {
110 paragraph "You can find the controller with your other 'Things'. It will look like this."
111 image "http://f.cl.ly/items/2O0v0h41301U14042z3i/GentleWakeUpController-tile-stopped.png"
112 paragraph "You can start and stop Gentle Wake up by tapping the control on the right."
113 image "http://f.cl.ly/items/3W323J3M1b3K0k0V3X3a/GentleWakeUpController-tile-running.png"
114 paragraph "If you look at the device details screen, you will find even more information about Gentle Wake Up and more fine grain controls."
115 image "http://f.cl.ly/items/291s3z2I2Q0r2q0x171H/GentleWakeUpController-richTile-stopped.png"
116 paragraph "The slider allows you to jump to any point in the dimming process. Think of it as a percentage. If Gentle Wake Up is set to dim down as you fall asleep, but your book is just too good to put down; simply drag the slider to the left and Gentle Wake Up will give you more time to finish your chapter and drift off to sleep."
117 image "http://f.cl.ly/items/0F0N2G0S3v1q0L0R3J3Y/GentleWakeUpController-richTile-running.png"
118 paragraph "In the lower left, you will see the amount of time remaining in the dimming cycle. It does not count down evenly. Instead, it will update whenever the slider is updated; typically every 6-18 seconds depending on the duration of your dimming cycle."
119 paragraph "Of course, you may also tap the middle to start or stop the dimming cycle at any time."
122 section("Starting and stopping the SmartApp itself", hideable: true, hidden: true) {
123 paragraph "Tap the 'play' button on the SmartApp to start or stop dimming."
124 image "http://f.cl.ly/items/0R2u1Z2H30393z2I2V3S/GentleWakeUp-appTouch2.png"
127 section("Turning off devices while dimming", hideable: true, hidden: true) {
128 paragraph "It's best to use other Devices and SmartApps for triggering the Controller device. However, that isn't always an option."
129 paragraph "If you turn off a switch that is being dimmed, it will either continue to dim, stop dimming, or jump to the end of the dimming cycle depending on your settings."
130 paragraph "Unfortunately, some switches take a little time to turn off and may not finish turning off before Gentle Wake Up sets its dim level again. You may need to try a few times to get it to stop."
131 paragraph "That's why it's best to use devices that aren't currently dimming. Remember that you can use other SmartApps to toggle the controller. :)"
137 dynamicPage(name:"numbersPage", title:"") {
140 paragraph(name: "pGraph", title: "These lights will dim", fancyDeviceString(dimmers))
144 input(name: "duration", type: "number", title: "For this many minutes", description: "30", required: false, defaultValue: 30)
148 input(name: "startLevel", type: "number", range: "0..99", title: "From this level", description: "Current Level", required: false, multiple: false)
149 input(name: "endLevel", type: "number", range: "0..99", title: "To this level", , description: "Between 0 and 99", required: true, multiple: false)
152 def colorDimmers = dimmersWithSetColorCommand()
155 input(name: "colorize", type: "bool", title: "Gradually change the color of ${fancyDeviceString(colorDimmers)}", description: null, required: false, defaultValue: "true")
162 if (usesOldSettings() && direction && direction == "Down") {
169 if (usesOldSettings() && direction && direction == "Down") {
175 def startLevelLabel() {
176 if (usesOldSettings()) { // using old settings
177 if (direction && direction == "Down") { // 99 -> 1
182 return hasStartLevel() ? "${startLevel}%" : "Current Level"
185 def endLevelLabel() {
186 if (usesOldSettings()) {
187 if (direction && direction == "Down") { // 99 -> 1
192 return "${endLevel}%"
196 ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
200 ["Saturday", "Sunday"]
203 def schedulingPage() {
204 dynamicPage(name: "schedulingPage", title: "Rules For Automatically Dimming Your Lights") {
206 section("Use Other SmartApps!") {
207 href(title: "Learn how to control Gentle Wake Up", page: "controllerExplanationPage", description: null)
210 section("Allow Automatic Dimming") {
211 input(name: "days", type: "enum", title: "On These Days", description: "Every day", required: false, multiple: true, options: weekdays() + weekends())
214 section("Start Dimming...") {
215 input(name: "startTime", type: "time", title: "At This Time", description: null, required: false)
216 input(name: "modeStart", title: "When Entering This Mode", type: "mode", required: false, mutliple: false, submitOnChange: true, description: null)
218 input(name: "modeStop", title: "Stop when leaving '${modeStart}' mode", type: "bool", required: false)
225 def completionPage() {
226 dynamicPage(name: "completionPage", title: "Completion Rules") {
228 section("Switches") {
229 input(name: "completionSwitches", type: "capability.switch", title: "Set these switches", description: null, required: false, multiple: true, submitOnChange: true)
230 if (completionSwitches) {
231 input(name: "completionSwitchesState", type: "enum", title: "To", description: null, required: false, multiple: false, options: ["on", "off"], defaultValue: "on")
232 input(name: "completionSwitchesLevel", type: "number", title: "Optionally, Set Dimmer Levels To", description: null, required: false, multiple: false, range: "(0..99)")
236 section("Notifications") {
237 input("recipients", "contact", title: "Send notifications to", required: false) {
238 input(name: "completionPhoneNumber", type: "phone", title: "Text This Number", description: "Phone number", required: false)
239 input(name: "completionPush", type: "bool", title: "Send A Push Notification", description: "Phone number", required: false)
241 input(name: "completionMusicPlayer", type: "capability.musicPlayer", title: "Speak Using This Music Player", required: false)
242 input(name: "completionMessage", type: "text", title: "With This Message", description: null, required: false)
245 section("Modes and Phrases") {
246 input(name: "completionMode", type: "mode", title: "Change ${location.name} Mode To", description: null, required: false)
247 input(name: "completionPhrase", type: "enum", title: "Execute The Phrase", description: null, required: false, multiple: false, options: location.helloHome.getPhrases().label)
251 input(name: "completionDelay", type: "number", title: "Delay This Many Minutes Before Executing These Actions", description: "0", required: false)
256 // ========================================================
258 // ========================================================
261 log.debug "Installing 'Gentle Wake Up' with settings: ${settings}"
267 log.debug "Updating 'Gentle Wake Up' with settings: ${settings}"
270 def controller = getController()
272 controller.label = app.label
278 private initialize() {
279 startLevel = 0//Chagne start level to 0 to make it possible for the light to be off!
280 stop("settingsChange")
283 log.debug "scheduling dimming routine to run at $startTime"
284 schedule(startTime, "scheduledStart")
287 // TODO: make this an option
288 subscribe(app, appHandler)
290 subscribe(location, locationHandler)
292 if (manualOverride) {
293 subscribe(dimmers, "switch.off", stopDimmersHandler)
296 /*if (!getAllChildDevices()) {
297 // create controller device and set name to the label used here
298 def dni = "${new Date().getTime()}"
299 log.debug "app.label: ${app.label}"
300 addChildDevice("smartthings", "Gentle Wake Up Controller", dni, null, ["label": app.label])
301 state.controllerDni = dni
305 def appHandler(evt) {
306 log.debug "appHandler evt: ${evt.value}"
307 if (evt.value == "touch") {
308 if (atomicState.running) {
316 def locationHandler(evt) {
317 log.debug "locationHandler evt: ${evt.value}"
323 def isSpecifiedMode = (evt.value == modeStart)
324 def modeStopIsTrue = (modeStop && modeStop != "false")
326 if (isSpecifiedMode && canStartAutomatically()) {
328 } else if (!isSpecifiedMode && modeStopIsTrue) {
334 def stopDimmersHandler(evt) {
335 log.trace "stopDimmersHandler evt: ${evt.value}"
336 def percentComplete = completionPercentage()
337 // Often times, the first thing we do is turn lights on or off so make sure we don't stop as soon as we start
338 if (percentComplete > 2 && percentComplete < 98) {
339 if (manualOverride == "cancel") {
340 log.debug "STOPPING in stopDimmersHandler"
341 stop("manualOverride")
342 } else if (manualOverride == "jumpTo") {
343 def end = dynamicEndLevel()
344 log.debug "Jumping to 99% complete in stopDimmersHandler"
349 log.debug "not stopping in stopDimmersHandler"
353 // ========================================================
355 // ========================================================
357 def scheduledStart() {
358 if (canStartAutomatically()) {
363 public def start(source) {
366 sendStartEvent(source)
370 atomicState.running = true
372 atomicState.start = new Date().getTime()
374 schedule("0 * * * * ?", "healthCheck")
378 public def stop(source) {
381 sendStopEvent(source)
383 atomicState.running = false
384 atomicState.start = 0
386 unschedule("healthCheck")
389 private healthCheck() {
390 log.trace "'Gentle Wake Up' healthCheck"
392 if (!atomicState.running) {
399 // ========================================================
401 // ========================================================
403 def sendStartEvent(source) {
404 log.trace "sendStartEvent(${source})"
406 name: "sessionStatus",
408 descriptionText: "${app.label} has started dimming",
413 if (source == "modeChange") {
414 eventData.descriptionText += " because of a mode change"
415 } else if (source == "schedule") {
416 eventData.descriptionText += " as scheduled"
417 } else if (source == "appTouch") {
418 eventData.descriptionText += " because you pressed play on the app"
419 } else if (source == "controller") {
420 eventData.descriptionText += " because you pressed play on the controller"
423 sendControllerEvent(eventData)
426 def sendStopEvent(source) {
427 log.trace "sendStopEvent(${source})"
429 name: "sessionStatus",
431 descriptionText: "${app.label} has stopped dimming",
436 if (source == "modeChange") {
437 eventData.descriptionText += " because of a mode change"
438 eventData.value += "cancelled"
439 } else if (source == "schedule") {
440 eventData.descriptionText = "${app.label} has finished dimming"
441 } else if (source == "appTouch") {
442 eventData.descriptionText += " because you pressed play on the app"
443 eventData.value += "cancelled"
444 } else if (source == "controller") {
445 eventData.descriptionText += " because you pressed stop on the controller"
446 eventData.value += "cancelled"
447 } else if (source == "settingsChange") {
448 eventData.descriptionText += " because the settings have changed"
449 eventData.value += "cancelled"
450 } else if (source == "manualOverride") {
451 eventData.descriptionText += " because the dimmer was manually turned off"
452 eventData.value += "cancelled"
455 // send 100% completion event
456 sendTimeRemainingEvent(100)
458 // send a non-displayed 0% completion to reset tiles
459 sendTimeRemainingEvent(0, false)
461 // send sessionStatus event last so the event feed is ordered properly
462 sendControllerEvent(eventData)
465 def sendTimeRemainingEvent(percentComplete, displayed = true) {
466 log.trace "sendTimeRemainingEvent(${percentComplete})"
468 def percentCompleteEventData = [
469 name: "percentComplete",
470 value: percentComplete as int,
471 displayed: displayed,
474 sendControllerEvent(percentCompleteEventData)
476 def duration = sanitizeInt(duration, 30)
477 def timeRemaining = duration - (duration * (percentComplete / 100))
478 def timeRemainingEventData = [
479 name: "timeRemaining",
480 value: displayableTime(timeRemaining),
481 displayed: displayed,
484 sendControllerEvent(timeRemainingEventData)
487 def sendControllerEvent(eventData) {
488 def controller = getController()
490 controller.controllerEvent(eventData)
494 def getController() {
495 def dni = state.controllerDni
497 log.warn "no controller dni"
500 def controller = getChildDevice(dni)
502 log.warn "no controller"
505 log.debug "controller: ${controller}"
509 // ========================================================
511 // ========================================================
514 private increment() {
516 if (!atomicState.running) {
520 def percentComplete = completionPercentage()
522 if (percentComplete > 99) {
526 updateDimmers(percentComplete)
528 if (percentComplete < 99) {
530 def runAgain = stepDuration()
531 log.debug "Rescheduling to run again in ${runAgain} seconds"
533 //runIn(runAgain, 'increment', [overwrite: true])
537 int completionDelay = completionDelaySeconds()
538 if (completionDelay) {
539 log.debug "Finished with steps. Scheduling completion for ${completionDelay} second(s) from now"
540 runIn(completionDelay, 'completion', [overwrite: true])
541 unschedule("healthCheck")
542 // don't let the health check start incrementing again while we wait for the delayed execution of completion
544 log.debug "Finished with steps. Execution completion"
552 def updateDimmers(percentComplete) {
553 dimmers.each { dimmer ->
555 def nextLevel = dynamicLevel(dimmer, percentComplete)
557 if (nextLevel == 0) {
563 def shouldChangeColors = (colorize && colorize != "false")
565 if (shouldChangeColors/*&& hasSetColorCommand(dimmer)*/) {
566 def hue = getHue(dimmer, nextLevel)
567 log.debug "Setting ${deviceLabel(dimmer)} level to ${nextLevel} and hue to ${hue}"
568 dimmer.setColor([hue: hue, saturation: 100, level: nextLevel])
570 log.debug "Setting ${deviceLabel(dimmer)} level to ${nextLevel}"
571 dimmer.setLevel(nextLevel)
576 sendTimeRemainingEvent(percentComplete)
579 int dynamicLevel(dimmer, percentComplete) {
580 def start = atomicState.startLevels[dimmer.id]
581 def end = dynamicEndLevel()
583 if (!percentComplete) {
587 def totalDiff = end - start
588 def actualPercentage = percentComplete / 100
589 def percentOfTotalDiff = totalDiff * actualPercentage
591 (start + percentOfTotalDiff) as int
594 // ========================================================
596 // ========================================================
598 private completion() {
599 log.trace "Starting completion block"
601 if (!atomicState.running) {
607 handleCompletionSwitches()
609 handleCompletionMessaging()
611 handleCompletionModesAndPhrases()
614 private handleCompletionSwitches() {
615 completionSwitches.each { completionSwitch ->
617 def isDimmer = hasSetLevelCommand(completionSwitch)
619 if (completionSwitchesLevel && isDimmer) {
620 completionSwitch.setLevel(completionSwitchesLevel)
622 def command = completionSwitchesState ?: "on"
623 completionSwitch."${command}"()
628 private handleCompletionMessaging() {
629 if (completionMessage) {
630 if (location.contactBookEnabled) {
631 sendNotificationToContacts(completionMessage, recipients)
633 if (completionPhoneNumber) {
634 sendSms(completionPhoneNumber, completionMessage)
636 if (completionPush) {
637 sendPush(completionMessage)
640 if (completionMusicPlayer) {
641 speak(completionMessage)
646 private handleCompletionModesAndPhrases() {
648 if (completionMode) {
649 setLocationMode(completionMode)
652 if (completionPhrase) {
653 location.helloHome.execute(completionPhrase)
659 def sound = textToSpeech(message)
660 def soundDuration = (sound.duration as Integer) + 2
661 log.debug "Playing $sound.uri"
662 completionMusicPlayer.playTrack(sound.uri)
663 log.debug "Scheduled resume in $soundDuration sec"
664 runIn(soundDuration, resumePlaying, [overwrite: true])
667 def resumePlaying() {
668 log.trace "resumePlaying()"
669 def sonos = completionMusicPlayer
671 def currentTrack = sonos.currentState("trackData").jsonValue
672 if (currentTrack.status == "playing") {
673 sonos.playTrack(currentTrack)
675 sonos.setTrack(currentTrack)
680 // ========================================================
682 // ========================================================
684 def setLevelsInState() {
685 def startLevels = [:]
686 dimmers.each { dimmer ->
687 if (usesOldSettings()) {
688 startLevels[dimmer.id] = defaultStart()
689 } else if (hasStartLevel()) {
690 startLevels[dimmer.id] = startLevel
692 def dimmerIsOff = dimmer.currentValue("switch") == "off"
693 startLevels[dimmer.id] = dimmerIsOff ? 0 : dimmer.currentValue("level")
697 atomicState.startLevels = startLevels
700 def canStartAutomatically() {
702 def today = new Date().format("EEEE")
703 log.debug "today: ${today}, days: ${days}"
705 //if (!days || days.contains(today)) {// if no days, assume every day
709 log.trace "should not run"
713 def completionPercentage() {
714 log.trace "checkingTime"
716 if (!atomicState.running) {
720 def now = new Date().getTime()
721 def timeElapsed = now - atomicState.start
722 def totalRunTime = totalRunTimeMillis() ?: 1
723 def percentComplete = timeElapsed / totalRunTime * 100
724 log.debug "percentComplete: ${percentComplete}"
726 return percentComplete
729 int totalRunTimeMillis() {
730 int minutes = sanitizeInt(duration, 30)
731 convertToMillis(minutes)
734 int convertToMillis(minutes) {
735 def seconds = minutes * 60
736 def millis = seconds * 1000
740 def timeRemaining(percentComplete) {
741 def normalizedPercentComplete = percentComplete / 100
742 def duration = sanitizeInt(duration, 30)
743 def timeElapsed = duration * normalizedPercentComplete
744 def timeRemaining = duration - timeElapsed
748 int millisToEnd(percentComplete) {
749 convertToMillis(timeRemaining(percentComplete))
752 String displayableTime(timeRemaining) {
753 def timeString = "${timeRemaining}"
754 def parts = timeString.split(/\./)
758 def minutes = parts[0]
759 if (parts.size() == 1) {
760 return "${minutes}:00"
762 def fraction = "0.${parts[1]}" as double
763 def seconds = "${60 * fraction as int}".padLeft(2, "0")
764 return "${minutes}:${seconds}"
767 def jumpTo(percentComplete) {
768 def millisToEnd = millisToEnd(percentComplete)
769 def endTime = new Date().getTime() + millisToEnd
770 def duration = sanitizeInt(duration, 30)
771 def durationMillis = convertToMillis(duration)
772 def shiftedStart = endTime - durationMillis
773 atomicState.start = shiftedStart
774 updateDimmers(percentComplete)
775 sendTimeRemainingEvent(percentComplete)
779 int dynamicEndLevel() {
780 if (usesOldSettings()) {
781 if (direction && direction == "Down") {
786 return endLevel as int
789 def getHue(dimmer, level) {
790 def start = atomicState.startLevels[dimmer.id] as int
791 def end = dynamicEndLevel()
793 return getDownHue(level)
795 return getUpHue(level)
799 def getUpHue(level) {
803 def getDownHue(level) {
807 private getBlueHue(level) {
808 if (level < 5) return 72
809 if (level < 10) return 71
810 if (level < 15) return 70
811 if (level < 20) return 69
812 if (level < 25) return 68
813 if (level < 30) return 67
814 if (level < 35) return 66
815 if (level < 40) return 65
816 if (level < 45) return 64
817 if (level < 50) return 63
818 if (level < 55) return 62
819 if (level < 60) return 61
820 if (level < 65) return 60
821 if (level < 70) return 59
822 if (level < 75) return 58
823 if (level < 80) return 57
824 if (level < 85) return 56
825 if (level < 90) return 55
826 if (level < 95) return 54
827 if (level >= 95) return 53
830 private getRedHue(level) {
831 if (level < 6) return 1
832 if (level < 12) return 2
833 if (level < 18) return 3
834 if (level < 24) return 4
835 if (level < 30) return 5
836 if (level < 36) return 6
837 if (level < 42) return 7
838 if (level < 48) return 8
839 if (level < 54) return 9
840 if (level < 60) return 10
841 if (level < 66) return 11
842 if (level < 72) return 12
843 if (level < 78) return 13
844 if (level < 84) return 14
845 if (level < 90) return 15
846 if (level < 96) return 16
847 if (level >= 96) return 17
850 private dimmersContainUnsupportedDevices() {
851 def found = dimmers.find { hasSetLevelCommand(it) == false }
855 private hasSetLevelCommand(device) {
856 return hasCommand(device, "setLevel")
859 private hasSetColorCommand(device) {
860 return hasCommand(device, "setColor")
863 private hasCommand(device, String command) {
864 return (device.supportedCommands.find { it.name == command } != null)
867 private dimmersWithSetColorCommand() {
868 def colorDimmers = []
869 dimmers.each { dimmer ->
870 //if (hasSetColorCommand(dimmer)) {
871 colorDimmers << dimmer
877 private int sanitizeInt(i, int defaultValue = 0) {
885 catch (Exception e) {
891 private completionDelaySeconds() {
892 int completionDelayMinutes = sanitizeInt(completionDelay)
893 int completionDelaySeconds = (completionDelayMinutes * 60)
894 return completionDelaySeconds ?: 0
897 private stepDuration() {
898 int minutes = sanitizeInt(duration, 30)
899 int stepDuration = (minutes * 60) / 100
900 return stepDuration ?: 1
903 private debug(message) {
904 log.debug "${message}\nstate: ${state}"
907 public smartThingsDateFormat() { "yyyy-MM-dd'T'HH:mm:ss.SSSZ" }
909 public humanReadableStartDate() {
910 new Date().parse(smartThingsDateFormat(), startTime).format("h:mm a", timeZone(startTime))
913 def fancyString(listOfStrings) {
915 def fancify = { list ->
916 return list.collect {
918 if (list.size() > 1 && it == list[-1]) {
919 label = "and ${label}"
925 return fancify(listOfStrings)
928 def fancyDeviceString(devices = []) {
929 fancyString(devices.collect { deviceLabel(it) })
932 def deviceLabel(device) {
933 return device.label ?: device.name
936 def schedulingHrefDescription() {
938 def descriptionParts = []
940 if (days == weekdays()) {
941 descriptionParts << "On weekdays,"
942 } else if (days == weekends()) {
943 descriptionParts << "On weekends,"
945 descriptionParts << "On ${fancyString(days)},"
949 descriptionParts << "${fancyDeviceString(dimmers)} will start dimming"
952 descriptionParts << "at ${humanReadableStartDate()}"
957 descriptionParts << "or"
959 descriptionParts << "when ${location.name} enters '${modeStart}' mode"
962 if (descriptionParts.size() <= 1) {
963 // dimmers will be in the list no matter what. No rules are set if only dimmers are in the list
967 return descriptionParts.join(" ")
970 def completionHrefDescription() {
972 def descriptionParts = []
973 def example = "Switch1 will be turned on. Switch2, Switch3, and Switch4 will be dimmed to 50%. The message '<message>' will be spoken, sent as a text, and sent as a push notification. The mode will be changed to '<mode>'. The phrase '<phrase>' will be executed"
975 if (completionSwitches) {
976 def switchesList = []
980 completionSwitches.each {
981 def isDimmer = completionSwitchesLevel ? hasSetLevelCommand(it) : false
984 dimmersList << deviceLabel(it)
988 switchesList << deviceLabel(it)
994 descriptionParts << "${fancyString(switchesList)} will be turned ${completionSwitchesState ?: 'on'}."
998 descriptionParts << "${fancyString(dimmersList)} will be dimmed to ${completionSwitchesLevel}%."
1003 if (completionMessage && (completionPhoneNumber || completionPush || completionMusicPlayer)) {
1004 def messageParts = []
1006 if (completionMusicPlayer) {
1007 messageParts << "spoken"
1009 if (completionPhoneNumber) {
1010 messageParts << "sent as a text"
1012 if (completionPush) {
1013 messageParts << "sent as a push notification"
1016 descriptionParts << "The message '${completionMessage}' will be ${fancyString(messageParts)}."
1019 if (completionMode) {
1020 descriptionParts << "The mode will be changed to '${completionMode}'."
1023 if (completionPhrase) {
1024 descriptionParts << "The phrase '${completionPhrase}' will be executed."
1027 return descriptionParts.join(" ")
1030 def numbersPageHrefDescription() {
1031 def title = "All dimmers will dim for ${duration ?: '30'} minutes from ${startLevelLabel()} to ${endLevelLabel()}"
1033 def colorDimmers = dimmersWithSetColorCommand()
1034 if (colorDimmers == dimmers) {
1035 title += " and will gradually change color."
1037 title += ".\n${fancyDeviceString(colorDimmers)} will gradually change color."
1043 def hueSatToHex(h, s) {
1044 def convertedRGB = hslToRgb(h, s, 0.5)
1045 return rgbToHex(convertedRGB)
1048 def hslToRgb(h, s, l) {
1052 r = g = b = l; // achromatic
1054 def hue2rgb = { p, q, t ->
1057 if (t < 1 / 6) return p + (q - p) * 6 * t;
1058 if (t < 1 / 2) return q;
1059 if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
1063 def q = l < 0.5 ? l * (1 + s) : l + s - l * s;
1066 r = hue2rgb(p, q, h + 1 / 3);
1067 g = hue2rgb(p, q, h);
1068 b = hue2rgb(p, q, h - 1 / 3);
1071 return [r * 255, g * 255, b * 255];
1074 def rgbToHex(red, green, blue) {
1077 n = Math.max(0, Math.min(n, 255));
1078 def hexOptions = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"]
1080 def firstDecimal = ((n - n % 16) / 16) as int
1081 def secondDecimal = (n % 16) as int
1083 return "${hexOptions[firstDecimal]}${hexOptions[secondDecimal]}"
1086 def rgbToHex = { r, g, b ->
1087 return toHex(r) + toHex(g) + toHex(b)
1090 return rgbToHex(red, green, blue)
1093 def usesOldSettings() {
1097 def hasStartLevel() {
1098 return (startLevel != null && startLevel != "")
1102 return (endLevel != null && endLevel != "")