switchbot 0.5.1 → 0.7.0

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
  SHA256:
3
- metadata.gz: b35104bc79a51e707f6a2295423a135e5ee06380eb4f5b2ad14b1fea15a72288
4
- data.tar.gz: d01885018b1ff8759a6ac2c8b1e2c14704d2e15105b282e74c24cc2a937a8f84
3
+ metadata.gz: e5dbe643e1c26eafa1c841dcf3cffb4e42ec799bba387b139fdd9d72e630ec6f
4
+ data.tar.gz: 2e243ec43219aab5fdbaf3dd4f20291676d7bdfebd7ee0ccdaee32b6c7cd21a4
5
5
  SHA512:
6
- metadata.gz: a3ecbbc03b18cbf6e7650f0171b645055a25eb541f1115b689fb7cde2a34a622f2f6ee898944fd45dacbd6cc6ebbbb0b0478bcdfb0e9e6703938fe24716c76e9
7
- data.tar.gz: 6c0fcb55db5bb61d78ed247dc2dc9c40a269dc4bed68a27832b1a22f74a718d68055f1fa40483603fc33c771be6f77ff0cd4980deee43f89b9928703162ac7bd
6
+ metadata.gz: '0468a22f7a997c01e036c473e81af30e6832a8e39fdfb0acbd0155904f19459b20d5944247de05eb165cf98b810844adaf554a6224113ad95a5b12f5b518b5ba'
7
+ data.tar.gz: 2a434cdcfd1956789526ad7f925871ccae53c7eb9c496b1a909a16cd2f6535b751e1d32abd5a79d3d1c9d4ba05d6306caf0c55d3c40c224bb3b988a227c093f0
@@ -8,7 +8,7 @@ jobs:
8
8
  strategy:
9
9
  matrix:
10
10
  # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
11
- ruby: [2.5, 2.6, 2.7, '3.0', 3.1]
11
+ ruby: [2.6, 2.7, '3.0', 3.1]
12
12
  name: Ruby ${{ matrix.ruby }}
13
13
  steps:
14
14
  - uses: actions/checkout@v2
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.5
2
+ TargetRubyVersion: 2.6
3
3
  NewCops: enable
4
4
 
5
5
  Style/Documentation:
data/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  ## Unreleased
2
- [full changelog](http://github.com/ytkg/switchbot/compare/v0.5.1...main)
2
+ [full changelog](http://github.com/ytkg/switchbot/compare/v0.7.0...main)
3
+
4
+ ## v0.7.0
5
+ [full changelog](http://github.com/ytkg/switchbot/compare/v0.6.0...v0.7.0)
6
+
7
+ * Add Lock
8
+ * https://github.com/ytkg/switchbot/pull/16
9
+ * :bomb: **[BREAKING CHANGE]** Support SwitchBot API v1.1
10
+ * https://github.com/ytkg/switchbot/pull/15
11
+ * The authentication method has been changed.
12
+
13
+ ```ruby
14
+ client = Switchbot::Client.new('YOUR_TOKEN', 'YOUR_SECRET')
15
+ ```
16
+
17
+ You must update the app to the latest version, V6.14 or later, in order to get the secret key.
18
+
19
+ ## v0.6.0
20
+ [full changelog](http://github.com/ytkg/switchbot/compare/v0.5.1...v0.6.0)
21
+
22
+ * Add ColorBulb
23
+ * https://github.com/ytkg/switchbot/pull/14
24
+ * :bomb: **[BREAKING CHANGE]** Update faraday v2 and Drop support ruby 2.5
25
+ * https://github.com/ytkg/switchbot/pull/12
3
26
 
4
27
  ## v0.5.1
5
28
  [full changelog](http://github.com/ytkg/switchbot/compare/v0.5.0...v0.5.1)
@@ -0,0 +1,124 @@
1
+ # Switchbot
2
+
3
+ [SwichBot API](https://github.com/OpenWonderLabs/SwitchBotAPI) client for Ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'switchbot'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install switchbot
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'switchbot'
25
+
26
+ client = Switchbot::Client.new('YOUR_TOKEN')
27
+ ```
28
+
29
+ ### Get device list
30
+ GET https://api.switch-bot.com/v1.0/devices
31
+ ```ruby
32
+ client.devices
33
+ #=> {:status_code=>100,
34
+ # :body=>
35
+ # {:device_list=>
36
+ # [{:device_id=>"500291B269BE",
37
+ # :device_name=>"Living Room Humidifier",
38
+ # :device_type=>"Humidifier",
39
+ # :enable_cloud_service=>true,
40
+ # :hub_device_id=>"000000000000"}],
41
+ # :infrared_remote_list=>
42
+ # [{:device_id=>"02-202008110034-13",
43
+ # :device_name=>"Living Room TV",
44
+ # :remote_type=>"TV",
45
+ # :hub_device_id=>"FA7310762361"}]},
46
+ # :message=>"success"}
47
+ ```
48
+
49
+ ### Get device status
50
+ GET https://api.switch-bot.com/v1.0/devices/C271111EC0AB/status
51
+ ```ruby
52
+ client.status(device_id: 'C271111EC0AB')
53
+ # or
54
+ client.device('C271111EC0AB').status
55
+ #=> {:status_code=>100,
56
+ # :body=>
57
+ # {:device_id=>"C271111EC0AB",
58
+ # :device_type=>"Meter",
59
+ # :hub_device_id=>"FA7310762361",
60
+ # :humidity=>52,
61
+ # :temperature=>26.1},
62
+ # :message=>"success"}
63
+ ```
64
+
65
+ ### Send device control commands
66
+ POST https://api.switch-bot.com/v1.0/devices/210/commands
67
+ ```ruby
68
+ client.commands(device_id: '210', command: 'turnOn')
69
+ # or
70
+ client.device('C271111EC0AB').commands(command: 'turnOn')
71
+ # or
72
+ client.device('C271111EC0AB').on
73
+ #=> {:status_code=>100,
74
+ # :body=>{},
75
+ # :message=>"success"}
76
+ ```
77
+
78
+ ### Get scene list
79
+ GET https://api.switch-bot.com/v1.0/scenes
80
+ ```ruby
81
+ client.scenes
82
+ #=> {:status_code=>100,
83
+ # :body=>
84
+ # [{:scene_id=>"T02-20200804130110",
85
+ # :scene_name=>"Close Office Devices"},
86
+ # {:scene_id=>"T02-202009221414-48924101",
87
+ # :scene_name=>"Set Office AC to 25"},
88
+ # {:scene_id=>"T02-202011051830-39363561",
89
+ # :scene_name=>"Set Bedroom to 24"},
90
+ # {:scene_id=>"T02-202011051831-82928991",
91
+ # :scene_name=>"Turn off home devices"},
92
+ # {:scene_id=>"T02-202011062059-26364981",
93
+ # :scene_name=>"Set Bedroom to 26 degree"}],
94
+ # :message=>"success"}
95
+ ```
96
+
97
+ ### Execute manual scenes
98
+ POST https://api.switch-bot.com/v1.0/scenes/T02-202009221414-48924101/execute
99
+ ```ruby
100
+ client.execute(scene_id: 'T02-202009221414-48924101')
101
+ # or
102
+ client.scene('T02-202009221414-48924101').execute
103
+ #=> {:status_code=>100,
104
+ # :body=>{},
105
+ # :message=>"success"}
106
+ ```
107
+
108
+ ## Development
109
+
110
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
111
+
112
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
113
+
114
+ ## Contributing
115
+
116
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/switchbot. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/switchbot/blob/master/CODE_OF_CONDUCT.md).
117
+
118
+ ## License
119
+
120
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
121
+
122
+ ## Code of Conduct
123
+
124
+ Everyone interacting in the Switchbot project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/switchbot/blob/master/CODE_OF_CONDUCT.md).
data/README.md CHANGED
@@ -19,15 +19,16 @@ Or install it yourself as:
19
19
  $ gem install switchbot
20
20
 
21
21
  ## Usage
22
-
23
22
  ```ruby
24
23
  require 'switchbot'
25
24
 
26
- client = Switchbot::Client.new('YOUR_TOKEN')
25
+ client = Switchbot::Client.new('YOUR_TOKEN', 'YOUR_SECRET')
27
26
  ```
28
27
 
28
+ For v0.6.0 and below, refer to README-v0.6.0-and-below.md.
29
+
29
30
  ### Get device list
30
- GET https://api.switch-bot.com/v1.0/devices
31
+ GET https://api.switch-bot.com/v1.1/devices
31
32
  ```ruby
32
33
  client.devices
33
34
  #=> {:status_code=>100,
@@ -47,7 +48,7 @@ client.devices
47
48
  ```
48
49
 
49
50
  ### Get device status
50
- GET https://api.switch-bot.com/v1.0/devices/C271111EC0AB/status
51
+ GET https://api.switch-bot.com/v1.1/devices/C271111EC0AB/status
51
52
  ```ruby
52
53
  client.status(device_id: 'C271111EC0AB')
53
54
  # or
@@ -63,7 +64,7 @@ client.device('C271111EC0AB').status
63
64
  ```
64
65
 
65
66
  ### Send device control commands
66
- POST https://api.switch-bot.com/v1.0/devices/210/commands
67
+ POST https://api.switch-bot.com/v1.1/devices/210/commands
67
68
  ```ruby
68
69
  client.commands(device_id: '210', command: 'turnOn')
69
70
  # or
@@ -76,7 +77,7 @@ client.device('C271111EC0AB').on
76
77
  ```
77
78
 
78
79
  ### Get scene list
79
- GET https://api.switch-bot.com/v1.0/scenes
80
+ GET https://api.switch-bot.com/v1.1/scenes
80
81
  ```ruby
81
82
  client.scenes
82
83
  #=> {:status_code=>100,
@@ -95,7 +96,7 @@ client.scenes
95
96
  ```
96
97
 
97
98
  ### Execute manual scenes
98
- POST https://api.switch-bot.com/v1.0/scenes/T02-202009221414-48924101/execute
99
+ POST https://api.switch-bot.com/v1.1/scenes/T02-202009221414-48924101/execute
99
100
  ```ruby
100
101
  client.execute(scene_id: 'T02-202009221414-48924101')
101
102
  # or
@@ -3,15 +3,17 @@
3
3
  module Switchbot
4
4
  class Client
5
5
  API_ENDPOINT = 'https://api.switch-bot.com'
6
+ API_VERSION = 'v1.1'
6
7
 
7
- def initialize(token)
8
+ def initialize(token, secret)
8
9
  @token = token
10
+ @secret = secret
9
11
  end
10
12
 
11
13
  def devices
12
14
  request(
13
15
  http_method: :get,
14
- endpoint: 'v1.0/devices'
16
+ endpoint: "/#{API_VERSION}/devices"
15
17
  )
16
18
  end
17
19
 
@@ -22,14 +24,14 @@ module Switchbot
22
24
  def status(device_id:)
23
25
  request(
24
26
  http_method: :get,
25
- endpoint: "/v1.0/devices/#{device_id}/status"
27
+ endpoint: "/#{API_VERSION}/devices/#{device_id}/status"
26
28
  )
27
29
  end
28
30
 
29
31
  def commands(device_id:, command:, parameter: 'default', command_type: 'command')
30
32
  request(
31
33
  http_method: :post,
32
- endpoint: "/v1.0/devices/#{device_id}/commands",
34
+ endpoint: "/#{API_VERSION}/devices/#{device_id}/commands",
33
35
  params: {
34
36
  command: command,
35
37
  parameter: parameter,
@@ -41,7 +43,7 @@ module Switchbot
41
43
  def scenes
42
44
  request(
43
45
  http_method: :get,
44
- endpoint: 'v1.0/scenes'
46
+ endpoint: "/#{API_VERSION}/scenes"
45
47
  )
46
48
  end
47
49
 
@@ -52,7 +54,7 @@ module Switchbot
52
54
  def execute(scene_id:)
53
55
  request(
54
56
  http_method: :post,
55
- endpoint: "/v1.0/scenes/#{scene_id}/execute"
57
+ endpoint: "/#{API_VERSION}/scenes/#{scene_id}/execute"
56
58
  )
57
59
  end
58
60
 
@@ -68,24 +70,41 @@ module Switchbot
68
70
  Humidifier.new(client: self, device_id: device_id)
69
71
  end
70
72
 
73
+ def color_bulb(device_id)
74
+ ColorBulb.new(client: self, device_id: device_id)
75
+ end
76
+
77
+ def lock(device_id)
78
+ Lock.new(client: self, device_id: device_id)
79
+ end
80
+
71
81
  private
72
82
 
73
83
  def headers
84
+ t = "#{Time.now.to_i}000"
85
+ nonce = SecureRandom.alphanumeric
86
+ sign = Base64.strict_encode64(OpenSSL::HMAC.digest('sha256', @secret, "#{@token}#{t}#{nonce}"))
87
+
74
88
  {
75
89
  'User-Agent' => "Switchbot v#{Switchbot::VERSION} (https://github.com/ytkg/switchbot)",
76
- 'Authorization' => @token
90
+ 'Authorization' => @token,
91
+ 'T' => t,
92
+ 'Sign' => sign,
93
+ 'Nonce' => nonce
77
94
  }
78
95
  end
79
96
 
80
97
  def connection
81
98
  Faraday.new(url: API_ENDPOINT, headers: headers) do |conn|
82
99
  conn.request :json
100
+ conn.response :json
101
+ conn.adapter :typhoeus, http_version: :httpv2_0 # rubocop:disable Naming/VariableNumber
83
102
  end
84
103
  end
85
104
 
86
105
  def request(http_method:, endpoint:, params: {})
87
106
  response = connection.public_send(http_method, endpoint, params)
88
- JSON.parse(response.body).deep_transform_keys(&:underscore).deep_symbolize_keys
107
+ response.body.deep_transform_keys(&:underscore).deep_symbolize_keys
89
108
  end
90
109
  end
91
110
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Switchbot
4
+ class ColorBulb < Device
5
+ def toggle
6
+ commands(command: 'toggle')
7
+ end
8
+
9
+ def brightness(value)
10
+ commands(command: 'setBrightness', parameter: value)
11
+ end
12
+
13
+ def color(value)
14
+ commands(command: 'setColor', parameter: value)
15
+ end
16
+
17
+ def color_temperature(value)
18
+ commands(command: 'setColorTemperature', parameter: value)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Switchbot
4
+ class Lock < Device
5
+ def lock
6
+ commands(command: 'lock')
7
+ end
8
+
9
+ def unlock
10
+ commands(command: 'unlock')
11
+ end
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Switchbot
4
- VERSION = '0.5.1'
4
+ VERSION = '0.7.0'
5
5
  end
data/lib/switchbot.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'securerandom'
4
+ require 'base64'
5
+ require 'openssl'
3
6
  require 'faraday'
4
- require 'faraday_middleware'
5
- require 'json'
7
+ require 'faraday/typhoeus'
6
8
  require 'active_support/all'
7
9
  require_relative 'switchbot/version'
8
10
  require_relative 'switchbot/client'
@@ -11,6 +13,8 @@ require_relative 'switchbot/scene'
11
13
  require_relative 'switchbot/bot'
12
14
  require_relative 'switchbot/light'
13
15
  require_relative 'switchbot/humidifier'
16
+ require_relative 'switchbot/color_bulb'
17
+ require_relative 'switchbot/lock'
14
18
 
15
19
  module Switchbot
16
20
  class Error < StandardError; end
data/switchbot.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.description = 'SwitchBot API client for Ruby'
13
13
  spec.homepage = 'https://github.com/ytkg/switchbot'
14
14
  spec.license = 'MIT'
15
- spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
16
16
 
17
17
  # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
18
18
 
@@ -32,8 +32,8 @@ Gem::Specification.new do |spec|
32
32
 
33
33
  # Uncomment to register a new dependency of your gem
34
34
  spec.add_dependency 'activesupport'
35
- spec.add_dependency 'faraday', '~> 1.3.0'
36
- spec.add_dependency 'faraday_middleware'
35
+ spec.add_dependency 'faraday', '>= 2.0.0'
36
+ spec.add_dependency 'faraday-typhoeus'
37
37
 
38
38
  spec.add_development_dependency 'rake', '~> 13.0'
39
39
  spec.add_development_dependency 'rspec', '~> 3.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switchbot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshiki Takagi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-04 00:00:00.000000000 Z
11
+ date: 2022-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -28,18 +28,18 @@ dependencies:
28
28
  name: faraday
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 1.3.0
33
+ version: 2.0.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 1.3.0
40
+ version: 2.0.0
41
41
  - !ruby/object:Gem::Dependency
42
- name: faraday_middleware
42
+ name: faraday-typhoeus
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -137,6 +137,7 @@ files:
137
137
  - CODE_OF_CONDUCT.md
138
138
  - Gemfile
139
139
  - LICENSE.txt
140
+ - README-v0.6.0-and-below.md
140
141
  - README.md
141
142
  - Rakefile
142
143
  - bin/console
@@ -144,9 +145,11 @@ files:
144
145
  - lib/switchbot.rb
145
146
  - lib/switchbot/bot.rb
146
147
  - lib/switchbot/client.rb
148
+ - lib/switchbot/color_bulb.rb
147
149
  - lib/switchbot/device.rb
148
150
  - lib/switchbot/humidifier.rb
149
151
  - lib/switchbot/light.rb
152
+ - lib/switchbot/lock.rb
150
153
  - lib/switchbot/scene.rb
151
154
  - lib/switchbot/version.rb
152
155
  - switchbot.gemspec
@@ -166,14 +169,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
166
169
  requirements:
167
170
  - - ">="
168
171
  - !ruby/object:Gem::Version
169
- version: 2.5.0
172
+ version: 2.6.0
170
173
  required_rubygems_version: !ruby/object:Gem::Requirement
171
174
  requirements:
172
175
  - - ">="
173
176
  - !ruby/object:Gem::Version
174
177
  version: '0'
175
178
  requirements: []
176
- rubygems_version: 3.3.3
179
+ rubygems_version: 3.3.22
177
180
  signing_key:
178
181
  specification_version: 4
179
182
  summary: SwitchBot API client for Ruby