Class: PhilipsHue::Bridge

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_name, api_url) ⇒ Bridge

creates an app through which to talk to a Philips Hue (new app names must be registered with the bridge)

app_name is used to register with the Hue
api_url is the hostname/IP address of the bridge


8
9
10
11
12
# File 'lib/philips_hue/bridge.rb', line 8

def initialize(app_name, api_url)
  @app_name = app_name
  @key = Digest::MD5.hexdigest(@app_name)
  @api_endpoint = "http://#{api_url}/api"
end

Instance Attribute Details

#api_endpointObject (readonly)

provide getter methods for these variables



15
16
17
# File 'lib/philips_hue/bridge.rb', line 15

def api_endpoint
  @api_endpoint
end

#app_nameObject (readonly)

provide getter methods for these variables



15
16
17
# File 'lib/philips_hue/bridge.rb', line 15

def app_name
  @app_name
end

#keyObject (readonly)

provide getter methods for these variables



15
16
17
# File 'lib/philips_hue/bridge.rb', line 15

def key
  @key
end

Class Method Details

.register(app_name, api_url) ⇒ Object

registers your app with the Hue this must be run for every new app name



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/philips_hue/bridge.rb', line 40

def self.register(app_name, api_url)
  puts "Press the link button on the Hue..."

  # pretty countdown
  10.downto(1) do |n|
    print n
    sleep 1
    print n == 1 ? "\r#{' '*30}\r" : ", "
  end

  # create a new bridge and submit a special POST request
  new_bridge = self.new(app_name, api_url)
  json_body = { :username => new_bridge.key, :devicetype => new_bridge.app_name}.to_json
  response = HTTParty.post(new_bridge.api_endpoint, :body => json_body)

  # quit if it didn't work (link button wasn't pressed)
  abort "Press link button and try again." if response.first["error"]

  # return the now-registered bridge
  new_bridge
end

Instance Method Details

#add_light(light_id, light_name) ⇒ Object

creates a new Light object



24
25
26
# File 'lib/philips_hue/bridge.rb', line 24

def add_light(light_id, light_name)
  Light.new(light_name, light_id, @api_endpoint, @key)
end

#light(light_id) ⇒ Object

helper method to get light by light_id



34
35
36
# File 'lib/philips_hue/bridge.rb', line 34

def light(light_id)
  self.lights[light_id.to_i-1]
end

#lightsObject

return the lights array or add them it if needed



29
30
31
# File 'lib/philips_hue/bridge.rb', line 29

def lights
  @lights ||= add_all_lights
end

#overviewObject

returns overall system status as JSON



18
19
20
21
# File 'lib/philips_hue/bridge.rb', line 18

def overview
  request_uri = "#{@api_endpoint}/#{@key}"
  HTTParty.get(request_uri)
end

#to_sObject

human-readable bridge summary



63
64
65
# File 'lib/philips_hue/bridge.rb', line 63

def to_s
  "#{app_name}: #{api_endpoint}"
end