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.Scene;
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 SceneChannelBuilder} class creates scene channels
31 * from structured scene data.
33 * @author Jacob Laursen - Initial contribution
36 public class SceneChannelBuilder extends BaseChannelBuilder {
39 private List<Scene> scenes;
41 private SceneChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid) {
42 super(translationProvider, channelGroupUid, HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE);
46 * Creates a {@link SceneChannelBuilder} for the given {@link HDPowerViewTranslationProvider} and
47 * {@link ChannelGroupUID}.
49 * @param translationProvider the {@link HDPowerViewTranslationProvider}
50 * @param channelGroupUid parent {@link ChannelGroupUID} for created channels
51 * @return channel builder
53 public static SceneChannelBuilder create(HDPowerViewTranslationProvider translationProvider,
54 ChannelGroupUID channelGroupUid) {
55 return new SceneChannelBuilder(translationProvider, channelGroupUid);
59 * Adds created channels to existing list.
61 * @param channels list that channels will be added to
62 * @return channel builder
64 public SceneChannelBuilder withChannels(List<Channel> channels) {
65 this.channels = channels;
72 * @param scenes the scenes
73 * @return channel builder
75 public SceneChannelBuilder withScenes(List<Scene> scenes) {
81 * Builds and returns the channels.
83 * @return the {@link Channel} list
85 public List<Channel> build() {
86 List<Scene> scenes = this.scenes;
88 return getChannelList(0);
90 List<Channel> channels = getChannelList(scenes.size());
91 scenes.stream().sorted().forEach(scene -> channels.add(createChannel(scene)));
95 private Channel createChannel(Scene scene) {
96 ChannelUID channelUid = new ChannelUID(channelGroupUid, Integer.toString(scene.id));
97 String description = translationProvider.getText("dynamic-channel.scene-activate.description", scene.getName());
98 return ChannelBuilder.create(channelUid, CoreItemFactory.SWITCH).withType(channelTypeUid)
99 .withLabel(scene.getName()).withDescription(description).withAutoUpdatePolicy(AutoUpdatePolicy.VETO)