switchbot 0.6.0 → 0.8.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: 63cae60c41beb637fe0fea73a634a4ef3e4f4d32354fe3691d16e0e88a24f9fd
4
- data.tar.gz: 9a2aa83c764d5595142d2c07e31c5fd8404ab6ceb4e2f9481801498eeb135f3f
3
+ metadata.gz: cc3479417cb8f17cdb727ce13981d7bca88163f75bc14419d7fcb1c446a355a0
4
+ data.tar.gz: a8fb35982f2b10684f89ad3a7bb20604198d24b2966cec99ca81aff5cfc51ce7
5
5
  SHA512:
6
- metadata.gz: 90501fd6d95b927e0c2d4fb741c384777947765b305ce0e8672f2da72aa62158dc98179cb01f1924af71725cc0d5d2bcf3292e7990f293d543535ba38b16f3f5
7
- data.tar.gz: faaf06892d72c0eaf1df45ad1091fcf05e30390d12bac1c3264932216742e2980606fb2b5f050978716b0eb40a0430f5af2f1d2f16c7905ac3b393902603c834
6
+ metadata.gz: a6f2df352088e371a871e10c8685139a33320c92254fd8ee9231dfc8927090e8fc3643d1dd706e09ca71ccce64646c0b5fceba7e2404e6c45bf38093dd7edef7
7
+ data.tar.gz: 725ea36d3fedf1f53d383e60dff9fd246865e920d023f98d4978a1f43f0ad68f8c977f28e839d8cc77002bfca9923cf0f7895eb5ff98f7dc8daaf264ca9b78f8
@@ -8,16 +8,14 @@ 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.6, 2.7, '3.0', 3.1]
11
+ ruby: [2.6, 2.7, '3.0', 3.1, 3.2]
12
12
  name: Ruby ${{ matrix.ruby }}
13
13
  steps:
14
- - uses: actions/checkout@v2
14
+ - uses: actions/checkout@v3
15
15
  - name: Set up Ruby
16
16
  uses: ruby/setup-ruby@v1
17
17
  with:
18
18
  ruby-version: ${{ matrix.ruby }}
19
+ bundler-cache: true
19
20
  - name: Run the default task
20
- run: |
21
- gem install bundler -v 2.2.4
22
- bundle install
23
- bundle exec rake
21
+ run: bundle exec rake
data/.rubocop.yml CHANGED
@@ -9,3 +9,6 @@ Metrics/BlockLength:
9
9
  Exclude:
10
10
  - switchbot.gemspec
11
11
  - spec/**/*
12
+
13
+ Gemspec/DevelopmentDependencies:
14
+ EnforcedStyle: gemspec
data/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
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.8.0...main)
3
+
4
+ ## v0.8.0
5
+ [full changelog](http://github.com/ytkg/switchbot/compare/v0.7.0...v0.8.0)
6
+
7
+ * Add Plug Mini
8
+ * https://github.com/ytkg/switchbot/pull/21
9
+
10
+ ## v0.7.0
11
+ [full changelog](http://github.com/ytkg/switchbot/compare/v0.6.0...v0.7.0)
12
+
13
+ * Add Lock
14
+ * https://github.com/ytkg/switchbot/pull/16
15
+ * :bomb: **[BREAKING CHANGE]** Support SwitchBot API v1.1
16
+ * https://github.com/ytkg/switchbot/pull/15
17
+ * The authentication method has been changed.
18
+
19
+ ```ruby
20
+ client = Switchbot::Client.new('YOUR_TOKEN', 'YOUR_SECRET')
21
+ ```
22
+
23
+ You must update the app to the latest version, V6.14 or later, in order to get the secret key.
3
24
 
4
25
  ## v0.6.0
5
26
  [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
@@ -1,6 +1,6 @@
1
1
  # Switchbot
2
2
 
3
- [SwichBot API](https://github.com/OpenWonderLabs/SwitchBotAPI) client for Ruby
3
+ [SwitchBot API](https://github.com/OpenWonderLabs/SwitchBotAPI) client for Ruby
4
4
 
5
5
  ## Installation
6
6
 
@@ -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
data/lib/switchbot/bot.rb CHANGED
@@ -1,7 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Switchbot
4
- class Bot < Device
4
+ class Bot
5
+ extend Forwardable
6
+
7
+ def_delegators :@device, :status, :commands, :on, :off, :on?, :off?
8
+
9
+ def initialize(client:, device_id:)
10
+ @device = Device.new(client: client, device_id: device_id)
11
+ end
12
+
5
13
  def press
6
14
  commands(command: 'press')
7
15
  end
@@ -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,27 @@ 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
+
81
+ def plug_mini(device_id)
82
+ PlugMini.new(client: self, device_id: device_id)
83
+ end
84
+
75
85
  private
76
86
 
77
87
  def headers
88
+ t = "#{Time.now.to_i}000"
89
+ nonce = SecureRandom.alphanumeric
90
+ sign = Base64.strict_encode64(OpenSSL::HMAC.digest('sha256', @secret, "#{@token}#{t}#{nonce}"))
91
+
78
92
  {
79
93
  'User-Agent' => "Switchbot v#{Switchbot::VERSION} (https://github.com/ytkg/switchbot)",
80
- 'Authorization' => @token
94
+ 'Authorization' => @token,
95
+ 'T' => t,
96
+ 'Sign' => sign,
97
+ 'Nonce' => nonce
81
98
  }
82
99
  end
83
100
 
@@ -85,6 +102,7 @@ module Switchbot
85
102
  Faraday.new(url: API_ENDPOINT, headers: headers) do |conn|
86
103
  conn.request :json
87
104
  conn.response :json
105
+ conn.adapter :typhoeus, http_version: :httpv2_0 # rubocop:disable Naming/VariableNumber
88
106
  end
89
107
  end
90
108
 
@@ -1,7 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Switchbot
4
- class ColorBulb < Device
4
+ class ColorBulb
5
+ extend Forwardable
6
+
7
+ def_delegators :@device, :status, :commands, :on, :off, :on?, :off?
8
+
9
+ def initialize(client:, device_id:)
10
+ @device = Device.new(client: client, device_id: device_id)
11
+ end
12
+
5
13
  def toggle
6
14
  commands(command: 'toggle')
7
15
  end
@@ -1,7 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Switchbot
4
- class Humidifier < Device
4
+ class Humidifier
5
+ extend Forwardable
6
+
7
+ def_delegators :@device, :status, :commands, :on, :off, :on?, :off?
8
+
9
+ def initialize(client:, device_id:)
10
+ @device = Device.new(client: client, device_id: device_id)
11
+ end
12
+
5
13
  def mode(value)
6
14
  commands(command: 'setMode', parameter: value.to_s)
7
15
  end
@@ -1,7 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Switchbot
4
- class Light < Device
4
+ class Light
5
+ extend Forwardable
6
+
7
+ def_delegators :@device, :commands, :on, :off
8
+
9
+ def initialize(client:, device_id:)
10
+ @device = Device.new(client: client, device_id: device_id)
11
+ end
12
+
5
13
  def brightness_up
6
14
  commands(command: 'brightnessUp')
7
15
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Switchbot
4
+ class Lock
5
+ extend Forwardable
6
+
7
+ def_delegators :@device, :status, :commands
8
+
9
+ def initialize(client:, device_id:)
10
+ @device = Device.new(client: client, device_id: device_id)
11
+ end
12
+
13
+ def lock
14
+ commands(command: 'lock')
15
+ end
16
+
17
+ def unlock
18
+ commands(command: 'unlock')
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Switchbot
4
+ class PlugMini
5
+ extend Forwardable
6
+
7
+ def_delegators :@device, :status, :commands, :on, :off, :on?, :off?
8
+
9
+ def initialize(client:, device_id:)
10
+ @device = Device.new(client: client, device_id: device_id)
11
+ end
12
+
13
+ def toggle
14
+ commands(command: 'toggle')
15
+ end
16
+ end
17
+ 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.8.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,8 @@ 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'
18
+ require_relative 'switchbot/plug_mini'
13
19
 
14
20
  module Switchbot
15
21
  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.8.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: 2023-03-21 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,8 @@ files:
134
149
  - lib/switchbot/device.rb
135
150
  - lib/switchbot/humidifier.rb
136
151
  - lib/switchbot/light.rb
152
+ - lib/switchbot/lock.rb
153
+ - lib/switchbot/plug_mini.rb
137
154
  - lib/switchbot/scene.rb
138
155
  - lib/switchbot/version.rb
139
156
  - switchbot.gemspec
@@ -145,7 +162,7 @@ metadata:
145
162
  source_code_uri: https://github.com/ytkg/switchbot
146
163
  changelog_uri: https://github.com/ytkg/switchbot/blob/main/CHANGELOG.md
147
164
  rubygems_mfa_required: 'true'
148
- post_install_message:
165
+ post_install_message:
149
166
  rdoc_options: []
150
167
  require_paths:
151
168
  - lib
@@ -160,8 +177,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
177
  - !ruby/object:Gem::Version
161
178
  version: '0'
162
179
  requirements: []
163
- rubygems_version: 3.1.6
164
- signing_key:
180
+ rubygems_version: 3.3.22
181
+ signing_key:
165
182
  specification_version: 4
166
183
  summary: SwitchBot API client for Ruby
167
184
  test_files: []