2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.hdpowerview.internal.builders;
15 import java.util.List;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants;
20 import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider;
21 import org.openhab.binding.hdpowerview.internal.dto.SceneCollection;
22 import org.openhab.core.library.CoreItemFactory;
23 import org.openhab.core.thing.Channel;
24 import org.openhab.core.thing.ChannelGroupUID;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.binding.builder.ChannelBuilder;
27 import org.openhab.core.thing.type.AutoUpdatePolicy;
30 * The {@link SceneGroupChannelBuilder} class creates scene group channels
31 * from structured scene collection data.
33 * @author Jacob Laursen - Initial contribution
36 public class SceneGroupChannelBuilder extends BaseChannelBuilder {
39 private List<SceneCollection> sceneCollections;
41 private SceneGroupChannelBuilder(HDPowerViewTranslationProvider translationProvider,
42 ChannelGroupUID channelGroupUid) {
43 super(translationProvider, channelGroupUid, HDPowerViewBindingConstants.CHANNELTYPE_SCENE_GROUP_ACTIVATE);
47 * Creates a {@link SceneGroupChannelBuilder} for the given {@link HDPowerViewTranslationProvider} and
48 * {@link ChannelGroupUID}.
50 * @param translationProvider the {@link HDPowerViewTranslationProvider}
51 * @param channelGroupUid parent {@link ChannelGroupUID} for created channels
52 * @return channel builder
54 public static SceneGroupChannelBuilder create(HDPowerViewTranslationProvider translationProvider,
55 ChannelGroupUID channelGroupUid) {
56 return new SceneGroupChannelBuilder(translationProvider, channelGroupUid);
60 * Adds created channels to existing list.
62 * @param channels list that channels will be added to
63 * @return channel builder
65 public SceneGroupChannelBuilder withChannels(List<Channel> channels) {
66 this.channels = channels;
71 * Sets the scene collections.
73 * @param sceneCollections the scene collections
74 * @return channel builder
76 public SceneGroupChannelBuilder withSceneCollections(List<SceneCollection> sceneCollections) {
77 this.sceneCollections = sceneCollections;
82 * Builds and returns the channels.
84 * @return the {@link Channel} list
86 public List<Channel> build() {
87 List<SceneCollection> sceneCollections = this.sceneCollections;
88 if (sceneCollections == null) {
89 return getChannelList(0);
91 List<Channel> channels = getChannelList(sceneCollections.size());
92 sceneCollections.stream().sorted().forEach(sceneCollection -> channels.add(createChannel(sceneCollection)));
96 private Channel createChannel(SceneCollection sceneCollection) {
97 ChannelUID channelUid = new ChannelUID(channelGroupUid, Integer.toString(sceneCollection.id));
98 String description = translationProvider.getText("dynamic-channel.scene-group-activate.description",
99 sceneCollection.getName());
100 return ChannelBuilder.create(channelUid, CoreItemFactory.SWITCH).withType(channelTypeUid)
101 .withLabel(sceneCollection.getName()).withDescription(description)
102 .withAutoUpdatePolicy(AutoUpdatePolicy.VETO).build();