Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davepullig/371a844b7a7ff889e38f2181f0acc847 to your computer and use it in GitHub Desktop.
Save davepullig/371a844b7a7ff889e38f2181f0acc847 to your computer and use it in GitHub Desktop.
"""
daves_utils.py
Extra stuff for Indigo
"""
import indigo
import datetime
import time
def set_device_brightness_from_variable(deviceId, variableId):
# Get the variable object
try:
brightnessValue = indigo.variables[variableId]
except:
indigo.server.log("Variable id %i doesn't exist." % variableId)
# Next, we need to turn it into an integer value and make sure
# it's in the appropriate range (0-99). We'll use a try block
# and log an error if the value can't be turned into an
# integer or if it's out of range
try:
# Convert the value to an int - it will throw if it can't
newBrightness = int(brightnessValue.value)
# Throw if it's not in range
if newBrightness not in range(101):
raise Exception
except:
indigo.server.log("Variable value is either not an integer or not from 0-100.")
return
# Is the device already at this brightness? Avoid sending again to reduce zwave network utilization
device = indigo.devices[deviceId]
if newBrightness == int(device.brightness):
return 0
# We now know newBrightness is an integer from 0-100. Set the value and
# catch any exceptions which most likely means the device isn't a dimmer.
try:
indigo.dimmer.setBrightness(deviceId, value=newBrightness)
except:
indigo.server.log("Device id %i is not a dimmer." % deviceId)
def store_device_state(deviceId):
# Setup variables
brightnessVar = "ls_" + str(deviceId) + "_prev_brightness"
statusVar = "ls_" + str(deviceId) + "_status"
device = indigo.devices[deviceId]
# Check if our variables to store the existing state of a light exist
if not (brightnessVar in indigo.variables):
indigo.variable.create(brightnessVar, "false", folder=925237811)
if not (statusVar in indigo.variables):
indigo.variable.create(statusVar, "false", folder=925237811)
# Has the device state already been modified? If so bail out
deviceIsModified = indigo.variables[statusVar]
isModified = deviceIsModified.getValue(str)
if "true" == isModified:
return 0
# Now set the flag to say we've modified the device
deviceIsModified.value = "true"
deviceIsModified.replaceOnServer()
# Now store if the device was offf or had a brightness
previousBrightness = indigo.variables[brightnessVar]
if not device.onState:
previousBrightness.value = "off"
else:
previousBrightness.value = str(device.brightness)
previousBrightness.replaceOnServer()
def restore_device_state(deviceId):
# Setup variables
brightnessVar = "ls_" + str(deviceId) + "_prev_brightness"
statusVar = "ls_" + str(deviceId) + "_status"
device = indigo.devices[deviceId]
# Don't do anything if we don't have a stored state
if not (brightnessVar in indigo.variables):
return 0
if not (statusVar in indigo.variables):
return 0
# Or if the variables exist but we're not in a modified state
deviceIsModified = indigo.variables[statusVar]
isModified = deviceIsModified.getValue(str)
if "false" == isModified:
return 0
# Also reset the flag and return without modifying the device if it has been turned off
if not device.onState:
deviceIsModified.value = "false"
deviceIsModified.replaceOnServer()
return 0
prevbrightnessVar = indigo.variables[brightnessVar]
prevBrightness = prevbrightnessVar.getValue(str)
if "off" == prevBrightness:
indigo.device.turnOff(deviceId)
else:
indigo.dimmer.setBrightness(deviceId, value=int(prevBrightness))
# Now set the flag to say we've finished modifying the device
deviceIsModified.value = "false"
deviceIsModified.replaceOnServer()
def get_seconds_since(startTimeVariableId):
dateString = indigo.variables[startTimeVariableId].value
timestamp = int(time.mktime(time.strptime(dateString,"%Y-%m-%d %H:%M:%S.%f")))
return int(time.time() - timestamp)
indigo.server.log("Loaded Dave's Utils attachment")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment