lita-hue 0.1.0.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1c7acccec83ef0eaab3081b86da9718b4f009e31
4
+ data.tar.gz: f2ee53222d8068278be5471995f63f621ae891d6
5
+ SHA512:
6
+ metadata.gz: fa6bbdb4976c5ad0ea90994256b2869a2cef35952cc4395d2dd9634b3809f6f33a1d57b8d3ae0e149a65c9c65ff6a5df6b0a2caa0f6907b307de4840fda52889
7
+ data.tar.gz: c3dbf5da024f25c307d8daa7706b09aef175a7a444c8297da245ca954fa0091ed7be43f88e4290fa7802d0ca693c8e0f24984f8b2d626f1df945797ed9c25866
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ script: bundle exec rake
5
+ before_install:
6
+ - gem update --system
7
+ services:
8
+ - redis-server
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,52 @@
1
+ # lita-hue
2
+
3
+ Lita plugin for controlling Phillips Hue lights.
4
+
5
+ While some proxy trickery might be possible, this is designed for when
6
+ lita is running on a computer that can talk directly to the Hue Bridge
7
+
8
+ Currently it can control individual lights one at a time and report global status
9
+
10
+ ## Installation
11
+
12
+ Add lita-hue to your Lita instance's Gemfile:
13
+
14
+ ``` ruby
15
+ gem "lita-hue"
16
+ ```
17
+
18
+ If you want to use the name that color functionality, also be sure to have
19
+ nodejs installed and the closest-color-keyword module installed
20
+
21
+ ```bash
22
+ brew install nodejs #or apt-get or yum
23
+ sudo npm install -g closest-color-keyword
24
+ ```
25
+
26
+ ## Configuration
27
+
28
+ The `register` command should good for getting set up, but if you can talk
29
+ to the bridge but not discover it (e.g. if lita is in a VM where uPNP traffic
30
+ is filtered), you can also run `ruby -rhue -e "puts Hue.register_default"` on a
31
+ different computer on the network with the `hue-lib` gem installed and copy
32
+ the contents of `~/.hue-lib` to the appropriate place
33
+
34
+ Due to the dependence on node, the name that color feature is disabled by default.
35
+ If you want it on, add the first of the following lines to your `lita_config.rb`
36
+ and a modification on second as applicable (the shown value is the default)
37
+
38
+ ```ruby
39
+ config.handlers.hue.nodejs_color_lookup = true
40
+ #assume we want to use Global node_modules only and that node is called `node`
41
+ #and in the path of the lita process
42
+ config.handlers.hue.nodejs_invocation = "NODE_PATH=$(npm -g root) node"
43
+ ```
44
+
45
+ ## Usage
46
+ e.g.
47
+
48
+ * `hue register` - register lita with your hue bridge
49
+ * `hue status` - list known bulbs and their states
50
+ * `hue 1 off` - turn off bulb with id 1 (ids are same as other apps, but can be seen in `status`)
51
+ * `hue 2 red`, `hue 3 aquamarine`, `hue 4 #ffa` - set bulbs to various CSS colors. Its not exact but its close
52
+ * `hue 3 blink`, `hue 1 on`, `hue 4 rainbow` - etc.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,3 @@
1
+ var kw = require('closest-color-keyword');
2
+ console.log(kw(process.argv[2]));
3
+
@@ -0,0 +1,12 @@
1
+ require "lita"
2
+
3
+ Lita.load_locales Dir[File.expand_path(
4
+ File.join("..", "..", "locales", "*.yml"), __FILE__
5
+ )]
6
+
7
+ require "lita/handlers/hue"
8
+
9
+ Lita::Handlers::Hue.template_root File.expand_path(
10
+ File.join("..", "..", "templates"),
11
+ __FILE__
12
+ )
@@ -0,0 +1,160 @@
1
+ require 'hue'
2
+ require 'css_color'
3
+ module Lita
4
+ module Handlers
5
+ class Hue < Handler
6
+ HueLib = ::Hue
7
+
8
+ config :nodejs_invocation, type: String, default: "NODE_PATH=$(npm -g root) node" #assume its in path using gloabl modules
9
+ config :nodejs_color_lookup, types: [TrueClass,FalseClass], default: false
10
+
11
+ route(/^hue register$/, :register)
12
+
13
+ route(/^hue (status|lights)$/, :list_lights)
14
+ route(/^hue \d{1,2} [^%]+$/, :light_command)
15
+
16
+ def light_command(res)
17
+ light_id = res.args[0]
18
+ command = res.args[1].to_sym
19
+ command = :color_loop if command == :rainbow
20
+
21
+ bulb = HueLib::Bulb.new(bridge(res), light_id)
22
+
23
+ if bulb.respond_to?(command) && bulb.method(command).arity == 0
24
+ bulb.send command
25
+ else #assume its a color and try to set it
26
+ _set_light_color(res, bulb, res.args[1])
27
+ end
28
+ rescue HueLib::API::Error => e
29
+ if e.instance_variable_get(:@type) == 3
30
+ res.reply "Error: Unkown bulb, ##{id}"
31
+ else
32
+ raise
33
+ end
34
+ end
35
+
36
+ def _set_light_color(res, bulb, color_str)
37
+ hex = CSSColor.parse(color_str).html
38
+
39
+ bulb.on if bulb.off?
40
+ bulb.color = hex_to_hsb(hex)
41
+ rescue CSSColor::UnknownColorError
42
+ res.reply "Error: Unkown color '#{color_str}' setting light ##{id}"
43
+ end
44
+
45
+ def list_lights(res)
46
+ lights = bridge(res).lights
47
+ lights.each do |id, light|
48
+ reply_str = "#{id}"
49
+ reply_str += (light["name"].empty? || light["name"] =~ /^light #{id}/i) ? ": " : " (#{light["name"]}): "
50
+ if !light["state"]["on"]
51
+ reply_str += "Off"
52
+ else
53
+ reply_str += "On: "
54
+ bulb = HueLib::Bulb.new(@bridge, id)
55
+ reply_str += color_to_str(bulb.color)
56
+ reply_str += ", #{bulb.brightness_percent}%"
57
+ end
58
+ res.reply reply_str
59
+ end
60
+ end
61
+
62
+ def register(res)
63
+ @bridge = HueLib.application
64
+ res.reply "Already registered (Bridge: #{@bridge.bridge_uri} AppId: #{@bridge.application_id})\nif you want to reregister remove ~/.hue-lib on the lita host"
65
+ rescue HueLib::Error
66
+ res.reply "Attempting registration..."
67
+ begin
68
+ r = HueLib.register_default
69
+ @bridge = r #don't overwrite last on exception case.
70
+ res.reply "registered! (Bridge: #{a.bridge_uri} AppId: #{r.application_id})"
71
+ rescue HueLib::API::Error => e
72
+ if e.instance_variable_get(:@type) == 101
73
+ res.reply "Go push the button on the bridge and then run register again (within 30s)"
74
+ else
75
+ raise
76
+ end
77
+ end
78
+ end
79
+
80
+ Lita.register_handler(self)
81
+
82
+ private
83
+ def str_to_hex(str)
84
+ CSSColor.parse(str).html
85
+ end
86
+
87
+ def color_to_str(color_obj)
88
+ c = color_obj.to_rgb
89
+ hex = ::Color::RGB.new(c.red, c.green, c.blue).html
90
+
91
+ if config.nodejs_color_lookup
92
+ @gem_root ||= File.expand_path "../../../..", __FILE__
93
+ name = %x{#{config.nodejs_invocation} #{@gem_root}/color_lookup.js "#{hex}"}.chomp
94
+
95
+ #reverse lookup for pedantry
96
+ if str_to_hex(name) != hex
97
+ name[/^/] = "~"
98
+ name += " (#{hex})"
99
+ end
100
+
101
+ name
102
+ else
103
+ hex
104
+ end
105
+ end
106
+
107
+ def bridge(res)
108
+ @bridge ||= HueLib.application
109
+ rescue HueLib::Error
110
+ res.reply "No hue configuration! run `hue register`"
111
+ raise
112
+ end
113
+
114
+ # cribbed from
115
+ # https://github.com/Veraticus/huey/blob/78e69d0e81fcc4436aa2e84e42eb6bfcc7c82855/lib/huey/bulb.rb#L80-L119
116
+ def hex_to_hsb(hex)
117
+ color = Color::RGB.from_html(hex)
118
+
119
+ # Manual calcuation is necessary here because of an error in the Color library
120
+ r = color.r
121
+ g = color.g
122
+ b = color.b
123
+ max = [r, g, b].max
124
+ min = [r, g, b].min
125
+ delta = max - min
126
+ v = max * 100
127
+
128
+ if (max != 0.0)
129
+ s = delta / max * 100
130
+ else
131
+ s = 0.0
132
+ end
133
+
134
+ if (s == 0.0)
135
+ h = 0.0
136
+ else
137
+ if (r == max)
138
+ h = (g - b) / delta
139
+ elsif (g == max)
140
+ h = 2 + (b - r) / delta
141
+ elsif (b == max)
142
+ h = 4 + (r - g) / delta
143
+ end
144
+
145
+ h *= 60.0
146
+
147
+ if (h < 0)
148
+ h += 360.0
149
+ end
150
+ end
151
+
152
+ hash = {}
153
+ hash[:hue] = (h * 182.04).round
154
+ hash[:sat] = (s / 100.0 * 255.0).round
155
+ hash[:bri] = (v / 100.0 * 255.0).round
156
+ hash
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "lita-hue"
3
+ spec.version = "0.1.0.pre"
4
+ spec.authors = ["Donald Guy"]
5
+ spec.email = ["donald.b.guy@gmail.com"]
6
+ spec.description = "Lita Plugin to control Phillips Hue Lights"
7
+ spec.summary = "Lita UI for hue-lib gem allowing control of Phillips Hue Lights"
8
+ spec.homepage = "http://github.com/donaldguy/lita-hue"
9
+ spec.metadata = { "lita_plugin_type" => "handler" }
10
+
11
+ spec.files = `git ls-files`.split($/)
12
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
13
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
14
+ spec.require_paths = ["lib"]
15
+
16
+ spec.add_dependency "hue-lib"
17
+ spec.add_dependency "css_color"
18
+
19
+ spec.add_runtime_dependency "lita", ">= 4.5"
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "pry-byebug"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rack-test"
25
+ spec.add_development_dependency "rspec", ">= 3.0.0"
26
+ end
@@ -0,0 +1,4 @@
1
+ en:
2
+ lita:
3
+ handlers:
4
+ hue:
@@ -0,0 +1,4 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Handlers::Hue, lita_handler: true do
4
+ end
@@ -0,0 +1,6 @@
1
+ require "lita-hue"
2
+ require "lita/rspec"
3
+
4
+ # A compatibility mode is provided for older plugins upgrading from Lita 3. Since this plugin
5
+ # was generated with Lita 4, the compatibility mode should be left disabled.
6
+ Lita.version_3_compatibility_mode = false
File without changes
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-hue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.pre
5
+ platform: ruby
6
+ authors:
7
+ - Donald Guy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hue-lib
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: css_color
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: lita
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '4.5'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '4.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rack-test
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 3.0.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 3.0.0
125
+ description: Lita Plugin to control Phillips Hue Lights
126
+ email:
127
+ - donald.b.guy@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".travis.yml"
134
+ - Gemfile
135
+ - README.md
136
+ - Rakefile
137
+ - color_lookup.js
138
+ - lib/lita-hue.rb
139
+ - lib/lita/handlers/hue.rb
140
+ - lita-hue.gemspec
141
+ - locales/en.yml
142
+ - spec/lita/handlers/hue_spec.rb
143
+ - spec/spec_helper.rb
144
+ - templates/.gitkeep
145
+ homepage: http://github.com/donaldguy/lita-hue
146
+ licenses: []
147
+ metadata:
148
+ lita_plugin_type: handler
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">"
161
+ - !ruby/object:Gem::Version
162
+ version: 1.3.1
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.4.5
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: Lita UI for hue-lib gem allowing control of Phillips Hue Lights
169
+ test_files:
170
+ - spec/lita/handlers/hue_spec.rb
171
+ - spec/spec_helper.rb