Class: Hue::JsClient

Inherits:
Client show all
Defined in:
lib/alexa_hue/hue/js_client.rb

Instance Attribute Summary collapse

Attributes inherited from Client

#groups, #lights, #scenes

Instance Method Summary collapse

Methods inherited from Client

#alert, #brightness, #color, #colorloop, #fade, #hue, #light, #mired, #reset, #saturation, #scene, #toggle_group, #toggle_lights, #toggle_scene, #toggle_system

Methods included from Helpers

#fix_schedule_syntax, #numbers_to_times, #parse_time, #set_time

Constructor Details

#initialize(options = {}) ⇒ JsClient

Returns a new instance of JsClient.



7
8
9
10
11
12
# File 'lib/alexa_hue/hue/js_client.rb', line 7

def initialize(options={})
  @client = Takeout::Client.new(uri: options[:uri], port: options[:port])
  @lights_array, @schedule_ids, @schedule_params, @command, @_group, @body = [], [], "", "0", Hue::RequestBody.new

  populate_client
end

Instance Attribute Details

#_groupObject

Returns the value of attribute _group.



5
6
7
# File 'lib/alexa_hue/hue/js_client.rb', line 5

def _group
  @_group
end

#bridge_ipObject

Returns the value of attribute bridge_ip.



5
6
7
# File 'lib/alexa_hue/hue/js_client.rb', line 5

def bridge_ip
  @bridge_ip
end

#clientObject

Returns the value of attribute client.



5
6
7
# File 'lib/alexa_hue/hue/js_client.rb', line 5

def client
  @client
end

#commandObject

Returns the value of attribute command.



5
6
7
# File 'lib/alexa_hue/hue/js_client.rb', line 5

def command
  @command
end

#schedule_idsObject

Returns the value of attribute schedule_ids.



5
6
7
# File 'lib/alexa_hue/hue/js_client.rb', line 5

def schedule_ids
  @schedule_ids
end

#schedule_paramsObject

Returns the value of attribute schedule_params.



5
6
7
# File 'lib/alexa_hue/hue/js_client.rb', line 5

def schedule_params
  @schedule_params
end

#userObject

Returns the value of attribute user.



5
6
7
# File 'lib/alexa_hue/hue/js_client.rb', line 5

def user
  @user
end

Instance Method Details

#confirmObject



14
15
16
# File 'lib/alexa_hue/hue/js_client.rb', line 14

def confirm
  @client.apply_alert(:alert => 'select')
end

#delete_schedules!Object



30
31
32
33
34
# File 'lib/alexa_hue/hue/js_client.rb', line 30

def delete_schedules!
  @schedule_ids.flatten!
  @schedule_ids.each { |k| @client.delete_schedule(schedule: k.dig("success","id")) }
  @schedule_ids = []
end

#offObject



63
64
# File 'lib/alexa_hue/hue/js_client.rb', line 63

def off
end

#onObject



57
58
59
60
61
# File 'lib/alexa_hue/hue/js_client.rb', line 57

def on
  if @body.scene
    @client.get_activate_scene(scene: @body.scene)
  end
end

#save_scene(scene_name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/alexa_hue/hue/js_client.rb', line 18

def save_scene(scene_name)
  fade(2) if @body.transitiontime == nil
  if @_group.empty?
    light_group = @client.get_all_lights.body["lights"]
  else
    light_group = @client.get_group(group: @_group).body["lights"]
  end
  params = {name: scene_name.gsub!(' ','-'), lights: light_group, transitiontime: @body.transitiontime}
  response = @client.put_scene(scene: scene_name, options: params).body
  confirm if response.first.keys[0] == "success"
end

#schedule(string, on_or_off = :default) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/alexa_hue/hue/js_client.rb', line 36

def schedule(string, on_or_off = :default)
  @body.on = (on_or_off == :on)
  set_time = set_time(string)
  unless set_time < Time.now
    set_time = set_time.to_s.split(' ')[0..1].join(' ').sub(' ',"T")
    @schedule_params = {:name=>"Hue_Switch Alarm",
           :description=>"",
           :localtime=>"#{set_time}",
           :status=>"enabled",
           :autodelete=>true
          }
    if @lights_array.any?
      lights_array.each {|l| @schedule_params[:command] = {:address=>"/api/#{@user}/lights/#{l}/state", :method=>"PUT", :body=>@body} }
    else
      @schedule_params[:command] = {:address=>"/api/#{@user}/groups/#{@_group}/action", :method=>"PUT", :body=>@body}
    end
    @schedule_ids.push(@client.post_schedules(options: @schedule_params).body)
    confirm if @schedule_ids.flatten.last.include?("success")
  end
end