switchbot 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63cae60c41beb637fe0fea73a634a4ef3e4f4d32354fe3691d16e0e88a24f9fd
4
- data.tar.gz: 9a2aa83c764d5595142d2c07e31c5fd8404ab6ceb4e2f9481801498eeb135f3f
3
+ metadata.gz: e5dbe643e1c26eafa1c841dcf3cffb4e42ec799bba387b139fdd9d72e630ec6f
4
+ data.tar.gz: 2e243ec43219aab5fdbaf3dd4f20291676d7bdfebd7ee0ccdaee32b6c7cd21a4
5
5
  SHA512:
6
- metadata.gz: 90501fd6d95b927e0c2d4fb741c384777947765b305ce0e8672f2da72aa62158dc98179cb01f1924af71725cc0d5d2bcf3292e7990f293d543535ba38b16f3f5
7
- data.tar.gz: faaf06892d72c0eaf1df45ad1091fcf05e30390d12bac1c3264932216742e2980606fb2b5f050978716b0eb40a0430f5af2f1d2f16c7905ac3b393902603c834
6
+ metadata.gz: '0468a22f7a997c01e036c473e81af30e6832a8e39fdfb0acbd0155904f19459b20d5944247de05eb165cf98b810844adaf554a6224113ad95a5b12f5b518b5ba'
7
+ data.tar.gz: 2a434cdcfd1956789526ad7f925871ccae53c7eb9c496b1a909a16cd2f6535b751e1d32abd5a79d3d1c9d4ba05d6306caf0c55d3c40c224bb3b988a227c093f0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  ## Unreleased
2
- [full changelog](http://github.com/ytkg/switchbot/compare/v0.6.0...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.
3
18
 
4
19
  ## v0.6.0
5
20
  [full changelog](http://github.com/ytkg/switchbot/compare/v0.5.1...v0.6.0)
@@ -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
 
@@ -72,12 +74,23 @@ module Switchbot
72
74
  ColorBulb.new(client: self, device_id: device_id)
73
75
  end
74
76
 
77
+ def lock(device_id)
78
+ Lock.new(client: self, device_id: device_id)
79
+ end
80
+
75
81
  private
76
82
 
77
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
+
78
88
  {
79
89
  'User-Agent' => "Switchbot v#{Switchbot::VERSION} (https://github.com/ytkg/switchbot)",
80
- 'Authorization' => @token
90
+ 'Authorization' => @token,
91
+ 'T' => t,
92
+ 'Sign' => sign,
93
+ 'Nonce' => nonce
81
94
  }
82
95
  end
83
96
 
@@ -85,6 +98,7 @@ module Switchbot
85
98
  Faraday.new(url: API_ENDPOINT, headers: headers) do |conn|
86
99
  conn.request :json
87
100
  conn.response :json
101
+ conn.adapter :typhoeus, http_version: :httpv2_0 # rubocop:disable Naming/VariableNumber
88
102
  end
89
103
  end
90
104
 
@@ -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.6.0'
4
+ VERSION = '0.7.0'
5
5
  end
data/lib/switchbot.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'securerandom'
4
+ require 'base64'
5
+ require 'openssl'
3
6
  require 'faraday'
7
+ require 'faraday/typhoeus'
4
8
  require 'active_support/all'
5
9
  require_relative 'switchbot/version'
6
10
  require_relative 'switchbot/client'
@@ -10,6 +14,7 @@ require_relative 'switchbot/bot'
10
14
  require_relative 'switchbot/light'
11
15
  require_relative 'switchbot/humidifier'
12
16
  require_relative 'switchbot/color_bulb'
17
+ require_relative 'switchbot/lock'
13
18
 
14
19
  module Switchbot
15
20
  class Error < StandardError; end
data/switchbot.gemspec CHANGED
@@ -33,6 +33,7 @@ Gem::Specification.new do |spec|
33
33
  # Uncomment to register a new dependency of your gem
34
34
  spec.add_dependency 'activesupport'
35
35
  spec.add_dependency 'faraday', '>= 2.0.0'
36
+ spec.add_dependency 'faraday-typhoeus'
36
37
 
37
38
  spec.add_development_dependency 'rake', '~> 13.0'
38
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.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshiki Takagi
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-05-25 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
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 2.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday-typhoeus
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -123,6 +137,7 @@ files:
123
137
  - CODE_OF_CONDUCT.md
124
138
  - Gemfile
125
139
  - LICENSE.txt
140
+ - README-v0.6.0-and-below.md
126
141
  - README.md
127
142
  - Rakefile
128
143
  - bin/console
@@ -134,6 +149,7 @@ files:
134
149
  - lib/switchbot/device.rb
135
150
  - lib/switchbot/humidifier.rb
136
151
  - lib/switchbot/light.rb
152
+ - lib/switchbot/lock.rb
137
153
  - lib/switchbot/scene.rb
138
154
  - lib/switchbot/version.rb
139
155
  - switchbot.gemspec
@@ -145,7 +161,7 @@ metadata:
145
161
  source_code_uri: https://github.com/ytkg/switchbot
146
162
  changelog_uri: https://github.com/ytkg/switchbot/blob/main/CHANGELOG.md
147
163
  rubygems_mfa_required: 'true'
148
- post_install_message:
164
+ post_install_message:
149
165
  rdoc_options: []
150
166
  require_paths:
151
167
  - lib
@@ -160,8 +176,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
176
  - !ruby/object:Gem::Version
161
177
  version: '0'
162
178
  requirements: []
163
- rubygems_version: 3.1.6
164
- signing_key:
179
+ rubygems_version: 3.3.22
180
+ signing_key:
165
181
  specification_version: 4
166
182
  summary: SwitchBot API client for Ruby
167
183
  test_files: []