So I’ve written this app specifically for my LIFX lights at home but there are some things that I’m not really across.
It’s works fine so far, I got sick of having to pass meaningless numbers to change my lights to a certain setting. I like to have it as it’s laid out in the LIFX app where brightness’s are in percentage and color_temp’s are in the K values.
I also added in a pulsing feature for light notifications.
Questions:
- How can I use this app within other apps using AppDaemon? imports and use etc. I’ve given it a go and couldn’t get it working.
- How can I make multiple entity_id’s passed to the app run simultaneous? This is only an issue for the pulsing because I’ve got a delay in between state transitions. I’d like to be able to send multiple lights through to the app and them all pulse at the same time to notify if someone is at the front door or whatever else can be dreamed up.
- This is my first app, so any tip or tricks would be appreciated!
- Oh and for some reason when I sent a color_temp under 153.85 it wouldn’t work, however that’s the state value that was in HA when changed the lights using the LIFX app? Perhaps it’s something to do with the rgb_color value?
import appdaemon.appapi as appapi
import time
# Comments:
#
# App to easily turn on LIFX lights with brightness, color_temp and rgb_color variables. Also allows light pulsing
# to use for notification triggers.
#
# Args:
#
# entity_id = entity_id of light to be turned on. You can send multiple entities
# eg. light.study or light.study,light.lounge
# brightness = light brightness in percentage
# eg. 100
# default: 75
# color_temp = light color temp
# eg. neutral or 3200
# default: neutral
# rgb_color = light rgb color
# eg. red or 255,0,0
# default: white
# pulse_count = pulse cycles to pulse between the original light states and the ones specified above. If the light is
# already off the light will pulse between passed variables and off state.
# eg. 2
# default: 0 - which is no pulse
# pulse_time_on = time in seconds to pulse on
# eg. 1 or 1.5
# defaults: 1 second
# pulse_time_off = time in seconds to pulse off
# eg. 1 or 1.5
# default: 1 second
#
# Version 1.0:
# Initial Version
class LIFX_Lights(appapi.AppDaemon):
def initialize(self):
self.log("LIFX Lights App Started.")
if "pulse_count" in self.args:
pulse_count = int(self.args["pulse_count"])
else:
pulse_count = "0" # 0 = always on, 1+ = the amount of times to pulse
if "pulse_time_on" in self.args:
pulse_time_on = float(self.args["pulse_time_on"])
else:
pulse_time_on = "1" # 1 second
if "pulse_time_off" in self.args:
pulse_time_off = float(self.args["pulse_time_off"])
else:
pulse_time_off = "1" # default to 1 second
if "brightness" in self.args:
brightness = self.args["brightness"]
else:
brightness = "75" # default to 75%
if "color_temp" in self.args:
color_temp = self.args["color_temp"]
else:
color_temp = "3500" # default to 3500K - Neutral
if "rgb_color" in self.args:
rgb_color = self.args["rgb_color"]
else:
rgb_color = "white" # default to white
if "entity_id" in self.args:
for entity in self.split_device_list(self.args["entity_id"]):
entity_id = entity
if (pulse_count > 0):
self.pulse_light_on_off(entity_id, brightness, color_temp, rgb_color, pulse_count, pulse_time_on, pulse_time_off)
else:
self.turn_light_on(entity_id, brightness, color_temp, rgb_color)
else:
self.log("No entity to turn on.")
def turn_light_on(self, entity_id, brightness, color_temp, rgb_color, from_cfg="yes"):
#brightness = 0-255, color_temp = , rgb_color = [0,0,0]-[255,255,255]
if from_cfg == "yes":
brightness = self.brightness_to_percent(brightness)
color_temp = self.color_temperature_to_value(color_temp)
rgb_color = self.rgb_to_value(rgb_color)
self.log("brightness {}, color_temp {}, rgb_color {}".format(brightness, color_temp, rgb_color))
self.turn_on(entity_id, brightness=brightness, color_temp=color_temp, rgb_color=rgb_color)
def pulse_light_on_off(self, entity_id, brightness, color_temp, rgb_color, pulse_count, pulse_time_on, pulse_time_off):
start_state = self.get_state(entity_id)
if start_state == "on":
start_brightness = float(self.get_state(entity_id, "brightness"))
start_color_temp = float(self.get_state(entity_id, "color_temp"))
start_rgb_color = self.get_state(entity_id, "rgb_color")
else:
start_brightness = None
start_color_temp = None
start_rgb_color = None
for x in range(0,pulse_count):
self.turn_light_on(entity_id, brightness, color_temp, rgb_color)
time.sleep(pulse_time_on)
if start_state == "on":
self.turn_light_on(entity_id, start_brightness, start_color_temp, start_rgb_color, "no")
else:
self.turn_off(entity_id)
time.sleep(pulse_time_off)
def brightness_to_percent(self, brightness):
return 255/100 * int(brightness)
def color_temperature_to_value(self, color_temp): # Doesn't work above 6000K? Why?
color_temp = str.lower(str(color_temp))
if(color_temp == "2500" or color_temp == "ultra warm"):
return 400 # Ultra Warm - 2500K
elif(color_temp == "2750" or color_temp == "incandescent"):
return 363.63 # Incandescent - 2750K
elif(color_temp == "3000" or color_temp == "warm"):
return 333.33 # Warm - 3000K
elif (color_temp == "3200" or color_temp == "neutral warm"):
return 312.5 # Neutral Warm - 3200K
elif (color_temp == "3500" or color_temp == "neutral"):
return 285.71 # Neutral - 3500K
elif (color_temp == "4000" or color_temp == "cool"):
return 250 # Cool - 4000K
elif (color_temp == "4500" or color_temp == "cool daylight"):
return 222.22 # Cool Daylight - 4500K
elif (color_temp == "5000" or color_temp == "soft daylight"):
return 200 # Soft Daylight - 5000K
elif (color_temp == "5500" or color_temp == "daylight"):
return 181.82 # Daylight - 5500K
elif (color_temp == "6000" or color_temp == "noon daylight"):
return 166.67 # Noon Daylight - 6000K
#elif (color_temp == 6500 or color_temp == "bright daylight"):
# return 153.85 # Bright Daylight - 6500K
#elif (color_temp == 7000 or color_temp == "cloudy daylight"):
# return 142.86 # Cloudy Daylight - 7000K
#elif (color_temp == 7500 or color_temp == "blue daylight"):
# return 133.33 # Blue Daylight - 7500K
#elif (color_temp == 8000 or color_temp == "blue overcast"):
# return 125 # Blue Overcast - 8000K
#elif (color_temp == 8500 or color_temp == "blue water"):
# return 117.65 # Blue Water - 8500K
#elif (color_temp == 9000 or color_temp == "blue ice"):
# return 111.11 # Blue Ice - 9000K
else:
return 285.71 # Neutral - 3500K
def rgb_to_value(self, rgb_color):
if (rgb_color == "red"):
return [255,0,0]
elif (rgb_color == "green"):
return [0, 255, 0]
elif (rgb_color == "blue"):
return [0, 0, 255]
elif (rgb_color == "white"):
return [255, 255, 255]
elif (rgb_color == "yellow"):
return [255, 255, 0]
elif (rgb_color == "purple" or rgb_color == "pink"):
return [255, 0, 255]
elif (rgb_color == "teal" or rgb_color == "light_blue"):
return [0, 255, 255]
else:
rgb_return = []
for x in self.split_device_list(rgb_color):
rgb_return.append(x)
return rgb_return