exe/yee in xilight-0.1.5 vs exe/yee in xilight-0.1.6

- old
+ new

@@ -18,25 +18,39 @@ JSON.load(IO.read(config_path)) rescue {} end - def self.get_light(index, name=nil) + def self.get_light(index, name=nil, attempt: 0) lights = ::Xilight::CLI.config['yeelights'] light_attribs = if name && name != '' lights.find{|l| l['name'].downcase.strip == name.downcase.strip } else lights[index.to_i] end - ::Xilight::Yeelight.new(light_attribs.map{|k,v| [k.to_sym, v]}.to_h) + light = ::Xilight::Yeelight.new(light_attribs.map{|k,v| [k.to_sym, v]}.to_h) + unless light.available? + self.discover + get_light(index, name, attempt: 1) if attempt.zero? + else + light + end rescue puts "Couldn't find light with " + ((name && name != '') ? "name #{name}" : "ID #{index}") puts "Known lights:" puts self.config.fetch('yeelights', []).map.with_index{|light, i| "\tLight ##{i} => #{(light['name'] || light['id'])}@#{light['host']}"} exit(1) end + def self.discover + ::Xilight::CLI.config = { + 'yeelights' => ::Xilight::Yeelight.discover.each do |light| + light.instance_variables.map{|v| [v.to_s[1..-1], light.instance_variable_get(v)] }.to_h + end + } + end + module Commands extend Dry::CLI::Registry class Version < Dry::CLI::Command desc "Print version" @@ -56,15 +70,11 @@ example [ "" ] def call(**) - ::Xilight::CLI.config = { - 'yeelights' => ::Xilight::Yeelight.discover.each do |light| - light.instance_variables.map{|v| [v.to_s[1..-1], light.instance_variable_get(v)] }.to_h - end - } + ::Xilight::CLI.discover end end class List < Dry::CLI::Command desc "List known Yeelights" @@ -133,9 +143,10 @@ "50 -n foo # 50% brightness for light with name foo ", "25 -l 3 # 25% brightness for light#3 " ] def call(brightness:, light: 0, name: nil, **) + binding.pry light =::Xilight::CLI.get_light(light, name) light.bright = brightness.to_i end end \ No newline at end of file