ewelink 3.1.0 → 3.3.2

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: 45f904d5e72d0038360340adc26dd7eb963f664804b43eee8235ee02d48f60ff
4
- data.tar.gz: 98ee94c2d9187ea17250c3f32c1932f8c0df8a2123c341a47159c32e2aec66e9
3
+ metadata.gz: 0fba630fc8ea0be922637f79d4618baccf676775fc37e35ffbd9847e479930f4
4
+ data.tar.gz: 8e03090c2c1f9e469b25a9b3ff4dc21da74fd150c6432196038c464cca1608bb
5
5
  SHA512:
6
- metadata.gz: 42977ef17b87c9c6710973968ca739d642108d6595861a870090ef13fbe5332b7ff99fba0dc8444e8022dfba065f4334426daf2dc423ed08314b3939cc41e69a
7
- data.tar.gz: 4fa2068348c252aecfa1f0d55f91013a17658a3403fa7813208f34f027757b2459181323aae9a9d8c71c915ac8c83e881150c4eacee77ed85832b1adaeadb451
6
+ metadata.gz: 854c5679733f93e5a54c3085858315874db86319b0d81898a64f807d6780f146cfe1f0767ce5b6e4c9ed576adcab59b3154a35399a2cae77a2d4bcac338e8d58
7
+ data.tar.gz: 328c7a4e3c10ca31d1b4d636c97c12e6a8af6358401aac77fea2739a0300cac162fa85ee1028dcf5097e42989a5d3035d39f8abf832030b3eabf0037dd43f244
data/README.mdown CHANGED
@@ -75,6 +75,14 @@ api = Ewelink::Api.new(email: 'john@example.com', password: 'secr$t')
75
75
  api.press_rf_bridge_button!(button[:uuid])
76
76
  ```
77
77
 
78
+ ### Additional options
79
+
80
+ - `async_actions` (`true` | `false`): To perform actions (pressing an RF
81
+ bridge button or turning a switch on/off) in asynchronous mode. (default:
82
+ `false`).
83
+ - `update_devices_status_on_connect` (`true` | `false`): To update devices
84
+ status (on, off) when connecting to Ewelink API (default: `false`).
85
+
78
86
  ### Configuring logger
79
87
 
80
88
  In order to have some debug informations about what kagu does, you could
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.1.0
1
+ 3.3.2
data/ewelink.gemspec CHANGED
@@ -18,7 +18,8 @@ Gem::Specification.new do |s|
18
18
  s.add_dependency 'activesupport', '>= 6.0.0', '< 7.0.0'
19
19
  s.add_dependency 'faye-websocket', '>= 0.11.0', '< 0.12.0'
20
20
  s.add_dependency 'httparty', '>= 0.18.0', '< 0.19.0'
21
+ s.add_dependency 'thread', '>= 0.2.0', '< 0.3.0'
21
22
 
22
23
  s.add_development_dependency 'byebug', '>= 11.0.0', '< 12.0.0'
23
- s.add_development_dependency 'rake', '>= 12.0.0', '< 13.0.0'
24
+ s.add_development_dependency 'rake', '>= 13.0.0', '< 14.0.0'
24
25
  end
data/lib/ewelink/api.rb CHANGED
@@ -18,11 +18,13 @@ module Ewelink
18
18
 
19
19
  attr_reader :email, :password, :phone_number
20
20
 
21
- def initialize(email: nil, password:, phone_number: nil)
21
+ def initialize(async_actions: false, email: nil, password:, phone_number: nil, update_devices_status_on_connect: false)
22
+ @async_actions = async_actions.present?
22
23
  @email = email.presence.try(:strip)
23
24
  @mutexs = {}
24
25
  @password = password.presence || raise(Error.new(":password must be specified"))
25
26
  @phone_number = phone_number.presence.try(:strip)
27
+ @update_devices_status_on_connect = update_devices_status_on_connect.present?
26
28
  @web_socket_authenticated = false
27
29
  @web_socket_switches_statuses = {}
28
30
 
@@ -31,25 +33,31 @@ module Ewelink
31
33
  start_web_socket_authentication_check_thread
32
34
  end
33
35
 
36
+ def async_actions?
37
+ @async_actions
38
+ end
39
+
34
40
  def press_rf_bridge_button!(uuid)
35
- synchronize(:press_rf_bridge_button) do
36
- button = find_rf_bridge_button!(uuid)
37
- web_socket_wait_for(-> { web_socket_authenticated? }, initialize_web_socket: true) do
38
- params = {
39
- 'action' => 'update',
40
- 'apikey' => button[:api_key],
41
- 'deviceid' => button[:device_id],
42
- 'params' => {
43
- 'cmd' => 'transmit',
44
- 'rfChl' => button[:channel],
45
- },
46
- 'sequence' => web_socket_sequence,
47
- 'ts' => 0,
48
- 'userAgent' => 'app',
49
- }
50
- Ewelink.logger.debug(self.class.name) { "Pressing RF bridge button #{button[:uuid].inspect}" }
51
- send_to_web_socket(JSON.generate(params))
52
- true
41
+ process_action do
42
+ synchronize(:press_rf_bridge_button) do
43
+ button = find_rf_bridge_button!(uuid)
44
+ web_socket_wait_for(-> { web_socket_authenticated? }, initialize_web_socket: true) do
45
+ params = {
46
+ 'action' => 'update',
47
+ 'apikey' => button[:api_key],
48
+ 'deviceid' => button[:device_id],
49
+ 'params' => {
50
+ 'cmd' => 'transmit',
51
+ 'rfChl' => button[:channel],
52
+ },
53
+ 'sequence' => web_socket_sequence,
54
+ 'ts' => 0,
55
+ 'userAgent' => 'app',
56
+ }
57
+ Ewelink.logger.debug(self.class.name) { "Pressing RF bridge button #{button[:uuid].inspect}" }
58
+ send_to_web_socket(JSON.generate(params))
59
+ true
60
+ end
53
61
  end
54
62
  end
55
63
  end
@@ -171,31 +179,37 @@ module Ewelink
171
179
  end
172
180
 
173
181
  def turn_switch!(uuid, on)
174
- if ['on', :on, 'true'].include?(on)
175
- on = true
176
- elsif ['off', :off, 'false'].include?(on)
177
- on = false
178
- end
179
- switch = find_switch!(uuid)
180
- @web_socket_switches_statuses[switch[:uuid]] = nil
181
- web_socket_wait_for(-> { web_socket_authenticated? }, initialize_web_socket: true) do
182
- params = {
183
- 'action' => 'update',
184
- 'apikey' => switch[:api_key],
185
- 'deviceid' => switch[:device_id],
186
- 'params' => {
187
- 'switch' => on ? 'on' : 'off',
188
- },
189
- 'sequence' => web_socket_sequence,
190
- 'ts' => 0,
191
- 'userAgent' => 'app',
192
- }
193
- Ewelink.logger.debug(self.class.name) { "Turning switch #{switch[:uuid].inspect} #{on ? 'on' : 'off'}" }
194
- send_to_web_socket(JSON.generate(params))
182
+ process_action do
183
+ if ['on', :on, 'true'].include?(on)
184
+ on = true
185
+ elsif ['off', :off, 'false'].include?(on)
186
+ on = false
187
+ end
188
+ switch = find_switch!(uuid)
189
+ @web_socket_switches_statuses[switch[:uuid]] = nil
190
+ web_socket_wait_for(-> { web_socket_authenticated? }, initialize_web_socket: true) do
191
+ params = {
192
+ 'action' => 'update',
193
+ 'apikey' => switch[:api_key],
194
+ 'deviceid' => switch[:device_id],
195
+ 'params' => {
196
+ 'switch' => on ? 'on' : 'off',
197
+ },
198
+ 'sequence' => web_socket_sequence,
199
+ 'ts' => 0,
200
+ 'userAgent' => 'app',
201
+ }
202
+ Ewelink.logger.debug(self.class.name) { "Turning switch #{switch[:uuid].inspect} #{on ? 'on' : 'off'}" }
203
+ send_to_web_socket(JSON.generate(params))
204
+ end
205
+ sleep(SWITCH_STATUS_CHANGE_CHECK_TIMEOUT)
206
+ switch_on?(switch[:uuid]) # Waiting for switch status update
207
+ true
195
208
  end
196
- sleep(SWITCH_STATUS_CHANGE_CHECK_TIMEOUT)
197
- switch_on?(switch[:uuid]) # Waiting for switch status update
198
- true
209
+ end
210
+
211
+ def update_devices_status_on_connect?
212
+ @update_devices_status_on_connect
199
213
  end
200
214
 
201
215
  private
@@ -284,6 +298,13 @@ module Ewelink
284
298
  SecureRandom.hex[0, 8]
285
299
  end
286
300
 
301
+ def process_action(&block)
302
+ return yield unless async_actions?
303
+ @async_actions_thread_pool ||= Thread.pool(1)
304
+ @async_actions_thread_pool.process(&block)
305
+ true
306
+ end
307
+
287
308
  def region
288
309
  @region ||= DEFAULT_REGION
289
310
  end
@@ -411,6 +432,7 @@ module Ewelink
411
432
  if json['apikey'].present? && !@web_socket_authenticated && json['apikey'] == api_key
412
433
  @web_socket_authenticated = true
413
434
  Ewelink.logger.debug(self.class.name) { "WebSocket successfully authenticated API key: #{json['apikey'].truncate(16).inspect}" }
435
+ Thread.new { switches.each { |switch| switch_on?(switch[:uuid]) } } if update_devices_status_on_connect?
414
436
  end
415
437
 
416
438
  if json['deviceid'].present? && json['params'].is_a?(Hash) && json['params']['switch'].present?
@@ -3,7 +3,7 @@ module Ewelink
3
3
  class Runner
4
4
 
5
5
  def run
6
- api = Api.new(options.slice(:email, :password, :phone_number))
6
+ api = Api.new(**options.slice(:email, :password, :phone_number))
7
7
  puts(JSON.pretty_generate(api.switches)) if options[:list_switches]
8
8
  puts(JSON.pretty_generate(api.rf_bridge_buttons)) if options[:list_rf_bridge_buttons]
9
9
  options[:turn_switches_on_uuids].each { |uuid| api.turn_switch!(uuid, :on) }
data/lib/ewelink.rb CHANGED
@@ -10,6 +10,7 @@ require 'logger'
10
10
  require 'openssl'
11
11
  require 'optparse'
12
12
  require 'set'
13
+ require 'thread/pool'
13
14
  require 'timeout'
14
15
 
15
16
  module Ewelink
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ewelink
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexis Toulotte
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-12 00:00:00.000000000 Z
11
+ date: 2021-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -70,6 +70,26 @@ dependencies:
70
70
  - - "<"
71
71
  - !ruby/object:Gem::Version
72
72
  version: 0.19.0
73
+ - !ruby/object:Gem::Dependency
74
+ name: thread
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 0.2.0
80
+ - - "<"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.3.0
83
+ type: :runtime
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 0.2.0
90
+ - - "<"
91
+ - !ruby/object:Gem::Version
92
+ version: 0.3.0
73
93
  - !ruby/object:Gem::Dependency
74
94
  name: byebug
75
95
  requirement: !ruby/object:Gem::Requirement
@@ -96,20 +116,20 @@ dependencies:
96
116
  requirements:
97
117
  - - ">="
98
118
  - !ruby/object:Gem::Version
99
- version: 12.0.0
119
+ version: 13.0.0
100
120
  - - "<"
101
121
  - !ruby/object:Gem::Version
102
- version: 13.0.0
122
+ version: 14.0.0
103
123
  type: :development
104
124
  prerelease: false
105
125
  version_requirements: !ruby/object:Gem::Requirement
106
126
  requirements:
107
127
  - - ">="
108
128
  - !ruby/object:Gem::Version
109
- version: 12.0.0
129
+ version: 13.0.0
110
130
  - - "<"
111
131
  - !ruby/object:Gem::Version
112
- version: 13.0.0
132
+ version: 14.0.0
113
133
  description: Manage eWeLink smart home devices
114
134
  email: al@alweb.org
115
135
  executables:
@@ -144,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
164
  - !ruby/object:Gem::Version
145
165
  version: '0'
146
166
  requirements: []
147
- rubygems_version: 3.0.3
167
+ rubygems_version: 3.2.15
148
168
  signing_key:
149
169
  specification_version: 4
150
170
  summary: Manage eWeLink devices