Class: Hass::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/hass/client.rb

Overview

The HomeAssistant server

Instance Method Summary collapse

Constructor Details

#initialize(host, port, token, base_path = '/api') ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
16
# File 'lib/hass/client.rb', line 9

def initialize(host, port, token, base_path = '/api')
  @host = host
  @port = port
  @token = token
  @base_path = base_path
  prepare_domains
  prepare_methods
end

Instance Method Details

#domainsObject



65
66
67
# File 'lib/hass/client.rb', line 65

def domains
  @domains ||= get('/services')
end

#get(path) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/hass/client.rb', line 18

def get(path)
  path = @base_path + path
  request = Net::HTTP::Get.new path
  header.each_pair { |field, content| request[field] = content }
  response = send_http(request)
  parse(response.body)
end

#handle_http_error(response) ⇒ Object



46
47
48
# File 'lib/hass/client.rb', line 46

def handle_http_error(response)
  raise "Server returned #{response.code}: #{response.body}"
end

#headerObject



57
58
59
60
61
62
63
# File 'lib/hass/client.rb', line 57

def header
  {
    'Content-Type' => 'application/json',
    'Authorization' => "Bearer #{@token}",
    'Accept-Encoding' => 'identity'
  }
end

#parse(response_text) ⇒ Object



50
51
52
53
54
55
# File 'lib/hass/client.rb', line 50

def parse(response_text)
  JSON.parse(response_text)
rescue JSON::ParserError => e
  puts "Cannot parse JSON data: #{e}"
  puts response_text
end

#post(path, data) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/hass/client.rb', line 26

def post(path, data)
  path = @base_path + path
  request = Net::HTTP::Post.new path
  request.body = data.to_json
  header.each_pair { |field, content| request[field] = content }
  response = send_http(request)
  parse(response.body)
end

#prepare_domainsObject



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/hass/client.rb', line 77

def prepare_domains
  domains.each do |domain|
    domain_name = snake_to_camel(domain['domain'])
    domain_class = Class.new(Domain)
    domain_class.const_set('DATA', domain)
    domain['services'].keys.each do |service|
      domain_class.send(:define_method, service.to_sym) do |params = {}|
        execute_service(service, params)
      end
    end
    Hass.const_set(domain_name, domain_class)
  end
end

#prepare_methodsObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/hass/client.rb', line 91

def prepare_methods
  domains.each do |domain|
    domain_name = snake_to_camel(domain['domain'])
    self.class.send(:define_method, domain['domain'].to_sym) do |entity_id|
      domain_class = Hass.const_get(domain_name).new(entity_id)
      domain_class.client = self
      domain_class
    end
  end
end

#send_http(request) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/hass/client.rb', line 35

def send_http(request)
  Net::HTTP.start(@host, @port, use_ssl: true) do |http|
    response = http.request request
    if response.code.to_i > 299
      handle_http_error(response)
      return {} # if handle does not throw an exception
    end
    return response
  end
end

#snake_to_camel(text) ⇒ Object



73
74
75
# File 'lib/hass/client.rb', line 73

def snake_to_camel(text)
  text.split('_').collect(&:capitalize).join
end

#statesObject



69
70
71
# File 'lib/hass/client.rb', line 69

def states
  get('/states')
end