Add support for non-interactive usage of huec

For example:

  huec scene 1
  huec off 2
This commit is contained in:
Paul van Tilburg 2016-02-02 17:34:11 +01:00
parent bc9fbf73a6
commit f1aa6352a0
1 changed files with 39 additions and 24 deletions

View File

@ -19,6 +19,10 @@ require "rainbow"
include Huey include Huey
def msg(str="")
puts(str) if @interactive
end
def status_line(blb) def status_line(blb)
mode = [] mode = []
if blb.reachable if blb.reachable
@ -62,14 +66,14 @@ end
def lights def lights
Huey::Bulb.all.each do |blb| Huey::Bulb.all.each do |blb|
puts status_line(blb) msg status_line(blb)
end end
true true
end end
def groups def groups
Huey::Group.all.each do |grp| Huey::Group.all.each do |grp|
puts "%2d: %s (%s)" % [grp.id, grp.name, grp.bulbs.map(&:id).join(', ')] msg "%2d: %s (%s)" % [grp.id, grp.name, grp.bulbs.map(&:id).join(', ')]
end end
true true
end end
@ -77,7 +81,7 @@ end
def events def events
events_cfg = File.join(@options[:config_dir], "events.yml") events_cfg = File.join(@options[:config_dir], "events.yml")
Huey::Event.import(events_cfg).each_with_index do |ev, idx| Huey::Event.import(events_cfg).each_with_index do |ev, idx|
puts "%2d: %s" % [idx, ev.name] msg "%2d: %s" % [idx, ev.name]
end end
true true
end end
@ -86,7 +90,7 @@ def scenes
@scenes = {} @scenes = {}
scenes_cfg = File.join(@options[:config_dir], "scenes.yml") scenes_cfg = File.join(@options[:config_dir], "scenes.yml")
YAML.load_file(scenes_cfg).each_with_index do |(name, entry), idx| YAML.load_file(scenes_cfg).each_with_index do |(name, entry), idx|
puts "%2d: %s" % [idx, name] msg "%2d: %s" % [idx, name]
@scenes[name] = entry.map do |ev_options| @scenes[name] = entry.map do |ev_options|
# Keys should be symbols # Keys should be symbols
options = ev_options.inject({}) { |opts, (k, v)| opts[k.to_sym] = v; opts } options = ev_options.inject({}) { |opts, (k, v)| opts[k.to_sym] = v; opts }
@ -101,7 +105,7 @@ end
def refresh! def refresh!
Huey::Bulb.all.each do |blb| Huey::Bulb.all.each do |blb|
blb.reload blb.reload
puts status_line(blb) msg status_line(blb)
end end
true true
end end
@ -109,7 +113,7 @@ end
def get(*names_or_ids) def get(*names_or_ids)
names_or_ids.map do |name_or_id| names_or_ids.map do |name_or_id|
blb = Huey::Bulb.find(name_or_id) blb = Huey::Bulb.find(name_or_id)
puts status_line(blb) msg status_line(blb)
true true
end end
end end
@ -124,13 +128,13 @@ rescue Huey::Errors::BulbOff
blb.update(on: true) blb.update(on: true)
retry retry
rescue Huey::Errors::Error => e rescue Huey::Errors::Error => e
puts "Error: #{e.message}" msg "Error: #{e.message}"
end end
def getgrp(*names_or_ids) def getgrp(*names_or_ids)
names_or_ids.map do |name_or_id| names_or_ids.map do |name_or_id|
Huey::Group.find(name_or_id).each do |blb| Huey::Group.find(name_or_id).each do |blb|
puts status_line(blb) msg status_line(blb)
end end
true true
end end
@ -179,7 +183,7 @@ def on(*names_or_ids)
end end
def commands def commands
puts <<EOT msg <<EOT
Lights Lights
lights shows the list of all lights lights shows the list of all lights
(ID, name and state) (ID, name and state)
@ -240,34 +244,45 @@ rescue OptionParser::InvalidOption => e
abort opt_parser.to_s abort opt_parser.to_s
end end
puts "Starting huec #{Hued::VERSION}..." @interactive = ARGV.empty?
msg "Starting huec #{Hued::VERSION}..."
bridge_cfg = YAML.load_file(File.join(@options[:config_dir], "bridge.yml")) bridge_cfg = YAML.load_file(File.join(@options[:config_dir], "bridge.yml"))
Huey.configure do |cfg| Huey.configure do |cfg|
cfg.hue_ip = bridge_cfg["ip"] cfg.hue_ip = bridge_cfg["ip"]
cfg.uuid = bridge_cfg["user"] cfg.uuid = bridge_cfg["user"]
end end
Huey.logger.level = @options[:hue_debug] ? Logger::DEBUG : Logger::FATAL Huey.logger.level = @options[:hue_debug] ? Logger::DEBUG : Logger::FATAL
puts "Configured bridge connection" msg "Configured bridge connection"
puts "Discovering lights..." msg "Discovering lights..."
lights lights
puts msg
puts "Discovering groups..." msg "Discovering groups..."
groups groups
puts msg
puts "Loading events..." msg "Loading events..."
events events
puts msg
puts "Loading scenes..." msg "Loading scenes..."
scenes scenes
puts msg
puts "All done!" msg "Loading sensors..."
puts "Use 'commands' to see a list of additional commands to pry's." sensors
puts msg
msg "All done!"
msg "Use 'commands' to see a list of additional commands to pry's."
msg
@prompt_stamp = Time.now @prompt_stamp = Time.now
Pry.config.prompt = proc { "#{prompt_status}> " }
binding.pry(quiet: true) if @interactive
Pry.config.prompt = proc { "#{prompt_status}> " }
binding.pry(quiet: true)
else
eval ARGV.join(" ")
end