diff --git a/lib/openhab/dsl/items/builder.rb b/lib/openhab/dsl/items/builder.rb index 60774d61e2..f5853fedd6 100644 --- a/lib/openhab/dsl/items/builder.rb +++ b/lib/openhab/dsl/items/builder.rb @@ -429,10 +429,12 @@ def channel(channel, config = {}) end # - # @!method expire(command: nil, state: nil) + # @!method expire(command: nil, state: nil, **config) # # Configure item expiration # + # @param config [Hash] Expire supports ignoreStateUpdates and ignoreCommands. + # Snake case is allowed, e.g. ignore_state_updates: true, ignore_commands: true # @return [void] # # @example Get the current expire setting @@ -448,13 +450,22 @@ def channel(channel, config = {}) # expire 5.minutes, state: NULL # @example Send a command on expiration # expire 5.minutes, command: OFF - def expire(*args, command: nil, state: nil) + # @example Specify the duration and command in the same string + # expire "5h,command=OFF" + # @example Set the expire configuration + # expire 5.minutes, ignore_state_updates: true + # + def expire(*args, command: nil, state: nil, **config) + args.flatten! unless (0..2).cover?(args.length) raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 0..2)" end return @expire if args.empty? + camelize = ->(str) { str.gsub(/_(.)/) { $1.upcase } } + config.transform_keys! { |k| camelize.call(k.to_s) } + state = args.last if args.length == 2 raise ArgumentError, "cannot provide both command and state" if command && state @@ -463,9 +474,11 @@ def expire(*args, command: nil, state: nil) duration = duration.to_s[2..].downcase if duration.is_a?(Duration) state = "'#{state}'" if state.respond_to?(:to_str) && type == :string - @expire = duration - @expire += ",state=#{state}" if state - @expire += ",command=#{command}" if command + + value = duration + value += ",state=#{state}" if state + value += ",command=#{command}" if command + @expire = [value, config] end # @!attribute [w] unit diff --git a/spec/openhab/dsl/items/builder_spec.rb b/spec/openhab/dsl/items/builder_spec.rb index c6caec23c5..d9eaeb0feb 100644 --- a/spec/openhab/dsl/items/builder_spec.rb +++ b/spec/openhab/dsl/items/builder_spec.rb @@ -157,6 +157,10 @@ switch_item "MySwitch2", expire: "2h" switch_item "MySwitch3", expire: [3.hours, OFF] switch_item "MySwitch4", expire: ["4h", { command: OFF }] + switch_item "MySwitch5", expire: ["4h", { ignore_state_updates: true }] + switch_item "MySwitch6" do + expire "4h", ignore_state_updates: true + end string_item "MyString", expire: [5.hours, "EXPIRED"] end @@ -164,6 +168,8 @@ expect(MySwitch2.metadata["expire"]&.value).to eq "2h" expect(MySwitch3.metadata["expire"]&.value).to eq "3h,state=OFF" expect(MySwitch4.metadata["expire"]&.value).to eq "4h,command=OFF" + expect(MySwitch5.metadata["expire"]).to eq ["4h", { "ignoreStateUpdates" => true }] + expect(MySwitch6.metadata["expire"]).to eq ["4h", { "ignoreStateUpdates" => true }] expect(MyString.metadata["expire"]&.value).to eq "5h,state='EXPIRED'" end