switchbot 0.7.0 → 0.9.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 +4 -4
- data/.github/CODEOWNERS +1 -0
- data/.github/dependabot.yml +11 -0
- data/.github/workflows/main.yml +5 -7
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +80 -49
- data/README.md +10 -4
- data/lib/switchbot/bot.rb +9 -1
- data/lib/switchbot/client.rb +14 -53
- data/lib/switchbot/color_bulb.rb +9 -1
- data/lib/switchbot/humidifier.rb +9 -1
- data/lib/switchbot/light.rb +9 -1
- data/lib/switchbot/lock.rb +9 -1
- data/lib/switchbot/plug_mini.rb +17 -0
- data/lib/switchbot/request.rb +49 -0
- data/lib/switchbot/version.rb +1 -1
- data/lib/switchbot.rb +2 -0
- metadata +7 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: be1e25f69c499cba70f375ebb2d95ba8603620f72a1c4288318a36f01f2ab7d1
|
|
4
|
+
data.tar.gz: ae15d1012b74b95b0e930a914201d3ba5bfcd62636894d5efb952d0af60b2452
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 842235f1b0db8564903ab0650d84a01a6312b9f1def3fbdce62c1ba2aee02c66ac1f8828f7144c565b1e2e63b045c311fc43a7068dc52e454426e7c6568c77ca
|
|
7
|
+
data.tar.gz: e59de5df165265cbbdab81c333c26196a193d3f1386413aa87375ae499235bd4ef6e212566326df2aafef349b6a101aec5ef1b113a7ea33ec54d85265a9bfdac
|
data/.github/CODEOWNERS
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
* @ytkg
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# To get started with Dependabot version updates, you'll need to specify which
|
|
2
|
+
# package ecosystems to update and where the package manifests are located.
|
|
3
|
+
# Please see the documentation for all configuration options:
|
|
4
|
+
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
|
|
5
|
+
|
|
6
|
+
version: 2
|
|
7
|
+
updates:
|
|
8
|
+
- package-ecosystem: "github-actions"
|
|
9
|
+
directory: "/"
|
|
10
|
+
schedule:
|
|
11
|
+
interval: "daily"
|
data/.github/workflows/main.yml
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
name: Ruby
|
|
2
2
|
|
|
3
|
-
on:
|
|
3
|
+
on: push
|
|
4
4
|
|
|
5
5
|
jobs:
|
|
6
6
|
build:
|
|
@@ -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, 3.3]
|
|
12
12
|
name: Ruby ${{ matrix.ruby }}
|
|
13
13
|
steps:
|
|
14
|
-
- uses: actions/checkout@
|
|
14
|
+
- uses: actions/checkout@v6
|
|
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
data/CHANGELOG.md
CHANGED
|
@@ -1,66 +1,97 @@
|
|
|
1
|
-
|
|
2
|
-
[full changelog](http://github.com/ytkg/switchbot/compare/v0.7.0...main)
|
|
1
|
+
# Changelog
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
[full changelog](http://github.com/ytkg/switchbot/compare/v0.6.0...v0.7.0)
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* :bomb: **[BREAKING CHANGE]** Support SwitchBot API v1.1
|
|
10
|
-
* https://github.com/ytkg/switchbot/pull/15
|
|
11
|
-
* The authentication method has been changed.
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
12
7
|
|
|
13
|
-
|
|
14
|
-
client = Switchbot::Client.new('YOUR_TOKEN', 'YOUR_SECRET')
|
|
15
|
-
```
|
|
8
|
+
## [Unreleased]
|
|
16
9
|
|
|
17
|
-
|
|
10
|
+
## [0.9.0] - 2026-02-23
|
|
18
11
|
|
|
19
|
-
|
|
20
|
-
[
|
|
12
|
+
### Added
|
|
13
|
+
- Allow `Switchbot::Client.new` to read `SWITCHBOT_API_TOKEN` and `SWITCHBOT_API_SECRET` when arguments are omitted. ([#25](https://github.com/ytkg/switchbot/pull/25))
|
|
14
|
+
Usage:
|
|
21
15
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
16
|
+
```ruby
|
|
17
|
+
ENV['SWITCHBOT_API_TOKEN'] = 'YOUR_TOKEN'
|
|
18
|
+
ENV['SWITCHBOT_API_SECRET'] = 'YOUR_SECRET'
|
|
19
|
+
client = Switchbot::Client.new
|
|
20
|
+
```
|
|
26
21
|
|
|
27
|
-
##
|
|
28
|
-
[full changelog](http://github.com/ytkg/switchbot/compare/v0.5.0...v0.5.1)
|
|
22
|
+
## [0.8.0] - 2023-03-21
|
|
29
23
|
|
|
30
|
-
|
|
31
|
-
|
|
24
|
+
### Added
|
|
25
|
+
- Add Plug Mini support. ([#21](https://github.com/ytkg/switchbot/pull/21))
|
|
32
26
|
|
|
33
|
-
##
|
|
34
|
-
[full changelog](http://github.com/ytkg/switchbot/compare/v0.4.0...v0.5.0)
|
|
27
|
+
## [0.7.0] - 2022-10-27
|
|
35
28
|
|
|
36
|
-
|
|
37
|
-
|
|
29
|
+
### Added
|
|
30
|
+
- Add Lock support. ([#16](https://github.com/ytkg/switchbot/pull/16))
|
|
38
31
|
|
|
39
|
-
|
|
40
|
-
[
|
|
32
|
+
### Changed
|
|
33
|
+
- **BREAKING:** Support SwitchBot API v1.1. ([#15](https://github.com/ytkg/switchbot/pull/15))
|
|
34
|
+
The authentication method has changed:
|
|
41
35
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
* https://github.com/ytkg/switchbot/pull/6
|
|
46
|
-
* Add Device#on? and Device#off?
|
|
47
|
-
* https://github.com/ytkg/switchbot/pull/5
|
|
36
|
+
```ruby
|
|
37
|
+
client = Switchbot::Client.new('YOUR_TOKEN', 'YOUR_SECRET')
|
|
38
|
+
```
|
|
48
39
|
|
|
49
|
-
|
|
50
|
-
[full changelog](http://github.com/ytkg/switchbot/compare/v0.2.0...v0.3.0)
|
|
40
|
+
App version 6.14 or later is required to get the secret key.
|
|
51
41
|
|
|
52
|
-
|
|
53
|
-
* https://github.com/ytkg/switchbot/pull/4
|
|
54
|
-
* Add Device#commands
|
|
55
|
-
* https://github.com/ytkg/switchbot/pull/3
|
|
42
|
+
## [0.6.0] - 2022-05-25
|
|
56
43
|
|
|
57
|
-
|
|
58
|
-
[
|
|
44
|
+
### Added
|
|
45
|
+
- Add ColorBulb support. ([#14](https://github.com/ytkg/switchbot/pull/14))
|
|
59
46
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
* Support scenes endpoint
|
|
63
|
-
* https://github.com/ytkg/switchbot/pull/1
|
|
47
|
+
### Changed
|
|
48
|
+
- Update Faraday to v2. ([#12](https://github.com/ytkg/switchbot/pull/12))
|
|
64
49
|
|
|
65
|
-
|
|
66
|
-
|
|
50
|
+
### Removed
|
|
51
|
+
- **BREAKING:** Drop support for Ruby 2.5. ([#12](https://github.com/ytkg/switchbot/pull/12))
|
|
52
|
+
|
|
53
|
+
## [0.5.1] - 2022-01-04
|
|
54
|
+
|
|
55
|
+
### Security
|
|
56
|
+
- Enable MFA requirement for gem releasing. ([#11](https://github.com/ytkg/switchbot/pull/11))
|
|
57
|
+
|
|
58
|
+
## [0.5.0] - 2021-06-25
|
|
59
|
+
|
|
60
|
+
### Added
|
|
61
|
+
- Add Bot support. ([#9](https://github.com/ytkg/switchbot/pull/9))
|
|
62
|
+
|
|
63
|
+
## [0.4.0] - 2021-04-23
|
|
64
|
+
|
|
65
|
+
### Added
|
|
66
|
+
- Add Humidifier support. ([#7](https://github.com/ytkg/switchbot/pull/7))
|
|
67
|
+
- Add Light support. ([#6](https://github.com/ytkg/switchbot/pull/6))
|
|
68
|
+
- Add `Device#on?` and `Device#off?`. ([#5](https://github.com/ytkg/switchbot/pull/5))
|
|
69
|
+
|
|
70
|
+
## [0.3.0] - 2021-03-07
|
|
71
|
+
|
|
72
|
+
### Added
|
|
73
|
+
- Add `Device#on` and `Device#off`. ([#4](https://github.com/ytkg/switchbot/pull/4))
|
|
74
|
+
- Add `Device#commands`. ([#3](https://github.com/ytkg/switchbot/pull/3))
|
|
75
|
+
|
|
76
|
+
## [0.2.0] - 2021-03-05
|
|
77
|
+
|
|
78
|
+
### Added
|
|
79
|
+
- Add resource methods. ([#2](https://github.com/ytkg/switchbot/pull/2))
|
|
80
|
+
- Support scenes endpoint. ([#1](https://github.com/ytkg/switchbot/pull/1))
|
|
81
|
+
|
|
82
|
+
## [0.1.0] - 2021-03-02
|
|
83
|
+
|
|
84
|
+
### Added
|
|
85
|
+
- First release.
|
|
86
|
+
|
|
87
|
+
[Unreleased]: https://github.com/ytkg/switchbot/compare/v0.9.0...HEAD
|
|
88
|
+
[0.9.0]: https://github.com/ytkg/switchbot/compare/v0.8.0...v0.9.0
|
|
89
|
+
[0.8.0]: https://github.com/ytkg/switchbot/compare/v0.7.0...v0.8.0
|
|
90
|
+
[0.7.0]: https://github.com/ytkg/switchbot/compare/v0.6.0...v0.7.0
|
|
91
|
+
[0.6.0]: https://github.com/ytkg/switchbot/compare/v0.5.1...v0.6.0
|
|
92
|
+
[0.5.1]: https://github.com/ytkg/switchbot/compare/v0.5.0...v0.5.1
|
|
93
|
+
[0.5.0]: https://github.com/ytkg/switchbot/compare/v0.4.0...v0.5.0
|
|
94
|
+
[0.4.0]: https://github.com/ytkg/switchbot/compare/v0.3.0...v0.4.0
|
|
95
|
+
[0.3.0]: https://github.com/ytkg/switchbot/compare/v0.2.0...v0.3.0
|
|
96
|
+
[0.2.0]: https://github.com/ytkg/switchbot/compare/v0.1.0...v0.2.0
|
|
97
|
+
[0.1.0]: https://github.com/ytkg/switchbot/releases/tag/v0.1.0
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Switchbot
|
|
2
2
|
|
|
3
|
-
[
|
|
3
|
+
[SwitchBot API](https://github.com/OpenWonderLabs/SwitchBotAPI) client for Ruby
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -22,10 +22,16 @@ Or install it yourself as:
|
|
|
22
22
|
```ruby
|
|
23
23
|
require 'switchbot'
|
|
24
24
|
|
|
25
|
+
# pass token and secret directly
|
|
25
26
|
client = Switchbot::Client.new('YOUR_TOKEN', 'YOUR_SECRET')
|
|
27
|
+
|
|
28
|
+
# or use environment variables
|
|
29
|
+
ENV['SWITCHBOT_API_TOKEN'] = 'YOUR_TOKEN'
|
|
30
|
+
ENV['SWITCHBOT_API_SECRET'] = 'YOUR_SECRET'
|
|
31
|
+
client = Switchbot::Client.new
|
|
26
32
|
```
|
|
27
33
|
|
|
28
|
-
For v0.6.0 and below, refer to README-v0.6.0-and-below.md.
|
|
34
|
+
For v0.6.0 and below, refer to [README-v0.6.0-and-below.md](https://github.com/ytkg/switchbot/blob/main/README-v0.6.0-and-below.md).
|
|
29
35
|
|
|
30
36
|
### Get device list
|
|
31
37
|
GET https://api.switch-bot.com/v1.1/devices
|
|
@@ -114,7 +120,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
|
114
120
|
|
|
115
121
|
## Contributing
|
|
116
122
|
|
|
117
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
123
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ytkg/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/ytkg/switchbot/blob/master/CODE_OF_CONDUCT.md).
|
|
118
124
|
|
|
119
125
|
## License
|
|
120
126
|
|
|
@@ -122,4 +128,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
|
122
128
|
|
|
123
129
|
## Code of Conduct
|
|
124
130
|
|
|
125
|
-
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/
|
|
131
|
+
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/ytkg/switchbot/blob/master/CODE_OF_CONDUCT.md).
|
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
|
|
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
|
data/lib/switchbot/client.rb
CHANGED
|
@@ -2,19 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
module Switchbot
|
|
4
4
|
class Client
|
|
5
|
-
API_ENDPOINT = 'https://api.switch-bot.com'
|
|
6
5
|
API_VERSION = 'v1.1'
|
|
7
6
|
|
|
8
|
-
def initialize(token, secret)
|
|
9
|
-
@token = token
|
|
10
|
-
@secret = secret
|
|
7
|
+
def initialize(token = nil, secret = nil)
|
|
8
|
+
@token = token || ENV.fetch('SWITCHBOT_API_TOKEN', '')
|
|
9
|
+
@secret = secret || ENV.fetch('SWITCHBOT_API_SECRET', '')
|
|
11
10
|
end
|
|
12
11
|
|
|
13
12
|
def devices
|
|
14
|
-
request(
|
|
15
|
-
http_method: :get,
|
|
16
|
-
endpoint: "/#{API_VERSION}/devices"
|
|
17
|
-
)
|
|
13
|
+
request.get("/#{API_VERSION}/devices")
|
|
18
14
|
end
|
|
19
15
|
|
|
20
16
|
def device(device_id)
|
|
@@ -22,29 +18,16 @@ module Switchbot
|
|
|
22
18
|
end
|
|
23
19
|
|
|
24
20
|
def status(device_id:)
|
|
25
|
-
request(
|
|
26
|
-
http_method: :get,
|
|
27
|
-
endpoint: "/#{API_VERSION}/devices/#{device_id}/status"
|
|
28
|
-
)
|
|
21
|
+
request.get("/#{API_VERSION}/devices/#{device_id}/status")
|
|
29
22
|
end
|
|
30
23
|
|
|
31
24
|
def commands(device_id:, command:, parameter: 'default', command_type: 'command')
|
|
32
|
-
request(
|
|
33
|
-
|
|
34
|
-
endpoint: "/#{API_VERSION}/devices/#{device_id}/commands",
|
|
35
|
-
params: {
|
|
36
|
-
command: command,
|
|
37
|
-
parameter: parameter,
|
|
38
|
-
commandType: command_type
|
|
39
|
-
}
|
|
40
|
-
)
|
|
25
|
+
request.post("/#{API_VERSION}/devices/#{device_id}/commands",
|
|
26
|
+
params: { command: command, parameter: parameter, commandType: command_type })
|
|
41
27
|
end
|
|
42
28
|
|
|
43
29
|
def scenes
|
|
44
|
-
request(
|
|
45
|
-
http_method: :get,
|
|
46
|
-
endpoint: "/#{API_VERSION}/scenes"
|
|
47
|
-
)
|
|
30
|
+
request.get("/#{API_VERSION}/scenes")
|
|
48
31
|
end
|
|
49
32
|
|
|
50
33
|
def scene(scene_id)
|
|
@@ -52,10 +35,7 @@ module Switchbot
|
|
|
52
35
|
end
|
|
53
36
|
|
|
54
37
|
def execute(scene_id:)
|
|
55
|
-
request(
|
|
56
|
-
http_method: :post,
|
|
57
|
-
endpoint: "/#{API_VERSION}/scenes/#{scene_id}/execute"
|
|
58
|
-
)
|
|
38
|
+
request.post("/#{API_VERSION}/scenes/#{scene_id}/execute")
|
|
59
39
|
end
|
|
60
40
|
|
|
61
41
|
def bot(device_id)
|
|
@@ -78,33 +58,14 @@ module Switchbot
|
|
|
78
58
|
Lock.new(client: self, device_id: device_id)
|
|
79
59
|
end
|
|
80
60
|
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
|
|
88
|
-
{
|
|
89
|
-
'User-Agent' => "Switchbot v#{Switchbot::VERSION} (https://github.com/ytkg/switchbot)",
|
|
90
|
-
'Authorization' => @token,
|
|
91
|
-
'T' => t,
|
|
92
|
-
'Sign' => sign,
|
|
93
|
-
'Nonce' => nonce
|
|
94
|
-
}
|
|
61
|
+
def plug_mini(device_id)
|
|
62
|
+
PlugMini.new(client: self, device_id: device_id)
|
|
95
63
|
end
|
|
96
64
|
|
|
97
|
-
|
|
98
|
-
Faraday.new(url: API_ENDPOINT, headers: headers) do |conn|
|
|
99
|
-
conn.request :json
|
|
100
|
-
conn.response :json
|
|
101
|
-
conn.adapter :typhoeus, http_version: :httpv2_0 # rubocop:disable Naming/VariableNumber
|
|
102
|
-
end
|
|
103
|
-
end
|
|
65
|
+
private
|
|
104
66
|
|
|
105
|
-
def request
|
|
106
|
-
|
|
107
|
-
response.body.deep_transform_keys(&:underscore).deep_symbolize_keys
|
|
67
|
+
def request
|
|
68
|
+
@request ||= Request.new(@token, @secret)
|
|
108
69
|
end
|
|
109
70
|
end
|
|
110
71
|
end
|
data/lib/switchbot/color_bulb.rb
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Switchbot
|
|
4
|
-
class ColorBulb
|
|
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
|
data/lib/switchbot/humidifier.rb
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Switchbot
|
|
4
|
-
class Humidifier
|
|
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
|
data/lib/switchbot/light.rb
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Switchbot
|
|
4
|
-
class Light
|
|
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
|
data/lib/switchbot/lock.rb
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Switchbot
|
|
4
|
-
class Lock
|
|
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
|
+
|
|
5
13
|
def lock
|
|
6
14
|
commands(command: 'lock')
|
|
7
15
|
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
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Switchbot
|
|
4
|
+
class Request
|
|
5
|
+
API_ENDPOINT = 'https://api.switch-bot.com'
|
|
6
|
+
|
|
7
|
+
def initialize(token, secret)
|
|
8
|
+
@token = token
|
|
9
|
+
@secret = secret
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def get(endpoint)
|
|
13
|
+
request(http_method: :get, endpoint: endpoint)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def post(endpoint, params: {})
|
|
17
|
+
request(http_method: :post, endpoint: endpoint, params: params)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def headers
|
|
23
|
+
t = "#{Time.now.to_i}000"
|
|
24
|
+
nonce = SecureRandom.alphanumeric
|
|
25
|
+
sign = Base64.strict_encode64(OpenSSL::HMAC.digest('sha256', @secret, "#{@token}#{t}#{nonce}"))
|
|
26
|
+
|
|
27
|
+
{
|
|
28
|
+
'User-Agent' => "Switchbot v#{Switchbot::VERSION} (https://github.com/ytkg/switchbot)",
|
|
29
|
+
'Authorization' => @token,
|
|
30
|
+
'T' => t,
|
|
31
|
+
'Sign' => sign,
|
|
32
|
+
'Nonce' => nonce
|
|
33
|
+
}
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def connection
|
|
37
|
+
Faraday.new(url: API_ENDPOINT, headers: headers) do |conn|
|
|
38
|
+
conn.request :json
|
|
39
|
+
conn.response :json
|
|
40
|
+
conn.adapter :typhoeus, http_version: :httpv2_0 # rubocop:disable Naming/VariableNumber
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def request(http_method:, endpoint:, params: {})
|
|
45
|
+
response = connection.public_send(http_method, endpoint, params)
|
|
46
|
+
response.body.deep_transform_keys(&:underscore).deep_symbolize_keys
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
data/lib/switchbot/version.rb
CHANGED
data/lib/switchbot.rb
CHANGED
|
@@ -8,6 +8,7 @@ require 'faraday/typhoeus'
|
|
|
8
8
|
require 'active_support/all'
|
|
9
9
|
require_relative 'switchbot/version'
|
|
10
10
|
require_relative 'switchbot/client'
|
|
11
|
+
require_relative 'switchbot/request'
|
|
11
12
|
require_relative 'switchbot/device'
|
|
12
13
|
require_relative 'switchbot/scene'
|
|
13
14
|
require_relative 'switchbot/bot'
|
|
@@ -15,6 +16,7 @@ require_relative 'switchbot/light'
|
|
|
15
16
|
require_relative 'switchbot/humidifier'
|
|
16
17
|
require_relative 'switchbot/color_bulb'
|
|
17
18
|
require_relative 'switchbot/lock'
|
|
19
|
+
require_relative 'switchbot/plug_mini'
|
|
18
20
|
|
|
19
21
|
module Switchbot
|
|
20
22
|
class Error < StandardError; end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: switchbot
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.9.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Yoshiki Takagi
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: exe
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: activesupport
|
|
@@ -129,6 +128,8 @@ executables: []
|
|
|
129
128
|
extensions: []
|
|
130
129
|
extra_rdoc_files: []
|
|
131
130
|
files:
|
|
131
|
+
- ".github/CODEOWNERS"
|
|
132
|
+
- ".github/dependabot.yml"
|
|
132
133
|
- ".github/workflows/main.yml"
|
|
133
134
|
- ".gitignore"
|
|
134
135
|
- ".rspec"
|
|
@@ -150,6 +151,8 @@ files:
|
|
|
150
151
|
- lib/switchbot/humidifier.rb
|
|
151
152
|
- lib/switchbot/light.rb
|
|
152
153
|
- lib/switchbot/lock.rb
|
|
154
|
+
- lib/switchbot/plug_mini.rb
|
|
155
|
+
- lib/switchbot/request.rb
|
|
153
156
|
- lib/switchbot/scene.rb
|
|
154
157
|
- lib/switchbot/version.rb
|
|
155
158
|
- switchbot.gemspec
|
|
@@ -161,7 +164,6 @@ metadata:
|
|
|
161
164
|
source_code_uri: https://github.com/ytkg/switchbot
|
|
162
165
|
changelog_uri: https://github.com/ytkg/switchbot/blob/main/CHANGELOG.md
|
|
163
166
|
rubygems_mfa_required: 'true'
|
|
164
|
-
post_install_message:
|
|
165
167
|
rdoc_options: []
|
|
166
168
|
require_paths:
|
|
167
169
|
- lib
|
|
@@ -176,8 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
176
178
|
- !ruby/object:Gem::Version
|
|
177
179
|
version: '0'
|
|
178
180
|
requirements: []
|
|
179
|
-
rubygems_version:
|
|
180
|
-
signing_key:
|
|
181
|
+
rubygems_version: 4.0.6
|
|
181
182
|
specification_version: 4
|
|
182
183
|
summary: SwitchBot API client for Ruby
|
|
183
184
|
test_files: []
|