use sunrise/sunset times to control Hue lights

This commit is contained in:
Sami Samhuri 2014-12-22 23:09:13 -08:00
parent ac72ba1f12
commit 2fa35e873a
3 changed files with 51 additions and 8 deletions

33
flux Executable file
View file

@ -0,0 +1,33 @@
#!/usr/bin/env ruby
require 'date'
require 'json'
TIMES = JSON.parse(File.read(File.expand_path('../sun-yyj.json', __FILE__)))['sunsets']
SETTINGS = {
'twilight' => 'sunrise',
'sunrise' => 'morning',
'sunset' => 'sunset',
'night' => 'night',
}
date = Date.today
month = date.month.to_s
day = date.day.to_s
if times = TIMES[month][day]
time = Time.now
hour, min = time.hour, time.min
padded_min = min < 10 ? "0#{min}" : "#{min}"
now = "#{hour}:#{padded_min}"
if found = times.detect { |k, v| now == v }
name = found[0]
if setting = SETTINGS[name]
puts "lights #{setting} - 3000"
exec "lights #{setting} - 3000"
else
raise "Unsure how to change lights for \"#{name}\""
end
end
else
raise "Cannot find today's date (#{date}) in times: #{times.inspect}"
end

25
lights
View file

@ -36,10 +36,16 @@ TEMPS = {
TEMPS['sunny'] = TEMPS['bright'] TEMPS['sunny'] = TEMPS['bright']
TEMPS['overcast'] = TEMPS['cloudy'] TEMPS['overcast'] = TEMPS['cloudy']
setting = ARGV.first setting = ARGV[0]
brightness = ARGV[1] || 80
transition = ARGV[2]
Huemote.set_ip '192.168.0.2' Huemote.set_ip '192.168.0.2'
lights = LIGHT_NAMES.map { |name| Huemote::Light.find(name) } lights = LIGHT_NAMES.map { |name| Huemote::Light.find(name) }
def kelvin_to_mireds(temp)
[[1_000_000 / temp, 154].max, 500].min
end
case setting case setting
when 'off' when 'off'
lights.each(&:off!) lights.each(&:off!)
@ -48,17 +54,20 @@ when 'on'
else else
lights.each(&:on!) lights.each(&:on!)
attrs = TEMPS[setting] || { attrs = TEMPS[setting] || {
brightness: (ARGV[1] || 80).to_i,
temp: setting.to_i, temp: setting.to_i,
brightness: brightness.to_i,
} }
if transition
# tenths of a second
attrs[:transitiontime] = transition.to_i
end
if attrs if attrs
attrs[:bri] = attrs.delete(:brightness)
if attrs[:temp]
attrs[:ct] = kelvin_to_mireds(attrs.delete(:temp))
end
lights.each do |l| lights.each do |l|
if attrs[:brightness] l.send(:set!, attrs)
l.brightness(attrs[:brightness])
end
if attrs[:temp]
l.kelvin(attrs[:temp])
end
end end
else else
puts "Unknown setting: #{setting}" puts "Unknown setting: #{setting}"

1
sun-yyj.json Normal file

File diff suppressed because one or more lines are too long