lightscape 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +75 -0
- data/Rakefile +1 -0
- data/lib/lightscape.rb +32 -0
- data/lib/lightscape/light.rb +60 -0
- data/lib/lightscape/version.rb +3 -0
- data/lightscape.gemspec +27 -0
- data/spec/light_spec.rb +69 -0
- data/spec/spec_helper.rb +8 -0
- metadata +141 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.9.3
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Hashrocket Workstation
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# Lightscape
|
2
|
+
|
3
|
+
## Overview
|
4
|
+
|
5
|
+
Warning: This is a brand new gem currently, and is likely to undergo
|
6
|
+
significant changes in the near future. It also does not include any help with
|
7
|
+
finding your bridge's IP address, or getting an API user set up, you'll have to
|
8
|
+
figure that out yourself. Support for those features is coming soon.
|
9
|
+
|
10
|
+
Lightscape is an API wrapper for the Phillips Hue light bulb bridge. The goal
|
11
|
+
of this library is to simplify some of the API and make it easier to manage
|
12
|
+
scenes and lighting presets programatically. There is also an existing gem
|
13
|
+
(Hue)[https://github.com/soffes/hue] that is a more direct adapter for the same
|
14
|
+
API.
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
gem 'lightscape'
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install lightscape
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
Specify which bridge you'd like to use via environment:
|
33
|
+
|
34
|
+
export BRIDGE=192.168.10.100
|
35
|
+
|
36
|
+
Specify a user or use the default "rubyclient"
|
37
|
+
|
38
|
+
export HUE_USER="myusername"
|
39
|
+
|
40
|
+
Find a light, and change it:
|
41
|
+
|
42
|
+
light = Lightscape::Light.find(1)
|
43
|
+
light.color = :blue
|
44
|
+
light.brightness = 255 # Range of (0..255), 0 is _NOT_ off
|
45
|
+
light.transition_time = 10 # 10 = (10 * 100ms) or 1 second
|
46
|
+
light.update! # Raises the JSON string on failure, or returns true
|
47
|
+
|
48
|
+
light.on! # Turn on immediately
|
49
|
+
light.off! # Turn off immediately
|
50
|
+
|
51
|
+
## Colors:
|
52
|
+
|
53
|
+
In order to get a basic pass at this going, color support is limited to pre-set
|
54
|
+
colors. This will definitely change as the gem develops.
|
55
|
+
|
56
|
+
See `Lightscape::COLORS.keys` for a list of colors
|
57
|
+
|
58
|
+
## Coming Soon:
|
59
|
+
|
60
|
+
* Setup flow (Currently, you must create a user manually)
|
61
|
+
* Better methods for setting bridge, user config options
|
62
|
+
* More colors, and a way to specify your own
|
63
|
+
* CLI
|
64
|
+
* Alerting
|
65
|
+
* Better access to current attributes
|
66
|
+
* Ability to create/load presets that describe light settings, then apply them to lights by name
|
67
|
+
* Ability to create/load a "scene" using presets mentioned
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
1. Fork it
|
72
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
73
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
74
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
75
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/lightscape.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require "lightscape/version"
|
2
|
+
require "lightscape/light"
|
3
|
+
|
4
|
+
module Lightscape
|
5
|
+
module Client
|
6
|
+
extend self
|
7
|
+
attr_writer :bridge, :user
|
8
|
+
|
9
|
+
def base
|
10
|
+
@base ||= RestClient::Resource.new "http://#{bridge}/api/#{user}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def bridge
|
14
|
+
@bridge ||= ENV.fetch("BRIDGE")
|
15
|
+
end
|
16
|
+
|
17
|
+
def user
|
18
|
+
@user ||= ENV.fetch("HUE_USER", "rubyclient")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Red: 0.675, 0.322
|
23
|
+
# Green: 0.4091, 0.518
|
24
|
+
# Blue: 0.167, 0.04
|
25
|
+
|
26
|
+
COLORS = {
|
27
|
+
red: [0.675, 0.322],
|
28
|
+
green: [0.4077, 0.5154],
|
29
|
+
blue: [0.167, 0.04],
|
30
|
+
white: [0.4440, 0.3500]
|
31
|
+
}.tap { |hsh| hsh.default [0.4440, 0.3500] }
|
32
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'rest_client'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Lightscape
|
5
|
+
class Light
|
6
|
+
def self.find number
|
7
|
+
new(number)
|
8
|
+
end
|
9
|
+
|
10
|
+
# The API client interface to the light
|
11
|
+
attr_reader :client
|
12
|
+
|
13
|
+
# A hash of pending changes to the light
|
14
|
+
# E.g. { on: true }
|
15
|
+
attr_accessor :pending_changes
|
16
|
+
|
17
|
+
def initialize number
|
18
|
+
@client = Lightscape::Client.base["lights/#{number}"]
|
19
|
+
@pending_changes = {}
|
20
|
+
end
|
21
|
+
|
22
|
+
def on= boolean
|
23
|
+
pending_changes[:on] = boolean
|
24
|
+
end
|
25
|
+
|
26
|
+
def on!
|
27
|
+
self.on = true
|
28
|
+
update!
|
29
|
+
end
|
30
|
+
|
31
|
+
def off!
|
32
|
+
self.on = false
|
33
|
+
update!
|
34
|
+
end
|
35
|
+
|
36
|
+
def color= symbol
|
37
|
+
self.on = true
|
38
|
+
pending_changes[:xy] = COLORS[symbol]
|
39
|
+
end
|
40
|
+
|
41
|
+
def transition_time= duration
|
42
|
+
pending_changes[:transitiontime] = duration
|
43
|
+
end
|
44
|
+
|
45
|
+
def brightness= number
|
46
|
+
self.on = true
|
47
|
+
pending_changes[:bri] = [0, [number, 255].min].max
|
48
|
+
end
|
49
|
+
|
50
|
+
# If you really want to use the same interface as the API
|
51
|
+
alias_method :bri=, :brightness=
|
52
|
+
|
53
|
+
def update!
|
54
|
+
response = client["/state"].put pending_changes.to_json
|
55
|
+
pending_changes = {}
|
56
|
+
raise response.to_str unless response =~ /success/
|
57
|
+
true
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lightscape.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'lightscape/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "lightscape"
|
8
|
+
spec.version = Lightscape::VERSION
|
9
|
+
spec.authors = ["Dave Lyon"]
|
10
|
+
spec.email = ["dave@davelyon.net"]
|
11
|
+
spec.description = %q{An API adapter for Phillips Hue lighting system}
|
12
|
+
spec.summary = %q{An API adapter for Phillips Hue lighting system}
|
13
|
+
spec.homepage = "https://github.com/davelyon/lightscape"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "rest-client", "~> 1.6"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
spec.add_development_dependency "pry"
|
27
|
+
end
|
data/spec/light_spec.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Lightscape::Light do
|
4
|
+
describe "getting the current state" do
|
5
|
+
end
|
6
|
+
|
7
|
+
describe "updating the light state" do
|
8
|
+
let(:light) { described_class.find(2) }
|
9
|
+
|
10
|
+
describe "#on!" do
|
11
|
+
it "turns the light on immediately" do
|
12
|
+
expect(light.on!).to be_true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#off!" do
|
17
|
+
it "turns the light off immediately" do
|
18
|
+
expect(light.off!).to be_true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#on=" do
|
23
|
+
it "turns the light on" do
|
24
|
+
light.on = true
|
25
|
+
expect(light.update!).to be_true
|
26
|
+
end
|
27
|
+
|
28
|
+
it "turns the light off" do
|
29
|
+
light.on = false
|
30
|
+
expect(light.update!).to be_true
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#color=" do
|
35
|
+
it "turns the light on if the color is changed" do
|
36
|
+
light.off!
|
37
|
+
light.color = :fuscia
|
38
|
+
expect(light.update!).to be_true
|
39
|
+
end
|
40
|
+
|
41
|
+
it "can change the color by name" do
|
42
|
+
light.color = :fuscia
|
43
|
+
expect(light.update!).to be_true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "changing brightness" do
|
48
|
+
|
49
|
+
it "can change the brightness" do
|
50
|
+
light.brightness = 100
|
51
|
+
expect(light.update!).to be_true
|
52
|
+
end
|
53
|
+
|
54
|
+
context "when the value given is larger than 255" do
|
55
|
+
it "uses 255" do
|
56
|
+
light.brightness = 300
|
57
|
+
expect(light.pending_changes[:bri]).to eq(255)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context "when the value given is negative" do
|
62
|
+
it "uses 0" do
|
63
|
+
light.brightness = -1
|
64
|
+
expect(light.pending_changes[:bri]).to eq(0)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lightscape
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dave Lyon
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-09-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rest-client
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.6'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.6'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: pry
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: An API adapter for Phillips Hue lighting system
|
95
|
+
email:
|
96
|
+
- dave@davelyon.net
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- .rspec
|
103
|
+
- .ruby-version
|
104
|
+
- Gemfile
|
105
|
+
- LICENSE.txt
|
106
|
+
- README.md
|
107
|
+
- Rakefile
|
108
|
+
- lib/lightscape.rb
|
109
|
+
- lib/lightscape/light.rb
|
110
|
+
- lib/lightscape/version.rb
|
111
|
+
- lightscape.gemspec
|
112
|
+
- spec/light_spec.rb
|
113
|
+
- spec/spec_helper.rb
|
114
|
+
homepage: https://github.com/davelyon/lightscape
|
115
|
+
licenses:
|
116
|
+
- MIT
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ! '>='
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
129
|
+
requirements:
|
130
|
+
- - ! '>='
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
requirements: []
|
134
|
+
rubyforge_project:
|
135
|
+
rubygems_version: 1.8.23
|
136
|
+
signing_key:
|
137
|
+
specification_version: 3
|
138
|
+
summary: An API adapter for Phillips Hue lighting system
|
139
|
+
test_files:
|
140
|
+
- spec/light_spec.rb
|
141
|
+
- spec/spec_helper.rb
|