Class: RubyMyq::System

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_myq/system.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, pass) ⇒ System

Returns a new instance of System.



9
10
11
12
13
14
15
# File 'lib/ruby_myq/system.rb', line 9

def initialize(user, pass)
  @username = user
  @password = pass
  (@username, @password)
  
  discover_endpoints
end

Instance Attribute Details

#garage_doorsObject (readonly)

Returns the value of attribute garage_doors.



8
9
10
# File 'lib/ruby_myq/system.rb', line 8

def garage_doors
  @garage_doors
end

Instance Method Details

#discover_endpointsObject



17
18
19
20
21
22
23
24
# File 'lib/ruby_myq/system.rb', line 17

def discover_endpoints
  empty_device_arrays
  response = request_device_list
  devices = response['items']
  devices.each do |device|
    instantiate_device(device, @headers)
  end
end

#empty_device_arraysObject



98
99
100
101
102
# File 'lib/ruby_myq/system.rb', line 98

def empty_device_arrays
  @gateways = []
  @garage_doors = []
  @lights = []
end

#find_door_by_id(id) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/ruby_myq/system.rb', line 26

def find_door_by_id(id)
  id = id.to_s
  @garage_doors.each do |door|
    return door if door.id == id
  end
  nil
end

#find_door_by_name(name) ⇒ Object



34
35
36
37
38
39
# File 'lib/ruby_myq/system.rb', line 34

def find_door_by_name(name)
  @garage_doors.each do |door|
    return door if door.name == name
  end
  nil
end

#instantiate_device(device, _headers) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/ruby_myq/system.rb', line 104

def instantiate_device(device, _headers)
  if device['device_type'] == 'garagedooropener'
    @garage_doors << RubyMyq::Device::GarageDoor.new(device, @headers)
    # elsif device["device_type"] == "hub"
    #     @gateways << RubyMyq::Device::Gateway.new(device, self)
    # elsif device["MyQDeviceTypeName"]=="???"
    # I need a MyQ light switch to implement this feature
    # @lights << RubyMyq::Device::LightSwitch.new(device)
  end
end

#login(username, password) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ruby_myq/system.rb', line 57

def (username, password)
  options = {
    headers: RubyMyq::HEADERS,
    body: { username: username, password: password }.to_json,
    format: :json
    # debug_output: STDOUT
  }

  response = HTTParty.post(, options)
  @security_token = response['SecurityToken']
  @headers = {}.dup
  @headers.merge!(RubyMyq::HEADERS)
  @headers.merge!(SecurityToken: @security_token)
  # @cached_login_response = response
  # "logged in successfully"
end

#login_uriObject

private



43
44
45
46
47
48
# File 'lib/ruby_myq/system.rb', line 43

def 
  uri = ''.dup
  uri << "https://#{RubyMyq::HOST_URI}/"
  uri << "#{RubyMyq::API_VERSION}/"
  uri << RubyMyq::LOGIN_ENDPOINT
end

#request_accountObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ruby_myq/system.rb', line 74

def 
  options = {
    headers: @headers,
    body: { expand: 'account' }.to_json,
    format: :json
    # debug_output: STDOUT
  }

  response = HTTParty.get(, options)
  @account_uri = response['Account']['href'].gsub(/v5/, 'v5.1')
end

#request_account_uriObject



50
51
52
53
54
55
# File 'lib/ruby_myq/system.rb', line 50

def 
  uri = ''.dup
  uri << "https://#{RubyMyq::HOST_URI}/"
  uri << "#{RubyMyq::API_VERSION}/"
  uri << RubyMyq::ACCOUNT_ENDPOINT
end

#request_device_listObject



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ruby_myq/system.rb', line 86

def request_device_list
  uri = "#{@account_uri}/#{RubyMyq::DEVICE_LIST_ENDPOINT}"

  options = {
    headers: @headers,
    format: :json
    # debug_output: STDOUT
  }

  HTTParty.get(uri, options)
end