Skip to content

Commit

Permalink
Support source in command! and toggle (#346)
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <[email protected]>
  • Loading branch information
jimtng authored Oct 3, 2024
1 parent b67408a commit bb18660
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
8 changes: 4 additions & 4 deletions lib/openhab/core/items/semantics/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ def command(command, **kwargs)

# Send a command to every item in the collection, even when {OpenHAB::DSL.ensure_states! ensure_states!} is in effect.
# @return [self]
def command!(command)
def command!(command, **kwargs)
# We cannot alias this to #command above, otherwise it will call
# DSL::Items::Ensure::Item#command which checks for ensure_states
each { |i| i.command!(command) }
each { |i| i.command!(command, **kwargs) }
self
end

Expand Down Expand Up @@ -198,7 +198,7 @@ def ensure
# Send a toggle command to every item in the collection
# @return [self]
#
def toggle
each(&:toggle)
def toggle(source: nil)
each { |i| i.toggle(source: source) }
end
end
6 changes: 3 additions & 3 deletions lib/openhab/core/items/switch_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ def format_type(command)
#
# @return [self]
#
def toggle
return on! unless state?
def toggle(source: nil)
return on!(source: source) unless state?

command!(!state)
command!(!state, source: source)
end

# @!method on?
Expand Down
7 changes: 7 additions & 0 deletions spec/openhab/core/items/semantics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@
expect(triggered).to be true
expect(LivingRoom_Light1_Custom).to be_on
end

it "accepts a source" do
source = nil
received_command(LivingRoom_Light1_Custom) { |event| source = event.source }
[LivingRoom_Light1_Custom].toggle(source: "source")
expect(source).to eq "source"
end
end

describe "#points" do
Expand Down
14 changes: 14 additions & 0 deletions spec/openhab/core/items/switch_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@
item.update(NULL).toggle
expect(item.state).to be ON
end

it "accepts a source" do
source = nil
received_command(item) { |event| source = event.source }

item.update(UNDEF).toggle(source: "undef")
expect(source).to eq "undef"

item.on.toggle(source: "on")
expect(source).to eq "on"

item.off.toggle(source: "off")
expect(source).to eq "off"
end
end

it "works with grep" do
Expand Down

0 comments on commit bb18660

Please sign in to comment.