lifx_toys 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 419305ca0af3ed891f47ae577bcd4ca61549b68b
4
+ data.tar.gz: 914030d2268346c7f13e5241e6a215f7c75d9ba2
5
+ SHA512:
6
+ metadata.gz: bf594021af4849e189b4eec1ce997e4a3867f77a9c63641dbfc68c5763b3bea9f9995dea58e209c6ae4e268b60b069ed29e37225e8b4d03b77f585142069aca5
7
+ data.tar.gz: 3c8f291188318bed8fc6526534277c41e6f842f0f4b56b7980c1b9d3332ad8bc6490434b42b4e6d1aa025054c8733b017a8b85d85443440eeaed77655a21ae83
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lifx_toys.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Lucas Parry
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # LifxToys
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/lifx_toys`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'lifx_toys'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install lifx_toys
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lifx_toys.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ require "lifx_toys"
4
+ require "lifx_toys/clouds"
5
+ require "lifx_toys/colors"
6
+ require "lifx_toys/sunrise"
7
+
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.libs << "test"
10
+ t.libs << "lib"
11
+ t.test_files = FileList['test/**/*_test.rb']
12
+ end
13
+
14
+ task :default => :test
15
+
16
+
17
+ desc 'Cloud lighting simulation on all lights'
18
+ task :clouds do
19
+ LifxToys::Clouds.new.run
20
+ end
21
+
22
+ desc 'Cycle random Hue, Saturation and brightness on all lights'
23
+ task :colors do
24
+ LifxToys::Colors.new.run
25
+ end
26
+
27
+ desc '30 minute sunrise, 0-15M brightness:0-100% kelvin:2500, 15-30M kelvin:2500-9000'
28
+ task :sunrise do
29
+ LifxToys::Sunrise.new.run
30
+ end
31
+
32
+ task :info do
33
+ puts all_lights.get_info
34
+ end
35
+
36
+ task :off do
37
+ puts all_lights.set_power_state 'off'
38
+ end
39
+
40
+ task :on do
41
+ puts all_lights.set_power_state 'on'
42
+ end
43
+
44
+ task :toggle do
45
+ puts all_lights.toggle
46
+ end
47
+
48
+ def all_lights
49
+ LifxToys::HttpApi.with_default_selector('all')
50
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lifx_toys"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "pry"
14
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ require_relative "lifx_light"
2
+ module LifxToys
3
+ class Clouds
4
+
5
+ MIN_KELVIN = 2500
6
+ MAX_KELVIN = 9000
7
+
8
+ MIN_BRIGHTNESS = 10
9
+ MAX_BRIGHTNESS = 100
10
+
11
+ CYCLE_TIME = 5.0
12
+
13
+ def initialize
14
+ @lights = LifxLight.get_lights.select{|x| x.connected }
15
+ end
16
+
17
+ def run
18
+ while (true)
19
+ @lights.each do |light|
20
+ light.set_color("kelvin:#{random_kelvin} brightness:#{random_brightness}", duration: CYCLE_TIME* 1.5)
21
+ sleep(CYCLE_TIME/ @lights.count)
22
+ end
23
+ end
24
+ end
25
+
26
+ def random_kelvin
27
+ rand(MIN_KELVIN..MAX_KELVIN)
28
+ end
29
+
30
+ def random_brightness
31
+ rand(MIN_BRIGHTNESS..MAX_BRIGHTNESS).to_f / 100
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,37 @@
1
+ require_relative "lifx_light"
2
+
3
+ module LifxToys
4
+ class Colors
5
+
6
+ HUE_RANGE = 0..360
7
+ BRIGHTNESS_RANGE = 20..80
8
+ SATURATION_RANGE = 20..100
9
+
10
+ CYCLE_TIME = 10.0
11
+
12
+ def initialize
13
+ @lights = LifxLight.get_lights.select{|x| x.connected }
14
+ end
15
+
16
+ def run
17
+ while (true)
18
+ @lights.each do |light|
19
+ light.set_color("hue:#{random_hue} saturation:#{random_saturation} brightness:#{random_brightness}", duration: CYCLE_TIME* 1.5)
20
+ sleep(CYCLE_TIME/ @lights.count)
21
+ end
22
+ end
23
+ end
24
+
25
+ def random_hue
26
+ rand(HUE_RANGE)
27
+ end
28
+
29
+ def random_saturation
30
+ rand(SATURATION_RANGE).to_f / 100
31
+ end
32
+
33
+ def random_brightness
34
+ rand(BRIGHTNESS_RANGE).to_f / 100
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,30 @@
1
+ require_relative '../http_api'
2
+
3
+ module LifxToys
4
+ module HttpApi
5
+
6
+ class WithDefaultSelector
7
+
8
+ attr_reader :selector
9
+
10
+ def initialize(selector)
11
+ @selector = selector
12
+ end
13
+
14
+ private
15
+
16
+ def method_missing(method_name, *args)
17
+ if ::LifxToys::HttpApi.respond_to? method_name
18
+ ::LifxToys::HttpApi.public_send(method_name, selector, *args)
19
+ else
20
+ raise
21
+ end
22
+ end
23
+
24
+ def make_request(method_name, *args)
25
+ HttpApi.public_send(method_name, selector, *args)
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,106 @@
1
+ require 'httparty'
2
+ require_relative 'http_api/with_default_selector'
3
+
4
+ module LifxToys
5
+ module HttpApi
6
+ class << self
7
+
8
+ def with_default_selector(selector)
9
+ WithDefaultSelector.new(selector)
10
+ end
11
+
12
+ def set_color(selector, color, options = { duration: 2.0,
13
+ power_on: true})
14
+ HTTParty.put(color_url(selector),
15
+ headers: authorization_headers,
16
+ query: options.merge({
17
+ color: color
18
+ })).tap do |response|
19
+ if response.success?
20
+ puts "info: set #{selector} to #{color}" if ENV["DEBUG"]
21
+ nil
22
+ else
23
+ puts "warning: light status - #{response}"
24
+ -1
25
+ end
26
+ end
27
+ end
28
+
29
+ def get_info(selector)
30
+ HTTParty.get(info_url(selector),
31
+ headers: authorization_headers)
32
+ end
33
+
34
+ def set_power_state(selector, state, options = {duration: 2})
35
+ HTTParty.put(power_url(selector),
36
+ headers: authorization_headers,
37
+ query: options.merge({
38
+ state: state
39
+ }))
40
+ end
41
+
42
+ def toggle_power_state(selector)
43
+ HTTParty.post(toggle_power_url(selector),
44
+ headers: authorization_headers)
45
+ end
46
+
47
+ def breathe(selector, color, options: {
48
+ # from_color: current_bulb_color,
49
+ period: 1.0,
50
+ cycles: 1.0,
51
+ persist: false,
52
+ power_on: true,
53
+ peak: 0.5 })
54
+ HTTParty.post(breathe_url(selector),
55
+ headers: authorization_headers,
56
+ query: options.merge({
57
+ color: color
58
+ }))
59
+ end
60
+
61
+ def pulse(selector, color, options: {
62
+ # from_color: current_bulb_color,
63
+ period: 1.0,
64
+ cycles: 1.0,
65
+ persist: false,
66
+ power_on: true,
67
+ peak: 1.0 })
68
+ HTTParty.post(pulse_url(selector),
69
+ headers: authorization_headers,
70
+ query: options.merge({
71
+ color: color
72
+ }))
73
+ end
74
+
75
+ private
76
+
77
+ def info_url(selector)
78
+ "https://api.lifx.com/v1beta1/lights/#{selector}"
79
+ end
80
+
81
+ def color_url(selector)
82
+ "#{info_url(selector)}/color"
83
+ end
84
+
85
+ def power_url(selector)
86
+ "#{info_url(selector)}/power"
87
+ end
88
+
89
+ def toggle_power_url(selector)
90
+ "#{info_url(selector)}/toggle"
91
+ end
92
+
93
+ def breathe_url(selector)
94
+ "#{info_url(selector)}/effects/breathe"
95
+ end
96
+
97
+ def pulse_url(selector)
98
+ "#{info_url(selector)}/effects/pulse"
99
+ end
100
+
101
+ def authorization_headers
102
+ {"Authorization" => "Bearer #{ENV["LIFX_TOKEN"]}"}
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,42 @@
1
+ require 'forwardable'
2
+ require_relative 'http_api'
3
+
4
+ module LifxToys
5
+ class LifxLight
6
+
7
+ extend Forwardable
8
+ def_delegators :@data, :label, :connected, :power, :color, :brightness, :product_name, :last_seen, :seconds_since_seen
9
+ def_delegators :@http_api, :set_color
10
+
11
+ attr_reader :light_id
12
+
13
+ def self.get_lights
14
+ @lights ||= HttpApi.with_default_selector("all").get_info.map do |data|
15
+ LifxLight.new(data)
16
+ end
17
+ end
18
+
19
+ def initialize(data)
20
+ @light_id = data["id"]
21
+ @data = OpenStruct.new(data)
22
+ @updated_at = Time.now
23
+ @http_api = HttpApi.with_default_selector(selector)
24
+ end
25
+
26
+ def update_info
27
+ @data ||= OpenStruct.new(@http_api.get_info)
28
+ @updated_at = Time.now
29
+ end
30
+
31
+ def inspect
32
+ "#<#{self.class.name} light_id=#{light_id.inspect} label=#{label.inspect} connected=#{connected.inspect} power=#{power.inspect} color=#{color.inspect} brightness=#{brightness.inspect} product_name=#{product_name.inspect} last_seen=#{last_seen.inspect} seconds_since_seen=#{seconds_since_seen.inspect} @updated_at=#{@updated_at}>"
33
+ end
34
+
35
+ private
36
+
37
+ def selector
38
+ "id:#{light_id}"
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,49 @@
1
+ require_relative "lifx_light"
2
+ module LifxToys
3
+ class Sunrise
4
+
5
+ MIN_KELVIN = 2500
6
+ MAX_KELVIN = 9000
7
+
8
+ MIN_BRIGHTNESS = 0.0
9
+ MAX_BRIGHTNESS = 1.0
10
+
11
+ SUNRISE_TIME = 30 * # minutes
12
+ 60.0 # seconds
13
+
14
+ attr_reader :sunrise_time,
15
+ :start_brightness,
16
+ :start_temperature,
17
+ :end_brightness,
18
+ :end_temperature
19
+
20
+ def initialize(selector = 'all',
21
+ sunrise_time = SUNRISE_TIME,
22
+ start_brightness = MIN_BRIGHTNESS,
23
+ start_temperature = MIN_KELVIN,
24
+ end_brightness = MAX_BRIGHTNESS,
25
+ end_temperature = MAX_KELVIN)
26
+
27
+ @sunrise_time = sunrise_time
28
+ @start_brightness = start_brightness
29
+ @start_temperature = start_temperature
30
+ @end_brightness = end_brightness
31
+ @end_temperature = end_temperature
32
+
33
+ @lights = HttpApi.with_default_selector(selector)
34
+ end
35
+
36
+ # warning: this method takes half the sunrise time to execute
37
+ def run
38
+ # pre-set light color to avoid green at low brightness
39
+ @lights.set_color("brightness:#{start_brightness} kelvin:#{start_temperature}", duration: 0, power_on: false)
40
+ @lights.set_color("brightness:#{end_brightness}", duration: half_sunrise_time)
41
+ sleep(half_sunrise_time)
42
+ @lights.set_color("kelvin:#{end_temperature}", duration: half_sunrise_time)
43
+ end
44
+
45
+ def half_sunrise_time
46
+ sunrise_time / 2
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module LifxToys
2
+ VERSION = "0.1.0"
3
+ end
data/lib/lifx_toys.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "lifx_toys/version"
2
+ require "lifx_toys/http_api"
3
+ require "lifx_toys/lifx_light"
4
+
5
+ module LifxToys
6
+ # Your code goes here...
7
+ end
data/lifx_toys.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lifx_toys/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lifx_toys"
8
+ spec.version = LifxToys::VERSION
9
+ spec.authors = ["Lucas Parry"]
10
+ spec.email = ["lparry@gmail.com"]
11
+
12
+ spec.summary = %q{Lucas's LIFX toys}
13
+ spec.description = %q{ruby wrapper and toys for the LIFX cloud api}
14
+ spec.homepage = "https://github.com/lparry/lifx_toys"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.7"
25
+ spec.add_development_dependency "pry", "~> 0.10"
26
+ spec.add_dependency "json", "~> 1.8"
27
+ spec.add_dependency "httparty", "~> 0.13"
28
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lifx_toys
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lucas Parry
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.8'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.8'
83
+ - !ruby/object:Gem::Dependency
84
+ name: httparty
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.13'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.13'
97
+ description: ruby wrapper and toys for the LIFX cloud api
98
+ email:
99
+ - lparry@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".travis.yml"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - bin/console
111
+ - bin/setup
112
+ - lib/lifx_toys.rb
113
+ - lib/lifx_toys/clouds.rb
114
+ - lib/lifx_toys/colors.rb
115
+ - lib/lifx_toys/http_api.rb
116
+ - lib/lifx_toys/http_api/with_default_selector.rb
117
+ - lib/lifx_toys/lifx_light.rb
118
+ - lib/lifx_toys/sunrise.rb
119
+ - lib/lifx_toys/version.rb
120
+ - lifx_toys.gemspec
121
+ homepage: https://github.com/lparry/lifx_toys
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.2.2
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Lucas's LIFX toys
145
+ test_files: []
146
+ has_rdoc: