Class: OpenHAB::DSL::Things::ThingBuilder

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

Overview

The ThingBuilder DSL allows you to customize a thing

Direct Known Subclasses

BridgeBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uid, label = nil, binding: nil, type: nil, bridge: nil, location: nil, config: {}, enabled: nil) ⇒ ThingBuilder

Constructor for ThingBuilder

Parameters:

  • uid (String)

    The ThingUID for the created Thing. This can consist one or more segments separated by a colon. When the uid contains:

    • One segment: When the uid contains one segment, binding or bridge id must be provided.
    • Two segments: typeid:thingid The binding or bridge id must be provided.
    • Three or more segments: bindingid:typeid:[bridgeid...]:thingid. The type and bridge can be omitted
  • label (String) (defaults to: nil)

    The Thing's label.

  • binding (String) (defaults to: nil)

    The binding id. When this argument is not provided, the binding id must be deducible from the uid, type, or bridge.

  • type (String) (defaults to: nil)

    The type id. When this argument is not provided, it will be deducible from the uid if it contains two or more segments. To create a Thing with a blank type id, use one segment for uid and provide the binding id.

  • bridge (String, BridgeBuilder) (defaults to: nil)

    The bridge uid, if the Thing should belong to a bridge.

  • location (String, Item) (defaults to: nil)

    The location of this Thing. When given an Item, use the item's label as the location.

  • config (Hash) (defaults to: {})

    The Thing's configuration, as required by the binding. The key can be strings or symbols.

  • enabled (true, false) (defaults to: nil)

    Whether the Thing should be enabled or disabled.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/openhab/dsl/things/builder.rb', line 190

def initialize(uid, label = nil, binding: nil, type: nil, bridge: nil, location: nil, config: {}, enabled: nil)
  @channels = []
  uid = uid.to_s
  uid_segments = uid.split(org.openhab.core.common.AbstractUID::SEPARATOR)
  @bridge_uid = nil
  bridge = bridge.uid if bridge.is_a?(org.openhab.core.thing.Bridge) || bridge.is_a?(BridgeBuilder)
  bridge = bridge&.to_s
  bridge_segments = bridge&.split(org.openhab.core.common.AbstractUID::SEPARATOR) || []
  type = type&.to_s

  # infer missing components
  type ||= uid_segments[0] if uid_segments.length == 2
  type ||= uid_segments[1] if uid_segments.length > 2
  binding ||= uid_segments[0] if uid_segments.length > 2
  binding ||= bridge_segments[0] if bridge_segments && bridge_segments.length > 2

  if bridge
    bridge_segments.unshift(binding) if bridge_segments.length < 3
    @bridge_uid = org.openhab.core.thing.ThingUID.new(*bridge_segments)
  end

  thinguid = if uid_segments.length > 2
               [binding, type, uid_segments.last].compact
             else
               [binding, type, @bridge_uid&.id, uid_segments.last].compact
             end

  @uid = org.openhab.core.thing.ThingUID.new(*thinguid)
  @thing_type_uid = org.openhab.core.thing.ThingTypeUID.new(*@uid.all_segments[0..1])
  @label = label
  @location = location
  @location = location.label if location.is_a?(Item)
  @config = config.transform_keys(&:to_s)
  @enabled = enabled
  @builder = org.openhab.core.thing.binding.builder.ThingBuilder unless instance_variable_defined?(:@builder)
end

Instance Attribute Details

#bridge_uidCore::Things::ThingUID? (readonly)

The bridge of this thing

Returns:



136
137
138
# File 'lib/openhab/dsl/things/builder.rb', line 136

def bridge_uid
  @bridge_uid
end

#channelsArray<ChannelBuilder> (readonly)

Explicitly configured channels on this thing

Returns:



145
146
147
# File 'lib/openhab/dsl/things/builder.rb', line 145

def channels
  @channels
end

#configHash? (readonly)

The config for this thing

Returns:

  • (Hash, nil)


139
140
141
# File 'lib/openhab/dsl/things/builder.rb', line 139

def config
  @config
end

#enabledtrue, ... (readonly)

If the thing should be enabled after it is created

Returns:

  • (true, false, nil)


142
143
144
# File 'lib/openhab/dsl/things/builder.rb', line 142

def enabled
  @enabled
end

#labelString?

The label for this thing

Returns:

  • (String, nil)


124
125
126
# File 'lib/openhab/dsl/things/builder.rb', line 124

def label
  @label
end

#locationString?

The location for this thing

Returns:

  • (String, nil)


127
128
129
# File 'lib/openhab/dsl/things/builder.rb', line 127

def location
  @location
end

#thing_type_uidThingTypeUID (readonly)

The type of this thing

Returns:

  • (ThingTypeUID)


133
134
135
# File 'lib/openhab/dsl/things/builder.rb', line 133

def thing_type_uid
  @thing_type_uid
end

#uidCore::Things::ThingUID (readonly)

The id for this thing



130
131
132
# File 'lib/openhab/dsl/things/builder.rb', line 130

def uid
  @uid
end

Instance Method Details

#channel(*args, **kwargs, &block) ⇒ Core::Things::Channel

Add an explicitly configured channel to this item



230
231
232
233
234
# File 'lib/openhab/dsl/things/builder.rb', line 230

def channel(*args, **kwargs, &block)
  channel = ChannelBuilder.new(*args, thing: self, **kwargs)
  channel.instance_eval(&block) if block
  channel.build.tap { |c| @channels << c }
end