class Hue::JsClient

Attributes

_group[RW]
bridge_ip[RW]
client[RW]
command[RW]
schedule_ids[RW]
schedule_params[RW]
user[RW]

Public Class Methods

new(options={}) click to toggle source
# 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

Public Instance Methods

confirm() click to toggle source
# File lib/alexa_hue/hue/js_client.rb, line 14
def confirm
  @client.apply_alert(:alert => 'select')
end
delete_schedules!() click to toggle source
# File lib/alexa_hue/hue/js_client.rb, line 25
def delete_schedules!
  @schedule_ids.flatten!
  @schedule_ids.each { |k| @client.delete_schedule(schedule: k.dig("success","id")) }
  @schedule_ids = []
end
off() click to toggle source
# File lib/alexa_hue/hue/js_client.rb, line 56
def off
end
save_scene(scene_name) click to toggle source
# File lib/alexa_hue/hue/js_client.rb, line 18
def save_scene(scene_name)
  fade(2) if @body.transitiontime == nil
  light_group = @_group.empty? ? @client.get_all_lights.body["lights"] : @client.get_group(group: @_group).body["lights"]
  params = {name: scene_name.gsub!(' ','-'), lights: light_group, transitiontime: @body.transitiontime}
  confirm if @client.put_scene(scene: scene_name, options: params).body.first.keys[0] == "success"
end
schedule(string, on_or_off = :default) click to toggle source
# File lib/alexa_hue/hue/js_client.rb, line 31
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
toggle_scene() click to toggle source
# File lib/alexa_hue/hue/js_client.rb, line 52
def toggle_scene
  @client.get_activate_scene(scene: @body.scene)
end