yeelight-lamp-client 0.1.0

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: 4daf27a8b30269c578aee1962bdd700e94abe886
4
+ data.tar.gz: 921511f4f0a975f2d530ab05ddaed32731fd7c07
5
+ SHA512:
6
+ metadata.gz: 2a973ab724c5a149d027f9c268ff1575ca9e033b509257c4effc4bd17038685416f25267f8954bc0eacd34bec1444bc7d7e762fafcaa7e48a78f5c475a41f69e
7
+ data.tar.gz: '08c983beb6227e9585a7ff4a4b886ab667f3a778ff3be1fa7a19705c0b32f9cfa143b2890297120edf4585c1e25a114fc7bab0812dff3f56bb9ab64fe360f87a'
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.16.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at antondavydov.o@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in yeelight-lamp-client.gemspec
6
+ gemspec
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ yeelight-lamp-client (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.11.0)
10
+ rake (10.5.0)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ bundler (~> 1.16)
17
+ minitest (~> 5.0)
18
+ rake (~> 10.0)
19
+ yeelight-lamp-client!
20
+
21
+ BUNDLED WITH
22
+ 1.16.0
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Anton Davydov
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.
@@ -0,0 +1,51 @@
1
+ # Yeelight::Lamp::Client
2
+
3
+ Simple client for Yeelight lamps
4
+
5
+ Inspired by:
6
+ https://github.com/nunows/Yeelight-Wifi
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'yeelight-lamp-client'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install yeelight-lamp-client
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ require_relative './lib/yeelight'
28
+
29
+ GREEN = '5701376'
30
+ RED = '16713472'
31
+
32
+ client = Yeelight::Client.new(LAMP_IP_ADDRESS, 55443)
33
+ client.get_prop('"ct"') # => JSON with props
34
+
35
+ lamp = Yeelight::Lamp.new(client)
36
+ lamp.toggle_color(GREEN, 2)
37
+ sleep 1
38
+ lamp.toggle_color(RED, 2)
39
+ ```
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/davydovanton/yeelight-lamp-client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
48
+
49
+ ## Code of Conduct
50
+
51
+ Everyone interacting in the Yeelight::Lamp::Client project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/yeelight-lamp-client/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "yeelight/lamp/client"
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 "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ require "yeelight/client"
2
+ require "yeelight/lamp"
3
+ require "yeelight/version"
4
+
5
+ module Yeelight
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,182 @@
1
+ require 'socket'
2
+ require 'json'
3
+ require 'timeout'
4
+
5
+ module Yeelight
6
+ class Client
7
+ def initialize(host, port)
8
+ @host = host
9
+ @port = port
10
+ end
11
+
12
+ def request(cmd)
13
+ begin
14
+ s = TCPSocket.open(@host, @port)
15
+ s.puts cmd
16
+ data = s.gets.chomp
17
+ s.close
18
+ response(data)
19
+ rescue Exception => msg
20
+ response(JSON.generate({:exception => msg}))
21
+ end
22
+ end
23
+
24
+ def response(data)
25
+ json = JSON.parse(data)
26
+ # create a standard response message
27
+ result = {
28
+ :status => json['result'] ? true : false,
29
+ :data => json
30
+ }
31
+ JSON.generate(result)
32
+ end
33
+
34
+ private :request, :response
35
+
36
+ # This method is used to retrieve current property of smart LED.
37
+ def get_prop(values)
38
+ cmd = "{\"id\":1,\"method\":\"get_prop\",\"params\":[#{values}]}\r\n"
39
+ request(cmd)
40
+ end
41
+
42
+ # This method is used to change the color temperature of a smart LED.
43
+ def set_ct_abx(ct_value, effect, duration)
44
+ cmd = "{\"id\":2,\"method\":\"set_ct_abx\",\"params\":[#{ct_value},\"#{effect}\",#{duration}]}\r\n"
45
+ request(cmd)
46
+ end
47
+
48
+ # This method is used to change the color RGB of a smart LED.
49
+ def set_rgb(rgb_value, effect, duration)
50
+ cmd = "{\"id\":3,\"method\":\"set_rgb\",\"params\":[#{rgb_value},\"#{effect}\",#{duration}]}\r\n"
51
+ request(cmd)
52
+ end
53
+
54
+ # This method is used to change the color HSV of a smart LED.
55
+ def set_hsv(hue, sat, effect, duration)
56
+ cmd = "{\"id\":4,\"method\":\"set_hsv\",\"params\":[#{hue},#{sat},\"#{effect}\",#{duration}]}\r\n"
57
+ request(cmd)
58
+ end
59
+
60
+ # This method is used to change the brightness of a smart LED.
61
+ def set_bright(brightness, effect, duration)
62
+ cmd = "{\"id\":5,\"method\":\"set_bright\",\"params\":[#{brightness},\"#{effect}\",#{duration}]}\r\n"
63
+ request(cmd)
64
+ end
65
+
66
+ # This method is used to switch on or off the smart LED (software managed on/off).
67
+ def set_power(power, effect, duration)
68
+ cmd = "{\"id\":6,\"method\":\"set_power\",\"params\":[\"#{power}\",\"#{effect}\",#{duration}]}\r\n"
69
+ request(cmd)
70
+ end
71
+
72
+ # This method is used to toggle the smart LED.
73
+ def toggle
74
+ cmd = "{\"id\":7,\"method\":\"toggle\",\"params\":[]}\r\n"
75
+ request(cmd)
76
+ end
77
+
78
+ # This method is used to save current state of smart LED in persistent memory.
79
+ # So if user powers off and then powers on the smart LED again (hard power reset),
80
+ # the smart LED will show last saved state.
81
+ # Note: The "automatic state saving" must be turn off
82
+ def set_default
83
+ cmd = "{\"id\":8,\"method\":\"set_default\",\"params\":[]}\r\n"
84
+ request(cmd)
85
+ end
86
+
87
+ # This method is used to start a color flow. Color flow is a series of smart
88
+ # LED visible state changing. It can be brightness changing, color changing
89
+ # or color temperature changing
90
+ def start_cf(count, action, flow_expression)
91
+ cmd = "{\"id\":9,\"method\":\"set_power\",\"params\":[#{count},#{action},\"#{flow_expression}\"]}\r\n"
92
+ request(cmd)
93
+ end
94
+
95
+ # This method is used to stop a running color flow.
96
+ def stop_cf
97
+ cmd = "{\"id\":10,\"method\":\"stop_cf\",\"params\":[]}\r\n"
98
+ request(cmd)
99
+ end
100
+
101
+ # This method is used to set the smart LED directly to specified state. If
102
+ # the smart LED is off, then it will turn on the smart LED firstly and then
103
+ # apply the specified scommand.
104
+ def set_scene(classe, val1, val2)
105
+ cmd = "{\"id\":11,\"method\":\"set_scene\",\"params\":[\"#{classe}\",#{val1},#{val2}]}\r\n"
106
+ request(cmd)
107
+ end
108
+
109
+ # This method is used to start a timer job on the smart LED
110
+ def cron_add(type, value)
111
+ cmd = "{\"id\":12,\"method\":\"cron_add\",\"params\":[#{type},#{value}]}\r\n"
112
+ request(cmd)
113
+ end
114
+
115
+ # This method is used to retrieve the setting of the current cron job
116
+ # of the specified type
117
+ def cron_get(type)
118
+ cmd = "{\"id\":13,\"method\":\"cron_get\",\"params\":[#{type}]}\r\n"
119
+ request(cmd)
120
+ end
121
+
122
+ # This method is used to stop the specified cron job.
123
+ def cron_del(type)
124
+ cmd = "{\"id\":14,\"method\":\"cron_del\",\"params\":[#{type}]}\r\n"
125
+ request(cmd)
126
+ end
127
+
128
+ # This method is used to change brightness, CT or color of a smart LED
129
+ # without knowing the current value, it's main used by controllers.
130
+ def set_adjust(action, prop)
131
+ cmd = "{\"id\":15,\"method\":\"set_adjust\",\"params\":[\"#{action}\",\"#{prop}\"]}\r\n"
132
+ request(cmd)
133
+ end
134
+
135
+ # This method is used to name the device. The name will be stored on the
136
+ # device and reported in discovering response. User can also read the name
137
+ # through “get_prop” method.
138
+ def set_name(name)
139
+ cmd = "{\"id\":16,\"method\":\"set_name\",\"params\":[\"#{name}\"]}\r\n"
140
+ request(cmd)
141
+ end
142
+
143
+ # This method is used to switch on the smart LED
144
+ def on
145
+ set_power("on", "smooth",1000)
146
+ end
147
+
148
+ # This method is used to switch off the smart LED
149
+ def off
150
+ set_power("off", "smooth",1000)
151
+ end
152
+
153
+
154
+ # This method is used to discover a smart LED in the network
155
+ def self.discover
156
+ host = "239.255.255.250"
157
+ port = 1982
158
+ socket = UDPSocket.new(Socket::AF_INET)
159
+
160
+ payload = []
161
+ payload << "M-SEARCH * HTTP/1.1\r\n"
162
+ payload << "HOST: #{host}:#{port}\r\n"
163
+ payload << "MAN: \"ssdp:discover\"\r\n"
164
+ payload << "ST: wifi_bulb"
165
+
166
+ socket.send(payload.join(), 0, host, port)
167
+
168
+ devices = []
169
+ begin
170
+ Timeout.timeout(2) do
171
+ loop do
172
+ devices << socket.recvfrom(2048)
173
+ end
174
+ end
175
+ rescue Timeout::Error => ex
176
+ ex
177
+ end
178
+
179
+ devices
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,30 @@
1
+ module Yeelight
2
+ class Lamp
3
+ VALUES = '"rgb", "ct", "color_mode", "hue", "sat"'
4
+
5
+ attr_reader :client
6
+
7
+ def initialize(client)
8
+ @client = client
9
+ end
10
+
11
+ def toggle_color(color_hex, duration = 1)
12
+ color = "0x#{color_hex}".to_i(16)
13
+
14
+ rgb, ct, color_mode, hue, sat = JSON.parse(client.get_prop(VALUES))['data']['result']
15
+
16
+ client.set_rgb(color, 'smooth', 500)
17
+ sleep duration
18
+
19
+ case color_mode.to_i
20
+ when 1
21
+ client.set_rgb(rgb, 'smooth', 500)
22
+ when 2
23
+ client.set_ct_abx(ct, 'smooth', 500)
24
+ when 3
25
+ client.set_hsv(hue, sat, 'smooth', 500)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,3 @@
1
+ module Yeelight
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,26 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "yeelight/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "yeelight-lamp-client"
8
+ spec.version = Yeelight::VERSION
9
+ spec.authors = ["Anton Davydov"]
10
+ spec.email = ["antondavydov.o@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby client for yeelight lamps}
13
+ spec.homepage = "https://github.com/davydovanton/yeelight-lamp-client"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yeelight-lamp-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anton Davydov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description:
56
+ email:
57
+ - antondavydov.o@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/yeelight.rb
73
+ - lib/yeelight/client.rb
74
+ - lib/yeelight/lamp.rb
75
+ - lib/yeelight/version.rb
76
+ - yeelight-lamp-client.gemspec
77
+ homepage: https://github.com/davydovanton/yeelight-lamp-client
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.6.13
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Ruby client for yeelight lamps
101
+ test_files: []