Skip to content

Commit

Permalink
Support expire configuration options in Item Builder
Browse files Browse the repository at this point in the history
`expire` can accept `ignoreStateUpdates` and `ignoreCommands` options

Signed-off-by: Jimmy Tanagra <[email protected]>
  • Loading branch information
jimtng committed Sep 22, 2023
1 parent 6b57eb1 commit 584291d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
24 changes: 19 additions & 5 deletions lib/openhab/dsl/items/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -448,7 +450,13 @@ 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)"
Expand All @@ -463,9 +471,15 @@ 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

# camelize keys ignore_commands => ignoreCommands
config.transform_keys! { |k| k.to_s.gsub(/_(.)/) { $1.upcase } }

@expire = [value, config]
end

# @!attribute [w] unit
Expand Down
6 changes: 6 additions & 0 deletions spec/openhab/dsl/items/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,19 @@
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

expect(MySwitch1.metadata["expire"]&.value).to eq "1h"
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

Expand Down

0 comments on commit 584291d

Please sign in to comment.