From 3d47e9cf882c199e8b4366bcd8111d455e9ece72 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Fri, 3 Jan 2025 14:25:30 -0700 Subject: [PATCH] Fix #toggle on dimmer items (#378) Signed-off-by: Cody Cutrer --- lib/openhab/core/items/switch_item.rb | 4 +--- spec/openhab/core/items/dimmer_item_spec.rb | 22 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/openhab/core/items/switch_item.rb b/lib/openhab/core/items/switch_item.rb index d367646590..5c6d410d90 100644 --- a/lib/openhab/core/items/switch_item.rb +++ b/lib/openhab/core/items/switch_item.rb @@ -50,9 +50,7 @@ def format_type(command) # @return [self] # def toggle(source: nil) - return on!(source: source) unless state? - - command!(!state, source: source) + command!(!on?, source: source) end # @!method on? diff --git a/spec/openhab/core/items/dimmer_item_spec.rb b/spec/openhab/core/items/dimmer_item_spec.rb index 870d36df7d..ffa0a3f7ce 100644 --- a/spec/openhab/core/items/dimmer_item_spec.rb +++ b/spec/openhab/core/items/dimmer_item_spec.rb @@ -89,6 +89,28 @@ end end + describe "#toggle" do + specify do + item.on.toggle + expect(item.state).to eq 0 + end + + specify do + item.off.toggle + expect(item.state).to eq 100 + end + + specify do + item.update(UNDEF).toggle + expect(item.state).to eq 100 + end + + specify do + item.update(NULL).toggle + expect(item.state).to eq 100 + end + end + it "works with grep" do items.build { switch_item "Switch1" } expect(items.grep(DimmerItem)).to match_array [DimmerOne, DimmerTwo]