hue_bridge 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11373903b6f45366294584a698152666145419f8
4
- data.tar.gz: 420e900fcbb39e6539f13e98a957fa966225885b
3
+ metadata.gz: 4b4954b76f2043d1e65f11b4ed3babd779106b06
4
+ data.tar.gz: 133e6453fe934a22d91e859cb39a6a0552f86481
5
5
  SHA512:
6
- metadata.gz: 1dd456ca637265767e185799aad3dda2ae18a049e60018df9703ed568154966e0b2dc49d3d0d841113b4f3ef0b0a011471b12d569ec06bb874378fc75423e5b9
7
- data.tar.gz: 66080c48bc755fc95551a63bbb0ce693bd2824a10ddb203e0708d605975a04ce947b4f14bd78f91e32198ee9e607e69e7746b84b93e8ba629f6aa3d5e6777b89
6
+ metadata.gz: c91f9f1f968533b5c2f2357728db18229fb57e91895061301311e34c3969ba9ff58abf9d018ba2c925b4632bec8621f275f5143a7aec97df51d87beec0298df5
7
+ data.tar.gz: 19bbbd8706528b3dd25624fd9fb047ac88556a93949207b92348a25fd19cc4b35267d9180dc0935fbab42cae66b5b1a3ba9bb2549a4ddf970434f8822a24ff56
@@ -0,0 +1,58 @@
1
+ module HueBridge
2
+ # Error raised when passing or setting invalid options.
3
+ class InvalidColorOption < StandardError; end
4
+
5
+ # Value object to represent a color
6
+ class Color
7
+ attr_reader :hue, :bri, :sat
8
+
9
+ # @param opts [Hash] the color options
10
+ # @option opts :bri The brightness
11
+ # @option opts :hue The hue
12
+ # @option opts :sat The saturation
13
+ def initialize(opts = {})
14
+ [:bri, :hue, :sat].each do |attr|
15
+ value = opts.fetch(attr, false)
16
+ send("#{attr}=", value) if value
17
+ end
18
+ end
19
+
20
+ # Returns a hash containing the color options.
21
+ #
22
+ # @return [Hash] the options
23
+ def to_h
24
+ hash = {}
25
+ [:bri, :hue, :sat].each do |attr|
26
+ value = send(attr)
27
+ hash[attr] = value if value
28
+ end
29
+ hash
30
+ end
31
+
32
+ alias to_hash to_h
33
+
34
+ # Sets the hue. Only values between 0 and 65535 are allowed.
35
+ # @param [Integer] value
36
+ def hue=(value)
37
+ fail InvalidColorOption,
38
+ 'Invalid hue' unless value.between?(0, 65535)
39
+ @hue = value
40
+ end
41
+
42
+ # Sets the saturation. Only values between 0 and 254 are allowed.
43
+ # @param [Integer] value
44
+ def sat=(value)
45
+ fail InvalidColorOption,
46
+ 'Invalid saturation' unless value.between?(0, 254)
47
+ @sat = value
48
+ end
49
+
50
+ # Sets the brightness. Only values between 1 and 254 are allowed.
51
+ # @param [Integer] value
52
+ def bri=(value)
53
+ fail InvalidColorOption,
54
+ 'Invalid brightness' unless value.between?(1, 254)
55
+ @bri = value
56
+ end
57
+ end
58
+ end
@@ -6,6 +6,8 @@ module HueBridge
6
6
  # light on, off and to toggle it.
7
7
  #
8
8
  class LightBulp
9
+ attr_reader :power
10
+
9
11
  # @param [Hash] options LightBulp options
10
12
  # @option options [String] :hue_bridge_ip The Hue Bridge's IP
11
13
  # @option options [String] :user_id The user id to access the api
@@ -18,27 +20,50 @@ module HueBridge
18
20
  end
19
21
 
20
22
  # Toggles the light bulp and returns it's state.
21
- # @return [Boolean] The state
23
+ # @return [Boolean] success of the operation
22
24
  #
23
25
  def toggle
24
- response = put("state", on: !@state)
25
- @state = get_state_from_response(response)
26
+ @power ||= false
27
+ response = put('state', on: !@power)
28
+ set_power_from_response!(response)
29
+ response_successful?(response)
26
30
  end
27
31
 
28
32
  # Turns the light bulp on and returns it's state.
29
- # @return [Boolean] The state
33
+ # @return [Boolean] success of the operation
30
34
  #
31
35
  def on
32
- response = put("state", on: true)
33
- @state = get_state_from_response(response)
36
+ response = put('state', on: true)
37
+ set_power_from_response!(response)
38
+ response_successful?(response)
34
39
  end
35
40
 
36
41
  # Turns the light bulp off and returns it's state.
37
- # @return [Boolean] The state
42
+ # @return [Boolean] success of the operation
38
43
  #
39
44
  def off
40
- response = put("state", on: false)
41
- @state = get_state_from_response(response)
45
+ response = put('state', on: false)
46
+ set_power_from_response!(response)
47
+ response_successful?(response)
48
+ end
49
+
50
+ # Invokes the alert sequence on the light bulp.
51
+ # @return [Boolean] success of the operation
52
+ #
53
+ def alert
54
+ response = put('state', alert: 'lselect')
55
+ response_successful?(response)
56
+ end
57
+
58
+ # Sets the color for the lightbulp.
59
+ # @see Color#initialize
60
+ # @return [Boolean] success of the operation
61
+ #
62
+ def set_color(opts = {})
63
+ color = Color.new(opts)
64
+ response = put('state', color.to_h)
65
+
66
+ response_successful?(response)
42
67
  end
43
68
 
44
69
  private
@@ -47,18 +72,34 @@ module HueBridge
47
72
  $stderr.puts(msg)
48
73
  end
49
74
 
50
- def get_state_from_response(response)
75
+ def set_power_from_response!(response)
51
76
  regex = /success.*\/lights\/\d*\/state\/on.*(?<state>true|false)\}\}\]/
52
77
  match = response.body.match(regex) || {}
53
78
 
79
+ @power = case match[:state]
80
+ when nil
81
+ log_error('Couldn\'t determin the power state from the response')
82
+ log_error(response.body)
83
+ false
84
+ when 'true'
85
+ true
86
+ when 'false'
87
+ false
88
+ end
89
+ end
90
+
91
+ def response_successful?(response)
92
+ regex = %r((?<state>success|error))
93
+ match = response.body.match(regex) || {}
94
+
54
95
  case match[:state]
55
96
  when nil
56
- log_error("Couldn't determin the state from the response")
97
+ log_error("Don't know how to handle the response")
57
98
  log_error(response.body)
58
99
  false
59
- when 'true'
100
+ when 'success'
60
101
  true
61
- when 'false'
102
+ when 'error'
62
103
  false
63
104
  end
64
105
  end
@@ -1,3 +1,3 @@
1
1
  module HueBridge
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/hue_bridge.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "hue_bridge/version"
2
+ require "hue_bridge/color"
2
3
  require "hue_bridge/light_bulp"
3
4
 
4
5
  begin
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hue_bridge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristopher Bredemeier
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-13 00:00:00.000000000 Z
11
+ date: 2016-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -114,6 +114,7 @@ files:
114
114
  - bin/setup
115
115
  - hue_bridge.gemspec
116
116
  - lib/hue_bridge.rb
117
+ - lib/hue_bridge/color.rb
117
118
  - lib/hue_bridge/light_bulp.rb
118
119
  - lib/hue_bridge/version.rb
119
120
  homepage: https://github.com/kbredemeier/hue_bridge
@@ -136,9 +137,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
137
  version: '0'
137
138
  requirements: []
138
139
  rubyforge_project:
139
- rubygems_version: 2.4.8
140
+ rubygems_version: 2.5.1
140
141
  signing_key:
141
142
  specification_version: 4
142
143
  summary: Gem to controll Philips hue lights with ruby
143
144
  test_files: []
144
- has_rdoc: