Class: Xilight::Yeelight
- Inherits:
-
Object
- Object
- Xilight::Yeelight
- Defined in:
- lib/xilight.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.discover ⇒ Object
This method is used to discover a smart LED on the local network.
Instance Method Summary collapse
- #available? ⇒ Boolean
-
#bright=(brightness) ⇒ Object
This method is used to change the brightness of the smart LED.
-
#cron_add(type, value) ⇒ Object
This method is used to start a timer job on the smart LED.
-
#cron_del(type) ⇒ Object
This method is used to stop the specified cron job.
-
#cron_get(type) ⇒ Object
This method is used to retrieve the setting of the current cron job of the specified type.
-
#ct_abx=(ct_value) ⇒ Object
This method is used to change the color temperature of the smart LED.
-
#get_prop(values) ⇒ Object
This method is used to retrieve current property of smart LED.
-
#hsv=(hue) ⇒ Object
This method is used to change the color HSV of the smart LED.
-
#initialize(location:, rgb: 0, hue: 0, sat: 0, ct: 0, color_mode: 0, bright: 0, power: :on, id:, name:, **kwargs) ⇒ Yeelight
constructor
A new instance of Yeelight.
- #name=(name) ⇒ Object
-
#off ⇒ Object
This method is used to switch off the smart LED.
-
#on ⇒ Object
This method is used to switch on the smart LED.
-
#power=(power) ⇒ Object
This method is used to switch on or off the smart LED (software managed on/off).
- #request(cmd) ⇒ Object
-
#rgb=(rgb_value) ⇒ Object
This method is used to change the color RGB of the smart LED.
-
#set_adjust(action, prop) ⇒ Object
This method is used to change brightness, CT or color of a smart LED without knowing the current value, it’s mainly used by controllers.
-
#set_bright(brightness, effect = 'smooth', duration = 200) ⇒ Object
This method is used to change the brightness of the smart LED.
-
#set_ct_abx(ct_value, effect = 'smooth', duration = 200) ⇒ Object
This method is used to change the color temperature of the smart LED.
-
#set_default ⇒ Object
This method is used to save the current state of smart LED in persistent memory.
-
#set_hsv(hue, sat, effect = 'smooth', duration = 200) ⇒ Object
This method is used to change the color HSV of the smart LED.
-
#set_name(name) ⇒ Object
This method is used to name the device.
-
#set_power(power, effect = 'smooth', duration = 200) ⇒ Object
This method is used to switch on or off the smart LED (software managed on/off).
-
#set_rgb(rgb_value, effect = 'smooth', duration = 200) ⇒ Object
This method is used to change the color RGB of the smart LED.
-
#set_scene(classe, val1, val2) ⇒ Object
This method is used to set the smart LED directly to specified state.
-
#start_cf(count = 0, action = 1, flow_expression = "1000,2,2700,100,500,1,255,10,5000,7,0,0,500,2,5000,1") ⇒ Object
This method is used to start a color flow.
-
#stop_cf ⇒ Object
This method is used to stop a running color flow.
-
#toggle ⇒ Object
This method is used to toggle the smart LED.
Constructor Details
#initialize(location:, rgb: 0, hue: 0, sat: 0, ct: 0, color_mode: 0, bright: 0, power: :on, id:, name:, **kwargs) ⇒ Yeelight
Returns a new instance of Yeelight.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/xilight.rb', line 12 def initialize(location:, rgb: 0, hue: 0, sat: 0, ct: 0, color_mode: 0, bright: 0, power: :on, id:, name:, **kwargs) @id = id @rgb = rgb.to_i @hue = hue.to_i @sat = sat.to_i @ct = ct.to_i @color_mode = color_mode.to_i @bright = bright.to_i @power = power.to_sym @location = location @name = name @host = location[/(?:\d+\.)+\d+/] @port = location[/(?!<:)\d+$/] end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
10 11 12 |
# File 'lib/xilight.rb', line 10 def id @id end |
Class Method Details
.discover ⇒ Object
This method is used to discover a smart LED on the local network
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/xilight.rb', line 190 def self.discover host = "239.255.255.250" port = 1982 socket = UDPSocket.new(Socket::AF_INET) payload = [] payload << "M-SEARCH * HTTP/1.1\r\n" payload << "HOST: #{host}:#{port}\r\n" payload << "MAN: \"ssdp:discover\"\r\n" payload << "ST: wifi_bulb" socket.send(payload.join(), 0, host, port) devices = [] begin Timeout.timeout(0.5) do loop do devices << socket.recvfrom(2048) end end rescue Timeout::Error => ex ex end devices.map do |(description, params)| = description.split("\r\n") .select{|x| x.include?(":")} .map{|x| x.split(":", 2) } .map do |key, value| [key.downcase.strip.gsub(/[^a-z]+/,"_").to_sym, value.strip] end.to_h Yeelight.new(**) end.uniq{|yl| yl.id } end |
Instance Method Details
#available? ⇒ Boolean
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/xilight.rb', line 27 def available?() begin Timeout::timeout(1) do begin s = TCPSocket.new(@host, @port) s.close return true rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH return false end end rescue Timeout::Error end return false end |
#bright=(brightness) ⇒ Object
This method is used to change the brightness of the smart LED.
100 101 102 |
# File 'lib/xilight.rb', line 100 def bright=brightness set_bright(brightness) end |
#cron_add(type, value) ⇒ Object
This method is used to start a timer job on the smart LED
146 147 148 |
# File 'lib/xilight.rb', line 146 def cron_add(type, value) request({id: 12,method: 'cron_add', params: [type,value]}) end |
#cron_del(type) ⇒ Object
This method is used to stop the specified cron job.
157 158 159 |
# File 'lib/xilight.rb', line 157 def cron_del(type) request({id: 14,method: 'cron_del', params: [type]}) end |
#cron_get(type) ⇒ Object
This method is used to retrieve the setting of the current cron job of the specified type
152 153 154 |
# File 'lib/xilight.rb', line 152 def cron_get(type) request({id: 13,method: 'cron_get', params: [type]}) end |
#ct_abx=(ct_value) ⇒ Object
This method is used to change the color temperature of the smart LED.
68 69 70 |
# File 'lib/xilight.rb', line 68 def ct_abx=ct_value set_ct_abx(ct_value) end |
#get_prop(values) ⇒ Object
This method is used to retrieve current property of smart LED.
58 59 60 |
# File 'lib/xilight.rb', line 58 def get_prop(values) request({id: 1,method: 'get_prop', params: values}) end |
#hsv=(hue) ⇒ Object
This method is used to change the color HSV of the smart LED.
90 91 92 |
# File 'lib/xilight.rb', line 90 def hsv=hue set_hsv(hue) end |
#name=(name) ⇒ Object
174 175 176 |
# File 'lib/xilight.rb', line 174 def name=(name) set_name(name) end |
#off ⇒ Object
This method is used to switch off the smart LED
184 185 186 |
# File 'lib/xilight.rb', line 184 def off set_power("off", "smooth",1000) end |
#on ⇒ Object
This method is used to switch on the smart LED
179 180 181 |
# File 'lib/xilight.rb', line 179 def on set_power("on", "smooth",1000) end |
#power=(power) ⇒ Object
This method is used to switch on or off the smart LED (software managed on/off).
110 111 112 |
# File 'lib/xilight.rb', line 110 def power=power set_power(power) end |
#request(cmd) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/xilight.rb', line 43 def request(cmd) begin Timeout.timeout(0.5) do s = TCPSocket.open(@host, @port) s.puts "#{JSON.generate(cmd)}\r\n" data = s.gets.chomp s.close JSON.parse(data) end rescue Timeout::Error puts "Yeelight connection timed out. Yeelight not accessible" end end |
#rgb=(rgb_value) ⇒ Object
This method is used to change the color RGB of the smart LED. Expects an integer representing a hex triplet (e.g. 0xFFFFFF )
80 81 82 |
# File 'lib/xilight.rb', line 80 def rgb=rgb_value set_rgb(rgb_value) end |
#set_adjust(action, prop) ⇒ Object
This method is used to change brightness, CT or color of a smart LED without knowing the current value, it’s mainly used by controllers.
163 164 165 |
# File 'lib/xilight.rb', line 163 def set_adjust(action, prop) request({id: 15,method: 'set_adjust', params: [action,prop]}) end |
#set_bright(brightness, effect = 'smooth', duration = 200) ⇒ Object
This method is used to change the brightness of the smart LED.
95 96 97 |
# File 'lib/xilight.rb', line 95 def set_bright(brightness, effect='smooth', duration=200) request({id: 5,method: 'set_bright', params: [brightness,effect,duration]}) end |
#set_ct_abx(ct_value, effect = 'smooth', duration = 200) ⇒ Object
This method is used to change the color temperature of the smart LED.
63 64 65 |
# File 'lib/xilight.rb', line 63 def set_ct_abx(ct_value, effect='smooth', duration=200) request({id: 2,method: 'set_ct_abx', params: [ct_value,effect,duration]}) end |
#set_default ⇒ Object
This method is used to save the current state of smart LED in persistent memory. If user powers off and then powers on the smart LED again (hard power reset), the smart LED will show last the saved state.
122 123 124 |
# File 'lib/xilight.rb', line 122 def set_default request({id: 8,method: 'set_default', params: []}) end |
#set_hsv(hue, sat, effect = 'smooth', duration = 200) ⇒ Object
This method is used to change the color HSV of the smart LED.
85 86 87 |
# File 'lib/xilight.rb', line 85 def set_hsv(hue, sat, effect='smooth', duration=200) request({id: 4,method: 'set_hsv', params: [hue,sat,effect,duration]}) end |
#set_name(name) ⇒ Object
This method is used to name the device. The name will be stored on the device and reported in the discovery response. Users can also read the device name through the “get_prop” method.
170 171 172 |
# File 'lib/xilight.rb', line 170 def set_name(name) request({id: 16,method: 'set_name', params: [name]}) end |
#set_power(power, effect = 'smooth', duration = 200) ⇒ Object
This method is used to switch on or off the smart LED (software managed on/off).
105 106 107 |
# File 'lib/xilight.rb', line 105 def set_power(power, effect='smooth', duration=200) request({id: 6,method: 'set_power', params: [power,effect,duration]}) end |
#set_rgb(rgb_value, effect = 'smooth', duration = 200) ⇒ Object
This method is used to change the color RGB of the smart LED. Expects an integer representing a hex triplet (e.g. 0xFFFFFF )
74 75 76 |
# File 'lib/xilight.rb', line 74 def set_rgb(rgb_value, effect='smooth', duration=200) request({id: 3,method: 'set_rgb', params: [rgb_value,effect,duration]}) end |
#set_scene(classe, val1, val2) ⇒ Object
This method is used to set the smart LED directly to specified state. If the smart LED is off, then it will first turn on the smartLED and then apply the specified command.
141 142 143 |
# File 'lib/xilight.rb', line 141 def set_scene(classe, val1, val2) request({id: 11,method: 'set_scene', params: [classe,val1,val2]}) end |
#start_cf(count = 0, action = 1, flow_expression = "1000,2,2700,100,500,1,255,10,5000,7,0,0,500,2,5000,1") ⇒ Object
This method is used to start a color flow. Color flow is a series of smart LED visible state changes. It can be either brightness changing, color changing or color temperature changing
129 130 131 |
# File 'lib/xilight.rb', line 129 def start_cf(count=0, action=1, flow_expression="1000,2,2700,100,500,1,255,10,5000,7,0,0,500,2,5000,1") request({id: 9, method: 'start_cf', params: [count,action,flow_expression]}) end |
#stop_cf ⇒ Object
This method is used to stop a running color flow.
134 135 136 |
# File 'lib/xilight.rb', line 134 def stop_cf request({id: 10,method: 'stop_cf', params: []}) end |
#toggle ⇒ Object
This method is used to toggle the smart LED.
115 116 117 |
# File 'lib/xilight.rb', line 115 def toggle request({id: 7,method: 'toggle', params: []}) end |