Class: Yeelight::Client

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/yeelight/client.rb', line 7

def initialize(host, port)
  @host = host
  @port = port
end

Class Method Details

.discoverObject

This method is used to discover a smart LED in the network



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/yeelight/client.rb', line 155

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(2) do
      loop do
        devices << socket.recvfrom(2048)
      end
    end
  rescue Timeout::Error => ex
    ex
  end

  devices
end

Instance Method Details

#cron_add(type, value) ⇒ Object

This method is used to start a timer job on the smart LED



110
111
112
113
# File 'lib/yeelight/client.rb', line 110

def cron_add(type, value)
  cmd = "{\"id\":12,\"method\":\"cron_add\",\"params\":[#{type},#{value}]}\r\n"
  request(cmd)
end

#cron_del(type) ⇒ Object

This method is used to stop the specified cron job.



123
124
125
126
# File 'lib/yeelight/client.rb', line 123

def cron_del(type)
  cmd = "{\"id\":14,\"method\":\"cron_del\",\"params\":[#{type}]}\r\n"
  request(cmd)
end

#cron_get(type) ⇒ Object

This method is used to retrieve the setting of the current cron job of the specified type



117
118
119
120
# File 'lib/yeelight/client.rb', line 117

def cron_get(type)
  cmd = "{\"id\":13,\"method\":\"cron_get\",\"params\":[#{type}]}\r\n"
  request(cmd)
end

#get_prop(values) ⇒ Object

This method is used to retrieve current property of smart LED.



37
38
39
40
# File 'lib/yeelight/client.rb', line 37

def get_prop(values)
  cmd = "{\"id\":1,\"method\":\"get_prop\",\"params\":[#{values}]}\r\n"
  request(cmd)
end

#offObject

This method is used to switch off the smart LED



149
150
151
# File 'lib/yeelight/client.rb', line 149

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

#onObject

This method is used to switch on the smart LED



144
145
146
# File 'lib/yeelight/client.rb', line 144

def on
  set_power("on", "smooth",1000)
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 main used by controllers.



130
131
132
133
# File 'lib/yeelight/client.rb', line 130

def set_adjust(action, prop)
  cmd = "{\"id\":15,\"method\":\"set_adjust\",\"params\":[\"#{action}\",\"#{prop}\"]}\r\n"
  request(cmd)
end

#set_bright(brightness, effect, duration) ⇒ Object

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



61
62
63
64
# File 'lib/yeelight/client.rb', line 61

def set_bright(brightness, effect, duration)
  cmd = "{\"id\":5,\"method\":\"set_bright\",\"params\":[#{brightness},\"#{effect}\",#{duration}]}\r\n"
  request(cmd)
end

#set_ct_abx(ct_value, effect, duration) ⇒ Object

This method is used to change the color temperature of a smart LED.



43
44
45
46
# File 'lib/yeelight/client.rb', line 43

def set_ct_abx(ct_value, effect, duration)
  cmd = "{\"id\":2,\"method\":\"set_ct_abx\",\"params\":[#{ct_value},\"#{effect}\",#{duration}]}\r\n"
  request(cmd)
end

#set_defaultObject

This method is used to save current state of smart LED in persistent memory. So if user powers off and then powers on the smart LED again (hard power reset), the smart LED will show last saved state. Note: The “automatic state saving” must be turn off



82
83
84
85
# File 'lib/yeelight/client.rb', line 82

def set_default
  cmd = "{\"id\":8,\"method\":\"set_default\",\"params\":[]}\r\n"
  request(cmd)
end

#set_hsv(hue, sat, effect, duration) ⇒ Object

This method is used to change the color HSV of a smart LED.



55
56
57
58
# File 'lib/yeelight/client.rb', line 55

def set_hsv(hue, sat, effect, duration)
  cmd = "{\"id\":4,\"method\":\"set_hsv\",\"params\":[#{hue},#{sat},\"#{effect}\",#{duration}]}\r\n"
  request(cmd)
end

#set_name(name) ⇒ Object

This method is used to name the device. The name will be stored on the device and reported in discovering response. User can also read the name through “get_prop” method.



138
139
140
141
# File 'lib/yeelight/client.rb', line 138

def set_name(name)
  cmd = "{\"id\":16,\"method\":\"set_name\",\"params\":[\"#{name}\"]}\r\n"
  request(cmd)
end

#set_power(power, effect, duration) ⇒ Object

This method is used to switch on or off the smart LED (software managed on/off).



67
68
69
70
# File 'lib/yeelight/client.rb', line 67

def set_power(power, effect, duration)
  cmd = "{\"id\":6,\"method\":\"set_power\",\"params\":[\"#{power}\",\"#{effect}\",#{duration}]}\r\n"
  request(cmd)
end

#set_rgb(rgb_value, effect, duration) ⇒ Object

This method is used to change the color RGB of a smart LED.



49
50
51
52
# File 'lib/yeelight/client.rb', line 49

def set_rgb(rgb_value, effect, duration)
  cmd = "{\"id\":3,\"method\":\"set_rgb\",\"params\":[#{rgb_value},\"#{effect}\",#{duration}]}\r\n"
  request(cmd)
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 turn on the smart LED firstly and then apply the specified scommand.



104
105
106
107
# File 'lib/yeelight/client.rb', line 104

def set_scene(classe, val1, val2)
  cmd = "{\"id\":11,\"method\":\"set_scene\",\"params\":[\"#{classe}\",#{val1},#{val2}]}\r\n"
  request(cmd)
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 changing. It can be brightness changing, color changing or color temperature changing



90
91
92
93
# File 'lib/yeelight/client.rb', line 90

def start_cf(count, action, flow_expression)
  cmd = "{\"id\":9,\"method\":\"set_power\",\"params\":[#{count},#{action},\"#{flow_expression}\"]}\r\n"
  request(cmd)
end

#stop_cfObject

This method is used to stop a running color flow.



96
97
98
99
# File 'lib/yeelight/client.rb', line 96

def stop_cf
  cmd = "{\"id\":10,\"method\":\"stop_cf\",\"params\":[]}\r\n"
  request(cmd)
end

#toggleObject

This method is used to toggle the smart LED.



73
74
75
76
# File 'lib/yeelight/client.rb', line 73

def toggle
  cmd = "{\"id\":7,\"method\":\"toggle\",\"params\":[]}\r\n"
  request(cmd)
end