Class: Yeelight::Lamp

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

Constant Summary collapse

VALUES =
'"rgb", "ct", "color_mode", "hue", "sat"'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Lamp

Returns a new instance of Lamp.



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

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/yeelight/lamp.rb', line 5

def client
  @client
end

Instance Method Details

#toggle_color(color_hex, duration = 1) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/yeelight/lamp.rb', line 11

def toggle_color(color_hex, duration = 1)
  color = "0x#{color_hex}".to_i(16)

  rgb, ct, color_mode, hue, sat = JSON.parse(client.get_prop(VALUES))['data']['result']

  client.set_rgb(color, 'smooth', 500)
  sleep duration

  case color_mode.to_i
  when 1
    client.set_rgb(rgb, 'smooth', 500)
  when 2
    client.set_ct_abx(ct, 'smooth', 500)
  when 3
    client.set_hsv(hue, sat, 'smooth', 500)
  end
end