class Hass::Domain

Base class for all domains (lights, switches, media_player…)

Constants

DATA

Just to make sure, the constant exists

Attributes

client[RW]
entity_id[R]

Public Class Methods

new(entity_id) click to toggle source
# File lib/hass/domain.rb, line 10
def initialize(entity_id)
  @entity_id = entity_id
end

Public Instance Methods

attributes() click to toggle source
# File lib/hass/domain.rb, line 47
def attributes
  state_data['attributes']
end
check_params(method_name, given_params) click to toggle source
# File lib/hass/domain.rb, line 18
def check_params(method_name, given_params)
  required_fields(method_name).each do |required_field|
    next if given_params.key?(required_field)

    raise "Parameter #{required_field} might be missing. #{method_help(method_name)}"
  end
end
data() click to toggle source
# File lib/hass/domain.rb, line 55
def data
  self.class.const_get('DATA')
end
execute_service(service, params = {}) click to toggle source
# File lib/hass/domain.rb, line 35
def execute_service(service, params = {})
  params['entity_id'] = @entity_id
  @client.post("/services/#{data['domain']}/#{service}", params)
rescue RuntimeError => error
  puts "Rescuing from #{error.class}: #{error}"
  check_params(service, params)
end
method_help(method_name) click to toggle source

Returns a method description as a help text

# File lib/hass/domain.rb, line 27
def method_help(method_name)
  param_help = required_fields(method_name).map { |name| "#{name}: #{name}_value" }
  method_hint = "#{method_name}(#{param_help.join(', ')})"
  fields = data['services'][method_name]['fields']
  method_description = fields.keys.map { |field| "#{field}: #{fields[field]['description']}" }.join("\n")
  "Hint: you can call this method with #{method_hint}\n#{method_description}"
end
required_fields(method_name) click to toggle source
# File lib/hass/domain.rb, line 14
def required_fields(method_name)
  data['services'][method_name]['fields'].keys.reject { |name| name == 'entity_id' }
end
state() click to toggle source
# File lib/hass/domain.rb, line 51
def state
  state_data['state']
end
state_data() click to toggle source
# File lib/hass/domain.rb, line 43
def state_data
  @client.get("/states/#{@entity_id}")
end