Skip to content

Instantly share code, notes, and snippets.

@colindean
Forked from Bunkerbewohner/light
Last active August 29, 2015 14:12
Show Gist options
  • Save colindean/4b57e197b71d8232bcbf to your computer and use it in GitHub Desktop.
Save colindean/4b57e197b71d8232bcbf to your computer and use it in GitHub Desktop.
A simple Ruby shell script to control LIFX (http://lifx.co) light bulbs, adds color support
#!/usr/bin/env ruby
require 'lifx'
available_colors = LIFX::Colors.instance_methods
if ARGV.length > 0
# see if a specific lamp is addressed
label = /^on|off|0x[0-9a-f]{6}$/.match(ARGV[0]) || available_colors.member?(ARGV[0]) ? nil : ARGV[0]
# discover lamps and wait for label data to be available
client = LIFX::Client.lan
client.discover!
sleep(0.3)
# select either the specified light or all lights if none was specified
light = label ? client.lights.with_label(label) : client.lights
# transition duration in seconds
duration = [4, ARGV[-1].to_i].max
# value is either first or second argument, depending whether a label was provided
value = label ? ARGV[1] : ARGV[0]
ensure_light_on = Proc.new {
if light.off?
light.turn_on!
sleep(0.2)
end
}
if value == 'on'
light.turn_on!
sleep(0.2)
light.set_color(LIFX::Color.rgb(255,255,255), duration: duration)
elsif value == 'off' and light.on?
light.set_color(LIFX::Color.rgb(0, 0, 0), duration: duration)
sleep(duration)
light.turn_off!
elsif value[0,2] == '0x'
r = value[2,2].hex
g = value[4,2].hex
b = value[6,2].hex
ensure_light_on.call()
color = LIFX::Color.rgb(r, g, b)
light.set_color(color, duration: duration)
elsif LIFX::Color.respond_to? value.to_sym
ensure_light_on.call()
color = LIFX::Color.send(value.to_sym)
light.set_color(color, duration: duration)
end
# make sure all commands are send before the program exits
client.flush
else
print("usage: light [label] on|off|0xRRGGBB|color [duration=4]
examples:
light on -> turns all lights on
light \"Kitchen\" off -> turns Kitchen light off
light 0xff0000 -> turns all lights red
light Bedroom on 30 -> turns up the light in the Bedroom over 30 seconds
light Bedroom green 10 -> turns green the light in the bedroom over 10 seconds
The colors available for the color argument are:
#{available_colors.collect(&:to_s).join(', ')}\n")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment