class MQTT::Homie::Node

Attributes

device[R]
type[R]

Public Class Methods

new(device, id, name, type) click to toggle source
Calls superclass method MQTT::Homie::Base::new
# File lib/mqtt/homie/node.rb, line 8
def initialize(device, id, name, type)
  super(id, name)
  @device = device
  @type = type
  @properties = {}
  @published = false
end

Public Instance Methods

[](id) click to toggle source
# File lib/mqtt/homie/node.rb, line 40
def [](id)
  @properties[id]
end
batch_update(hash) click to toggle source

takes a hash with property names as keys, and values as values

# File lib/mqtt/homie/node.rb, line 53
def batch_update(hash)
  mqtt.batch_publish do
    hash.each do |(k, v)|
      self[k].value = v
    end
  end
end
each(&block) click to toggle source
# File lib/mqtt/homie/node.rb, line 44
def each(&block)
  @properties.each_value(&block)
end
mqtt() click to toggle source
# File lib/mqtt/homie/node.rb, line 48
def mqtt
  device.mqtt
end
property(*args, &block) click to toggle source
# File lib/mqtt/homie/node.rb, line 20
               def property(*args, &block)
  device.init do |prior_state|
    property = Property.new(self, *args, &block)
    raise ArgumentError, "Property '#{property.id}' already exists on '#{id}'" if @properties.key?(property.id)
    @properties[property.id] = property
    property.publish if prior_state == :ready
    property
  end
end
publish() click to toggle source
# File lib/mqtt/homie/node.rb, line 61
def publish
  mqtt.batch_publish do
    unless @published
      mqtt.publish("#{topic}/$name", name, retain: true, qos: 1)
      mqtt.publish("#{topic}/$type", @type.to_s, retain: true, qos: 1)
      @published = true
    end

    mqtt.publish("#{topic}/$properties", @properties.keys.join(","), retain: true, qos: 1)
    @properties.each_value(&:publish)
  end
end
remove_property(id) click to toggle source
# File lib/mqtt/homie/node.rb, line 30
def remove_property(id)
  return false unless (property = @properties[id])
  init do
    property.unpublish
    @properties.delete(id)
    mqtt.publish("#{topic}/$properties", @properties.keys.join(","), retain: true, qos: 1) if @published
  end
  true
end
topic() click to toggle source
# File lib/mqtt/homie/node.rb, line 16
def topic
  "#{device.topic}/#{id}"
end
unpublish() click to toggle source
# File lib/mqtt/homie/node.rb, line 74
def unpublish
  return unless @published
  @published = false

  mqtt.publish("#{topic}/$name", retain: true, qos: 0)
  mqtt.publish("#{topic}/$type", retain: true, qos: 0)
  mqtt.publish("#{topic}/$properties", retain: true, qos: 0)

  @properties.each_value(&:unpublish)
end