]> git.basschouten.com Git - openhab-addons.git/blob
ad11985cb4cdc22044e44673b85fe4455d1412aa
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.hdpowerview.internal.builders;
14
15 import java.util.List;
16
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.api.responses.Scenes.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
28 /**
29  * The {@link SceneChannelBuilder} class creates scene channels
30  * from structured scene data.
31  *
32  * @author Jacob Laursen - Initial contribution
33  */
34 @NonNullByDefault
35 public class SceneChannelBuilder extends BaseChannelBuilder {
36
37     @Nullable
38     private List<Scene> scenes;
39
40     private SceneChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid) {
41         super(translationProvider, channelGroupUid, HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE);
42     }
43
44     /**
45      * Creates a {@link SceneChannelBuilder} for the given {@link HDPowerViewTranslationProvider} and
46      * {@link ChannelGroupUID}.
47      * 
48      * @param translationProvider the {@link HDPowerViewTranslationProvider}
49      * @param channelGroupUid parent {@link ChannelGroupUID} for created channels
50      * @return channel builder
51      */
52     public static SceneChannelBuilder create(HDPowerViewTranslationProvider translationProvider,
53             ChannelGroupUID channelGroupUid) {
54         return new SceneChannelBuilder(translationProvider, channelGroupUid);
55     }
56
57     /**
58      * Adds created channels to existing list.
59      * 
60      * @param channels list that channels will be added to
61      * @return channel builder
62      */
63     public SceneChannelBuilder withChannels(List<Channel> channels) {
64         this.channels = channels;
65         return this;
66     }
67
68     /**
69      * Sets the scenes.
70      * 
71      * @param scenes the scenes
72      * @return channel builder
73      */
74     public SceneChannelBuilder withScenes(List<Scene> scenes) {
75         this.scenes = scenes;
76         return this;
77     }
78
79     /**
80      * Builds and returns the channels.
81      *
82      * @return the {@link Channel} list
83      */
84     public List<Channel> build() {
85         List<Scene> scenes = this.scenes;
86         if (scenes == null) {
87             return getChannelList(0);
88         }
89         List<Channel> channels = getChannelList(scenes.size());
90         scenes.stream().sorted().forEach(scene -> channels.add(createChannel(scene)));
91         return channels;
92     }
93
94     private Channel createChannel(Scene scene) {
95         ChannelUID channelUid = new ChannelUID(channelGroupUid, Integer.toString(scene.id));
96         String description = translationProvider.getText("dynamic-channel.scene-activate.description", scene.getName());
97         return ChannelBuilder.create(channelUid, CoreItemFactory.SWITCH).withType(channelTypeUid)
98                 .withLabel(scene.getName()).withDescription(description).build();
99     }
100 }