Class: Ewelink::Api
- Inherits:
-
Object
- Object
- Ewelink::Api
- Defined in:
- lib/ewelink/api.rb
Constant Summary collapse
- APP_ID =
'Uw83EKZFxdif7XFXEsrpduz5YyjP7nTl'.freeze
- APP_SECRET =
'mXLOjea0woSMvK9gw7Fjsy7YlFO4iSu6'.freeze
- DEFAULT_COUNTRY_CODE =
'+44'.freeze
- DEFAULT_REGION =
'us'.freeze
- REQUEST_TIMEOUT =
10.seconds
- RF_BRIDGE_DEVICE_UIID =
28
- SWITCH_DEVICES_UIIDS =
[1, 5, 6, 24].freeze
- URL =
'https://#{region}-apia.coolkit.cc'.freeze
- VERSION =
8
Instance Attribute Summary collapse
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#phone_number ⇒ Object
readonly
Returns the value of attribute phone_number.
Instance Method Summary collapse
- #async_actions? ⇒ Boolean
-
#initialize(password:, async_actions: false, email: nil, phone_number: nil) ⇒ Api
constructor
A new instance of Api.
- #press_rf_bridge_button!(uuid) ⇒ Object
- #reload ⇒ Object
- #rf_bridge_buttons ⇒ Object
- #switch_on?(uuid) ⇒ Boolean
- #switches ⇒ Object
- #turn_switch!(uuid, on) ⇒ Object
Constructor Details
#initialize(password:, async_actions: false, email: nil, phone_number: nil) ⇒ Api
Returns a new instance of Api.
17 18 19 20 21 22 23 24 25 |
# File 'lib/ewelink/api.rb', line 17 def initialize(password:, async_actions: false, email: nil, phone_number: nil) @async_actions = async_actions.present? @email = email.presence.try(:strip) @mutexs = {} @password = password.presence || raise(Error.new(':password must be specified')) @phone_number = phone_number.presence.try(:strip) raise(Error.new(':email or :phone_number must be specified')) if email.blank? && phone_number.blank? end |
Instance Attribute Details
#email ⇒ Object (readonly)
Returns the value of attribute email.
15 16 17 |
# File 'lib/ewelink/api.rb', line 15 def email @email end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
15 16 17 |
# File 'lib/ewelink/api.rb', line 15 def password @password end |
#phone_number ⇒ Object (readonly)
Returns the value of attribute phone_number.
15 16 17 |
# File 'lib/ewelink/api.rb', line 15 def phone_number @phone_number end |
Instance Method Details
#async_actions? ⇒ Boolean
27 28 29 |
# File 'lib/ewelink/api.rb', line 27 def async_actions? @async_actions end |
#press_rf_bridge_button!(uuid) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ewelink/api.rb', line 31 def (uuid) process_action do synchronize(:press_rf_bridge_button) do = (uuid) headers = authentication_headers.merge( 'X-CK-Appid' => APP_ID, 'X-CK-Nonce' => nonce, ) params = { 'type' => 1, 'id' => [:device_id], 'params' => { 'cmd' => 'transmit', 'rfChl' => [:channel], }, } body = JSON.generate(params) response = rest_request(:post, '/v2/device/thing/status', headers:, body:) response['error'] == 0 end end end |
#reload ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ewelink/api.rb', line 54 def reload Ewelink.logger.debug(self.class.name) { 'Reloading API (authentication token, api key, devices, region, connections,...)' } %i( @authentication_infos @devices @homes_ids @region @rf_bridge_buttons @switches ).each do |variable| remove_instance_variable(variable) if instance_variable_defined?(variable) end self end |
#rf_bridge_buttons ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ewelink/api.rb', line 70 def synchronize(:rf_bridge_buttons) do @rf_bridge_buttons ||= [].tap do || rf_bridge_devices = devices.select { |device| device['itemData']['extra']['uiid'] == RF_BRIDGE_DEVICE_UIID }.tap do |devices| Ewelink.logger.debug(self.class.name) { "Found #{devices.size} RF 433MHz bridge device(s)" } end rf_bridge_devices.each do |device| api_key = device['itemData']['apikey'].presence || next device_id = device['itemData']['deviceid'].presence || next device_name = device['itemData']['name'].presence || next = device['itemData']['params']['rfList'].each do |rf| = { api_key:, channel: rf['rfChl'], device_id:, device_name:, } remote_info = device['itemData']['tags']['zyx_info'].find { |info| info['buttonName'].find { |data| data.key?([:channel].to_s) } }.presence || next remote_name = remote_info['name'].try(:squish).presence || next = remote_info['buttonName'].find { |info| info.key?([:channel].to_s) }.presence || next = .values.first.try(:squish).presence || next .merge!({ name: , remote_name:, remote_type: remote_info['remote_type'], }) [:uuid] = Digest::UUID.uuid_v5(Digest::UUID::DNS_NAMESPACE, "#{[:device_id]}/#{[:channel]}") << end end end.tap { || Ewelink.logger.debug(self.class.name) { "Found #{.size} RF 433MHz bridge button(s)" } } end end |
#switch_on?(uuid) ⇒ Boolean
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/ewelink/api.rb', line 104 def switch_on?(uuid) switch = find_switch!(uuid) headers = authentication_headers.merge( 'X-CK-Appid' => APP_ID, 'X-CK-Nonce' => nonce, ) params = { 'type' => 1, 'id' => switch[:device_id], 'params' => 'switch', } Ewelink.logger.debug(self.class.name) { "Checking switch #{switch[:uuid].inspect} status" } response = rest_request(:get, '/v2/device/thing/status', headers:, query: params) response['data']['params']['switch'] == 'on' end |
#switches ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ewelink/api.rb', line 120 def switches synchronize(:switches) do @switches ||= [].tap do |switches| switch_devices = devices.select { |device| SWITCH_DEVICES_UIIDS.include?(device['itemData']['extra']['uiid']) } switch_devices.each do |device| api_key = device['itemData']['apikey'].presence || next device_id = device['itemData']['deviceid'].presence || next name = device['itemData']['name'].presence || next switch = { api_key:, device_id:, model: device['itemData']['productModel'], name:, } switch[:uuid] = Digest::UUID.uuid_v5(Digest::UUID::DNS_NAMESPACE, switch[:device_id]) switches << switch end end.tap { |switches| Ewelink.logger.debug(self.class.name) { "Found #{switches.size} switch(es)" } } end end |
#turn_switch!(uuid, on) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/ewelink/api.rb', line 141 def turn_switch!(uuid, on) process_action do if ['on', :on, 'true'].include?(on) on = true elsif ['off', :off, 'false'].include?(on) on = false end switch = find_switch!(uuid) headers = authentication_headers.merge( 'X-CK-Appid' => APP_ID, 'X-CK-Nonce' => nonce, ) params = { 'type' => 1, 'id' => switch[:device_id], 'params' => { 'switch' => on ? 'on' : 'off', }, } body = JSON.generate(params) Ewelink.logger.debug(self.class.name) { "Turning switch #{switch[:uuid].inspect} #{on ? 'on' : 'off'}" } response = rest_request(:post, '/v2/device/thing/status', headers:, body:) response['error'] == 0 end end |