Class: OpenHAB::DSL::Things::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/openhab/dsl/things/builder.rb

Overview

A thing builder allows you to dynamically create openHAB things at runtime. This can be useful either to create things as soon as the script loads, or even later based on a rule executing.

Examples:

Create a Thing from the Astro Binding

things.build do
  thing "astro:sun:home", "Astro Sun Data", config: { "geolocation" => "0,0" }
end

Create a Thing with Channels

thing_config = {
  availabilityTopic: "my-switch/status",
  payloadAvailable: "online",
  payloadNotAvailable: "offline"
}
things.build do
  thing("mqtt:topic:my-switch", "My Switch", bridge: "mqtt:broker:mosquitto", config: thing_config) do
    channel("switch1", "switch", config: {
      stateTopic: "stat/my-switch/switch1/state", commandTopic: "cmnd/my-switch/switch1/command"
    })
    channel("button1", "string", config: {
      stateTopic: "stat/my-switch/button1/state", commandTopic: "cmnd/my-switch/button1/command"
    })
  end
end

Create a Thing within a Bridge

things.build do
  bridge "mqtt:broker:mosquitto", config: { host: "127.0.0.1", enableDiscovery: false } do
    thing "mqtt:topic:window1", "My Window Sensor" do
      channel "contact1", "contact", config: {
        stateTopic: "zigbee2mqtt/window1/contact",
        on: "false",
        off: "true"
      }
    end
  end
end

items.build do
  contact_item Window1_Contact, channel: "mqtt:topic:window1:contact1"
end

Create a Thing separately from the Bridge

things.build do
  bridge = bridge "mqtt:broker:mosquitto", config: { host: "127.0.0.1", enableDiscovery: false }

  thing "mqtt:topic:window1", "My Window Sensor", bridge: bridge do
    channel "contact1", "contact", config: {
      stateTopic: "zigbee2mqtt/window1/contact",
      on: "false",
      off: "true"
    }
  end
end

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider, update: false) ⇒ Builder

Returns a new instance of Builder.



73
74
75
76
# File 'lib/openhab/dsl/things/builder.rb', line 73

def initialize(provider, update: false)
  @provider = Core::Things::Provider.current(provider)
  @update = update
end

Instance Attribute Details

#providerorg.openhab.core.thing.ManagedThingProvider (readonly)

Returns:

  • (org.openhab.core.thing.ManagedThingProvider)


71
72
73
# File 'lib/openhab/dsl/things/builder.rb', line 71

def provider
  @provider
end

Instance Method Details

#bridge(*args, **kwargs, &block) ⇒ Object

Create a new Bridge



80
81
82
# File 'lib/openhab/dsl/things/builder.rb', line 80

def bridge(*args, **kwargs, &block)
  build(BridgeBuilder, *args, **kwargs, &block)
end

#thing(*args, **kwargs, &block) ⇒ Object

Create a new Thing



86
87
88
# File 'lib/openhab/dsl/things/builder.rb', line 86

def thing(*args, **kwargs, &block)
  build(ThingBuilder, *args, **kwargs, &block)
end