Skip to content

Commit

Permalink
Allow literal states for sitemap visibility rules (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccutrer authored Sep 14, 2024
1 parent de562b9 commit 5068104
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/openhab/dsl/sitemaps/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ class WidgetBuilder
# One or more value color rules (see {#value_color})
# @param icon_color [String, Hash<String, String>, Hash<Array<String>, String>, nil]
# One or more icon color rules (see {#icon_color})
# @param visibility [String, Array<String>, Array<Array<String>>, nil]
# @param visibility [String,
# Core::Types::State,
# Array<String>,
# Array<Core::Types::State>,
# Array<Array<String>>,
# nil]
# One or more visibility rules (see {#visibility})
# @!visibility private
def initialize(type,
Expand Down Expand Up @@ -286,8 +291,12 @@ def add_conditions_to_container(container, conditions)
supports_and_conditions = SitemapBuilder.factory.respond_to?(:create_condition)

Array.wrap(conditions).each do |c|
c = c.to_s if c.is_a?(Core::Types::State)
unless c.is_a?(String) || c.is_a?(Symbol)
raise ArgumentError, "#{c.inspect} is not a valid condition data type for #{inspect}"
end
unless (match = CONDITION_PATTERN.match(c))
raise ArgumentError, "Syntax error in condition #{c.inspect}"
raise ArgumentError, "Syntax error in condition #{c.inspect} for #{inspect}"
end

condition = supports_and_conditions ? SitemapBuilder.factory.create_condition : container
Expand Down
16 changes: 16 additions & 0 deletions spec/openhab/dsl/sitemaps/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@
expect(cond.state).to eq "ON"
end

it "supports a condition with a literal state" do
s = sitemaps.build do
sitemap "default", label: "My Residence" do
switch item: "Switch1", visibility: ON
end
end

switch = s.children.first
cond = switch.visibility.first
# @deprecated OH 4.0
cond = cond.conditions.first if cond.respond_to?(:conditions)
expect(cond.item).to be_nil
expect(cond.condition).to be_nil
expect(cond.state).to eq "ON"
end

it "supports a condition with operator and state" do
s = sitemaps.build do
sitemap "default", label: "My Residence" do
Expand Down

0 comments on commit 5068104

Please sign in to comment.