Module: MQTT::Homie::HomieAttribute::InstanceMethods
- Defined in:
- lib/mqtt/homie/homie_attribute.rb
Instance Method Summary collapse
-
#homie_attr_init(data = {}) ⇒ Object
initialize all homie attributes from the given hash.
-
#homie_attr_set(name, value) ⇒ Object
set attribute with validation.
-
#homie_attr_set!(name, value) ⇒ Object
set attribute without validation.
-
#homie_attr_validate(name, value) ⇒ Object
attribute validation.
- #homie_attributes ⇒ Object
Instance Method Details
#homie_attr_init(data = {}) ⇒ Object
initialize all homie attributes from the given hash
44 45 46 47 48 49 50 51 |
# File 'lib/mqtt/homie/homie_attribute.rb', line 44 def homie_attr_init(data = {}) self.class.homie_attr_list.each do |name, | #puts "name: #{name}, default: #{options[:default]}, options: #{options.inspect}" value = data.include?(name) ? data[name] : [:default] value = value.call(self) if value.kind_of?(Proc) homie_attr_set(name, value) end end |
#homie_attr_set(name, value) ⇒ Object
set attribute with validation
59 60 61 62 |
# File 'lib/mqtt/homie/homie_attribute.rb', line 59 def homie_attr_set(name, value) homie_attr_validate(name, value) homie_attr_set!(name, value) end |
#homie_attr_set!(name, value) ⇒ Object
set attribute without validation
54 55 56 |
# File 'lib/mqtt/homie/homie_attribute.rb', line 54 def homie_attr_set!(name, value) instance_variable_set("@#{name}", value) end |
#homie_attr_validate(name, value) ⇒ Object
attribute validation
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/mqtt/homie/homie_attribute.rb', line 80 def homie_attr_validate(name, value) = self.class.(name) if value.nil? required = [:required] required = required.call(self) if required.kind_of?(Proc) raise "#{name} is required for #{object_type} #{@id}" if required end datatype = [:datatype] if datatype && !value.nil? && !datatype_match?(datatype, value) raise "expected #{name} to be a #{datatype} for #{object_type} #{@id}" end enum = [:enum] if enum.kind_of?(Array) && !value.nil? && !enum.include?(value.to_sym) raise "expected #{name} (#{value}) to be one of #{enum.join(",")}" end end |
#homie_attributes ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mqtt/homie/homie_attribute.rb', line 64 def homie_attributes data = {} # if this object has an id, it needs a $ attribute prefix. # otherwise assume it is a hierarchical attribute like $stats/* or $fw/* attrib_prefix = self.class.homie_has_id? ? "$" : "" self.class.homie_attr_list.each do |name, | next if [:hidden] key = [:topic] || (attrib_prefix + name.to_s) value = instance_variable_get("@#{name}") next if value == nil data[key] = value.kind_of?(Array) ? value.collect { |i| i.id }.join(",") : value end data end |