class Hass::Client

The HomeAssistant server

Public Class Methods

new(host, port, token, base_path = '/api') click to toggle source
# 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

Public Instance Methods

domains() click to toggle source
# File lib/hass/client.rb, line 65
def domains
  @domains ||= get('/services')
end
get(path) click to toggle source
# 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) click to toggle source
# File lib/hass/client.rb, line 46
def handle_http_error(response)
  raise "Server returned #{response.code}: #{response.body}"
end
header() click to toggle source
# File lib/hass/client.rb, line 57
def header
  {
    'Content-Type' => 'application/json',
    'Authorization' => "Bearer #{@token}",
    'Accept-Encoding' => 'identity'
  }
end
parse(response_text) click to toggle source
# 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) click to toggle source
# 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_domains() click to toggle source
# 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_methods() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/hass/client.rb, line 73
def snake_to_camel(text)
  text.split('_').collect(&:capitalize).join
end
states() click to toggle source
# File lib/hass/client.rb, line 69
def states
  get('/states')
end