Class: OpenHAB::DSL::Things::ThingBuilder
- Inherits:
-
Object
- Object
- OpenHAB::DSL::Things::ThingBuilder
- Defined in:
- lib/openhab/dsl/things/builder.rb
Overview
The ThingBuilder DSL allows you to customize a thing
Direct Known Subclasses
Instance Attribute Summary collapse
-
#bridge_uid ⇒ Core::Things::ThingUID?
readonly
The bridge of this thing.
-
#channels ⇒ Array<ChannelBuilder>
readonly
Explicitly configured channels on this thing.
-
#config ⇒ Hash?
readonly
The config for this thing.
-
#enabled ⇒ true, ...
readonly
If the thing should be enabled after it is created.
-
#label ⇒ String?
The label for this thing.
-
#location ⇒ String?
The location for this thing.
-
#thing_type_uid ⇒ ThingTypeUID
readonly
The type of this thing.
-
#uid ⇒ Core::Things::ThingUID
readonly
The id for this thing.
Instance Method Summary collapse
-
#channel(*args, **kwargs, &block) ⇒ Core::Things::Channel
Add an explicitly configured channel to this item.
-
#initialize(uid, label = nil, binding: nil, type: nil, bridge: nil, location: nil, config: {}, enabled: nil) ⇒ ThingBuilder
constructor
Constructor for ThingBuilder.
Constructor Details
#initialize(uid, label = nil, binding: nil, type: nil, bridge: nil, location: nil, config: {}, enabled: nil) ⇒ ThingBuilder
Constructor for ThingBuilder
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_uid ⇒ Core::Things::ThingUID? (readonly)
The bridge of this thing
136 137 138 |
# File 'lib/openhab/dsl/things/builder.rb', line 136 def bridge_uid @bridge_uid end |
#channels ⇒ Array<ChannelBuilder> (readonly)
Explicitly configured channels on this thing
145 146 147 |
# File 'lib/openhab/dsl/things/builder.rb', line 145 def channels @channels end |
#config ⇒ Hash? (readonly)
The config for this thing
139 140 141 |
# File 'lib/openhab/dsl/things/builder.rb', line 139 def config @config end |
#enabled ⇒ true, ... (readonly)
If the thing should be enabled after it is created
142 143 144 |
# File 'lib/openhab/dsl/things/builder.rb', line 142 def enabled @enabled end |
#label ⇒ String?
The label for this thing
124 125 126 |
# File 'lib/openhab/dsl/things/builder.rb', line 124 def label @label end |
#location ⇒ String?
The location for this thing
127 128 129 |
# File 'lib/openhab/dsl/things/builder.rb', line 127 def location @location end |
#thing_type_uid ⇒ ThingTypeUID (readonly)
The type of this thing
133 134 135 |
# File 'lib/openhab/dsl/things/builder.rb', line 133 def thing_type_uid @thing_type_uid end |
#uid ⇒ Core::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 |