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

- old
+ new

@@ -18,18 +18,26 @@ JSON.load(IO.read(config_path)) rescue {} end + def self.rgb_to_hex(r,g,b) + [r,g,b].map{|x| x.to_i.to_s(16).rjust(2, '0') }.join + end + + def self.rgb_to_i(r,g,b) + rgb_to_hex(r,g,b).to_i(16) + end + 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 - light = ::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 @@ -106,12 +114,11 @@ "255 255 255 -l 3# set light #3 to white (red 255, green 255, blue 255)", ] def call(r:, g:, b:, light: 0, name: nil, **) light =::Xilight::CLI.get_light(light, name) - as_hex = [r,g,b].map{|x| x.to_i.to_s(16).rjust(2, '0') }.join - light.rgb = as_hex.to_i(16) + light.rgb = ::Xilight::CLI.rgb_to_i(r,g,b) end end class Hsv < Dry::CLI::Command desc "Set HSV for Yeelight" @@ -143,11 +150,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 @@ -217,10 +223,48 @@ config['yeelights'][light.to_i]['name'] = name ::Xilight::CLI.config = config end end + module ColorFlow + class On < Dry::CLI::Command + desc "Start ColorFlow" + option :light, desc: "ID/index of Yeelight to target", default: "0", aliases: ["-l"] + option :name, desc: "Name of Yeelight to target", default: nil, aliases: ["-n"] + + example [ + "on # start color flow on default/first light", + "on -n kitchen # start color flow on light with name 'kitchen'", + "on -l 3 # start color flow on light #3" + ] + def call(light: 0, name: nil, **) + light =::Xilight::CLI.get_light(light, name) + flow_expression = COLORS.values.first(12).each_slice(2).map(&:first).map do |(r,g,b)| + "1000,2,#{::Xilight::CLI.rgb_to_i(r,g,b)},100" + end.join(',') + light.start_cf(0, 1, flow_expression) + end + end + + class Off < Dry::CLI::Command + desc "Stop ColorFlow" + option :light, desc: "ID/index of Yeelight to target", default: "0", aliases: ["-l"] + option :name, desc: "Name of Yeelight to target", default: nil, aliases: ["-n"] + + example [ + "off # start color flow on default/first light", + "off -n kitchen # start color flow on light with name 'kitchen'", + "off -l 3 # start color flow on light #3" + ] + + def call(light: 0, name: nil, **) + light =::Xilight::CLI.get_light(light, name) + light.power = 'off' + end + end + end + COLORS = { red: [255, 0, 0], orange: [255, 128, 0], yellow: [255, 255, 0], lightgreen: [128, 255, 0], @@ -258,9 +302,14 @@ register "color", aliases: ["c"] do |prefix| COLORS.keys.each do |color| class_name = color.to_s[0].upcase + color.to_s[1..-1] prefix.register color.to_s, Object.const_get(class_name) end + end + + register "color_flow", aliases: ["cf"] do |prefix| + prefix.register "on", ColorFlow::On, aliases: ["o"] + prefix.register "off", ColorFlow::Off, aliases: ["x"] end register "version", Version, aliases: ["v", "-v", "--version"] register "discover", Discover, aliases: ["d"] register "rgb", Rgb \ No newline at end of file