Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for icons in sitemap builder switch mappings
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
jimtng committed Jan 10, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 5250e15 commit f5f5bbb
Showing 2 changed files with 119 additions and 11 deletions.
41 changes: 38 additions & 3 deletions lib/openhab/dsl/sitemaps/builder.rb
Original file line number Diff line number Diff line change
@@ -281,9 +281,41 @@ def add_conditions_to_container(container, conditions)
class SwitchBuilder < WidgetBuilder
# Mappings from command to label
#
# Keys can be any {Core::Types::Command command}, values are strings.
# If an array is given, the same value is used for both command and label.
# If a hash is given, the keys are the commands and the values are the labels.
# The keys can be any {Core::Types::Command command}, string or symbol.
# They will be converted to strings.
#
# If an array is given:
# - Scalar elements define the command, and the label is the same as the command.
# - Array elements contain the command, label, and optional third element for the icon.
# - Hash elements contain the command, label, and optional icon defined by the corresponding keys.
#
# @since openHAB 4.1 added support for icons
#
# @example A Hash to specify different command and label
# switch mappings: { off: "Off", cool: "Cool", heat: "Heat" }
#
# @example A simple array with the same command and label
# switch mappings: %w[off cool heat]
#
# @example An array of arrays containing the command, label, and icon
# switch mappings: [
# %w[off Off f7:power],
# %w[cool Cool f7:snow],
# %w[heat Heat f7:flame],
# %w[auto Auto] # no icon
# ]
#
# @example An array of hashes for the command, label, and icon
# switch mappings: [
# {command: "off", label: "Off", icon: "f7:power"},
# {command: "cool", label: "Cool", icon: "f7:snow"},
# {command: "heat", label: "Heat", icon: "f7:flame"},
# {command: "auto", label: "Auto"} # no icon
# ]
#
# @return [Hash, Array, nil]
# @see LinkableWidgetBuilder#switch
# @see https://www.openhab.org/docs/ui/sitemaps.html#mappings
attr_accessor :mappings

@@ -300,10 +332,13 @@ def initialize(type, builder_proxy, mappings: nil, **kwargs, &block)
# @!visibility private
def build
widget = super
mappings&.each do |cmd, label|
mappings&.each do |cmd, label, icon|
mapping = SitemapBuilder.factory.create_mapping
cmd, label, icon = cmd.values_at(:command, :label, :icon) if cmd.is_a?(Hash)
mapping.cmd = cmd.to_s
mapping.label = label&.to_s || cmd.to_s
# @deprecated OH 4.1 the if check is not needed in OH4.1+
mapping.icon = icon if icon
widget.mappings.add(mapping)
end
widget
89 changes: 81 additions & 8 deletions spec/openhab/dsl/sitemaps/builder_spec.rb
Original file line number Diff line number Diff line change
@@ -371,15 +371,88 @@
end
end

it "can add a switch with array mappings" do
sitemaps.build do
sitemap "default" do
switch label: "My Switch", mappings: %w[off cool heat]
context "when adding a switch with array mappings" do
it "can contain scalar that represent the command and the label" do
sitemaps.build do
sitemap "default" do
switch label: "My Switch", mappings: %w[off cool heat]
end
end
switch = sitemaps["default"].children.first
expect(switch.mappings.map(&:cmd)).to eq %w[off cool heat]
expect(switch.mappings.map(&:label)).to eq %w[off cool heat]
end

it "can contain arrays of command and label" do
sitemaps.build do
sitemap "default" do
switch label: "My Switch", mappings: [%w[OFF off], %w[COOL cool], %w[HEAT heat]]
end
end
switch = sitemaps["default"].children.first
expect(switch.mappings.map(&:cmd)).to eq %w[OFF COOL HEAT]
expect(switch.mappings.map(&:label)).to eq %w[off cool heat]
end

it "can contain arrays of command, label, and an optional icon" do
sitemaps.build do
sitemap "default" do
# @deprecated OH 4.1
if OpenHAB::Core::VERSION >= "4.1.0"
switch label: "My Switch", mappings: [%w[OFF off], %w[COOL cool f7:snow], %w[HEAT heat f7:flame]]
else
switch label: "My Switch", mappings: [%w[OFF off], %w[COOL cool], %w[HEAT heat]]
end
end
end
switch = sitemaps["default"].children.first
expect(switch.mappings.map(&:cmd)).to eq %w[OFF COOL HEAT]
expect(switch.mappings.map(&:label)).to eq %w[off cool heat]
# @deprecated OH 4.1 - the if check is not needed in OH4.1+
expect(switch.mappings.map(&:icon)).to eq [nil, "f7:snow", "f7:flame"] if OpenHAB::Core::VERSION >= "4.1.0"
end

it "can contain hashes of command, label, and an optional icon" do
sitemaps.build do
sitemap "default" do
# @deprecated OH 4.1
if OpenHAB::Core::VERSION >= "4.1.0"
switch label: "My Switch", mappings: [
{ command: "OFF", label: "off" },
{ command: "COOL", label: "cool", icon: "f7:snow" },
{ command: "HEAT", label: "heat", icon: "f7:flame" }
]
else
switch label: "My Switch", mappings: [
{ command: "OFF", label: "off" },
{ command: "COOL", label: "cool" },
{ command: "HEAT", label: "heat" }
]
end
end
end
switch = sitemaps["default"].children.first
expect(switch.mappings.map(&:cmd)).to eq %w[OFF COOL HEAT]
expect(switch.mappings.map(&:label)).to eq %w[off cool heat]
# @deprecated OH 4.1 - the if check is not needed in OH4.1+
expect(switch.mappings.map(&:icon)).to eq [nil, "f7:snow", "f7:flame"] if OpenHAB::Core::VERSION >= "4.1.0"
end

it "can contain a mix of scalar, arrays, and hashes" do
sitemaps.build do
sitemap "default" do
# @deprecated OH 4.1
switch label: "My Switch", mappings: [
"OFF",
%w[COOL cool],
{ command: "HEAT", label: "heat" }
]
end
end
switch = sitemaps["default"].children.first
expect(switch.mappings.map(&:cmd)).to eq %w[OFF COOL HEAT]
expect(switch.mappings.map(&:label)).to eq %w[OFF cool heat]
end
switch = sitemaps["default"].children.first
expect(switch.mappings.map(&:label)).to eq %w[off cool heat]
expect(switch.mappings.map(&:cmd)).to eq %w[off cool heat]
end

it "can add a switch with hash mappings" do
@@ -389,8 +462,8 @@
end
end
switch = sitemaps["default"].children.first
expect(switch.mappings.map(&:label)).to eq %w[off cool heat]
expect(switch.mappings.map(&:cmd)).to eq %w[OFF COOL HEAT]
expect(switch.mappings.map(&:label)).to eq %w[off cool heat]
end

it "can add a mapview" do

0 comments on commit f5f5bbb

Please sign in to comment.