ewelink 1.0.1 → 1.1.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: 344143666abe75f11d9d3ccdb49116b6ded1579a609820dac2b26f97de27dcac
4
- data.tar.gz: 05fcfdaa151396125266d0125d3e28124eaae6da5a607a531a4729a937425902
3
+ metadata.gz: aa2540d5e8dcf125b91c086731711b772516e28466e33b2bb668bd143abb5fe7
4
+ data.tar.gz: caf1bfa5d91d0edefc143f49230b0592aafecbbb40e7e80a7d5279f5c3685e0d
5
5
  SHA512:
6
- metadata.gz: b83fe71850b9c0b1595df52687082452e3c8e0da75c896027d472fbfd1df2dc0c3688181f242db506b76bb415aee6a8f60055e51c8af8963c7062ce2e2a27996
7
- data.tar.gz: 84577222fc03a3a20cb08eb5a8ad3243635b2694d33b4bee9bb52582bf9cc4ea8e2e404a7315206adf2bbce3e8ffdbb2b12792decf6ad24f17a844d4a24a6126
6
+ metadata.gz: c09430a4c2f0a31d62c20828f8e0591cff4f808e9660ae3d309c5fd67edde27aa750ba2d864b1c2d0ad99268b32cb8b05837c407afa268a74ae4d6254aac3859
7
+ data.tar.gz: 2a5d636f6a571ece7e7cf83cac4eae90b9457212e6260b946c1172dd773e1a438e4ecdf31dee531ada59810440fa4e7a1bfa0af996c974c133e52f61a644638d
data/README.mdown CHANGED
@@ -40,24 +40,30 @@ api.rf_bridge_buttons.each do |button|
40
40
  end
41
41
  ```
42
42
 
43
- ### Set switch on or off
43
+ ### Turn switch on or off
44
44
 
45
45
  ```ruby
46
46
  require 'ewelink'
47
47
 
48
48
  api = Ewelink::Api.new(phone_number: '+687 414243', password: 'secr$t')
49
- api.switch_on!(switch[:uuid])
50
- api.switch_off!(switch[:uuid])
49
+ api.turn_switch!(switch[:uuid], :on)
50
+ api.turn_switch!(switch[:uuid], :off)
51
51
  ```
52
52
 
53
- ### Check if switch is on or off
53
+ Or :
54
+
55
+ ```ruby
56
+ api.turn_switch!(switch[:uuid], true)
57
+ api.turn_switch!(switch[:uuid], false)
58
+ ```
59
+
60
+ ### Check if switch is on
54
61
 
55
62
  ```ruby
56
63
  require 'ewelink'
57
64
 
58
65
  api = Ewelink::Api.new(phone_number: '+687 414243', password: 'secr$t')
59
66
  puts api.switch_on?(switch[:uuid])
60
- puts api.switch_off?(switch[:uuid])
61
67
  ```
62
68
 
63
69
  ### Press RF bridge button
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.1.0
data/lib/ewelink/api.rb CHANGED
@@ -77,18 +77,6 @@ module Ewelink
77
77
  end.tap { |buttons| Ewelink.logger.debug(self.class.name) { "Found #{buttons.size} RF 433MHz bridge button(s)" } }
78
78
  end
79
79
 
80
- def switch_off!(uuid)
81
- update_switch_on!(uuid, false)
82
- end
83
-
84
- def switch_off?(uuid)
85
- !switch_on?(uuid)
86
- end
87
-
88
- def switch_on!(uuid)
89
- update_switch_on!(uuid, true)
90
- end
91
-
92
80
  def switch_on?(uuid)
93
81
  switch = find_switch!(uuid)
94
82
  params = {
@@ -118,6 +106,27 @@ module Ewelink
118
106
  end.tap { |switches| Ewelink.logger.debug(self.class.name) { "Found #{switches.size} switch(es)" } }
119
107
  end
120
108
 
109
+ def turn_switch!(uuid, on)
110
+ if ['on', :on, 'true'].include?(on)
111
+ on = true
112
+ elsif ['off', :off, 'false'].include?(on)
113
+ on = false
114
+ end
115
+ switch = find_switch!(uuid)
116
+ params = {
117
+ 'appid' => APP_ID,
118
+ 'deviceid' => switch[:device_id],
119
+ 'nonce' => nonce,
120
+ 'params' => {
121
+ 'switch' => on ? 'on' : 'off',
122
+ },
123
+ 'ts' => Time.now.to_i,
124
+ 'version' => VERSION,
125
+ }
126
+ http_request(:post, '/api/user/device/status', body: JSON.generate(params), headers: authentication_headers)
127
+ true
128
+ end
129
+
121
130
  private
122
131
 
123
132
  def authentication_headers
@@ -203,22 +212,6 @@ module Ewelink
203
212
  (@mutexs[name] ||= Mutex.new).synchronize(&block)
204
213
  end
205
214
 
206
- def update_switch_on!(uuid, on)
207
- switch = find_switch!(uuid)
208
- params = {
209
- 'appid' => APP_ID,
210
- 'deviceid' => switch[:device_id],
211
- 'nonce' => nonce,
212
- 'params' => {
213
- 'switch' => on ? 'on' : 'off',
214
- },
215
- 'ts' => Time.now.to_i,
216
- 'version' => VERSION,
217
- }
218
- http_request(:post, '/api/user/device/status', body: JSON.generate(params), headers: authentication_headers)
219
- true
220
- end
221
-
222
215
  end
223
216
 
224
217
  end
@@ -6,8 +6,8 @@ module Ewelink
6
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
- options[:switches_on_uuids].each { |uuid| api.switch_on!(uuid) }
10
- options[:switches_off_uuids].each { |uuid| api.switch_off!(uuid) }
9
+ options[:turn_switches_on_uuids].each { |uuid| api.turn_switch!(uuid, :on) }
10
+ options[:turn_switches_off_uuids].each { |uuid| api.turn_switch!(uuid, :off) }
11
11
  options[:press_rf_bridge_buttons_uuids].each { |uuid| api.press_rf_bridge_button!(uuid) }
12
12
  end
13
13
 
@@ -15,7 +15,7 @@ module Ewelink
15
15
 
16
16
  def options
17
17
  @options ||= begin
18
- options = { press_rf_bridge_buttons_uuids: [], switches_off_uuids: [], switches_on_uuids: [] }
18
+ options = { press_rf_bridge_buttons_uuids: [], turn_switches_off_uuids: [], turn_switches_on_uuids: [] }
19
19
  parser = OptionParser.new do |opts|
20
20
  opts.banner = 'Manage eWeLink smart home devices'
21
21
  opts.version = File.read(File.expand_path('../../VERSION', __dir__)).strip
@@ -37,11 +37,11 @@ module Ewelink
37
37
  opts.on('--list-rf-bridge-buttons', 'List all RF 433MHz bridge buttons in JSON format') do
38
38
  options[:list_rf_bridge_buttons] = true
39
39
  end
40
- opts.on('--switch-on SWITCH_UUID', 'Set the switch with specified UUID on') do |uuid|
41
- options[:switches_on_uuids] << uuid
40
+ opts.on('--turn-switch-on SWITCH_UUID', 'Turn the switch with specified UUID on') do |uuid|
41
+ options[:turn_switches_on_uuids] << uuid
42
42
  end
43
- opts.on('--switch-off SWITCH_UUID', 'Set the switch with specified UUID off') do |uuid|
44
- options[:switches_off_uuids] << uuid
43
+ opts.on('--turn-switch-off SWITCH_UUID', 'Turn the switch with specified UUID off') do |uuid|
44
+ options[:turn_switches_off_uuids] << uuid
45
45
  end
46
46
  opts.on('--press-rf-bridge-button BUTTON_UUID', 'Press RF 433MHz bridge button with specified UUID') do |uuid|
47
47
  options[:press_rf_bridge_buttons_uuids] << uuid
@@ -61,7 +61,7 @@ module Ewelink
61
61
  STDERR.puts(parser.summarize)
62
62
  exit(1)
63
63
  end
64
- if [:list_switches, :list_rf_bridge_buttons, :switches_on_uuids, :switches_off_uuids, :press_rf_bridge_buttons_uuids].map { |action| options[action] }.all?(&:blank?)
64
+ if [:list_switches, :list_rf_bridge_buttons, :turn_switches_on_uuids, :turn_switches_off_uuids, :press_rf_bridge_buttons_uuids].map { |action| options[action] }.all?(&:blank?)
65
65
  STDERR.puts('An action must be specified (listing switches, press RF bridge button, etc.)')
66
66
  STDERR.puts(parser.summarize)
67
67
  exit(1)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ewelink
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexis Toulotte