-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
alternativesurfer
committed
Dec 9, 2014
1 parent
4dfb72c
commit 9f23891
Showing
1 changed file
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* Lights on Arrival | ||
* | ||
* Copyright 2014 Alex Malikov | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License | ||
* for the specific language governing permissions and limitations under the License. | ||
* | ||
*/ | ||
definition( | ||
name: "Lights on Arrival", | ||
namespace: "625alex", | ||
author: "Alex Malikov", | ||
description: "Turn lights on when arriving if it's dark outside", | ||
category: "Convenience", | ||
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png", | ||
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/[email protected]") | ||
|
||
|
||
preferences { | ||
section("When one of these persons arrives") { | ||
input "people", "capability.presenceSensor", multiple: true | ||
} | ||
section("And it's dark...") { | ||
input "luminance", "capability.illuminanceMeasurement", title: "Where?" | ||
} | ||
section("Then flash..."){ | ||
input "switches", "capability.switch", title: "These lights", multiple: true | ||
input "numFlashes", "number", title: "This number of times (default 3)", required: false | ||
} | ||
section("Time settings in milliseconds (optional)..."){ | ||
input "onFor", "number", title: "On for (default 1000)", required: false | ||
input "offFor", "number", title: "Off for (default 1000)", required: false | ||
} | ||
} | ||
|
||
def installed() { | ||
log.debug "Installed with settings: ${settings}" | ||
|
||
subscribe() | ||
} | ||
|
||
def updated() { | ||
log.debug "Updated with settings: ${settings}" | ||
|
||
unsubscribe() | ||
subscribe() | ||
} | ||
|
||
def initialize() { | ||
subscribe(people, "presence", presenseHandler) | ||
} | ||
|
||
def presenceHandler(evt) { | ||
log.debug "presence $evt.value" | ||
if (evt.value == "present") { | ||
def lightSensorState = luminance.currentIlluminance | ||
log.debug "SENSOR = $lightSensorState" | ||
if (lightSensorState && lightSensorState < 20) { | ||
log.trace "light.on() ... [luminance: ${lightSensorState}]" | ||
} else if (evt.value == "not present") { | ||
flashLights() | ||
} | ||
else if (evt.value == "not present") { | ||
flashLights() | ||
} | ||
} | ||
} | ||
private flashLights() { | ||
def doFlash = true | ||
def onFor = onFor ?: 600000 | ||
def offFor = offFor ?: 0 | ||
def numFlashes = numFlashes ?: 1 | ||
|
||
log.debug "LAST ACTIVATED IS: ${state.lastActivated}" | ||
if (state.lastActivated) { | ||
def elapsed = now() - state.lastActivated | ||
def sequenceTime = (numFlashes + 1) * (onFor + offFor) | ||
doFlash = elapsed > sequenceTime | ||
log.debug "DO FLASH: $doFlash, ELAPSED: $elapsed, LAST ACTIVATED: ${state.lastActivated}" | ||
} | ||
|
||
if (doFlash) { | ||
log.debug "FLASHING $numFlashes times" | ||
state.lastActivated = now() | ||
log.debug "LAST ACTIVATED SET TO: ${state.lastActivated}" | ||
def initialActionOn = switches.collect{it.currentSwitch != "on"} | ||
def delay = 1L | ||
numFlashes.times { | ||
log.trace "Switch on after $delay msec" | ||
switches.eachWithIndex {s, i -> | ||
if (initialActionOn[i]) { | ||
s.on(delay: delay) | ||
} | ||
else { | ||
s.off(delay:delay) | ||
} | ||
} | ||
delay += onFor | ||
log.trace "Switch off after $delay msec" | ||
switches.eachWithIndex {s, i -> | ||
if (initialActionOn[i]) { | ||
s.off(delay: delay) | ||
} | ||
else { | ||
s.on(delay:delay) | ||
} | ||
} | ||
delay += offFor | ||
} | ||
} | ||
} |