Class: OpenHAB::Core::Things::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/openhab/core/things/channel.rb

Overview

Channel is a part of a Thing that represents a functionality of it. Therefore Items can be linked a to a channel.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#channel_typeChannelType (readonly)

Returns:



51
52
53
# File 'lib/openhab/core/things/channel.rb', line 51

def channel_type
  ChannelType.registry.get_channel_type(channel_type_uid)
end

#channel_type_uidChannelTypeUID (readonly)

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/openhab/core/things/channel.rb', line 29

class Channel
  extend Forwardable

  delegate %i[item items thing] => :uid

  # @return [String]
  def inspect
    r = "#<OpenHAB::Core::Things::Channel #{uid}"
    r += " #{label.inspect}" if label
    r += " description=#{description.inspect}" if description
    r += " kind=#{kind.inspect}"
    r += " channel_type_uid=#{channel_type_uid.inspect}" if channel_type_uid
    r += " configuration=#{configuration.properties.to_h}" unless configuration.properties.empty?
    r += " properties=#{properties.to_h}" unless properties.empty?
    r += " default_tags=#{default_tags.to_a}" unless default_tags.empty?
    r += " auto_update_policy=#{auto_update_policy}" if auto_update_policy
    r += " accepted_item_type=#{accepted_item_type}" if accepted_item_type
    "#{r}>"
  end

  # @!attribute [r] channel_type
  # @return [ChannelType]
  def channel_type
    ChannelType.registry.get_channel_type(channel_type_uid)
  end

  # @return [String]
  def to_s
    uid.to_s
  end

  # @!attribute item_name [r]
  # Return the name of the item this channel is linked to. If a channel is linked to more than one item,
  # this method only returns the first item.
  #
  # @return [String, nil]
  def item_name
    item_names.first
  end

  # @!attribute item_names [r]
  # Return the names of all of the items this channel is linked to.
  #
  # @return [Array<String>]
  def item_names
    Things::Links::Provider.registry.get_linked_item_names(uid)
  end

  # @!attribute item [r]
  # Return the item this channel is linked to. If a channel is linked to more than one item,
  # this method only returns the first item.
  #
  # @return [Items::Item, nil]
  def item
    items.first
  end

  # @!attribute items [r]
  # Return all of the items this channel is linked to.
  #
  # @return [Array<Items::Item>]
  def items
    Things::Links::Provider.registry.get_linked_items(uid).map { |item| Items::Proxy.new(item) }
  end

  #
  # @!attribute links [r]
  # Returns all of the channel's links (items and link configurations).
  #
  # @return [Items::ItemChannelLinks] An array of ItemChannelLink or an empty array
  #
  # @example Get the configuration of the first link
  #   things["mqtt:topic:livingroom-light"].channel["power"].links.first.configuration
  #
  # @example Remove all managed links
  #   things["mqtt:topic:livingroom-light"].channel["power"].links.clear
  #
  # @see link
  # @see unlink
  #
  def links
    Items::ItemChannelLinks.new(uid, Things::Links::Provider.registry.get_links(uid))
  end

  #
  # @return [ItemChannelLink, nil]
  #
  # @overload link
  #   Returns the channel's link. If an channel is linked to more than one item,
  #   this method only returns the first link.
  #
  #   @return [Things::ItemChannelLink, nil]
  #
  # @overload link(item, config = {})
  #
  #   Links the channel to an item.
  #
  #   @param [String, Items::Item] item The item to link to.
  #   @param [Hash] config The configuration for the link.
  #
  #   @return [Things::ItemChannelLink] The created link.
  #
  #   @example Link a channel to an item
  #     things["mqtt:topic:livingroom-light"].channels["power"].link(LivingRoom_Light_Power)
  #
  #   @example Specify a link configuration
  #     things["mqtt:topic:outdoor-thermometer"].channels["temperature"].link(
  #       High_Temperature_Alert,
  #       profile: "system:hysteresis",
  #       lower: "29 °C",
  #       upper: "30 °C")
  #
  #   @see links
  #   @see unlink
  #
  def link(item = nil, config = nil)
    return Things::Links::Provider.registry.get_links(uid).first if item.nil? && config.nil?

    config ||= {}
    Core::Things::Links::Provider.create_link(item, self, config).tap do |new_link|
      provider = Core::Things::Links::Provider.current
      if !(current_link = provider.get(new_link.uid))
        provider.add(new_link)
      elsif current_link.configuration != config
        provider.update(new_link)
      end
    end
  end

  #
  # Removes a link to an item from managed link providers.
  #
  # @param [String, Items::Item] item The item to remove the link to.
  #
  # @return [Things::ItemChannelLink, nil] The removed link, if found.
  # @raise [FrozenError] if the link is not managed by a managed link provider.
  #
  # @see link
  # @see links
  #
  def unlink(item)
    link_to_delete = Things::Links::Provider.create_link(item, self, {})
    provider = Things::Links::Provider.registry.provider_for(link_to_delete.uid)
    unless provider.is_a?(ManagedProvider)
      raise FrozenError,
            "Cannot remove the link #{link_to_delete.uid} from non-managed provider #{provider.inspect}"
    end

    provider.remove(link_to_delete.uid)
  end

  # @deprecated OH3.4 this whole section is not needed in OH4+. Also see Thing#config_eql?
  if Core.version < Core::V4_0
    # @!visibility private
    module ChannelComparable
      # @!visibility private
      # This is only needed in OH3 because it is implemented in OH4 core
      def ==(other)
        return true if equal?(other)
        return false unless other.is_a?(Channel)

        %i[class
           uid
           label
           description
           kind
           channel_type_uid
           configuration
           properties
           default_tags
           auto_update_policy
           accepted_item_type].all? do |attr|
          send(attr) == other.send(attr)
        end
      end
    end
    org.openhab.core.thing.binding.builder.ChannelBuilder.const_get(:ChannelImpl).prepend(ChannelComparable)
  end
end

#itemItems::Item? (readonly)

Return the item this channel is linked to. If a channel is linked to more than one item, this method only returns the first item.

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/openhab/core/things/channel.rb', line 29

class Channel
  extend Forwardable

  delegate %i[item items thing] => :uid

  # @return [String]
  def inspect
    r = "#<OpenHAB::Core::Things::Channel #{uid}"
    r += " #{label.inspect}" if label
    r += " description=#{description.inspect}" if description
    r += " kind=#{kind.inspect}"
    r += " channel_type_uid=#{channel_type_uid.inspect}" if channel_type_uid
    r += " configuration=#{configuration.properties.to_h}" unless configuration.properties.empty?
    r += " properties=#{properties.to_h}" unless properties.empty?
    r += " default_tags=#{default_tags.to_a}" unless default_tags.empty?
    r += " auto_update_policy=#{auto_update_policy}" if auto_update_policy
    r += " accepted_item_type=#{accepted_item_type}" if accepted_item_type
    "#{r}>"
  end

  # @!attribute [r] channel_type
  # @return [ChannelType]
  def channel_type
    ChannelType.registry.get_channel_type(channel_type_uid)
  end

  # @return [String]
  def to_s
    uid.to_s
  end

  # @!attribute item_name [r]
  # Return the name of the item this channel is linked to. If a channel is linked to more than one item,
  # this method only returns the first item.
  #
  # @return [String, nil]
  def item_name
    item_names.first
  end

  # @!attribute item_names [r]
  # Return the names of all of the items this channel is linked to.
  #
  # @return [Array<String>]
  def item_names
    Things::Links::Provider.registry.get_linked_item_names(uid)
  end

  # @!attribute item [r]
  # Return the item this channel is linked to. If a channel is linked to more than one item,
  # this method only returns the first item.
  #
  # @return [Items::Item, nil]
  def item
    items.first
  end

  # @!attribute items [r]
  # Return all of the items this channel is linked to.
  #
  # @return [Array<Items::Item>]
  def items
    Things::Links::Provider.registry.get_linked_items(uid).map { |item| Items::Proxy.new(item) }
  end

  #
  # @!attribute links [r]
  # Returns all of the channel's links (items and link configurations).
  #
  # @return [Items::ItemChannelLinks] An array of ItemChannelLink or an empty array
  #
  # @example Get the configuration of the first link
  #   things["mqtt:topic:livingroom-light"].channel["power"].links.first.configuration
  #
  # @example Remove all managed links
  #   things["mqtt:topic:livingroom-light"].channel["power"].links.clear
  #
  # @see link
  # @see unlink
  #
  def links
    Items::ItemChannelLinks.new(uid, Things::Links::Provider.registry.get_links(uid))
  end

  #
  # @return [ItemChannelLink, nil]
  #
  # @overload link
  #   Returns the channel's link. If an channel is linked to more than one item,
  #   this method only returns the first link.
  #
  #   @return [Things::ItemChannelLink, nil]
  #
  # @overload link(item, config = {})
  #
  #   Links the channel to an item.
  #
  #   @param [String, Items::Item] item The item to link to.
  #   @param [Hash] config The configuration for the link.
  #
  #   @return [Things::ItemChannelLink] The created link.
  #
  #   @example Link a channel to an item
  #     things["mqtt:topic:livingroom-light"].channels["power"].link(LivingRoom_Light_Power)
  #
  #   @example Specify a link configuration
  #     things["mqtt:topic:outdoor-thermometer"].channels["temperature"].link(
  #       High_Temperature_Alert,
  #       profile: "system:hysteresis",
  #       lower: "29 °C",
  #       upper: "30 °C")
  #
  #   @see links
  #   @see unlink
  #
  def link(item = nil, config = nil)
    return Things::Links::Provider.registry.get_links(uid).first if item.nil? && config.nil?

    config ||= {}
    Core::Things::Links::Provider.create_link(item, self, config).tap do |new_link|
      provider = Core::Things::Links::Provider.current
      if !(current_link = provider.get(new_link.uid))
        provider.add(new_link)
      elsif current_link.configuration != config
        provider.update(new_link)
      end
    end
  end

  #
  # Removes a link to an item from managed link providers.
  #
  # @param [String, Items::Item] item The item to remove the link to.
  #
  # @return [Things::ItemChannelLink, nil] The removed link, if found.
  # @raise [FrozenError] if the link is not managed by a managed link provider.
  #
  # @see link
  # @see links
  #
  def unlink(item)
    link_to_delete = Things::Links::Provider.create_link(item, self, {})
    provider = Things::Links::Provider.registry.provider_for(link_to_delete.uid)
    unless provider.is_a?(ManagedProvider)
      raise FrozenError,
            "Cannot remove the link #{link_to_delete.uid} from non-managed provider #{provider.inspect}"
    end

    provider.remove(link_to_delete.uid)
  end

  # @deprecated OH3.4 this whole section is not needed in OH4+. Also see Thing#config_eql?
  if Core.version < Core::V4_0
    # @!visibility private
    module ChannelComparable
      # @!visibility private
      # This is only needed in OH3 because it is implemented in OH4 core
      def ==(other)
        return true if equal?(other)
        return false unless other.is_a?(Channel)

        %i[class
           uid
           label
           description
           kind
           channel_type_uid
           configuration
           properties
           default_tags
           auto_update_policy
           accepted_item_type].all? do |attr|
          send(attr) == other.send(attr)
        end
      end
    end
    org.openhab.core.thing.binding.builder.ChannelBuilder.const_get(:ChannelImpl).prepend(ChannelComparable)
  end
end

#item_nameString? (readonly)

Return the name of the item this channel is linked to. If a channel is linked to more than one item, this method only returns the first item.

Returns:

  • (String, nil)


65
66
67
# File 'lib/openhab/core/things/channel.rb', line 65

def item_name
  item_names.first
end

#item_namesArray<String> (readonly)

Return the names of all of the items this channel is linked to.

Returns:



73
74
75
# File 'lib/openhab/core/things/channel.rb', line 73

def item_names
  Things::Links::Provider.registry.get_linked_item_names(uid)
end

#itemsArray<Items::Item> (readonly)

Return all of the items this channel is linked to.

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/openhab/core/things/channel.rb', line 29

class Channel
  extend Forwardable

  delegate %i[item items thing] => :uid

  # @return [String]
  def inspect
    r = "#<OpenHAB::Core::Things::Channel #{uid}"
    r += " #{label.inspect}" if label
    r += " description=#{description.inspect}" if description
    r += " kind=#{kind.inspect}"
    r += " channel_type_uid=#{channel_type_uid.inspect}" if channel_type_uid
    r += " configuration=#{configuration.properties.to_h}" unless configuration.properties.empty?
    r += " properties=#{properties.to_h}" unless properties.empty?
    r += " default_tags=#{default_tags.to_a}" unless default_tags.empty?
    r += " auto_update_policy=#{auto_update_policy}" if auto_update_policy
    r += " accepted_item_type=#{accepted_item_type}" if accepted_item_type
    "#{r}>"
  end

  # @!attribute [r] channel_type
  # @return [ChannelType]
  def channel_type
    ChannelType.registry.get_channel_type(channel_type_uid)
  end

  # @return [String]
  def to_s
    uid.to_s
  end

  # @!attribute item_name [r]
  # Return the name of the item this channel is linked to. If a channel is linked to more than one item,
  # this method only returns the first item.
  #
  # @return [String, nil]
  def item_name
    item_names.first
  end

  # @!attribute item_names [r]
  # Return the names of all of the items this channel is linked to.
  #
  # @return [Array<String>]
  def item_names
    Things::Links::Provider.registry.get_linked_item_names(uid)
  end

  # @!attribute item [r]
  # Return the item this channel is linked to. If a channel is linked to more than one item,
  # this method only returns the first item.
  #
  # @return [Items::Item, nil]
  def item
    items.first
  end

  # @!attribute items [r]
  # Return all of the items this channel is linked to.
  #
  # @return [Array<Items::Item>]
  def items
    Things::Links::Provider.registry.get_linked_items(uid).map { |item| Items::Proxy.new(item) }
  end

  #
  # @!attribute links [r]
  # Returns all of the channel's links (items and link configurations).
  #
  # @return [Items::ItemChannelLinks] An array of ItemChannelLink or an empty array
  #
  # @example Get the configuration of the first link
  #   things["mqtt:topic:livingroom-light"].channel["power"].links.first.configuration
  #
  # @example Remove all managed links
  #   things["mqtt:topic:livingroom-light"].channel["power"].links.clear
  #
  # @see link
  # @see unlink
  #
  def links
    Items::ItemChannelLinks.new(uid, Things::Links::Provider.registry.get_links(uid))
  end

  #
  # @return [ItemChannelLink, nil]
  #
  # @overload link
  #   Returns the channel's link. If an channel is linked to more than one item,
  #   this method only returns the first link.
  #
  #   @return [Things::ItemChannelLink, nil]
  #
  # @overload link(item, config = {})
  #
  #   Links the channel to an item.
  #
  #   @param [String, Items::Item] item The item to link to.
  #   @param [Hash] config The configuration for the link.
  #
  #   @return [Things::ItemChannelLink] The created link.
  #
  #   @example Link a channel to an item
  #     things["mqtt:topic:livingroom-light"].channels["power"].link(LivingRoom_Light_Power)
  #
  #   @example Specify a link configuration
  #     things["mqtt:topic:outdoor-thermometer"].channels["temperature"].link(
  #       High_Temperature_Alert,
  #       profile: "system:hysteresis",
  #       lower: "29 °C",
  #       upper: "30 °C")
  #
  #   @see links
  #   @see unlink
  #
  def link(item = nil, config = nil)
    return Things::Links::Provider.registry.get_links(uid).first if item.nil? && config.nil?

    config ||= {}
    Core::Things::Links::Provider.create_link(item, self, config).tap do |new_link|
      provider = Core::Things::Links::Provider.current
      if !(current_link = provider.get(new_link.uid))
        provider.add(new_link)
      elsif current_link.configuration != config
        provider.update(new_link)
      end
    end
  end

  #
  # Removes a link to an item from managed link providers.
  #
  # @param [String, Items::Item] item The item to remove the link to.
  #
  # @return [Things::ItemChannelLink, nil] The removed link, if found.
  # @raise [FrozenError] if the link is not managed by a managed link provider.
  #
  # @see link
  # @see links
  #
  def unlink(item)
    link_to_delete = Things::Links::Provider.create_link(item, self, {})
    provider = Things::Links::Provider.registry.provider_for(link_to_delete.uid)
    unless provider.is_a?(ManagedProvider)
      raise FrozenError,
            "Cannot remove the link #{link_to_delete.uid} from non-managed provider #{provider.inspect}"
    end

    provider.remove(link_to_delete.uid)
  end

  # @deprecated OH3.4 this whole section is not needed in OH4+. Also see Thing#config_eql?
  if Core.version < Core::V4_0
    # @!visibility private
    module ChannelComparable
      # @!visibility private
      # This is only needed in OH3 because it is implemented in OH4 core
      def ==(other)
        return true if equal?(other)
        return false unless other.is_a?(Channel)

        %i[class
           uid
           label
           description
           kind
           channel_type_uid
           configuration
           properties
           default_tags
           auto_update_policy
           accepted_item_type].all? do |attr|
          send(attr) == other.send(attr)
        end
      end
    end
    org.openhab.core.thing.binding.builder.ChannelBuilder.const_get(:ChannelImpl).prepend(ChannelComparable)
  end
end

Returns all of the channel's links (items and link configurations).

Examples:

Get the configuration of the first link

things["mqtt:topic:livingroom-light"].channel["power"].links.first.configuration

Remove all managed links

things["mqtt:topic:livingroom-light"].channel["power"].links.clear

Returns:

See Also:



109
110
111
# File 'lib/openhab/core/things/channel.rb', line 109

def links
  Items::ItemChannelLinks.new(uid, Things::Links::Provider.registry.get_links(uid))
end

#thingThing? (readonly)

Return the thing this channel is associated with.

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/openhab/core/things/channel.rb', line 29

class Channel
  extend Forwardable

  delegate %i[item items thing] => :uid

  # @return [String]
  def inspect
    r = "#<OpenHAB::Core::Things::Channel #{uid}"
    r += " #{label.inspect}" if label
    r += " description=#{description.inspect}" if description
    r += " kind=#{kind.inspect}"
    r += " channel_type_uid=#{channel_type_uid.inspect}" if channel_type_uid
    r += " configuration=#{configuration.properties.to_h}" unless configuration.properties.empty?
    r += " properties=#{properties.to_h}" unless properties.empty?
    r += " default_tags=#{default_tags.to_a}" unless default_tags.empty?
    r += " auto_update_policy=#{auto_update_policy}" if auto_update_policy
    r += " accepted_item_type=#{accepted_item_type}" if accepted_item_type
    "#{r}>"
  end

  # @!attribute [r] channel_type
  # @return [ChannelType]
  def channel_type
    ChannelType.registry.get_channel_type(channel_type_uid)
  end

  # @return [String]
  def to_s
    uid.to_s
  end

  # @!attribute item_name [r]
  # Return the name of the item this channel is linked to. If a channel is linked to more than one item,
  # this method only returns the first item.
  #
  # @return [String, nil]
  def item_name
    item_names.first
  end

  # @!attribute item_names [r]
  # Return the names of all of the items this channel is linked to.
  #
  # @return [Array<String>]
  def item_names
    Things::Links::Provider.registry.get_linked_item_names(uid)
  end

  # @!attribute item [r]
  # Return the item this channel is linked to. If a channel is linked to more than one item,
  # this method only returns the first item.
  #
  # @return [Items::Item, nil]
  def item
    items.first
  end

  # @!attribute items [r]
  # Return all of the items this channel is linked to.
  #
  # @return [Array<Items::Item>]
  def items
    Things::Links::Provider.registry.get_linked_items(uid).map { |item| Items::Proxy.new(item) }
  end

  #
  # @!attribute links [r]
  # Returns all of the channel's links (items and link configurations).
  #
  # @return [Items::ItemChannelLinks] An array of ItemChannelLink or an empty array
  #
  # @example Get the configuration of the first link
  #   things["mqtt:topic:livingroom-light"].channel["power"].links.first.configuration
  #
  # @example Remove all managed links
  #   things["mqtt:topic:livingroom-light"].channel["power"].links.clear
  #
  # @see link
  # @see unlink
  #
  def links
    Items::ItemChannelLinks.new(uid, Things::Links::Provider.registry.get_links(uid))
  end

  #
  # @return [ItemChannelLink, nil]
  #
  # @overload link
  #   Returns the channel's link. If an channel is linked to more than one item,
  #   this method only returns the first link.
  #
  #   @return [Things::ItemChannelLink, nil]
  #
  # @overload link(item, config = {})
  #
  #   Links the channel to an item.
  #
  #   @param [String, Items::Item] item The item to link to.
  #   @param [Hash] config The configuration for the link.
  #
  #   @return [Things::ItemChannelLink] The created link.
  #
  #   @example Link a channel to an item
  #     things["mqtt:topic:livingroom-light"].channels["power"].link(LivingRoom_Light_Power)
  #
  #   @example Specify a link configuration
  #     things["mqtt:topic:outdoor-thermometer"].channels["temperature"].link(
  #       High_Temperature_Alert,
  #       profile: "system:hysteresis",
  #       lower: "29 °C",
  #       upper: "30 °C")
  #
  #   @see links
  #   @see unlink
  #
  def link(item = nil, config = nil)
    return Things::Links::Provider.registry.get_links(uid).first if item.nil? && config.nil?

    config ||= {}
    Core::Things::Links::Provider.create_link(item, self, config).tap do |new_link|
      provider = Core::Things::Links::Provider.current
      if !(current_link = provider.get(new_link.uid))
        provider.add(new_link)
      elsif current_link.configuration != config
        provider.update(new_link)
      end
    end
  end

  #
  # Removes a link to an item from managed link providers.
  #
  # @param [String, Items::Item] item The item to remove the link to.
  #
  # @return [Things::ItemChannelLink, nil] The removed link, if found.
  # @raise [FrozenError] if the link is not managed by a managed link provider.
  #
  # @see link
  # @see links
  #
  def unlink(item)
    link_to_delete = Things::Links::Provider.create_link(item, self, {})
    provider = Things::Links::Provider.registry.provider_for(link_to_delete.uid)
    unless provider.is_a?(ManagedProvider)
      raise FrozenError,
            "Cannot remove the link #{link_to_delete.uid} from non-managed provider #{provider.inspect}"
    end

    provider.remove(link_to_delete.uid)
  end

  # @deprecated OH3.4 this whole section is not needed in OH4+. Also see Thing#config_eql?
  if Core.version < Core::V4_0
    # @!visibility private
    module ChannelComparable
      # @!visibility private
      # This is only needed in OH3 because it is implemented in OH4 core
      def ==(other)
        return true if equal?(other)
        return false unless other.is_a?(Channel)

        %i[class
           uid
           label
           description
           kind
           channel_type_uid
           configuration
           properties
           default_tags
           auto_update_policy
           accepted_item_type].all? do |attr|
          send(attr) == other.send(attr)
        end
      end
    end
    org.openhab.core.thing.binding.builder.ChannelBuilder.const_get(:ChannelImpl).prepend(ChannelComparable)
  end
end

#uidChannelUID (readonly)

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/openhab/core/things/channel.rb', line 29

class Channel
  extend Forwardable

  delegate %i[item items thing] => :uid

  # @return [String]
  def inspect
    r = "#<OpenHAB::Core::Things::Channel #{uid}"
    r += " #{label.inspect}" if label
    r += " description=#{description.inspect}" if description
    r += " kind=#{kind.inspect}"
    r += " channel_type_uid=#{channel_type_uid.inspect}" if channel_type_uid
    r += " configuration=#{configuration.properties.to_h}" unless configuration.properties.empty?
    r += " properties=#{properties.to_h}" unless properties.empty?
    r += " default_tags=#{default_tags.to_a}" unless default_tags.empty?
    r += " auto_update_policy=#{auto_update_policy}" if auto_update_policy
    r += " accepted_item_type=#{accepted_item_type}" if accepted_item_type
    "#{r}>"
  end

  # @!attribute [r] channel_type
  # @return [ChannelType]
  def channel_type
    ChannelType.registry.get_channel_type(channel_type_uid)
  end

  # @return [String]
  def to_s
    uid.to_s
  end

  # @!attribute item_name [r]
  # Return the name of the item this channel is linked to. If a channel is linked to more than one item,
  # this method only returns the first item.
  #
  # @return [String, nil]
  def item_name
    item_names.first
  end

  # @!attribute item_names [r]
  # Return the names of all of the items this channel is linked to.
  #
  # @return [Array<String>]
  def item_names
    Things::Links::Provider.registry.get_linked_item_names(uid)
  end

  # @!attribute item [r]
  # Return the item this channel is linked to. If a channel is linked to more than one item,
  # this method only returns the first item.
  #
  # @return [Items::Item, nil]
  def item
    items.first
  end

  # @!attribute items [r]
  # Return all of the items this channel is linked to.
  #
  # @return [Array<Items::Item>]
  def items
    Things::Links::Provider.registry.get_linked_items(uid).map { |item| Items::Proxy.new(item) }
  end

  #
  # @!attribute links [r]
  # Returns all of the channel's links (items and link configurations).
  #
  # @return [Items::ItemChannelLinks] An array of ItemChannelLink or an empty array
  #
  # @example Get the configuration of the first link
  #   things["mqtt:topic:livingroom-light"].channel["power"].links.first.configuration
  #
  # @example Remove all managed links
  #   things["mqtt:topic:livingroom-light"].channel["power"].links.clear
  #
  # @see link
  # @see unlink
  #
  def links
    Items::ItemChannelLinks.new(uid, Things::Links::Provider.registry.get_links(uid))
  end

  #
  # @return [ItemChannelLink, nil]
  #
  # @overload link
  #   Returns the channel's link. If an channel is linked to more than one item,
  #   this method only returns the first link.
  #
  #   @return [Things::ItemChannelLink, nil]
  #
  # @overload link(item, config = {})
  #
  #   Links the channel to an item.
  #
  #   @param [String, Items::Item] item The item to link to.
  #   @param [Hash] config The configuration for the link.
  #
  #   @return [Things::ItemChannelLink] The created link.
  #
  #   @example Link a channel to an item
  #     things["mqtt:topic:livingroom-light"].channels["power"].link(LivingRoom_Light_Power)
  #
  #   @example Specify a link configuration
  #     things["mqtt:topic:outdoor-thermometer"].channels["temperature"].link(
  #       High_Temperature_Alert,
  #       profile: "system:hysteresis",
  #       lower: "29 °C",
  #       upper: "30 °C")
  #
  #   @see links
  #   @see unlink
  #
  def link(item = nil, config = nil)
    return Things::Links::Provider.registry.get_links(uid).first if item.nil? && config.nil?

    config ||= {}
    Core::Things::Links::Provider.create_link(item, self, config).tap do |new_link|
      provider = Core::Things::Links::Provider.current
      if !(current_link = provider.get(new_link.uid))
        provider.add(new_link)
      elsif current_link.configuration != config
        provider.update(new_link)
      end
    end
  end

  #
  # Removes a link to an item from managed link providers.
  #
  # @param [String, Items::Item] item The item to remove the link to.
  #
  # @return [Things::ItemChannelLink, nil] The removed link, if found.
  # @raise [FrozenError] if the link is not managed by a managed link provider.
  #
  # @see link
  # @see links
  #
  def unlink(item)
    link_to_delete = Things::Links::Provider.create_link(item, self, {})
    provider = Things::Links::Provider.registry.provider_for(link_to_delete.uid)
    unless provider.is_a?(ManagedProvider)
      raise FrozenError,
            "Cannot remove the link #{link_to_delete.uid} from non-managed provider #{provider.inspect}"
    end

    provider.remove(link_to_delete.uid)
  end

  # @deprecated OH3.4 this whole section is not needed in OH4+. Also see Thing#config_eql?
  if Core.version < Core::V4_0
    # @!visibility private
    module ChannelComparable
      # @!visibility private
      # This is only needed in OH3 because it is implemented in OH4 core
      def ==(other)
        return true if equal?(other)
        return false unless other.is_a?(Channel)

        %i[class
           uid
           label
           description
           kind
           channel_type_uid
           configuration
           properties
           default_tags
           auto_update_policy
           accepted_item_type].all? do |attr|
          send(attr) == other.send(attr)
        end
      end
    end
    org.openhab.core.thing.binding.builder.ChannelBuilder.const_get(:ChannelImpl).prepend(ChannelComparable)
  end
end

Instance Method Details

#inspectString

Returns:

  • (String)


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/openhab/core/things/channel.rb', line 35

def inspect
  r = "#<OpenHAB::Core::Things::Channel #{uid}"
  r += " #{label.inspect}" if label
  r += " description=#{description.inspect}" if description
  r += " kind=#{kind.inspect}"
  r += " channel_type_uid=#{channel_type_uid.inspect}" if channel_type_uid
  r += " configuration=#{configuration.properties.to_h}" unless configuration.properties.empty?
  r += " properties=#{properties.to_h}" unless properties.empty?
  r += " default_tags=#{default_tags.to_a}" unless default_tags.empty?
  r += " auto_update_policy=#{auto_update_policy}" if auto_update_policy
  r += " accepted_item_type=#{accepted_item_type}" if accepted_item_type
  "#{r}>"
end

Overloads:

  • #linkThings::ItemChannelLink?

    Returns the channel's link. If an channel is linked to more than one item, this method only returns the first link.

    Returns:

  • #link(item, config = {}) ⇒ Things::ItemChannelLink

    Links the channel to an item.

    Examples:

    Link a channel to an item

    things["mqtt:topic:livingroom-light"].channels["power"].link(LivingRoom_Light_Power)

    Specify a link configuration

    things["mqtt:topic:outdoor-thermometer"].channels["temperature"].link(
      High_Temperature_Alert,
      profile: "system:hysteresis",
      lower: "29 °C",
      upper: "30 °C")

    Parameters:

    • item (String, Items::Item)

      The item to link to.

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

      The configuration for the link.

    Returns:

    See Also:

Returns:



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/openhab/core/things/channel.rb', line 144

def link(item = nil, config = nil)
  return Things::Links::Provider.registry.get_links(uid).first if item.nil? && config.nil?

  config ||= {}
  Core::Things::Links::Provider.create_link(item, self, config).tap do |new_link|
    provider = Core::Things::Links::Provider.current
    if !(current_link = provider.get(new_link.uid))
      provider.add(new_link)
    elsif current_link.configuration != config
      provider.update(new_link)
    end
  end
end

#to_sString

Returns:

  • (String)


56
57
58
# File 'lib/openhab/core/things/channel.rb', line 56

def to_s
  uid.to_s
end

Removes a link to an item from managed link providers.

Parameters:

  • item (String, Items::Item)

    The item to remove the link to.

Returns:

Raises:

  • (FrozenError)

    if the link is not managed by a managed link provider.

See Also:



169
170
171
172
173
174
175
176
177
178
# File 'lib/openhab/core/things/channel.rb', line 169

def unlink(item)
  link_to_delete = Things::Links::Provider.create_link(item, self, {})
  provider = Things::Links::Provider.registry.provider_for(link_to_delete.uid)
  unless provider.is_a?(ManagedProvider)
    raise FrozenError,
          "Cannot remove the link #{link_to_delete.uid} from non-managed provider #{provider.inspect}"
  end

  provider.remove(link_to_delete.uid)
end