diff --git a/lib/openhab/core/items/switch_item.rb b/lib/openhab/core/items/switch_item.rb index d36764659..5c6d410d9 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 870d36df7..ffa0a3f7c 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]