openhab-scripting 5.18.1 → 5.19.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -57,6 +57,126 @@ module OpenHAB
57
57
  uid.to_s
58
58
  end
59
59
 
60
+ # @!attribute item_name [r]
61
+ # Return the name of the item this channel is linked to. If a channel is linked to more than one item,
62
+ # this method only returns the first item.
63
+ #
64
+ # @return [String, nil]
65
+ def item_name
66
+ item_names.first
67
+ end
68
+
69
+ # @!attribute item_names [r]
70
+ # Return the names of all of the items this channel is linked to.
71
+ #
72
+ # @return [Array<String>]
73
+ def item_names
74
+ Things::Links::Provider.registry.get_linked_item_names(uid)
75
+ end
76
+
77
+ # @!attribute item [r]
78
+ # Return the item this channel is linked to. If a channel is linked to more than one item,
79
+ # this method only returns the first item.
80
+ #
81
+ # @return [Items::Item, nil]
82
+ def item
83
+ items.first
84
+ end
85
+
86
+ # @!attribute items [r]
87
+ # Return all of the items this channel is linked to.
88
+ #
89
+ # @return [Array<Items::Item>]
90
+ def items
91
+ Things::Links::Provider.registry.get_linked_items(uid).map { |item| Items::Proxy.new(item) }
92
+ end
93
+
94
+ #
95
+ # @!attribute links [r]
96
+ # Returns all of the channel's links (items and link configurations).
97
+ #
98
+ # @return [Items::ItemChannelLinks] An array of ItemChannelLink or an empty array
99
+ #
100
+ # @example Get the configuration of the first link
101
+ # things["mqtt:topic:livingroom-light"].channel["power"].links.first.configuration
102
+ #
103
+ # @example Remove all managed links
104
+ # things["mqtt:topic:livingroom-light"].channel["power"].links.clear
105
+ #
106
+ # @see link
107
+ # @see unlink
108
+ #
109
+ def links
110
+ Items::ItemChannelLinks.new(uid, Things::Links::Provider.registry.get_links(uid))
111
+ end
112
+
113
+ #
114
+ # @return [ItemChannelLink, nil]
115
+ #
116
+ # @overload link
117
+ # Returns the channel's link. If an channel is linked to more than one item,
118
+ # this method only returns the first link.
119
+ #
120
+ # @return [Things::ItemChannelLink, nil]
121
+ #
122
+ # @overload link(item, config = {})
123
+ #
124
+ # Links the channel to an item.
125
+ #
126
+ # @param [String, Items::Item] item The item to link to.
127
+ # @param [Hash] config The configuration for the link.
128
+ #
129
+ # @return [Things::ItemChannelLink] The created link.
130
+ #
131
+ # @example Link a channel to an item
132
+ # things["mqtt:topic:livingroom-light"].channels["power"].link(LivingRoom_Light_Power)
133
+ #
134
+ # @example Specify a link configuration
135
+ # things["mqtt:topic:outdoor-thermometer"].channels["temperature"].link(
136
+ # High_Temperature_Alert,
137
+ # profile: "system:hysteresis",
138
+ # lower: "29 °C",
139
+ # upper: "30 °C")
140
+ #
141
+ # @see links
142
+ # @see unlink
143
+ #
144
+ def link(item = nil, config = nil)
145
+ return Things::Links::Provider.registry.get_links(uid).first if item.nil? && config.nil?
146
+
147
+ config ||= {}
148
+ Core::Things::Links::Provider.create_link(item, self, config).tap do |new_link|
149
+ provider = Core::Things::Links::Provider.current
150
+ if !(current_link = provider.get(new_link.uid))
151
+ provider.add(new_link)
152
+ elsif current_link.configuration != config
153
+ provider.update(new_link)
154
+ end
155
+ end
156
+ end
157
+
158
+ #
159
+ # Removes a link to an item from managed link providers.
160
+ #
161
+ # @param [String, Items::Item] item The item to remove the link to.
162
+ #
163
+ # @return [Things::ItemChannelLink, nil] The removed link, if found.
164
+ # @raise [FrozenError] if the link is not managed by a managed link provider.
165
+ #
166
+ # @see link
167
+ # @see links
168
+ #
169
+ def unlink(item)
170
+ link_to_delete = Things::Links::Provider.create_link(item, self, {})
171
+ provider = Things::Links::Provider.registry.provider_for(link_to_delete.uid)
172
+ unless provider.is_a?(ManagedProvider)
173
+ raise FrozenError,
174
+ "Cannot remove the link #{link_to_delete.uid} from non-managed provider #{provider.inspect}"
175
+ end
176
+
177
+ provider.remove(link_to_delete.uid)
178
+ end
179
+
60
180
  # @deprecated OH3.4 this whole section is not needed in OH4+. Also see Thing#config_eql?
61
181
  if Core.version < Core::V4_0
62
182
  # @!visibility private
@@ -28,7 +28,8 @@ module OpenHAB
28
28
  config = Configuration.new(config.transform_keys(&:to_s))
29
29
  channel = ChannelUID.new(channel) if channel.is_a?(String)
30
30
  channel = channel.uid if channel.is_a?(Channel)
31
- org.openhab.core.thing.link.ItemChannelLink.new(item.name, channel, config)
31
+ item = item.name if item.is_a?(Item)
32
+ org.openhab.core.thing.link.ItemChannelLink.new(item, channel, config)
32
33
  end
33
34
  end
34
35
 
@@ -992,9 +992,14 @@ module OpenHAB
992
992
  #
993
993
  # Creates a channel linked trigger
994
994
  #
995
+ # @param [Item, String, nil] item The item to create a trigger for. If nil, all items are matched.
996
+ # @param [Core::Things::Channel, Core::Things::ChannelUID, String, nil] channel
997
+ # The channel to create a trigger for. If nil, all channels are matched.
995
998
  # @param [Object] attach object to be attached to the trigger
996
999
  # @return [void]
997
1000
  #
1001
+ # @since openHAB 4.0 Support for filtering with item and channel was added
1002
+ #
998
1003
  # @example
999
1004
  # rule "channel linked" do
1000
1005
  # channel_linked
@@ -1002,9 +1007,10 @@ module OpenHAB
1002
1007
  # logger.info("#{event.link.item.name} linked to #{event.link.channel_uid}.")
1003
1008
  # end
1004
1009
  # end
1005
- def channel_linked(attach: nil)
1006
- @ruby_triggers << [:channel_linked]
1007
- event("openhab/links/*/added", types: "ItemChannelLinkAddedEvent", attach: attach)
1010
+ def channel_linked(item: nil, channel: nil, attach: nil)
1011
+ pattern = (item.nil? && channel.nil?) ? "*" : "#{item || "*"}-#{channel || "*"}"
1012
+ @ruby_triggers << [:channel_linked, pattern]
1013
+ event("openhab/links/#{pattern}/added", types: "ItemChannelLinkAddedEvent", attach: attach)
1008
1014
  end
1009
1015
 
1010
1016
  #
@@ -1013,9 +1019,14 @@ module OpenHAB
1013
1019
  # Note that the item or the thing it's linked to may no longer exist,
1014
1020
  # so if you try to access those objects they'll be nil.
1015
1021
  #
1022
+ # @param [Item, String, nil] item The item to create a trigger for. If nil, all items are matched.
1023
+ # @param [Core::Things::Channel, Core::Things::ChannelUID, String, nil] channel
1024
+ # The channel to create a trigger for. If nil, all channels are matched.
1016
1025
  # @param [Object] attach object to be attached to the trigger
1017
1026
  # @return [void]
1018
1027
  #
1028
+ # @since openHAB 4.0 Support for filtering with item and channel was added
1029
+ #
1019
1030
  # @example
1020
1031
  # rule "channel unlinked" do
1021
1032
  # channel_unlinked
@@ -1023,9 +1034,10 @@ module OpenHAB
1023
1034
  # logger.info("#{event.link.item_name} unlinked from #{event.link.channel_uid}.")
1024
1035
  # end
1025
1036
  # end
1026
- def channel_unlinked(attach: nil)
1027
- @ruby_triggers << [:channel_linked]
1028
- event("openhab/links/*/removed", types: "ItemChannelLinkRemovedEvent", attach: attach)
1037
+ def channel_unlinked(item: nil, channel: nil, attach: nil)
1038
+ pattern = (item.nil? && channel.nil?) ? "*" : "#{item || "*"}-#{channel || "*"}"
1039
+ @ruby_triggers << [:channel_unlinked, pattern]
1040
+ event("openhab/links/#{pattern}/removed", types: "ItemChannelLinkRemovedEvent", attach: attach)
1029
1041
  end
1030
1042
 
1031
1043
  #
@@ -1556,9 +1568,12 @@ module OpenHAB
1556
1568
  #
1557
1569
  # Creates an item added trigger
1558
1570
  #
1571
+ # @param [String, nil] pattern The pattern to match items against
1559
1572
  # @param [Object] attach object to be attached to the trigger
1560
1573
  # @return [void]
1561
1574
  #
1575
+ # @since openHAB 4.0 Support for pattern filter was added
1576
+ #
1562
1577
  # @example
1563
1578
  # rule "item added" do
1564
1579
  # item_added
@@ -1566,17 +1581,20 @@ module OpenHAB
1566
1581
  # logger.info("#{event.item.name} added.")
1567
1582
  # end
1568
1583
  # end
1569
- def item_added(attach: nil)
1570
- @ruby_triggers << [:item_added]
1571
- event("openhab/items/*/added", types: "ItemAddedEvent", attach: attach)
1584
+ def item_added(pattern = "*", attach: nil)
1585
+ @ruby_triggers << [:item_added, pattern]
1586
+ event("openhab/items/#{pattern}/added", types: "ItemAddedEvent", attach: attach)
1572
1587
  end
1573
1588
 
1574
1589
  #
1575
1590
  # Creates an item removed trigger
1576
1591
  #
1592
+ # @param [String, nil] pattern The pattern to match items against
1577
1593
  # @param [Object] attach object to be attached to the trigger
1578
1594
  # @return [void]
1579
1595
  #
1596
+ # @since openHAB 4.0 Support for pattern filter was added
1597
+ #
1580
1598
  # @example
1581
1599
  # rule "item removed" do
1582
1600
  # item_removed
@@ -1584,14 +1602,15 @@ module OpenHAB
1584
1602
  # logger.info("#{event.item.name} removed.")
1585
1603
  # end
1586
1604
  # end
1587
- def item_removed(attach: nil)
1588
- @ruby_triggers << [:item_removed]
1589
- event("openhab/items/*/removed", types: "ItemRemovedEvent", attach: attach)
1605
+ def item_removed(pattern = "*", attach: nil)
1606
+ @ruby_triggers << [:item_removed, pattern]
1607
+ event("openhab/items/#{pattern}/removed", types: "ItemRemovedEvent", attach: attach)
1590
1608
  end
1591
1609
 
1592
1610
  #
1593
1611
  # Creates an item updated trigger
1594
1612
  #
1613
+ # @param [String, nil] pattern The pattern to match items against
1595
1614
  # @param [Object] attach object to be attached to the trigger
1596
1615
  # @return [void]
1597
1616
  #
@@ -1603,17 +1622,20 @@ module OpenHAB
1603
1622
  # end
1604
1623
  # end
1605
1624
  #
1606
- def item_updated(attach: nil)
1607
- @ruby_triggers << [:item_updated]
1608
- event("openhab/items/*/updated", types: "ItemUpdatedEvent", attach: attach)
1625
+ def item_updated(pattern = "*", attach: nil)
1626
+ @ruby_triggers << [:item_updated, pattern]
1627
+ event("openhab/items/#{pattern}/updated", types: "ItemUpdatedEvent", attach: attach)
1609
1628
  end
1610
1629
 
1611
1630
  #
1612
1631
  # Creates a thing added trigger
1613
1632
  #
1633
+ # @param [String, nil] pattern The pattern to match things against
1614
1634
  # @param [Object] attach object to be attached to the trigger
1615
1635
  # @return [void]
1616
1636
  #
1637
+ # @since openHAB 4.0 Support for pattern filter was added
1638
+ #
1617
1639
  # @example
1618
1640
  # rule "thing added" do
1619
1641
  # thing_added
@@ -1621,17 +1643,20 @@ module OpenHAB
1621
1643
  # logger.info("#{event.thing.uid} added.")
1622
1644
  # end
1623
1645
  # end
1624
- def thing_added(attach: nil)
1625
- @ruby_triggers << [:thing_added]
1626
- event("openhab/things/*/added", types: "ThingAddedEvent", attach: attach)
1646
+ def thing_added(pattern = "*", attach: nil)
1647
+ @ruby_triggers << [:thing_added, pattern]
1648
+ event("openhab/things/#{pattern}/added", types: "ThingAddedEvent", attach: attach)
1627
1649
  end
1628
1650
 
1629
1651
  #
1630
1652
  # Creates a thing removed trigger
1631
1653
  #
1654
+ # @param [String, nil] pattern The pattern to match things against
1632
1655
  # @param [Object] attach object to be attached to the trigger
1633
1656
  # @return [void]
1634
1657
  #
1658
+ # @since openHAB 4.0 Support for pattern filter was added
1659
+ #
1635
1660
  # @example
1636
1661
  # rule "thing removed" do
1637
1662
  # thing_removed
@@ -1639,17 +1664,20 @@ module OpenHAB
1639
1664
  # logger.info("#{event.thing.uid} removed.")
1640
1665
  # end
1641
1666
  # end
1642
- def thing_removed(attach: nil)
1643
- @ruby_triggers << [:thing_removed]
1644
- event("openhab/things/*/removed", types: "ThingRemovedEvent", attach: attach)
1667
+ def thing_removed(pattern = "*", attach: nil)
1668
+ @ruby_triggers << [:thing_removed, pattern]
1669
+ event("openhab/things/#{pattern}/removed", types: "ThingRemovedEvent", attach: attach)
1645
1670
  end
1646
1671
 
1647
1672
  #
1648
1673
  # Creates a thing updated trigger
1649
1674
  #
1675
+ # @param [String, nil] pattern The pattern to match things against
1650
1676
  # @param [Object] attach object to be attached to the trigger
1651
1677
  # @return [void]
1652
1678
  #
1679
+ # @since openHAB 4.0 Support for pattern filter was added
1680
+ #
1653
1681
  # @example
1654
1682
  # rule "thing updated" do
1655
1683
  # thing_updated
@@ -1658,9 +1686,9 @@ module OpenHAB
1658
1686
  # end
1659
1687
  # end
1660
1688
  #
1661
- def thing_updated(attach: nil)
1662
- @ruby_triggers << [:thing_updated]
1663
- event("openhab/things/*/updated", types: "ThingUpdatedEvent", attach: attach)
1689
+ def thing_updated(pattern = "*", attach: nil)
1690
+ @ruby_triggers << [:thing_updated, pattern]
1691
+ event("openhab/things/#{pattern}/updated", types: "ThingUpdatedEvent", attach: attach)
1664
1692
  end
1665
1693
 
1666
1694
  #
@@ -737,7 +737,12 @@ module OpenHAB
737
737
  def build
738
738
  widget = super
739
739
  buttons.each do |button|
740
- button_object = SitemapBuilder.factory.create_button
740
+ button_object = if SitemapBuilder.factory.respond_to?(:create_button_definition)
741
+ SitemapBuilder.factory.create_button_definition
742
+ else
743
+ # @deprecated OH 4.1 in OH 4.2 this clause is not needed
744
+ SitemapBuilder.factory.create_button
745
+ end
741
746
  button_object.row = button[0]
742
747
  button_object.column = button[1]
743
748
  button_object.cmd = button[2]
@@ -4,6 +4,6 @@ module OpenHAB
4
4
  module DSL
5
5
  # Version of openHAB helper libraries
6
6
  # @return [String]
7
- VERSION = "5.18.1"
7
+ VERSION = "5.19.1"
8
8
  end
9
9
  end
@@ -28,7 +28,7 @@ module OpenHAB
28
28
  end
29
29
  end
30
30
 
31
- module HistoricState
31
+ module PersistedState
32
32
  def timestamp
33
33
  # PersistenceExtensions uses an anonymous class to wrap the current
34
34
  # state if that happens to be an answer. Except it calls
@@ -41,7 +41,7 @@ module OpenHAB
41
41
  super
42
42
  end
43
43
  end
44
- Core::Items::Persistence::HistoricState.prepend(HistoricState)
44
+ Core::Items::Persistence::PersistedState.prepend(PersistedState)
45
45
 
46
46
  attr_reader :id
47
47
 
@@ -54,14 +54,18 @@ module OpenHAB
54
54
  @data = Hash.new { |h, k| h[k] = [] }
55
55
  end
56
56
 
57
- def store(item, date = nil, state = nil)
58
- date = nil if date.is_a?(String) # alias overload
57
+ def store(item, date = nil, state = nil, item_alias = nil)
58
+ if date.is_a?(String) # alias overload
59
+ item_alias = date
60
+ date = nil
61
+ end
59
62
  state ||= item.state
60
63
  date ||= ZonedDateTime.now
64
+ item_alias ||= item.name
61
65
 
62
66
  new_item = HistoricItem.new(date, state, item.name)
63
67
 
64
- item_history = @data[item.name]
68
+ item_history = @data[item_alias]
65
69
 
66
70
  insert_index = item_history.bsearch_index do |i|
67
71
  i.timestamp.compare_to(date).positive?
@@ -79,6 +83,7 @@ module OpenHAB
79
83
  historic_item = item_history.delete_at(index)
80
84
  @data.delete(historic_item.name) if item_history.empty?
81
85
  end
86
+ true
82
87
  end
83
88
 
84
89
  def query(filter)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openhab-scripting
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.18.1
4
+ version: 5.19.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian O'Connell
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2024-04-06 00:00:00.000000000 Z
13
+ date: 2024-05-21 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -485,7 +485,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
485
485
  - !ruby/object:Gem::Version
486
486
  version: '0'
487
487
  requirements: []
488
- rubygems_version: 3.5.7
488
+ rubygems_version: 3.5.10
489
489
  signing_key:
490
490
  specification_version: 4
491
491
  summary: JRuby Helper Libraries for openHAB Scripting