use sunrise/sunset times to control Hue lights
This commit is contained in:
parent
ac72ba1f12
commit
2fa35e873a
3 changed files with 51 additions and 8 deletions
33
flux
Executable file
33
flux
Executable 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
|
||||
23
lights
23
lights
|
|
@ -36,10 +36,16 @@ TEMPS = {
|
|||
TEMPS['sunny'] = TEMPS['bright']
|
||||
TEMPS['overcast'] = TEMPS['cloudy']
|
||||
|
||||
setting = ARGV.first
|
||||
setting = ARGV[0]
|
||||
brightness = ARGV[1] || 80
|
||||
transition = ARGV[2]
|
||||
Huemote.set_ip '192.168.0.2'
|
||||
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
|
||||
when 'off'
|
||||
lights.each(&:off!)
|
||||
|
|
@ -48,17 +54,20 @@ when 'on'
|
|||
else
|
||||
lights.each(&:on!)
|
||||
attrs = TEMPS[setting] || {
|
||||
brightness: (ARGV[1] || 80).to_i,
|
||||
temp: setting.to_i,
|
||||
brightness: brightness.to_i,
|
||||
}
|
||||
if transition
|
||||
# tenths of a second
|
||||
attrs[:transitiontime] = transition.to_i
|
||||
end
|
||||
if attrs
|
||||
lights.each do |l|
|
||||
if attrs[:brightness]
|
||||
l.brightness(attrs[:brightness])
|
||||
end
|
||||
attrs[:bri] = attrs.delete(:brightness)
|
||||
if attrs[:temp]
|
||||
l.kelvin(attrs[:temp])
|
||||
attrs[:ct] = kelvin_to_mireds(attrs.delete(:temp))
|
||||
end
|
||||
lights.each do |l|
|
||||
l.send(:set!, attrs)
|
||||
end
|
||||
else
|
||||
puts "Unknown setting: #{setting}"
|
||||
|
|
|
|||
1
sun-yyj.json
Normal file
1
sun-yyj.json
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue