Class: Xilight::Yeelight

Inherits:
Object
  • Object
show all
Defined in:
lib/xilight.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#idObject (readonly)

Returns the value of attribute id


10
11
12
# File 'lib/xilight.rb', line 10

def id
  @id
end

Class Method Details

.discoverObject

This method is used to discover a smart LED on the local network


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/xilight.rb', line 174

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)|
    options = 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(**options)
  end.uniq{|yl| yl.id }
end

Instance Method Details

#bright=(brightness) ⇒ Object

This method is used to change the brightness of the smart LED.


84
85
86
# File 'lib/xilight.rb', line 84

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


130
131
132
# File 'lib/xilight.rb', line 130

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.


141
142
143
# File 'lib/xilight.rb', line 141

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


136
137
138
# File 'lib/xilight.rb', line 136

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.


52
53
54
# File 'lib/xilight.rb', line 52

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.


42
43
44
# File 'lib/xilight.rb', line 42

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.


74
75
76
# File 'lib/xilight.rb', line 74

def hsv=hue
  set_hsv(hue)
end

#name=(name) ⇒ Object


158
159
160
# File 'lib/xilight.rb', line 158

def name=(name)
  set_name(name)
end

#offObject

This method is used to switch off the smart LED


168
169
170
# File 'lib/xilight.rb', line 168

def off
  set_power("off", "smooth",1000)
end

#onObject

This method is used to switch on the smart LED


163
164
165
# File 'lib/xilight.rb', line 163

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).


94
95
96
# File 'lib/xilight.rb', line 94

def power=power
  set_power(power)
end

#request(cmd) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/xilight.rb', line 27

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 )


64
65
66
# File 'lib/xilight.rb', line 64

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.


147
148
149
# File 'lib/xilight.rb', line 147

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.


79
80
81
# File 'lib/xilight.rb', line 79

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.


47
48
49
# File 'lib/xilight.rb', line 47

def set_ct_abx(ct_value, effect='smooth', duration=200)
  request({id: 2,method: 'set_ct_abx', params: [ct_value,effect,duration]})
end

#set_defaultObject

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.


106
107
108
# File 'lib/xilight.rb', line 106

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.


69
70
71
# File 'lib/xilight.rb', line 69

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.


154
155
156
# File 'lib/xilight.rb', line 154

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).


89
90
91
# File 'lib/xilight.rb', line 89

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 )


58
59
60
# File 'lib/xilight.rb', line 58

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.


125
126
127
# File 'lib/xilight.rb', line 125

def set_scene(classe, val1, val2)
  request({id: 11,method: 'set_scene', params: [classe,val1,val2]})
end

#start_cf(count, action, flow_expression) ⇒ 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


113
114
115
# File 'lib/xilight.rb', line 113

def start_cf(count, action, flow_expression)
  request({id: 9,method: 'set_power', params: [count,action,flow_expression]})
end

#stop_cfObject

This method is used to stop a running color flow.


118
119
120
# File 'lib/xilight.rb', line 118

def stop_cf
  request({id: 10,method: 'stop_cf', params: []})
end

#toggleObject

This method is used to toggle the smart LED.


99
100
101
# File 'lib/xilight.rb', line 99

def toggle
  request({id: 7,method: 'toggle', params: []})
end