]> git.basschouten.com Git - openhab-addons.git/blob
2b70b21d2e6a06287fecbf85f01e822dccf67a6d
[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.SceneCollections.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
28 /**
29  * The {@link SceneGroupChannelBuilder} class creates scene group channels
30  * from structured scene collection data.
31  *
32  * @author Jacob Laursen - Initial contribution
33  */
34 @NonNullByDefault
35 public class SceneGroupChannelBuilder extends BaseChannelBuilder {
36
37     @Nullable
38     private List<SceneCollection> sceneCollections;
39
40     private SceneGroupChannelBuilder(HDPowerViewTranslationProvider translationProvider,
41             ChannelGroupUID channelGroupUid) {
42         super(translationProvider, channelGroupUid, HDPowerViewBindingConstants.CHANNELTYPE_SCENE_GROUP_ACTIVATE);
43     }
44
45     /**
46      * Creates a {@link SceneGroupChannelBuilder} for the given {@link HDPowerViewTranslationProvider} and
47      * {@link ChannelGroupUID}.
48      * 
49      * @param translationProvider the {@link HDPowerViewTranslationProvider}
50      * @param channelGroupUid parent {@link ChannelGroupUID} for created channels
51      * @return channel builder
52      */
53     public static SceneGroupChannelBuilder create(HDPowerViewTranslationProvider translationProvider,
54             ChannelGroupUID channelGroupUid) {
55         return new SceneGroupChannelBuilder(translationProvider, channelGroupUid);
56     }
57
58     /**
59      * Adds created channels to existing list.
60      * 
61      * @param channels list that channels will be added to
62      * @return channel builder
63      */
64     public SceneGroupChannelBuilder withChannels(List<Channel> channels) {
65         this.channels = channels;
66         return this;
67     }
68
69     /**
70      * Sets the scene collections.
71      * 
72      * @param sceneCollections the scene collections
73      * @return channel builder
74      */
75     public SceneGroupChannelBuilder withSceneCollections(List<SceneCollection> sceneCollections) {
76         this.sceneCollections = sceneCollections;
77         return this;
78     }
79
80     /**
81      * Builds and returns the channels.
82      *
83      * @return the {@link Channel} list
84      */
85     public List<Channel> build() {
86         List<SceneCollection> sceneCollections = this.sceneCollections;
87         if (sceneCollections == null) {
88             return getChannelList(0);
89         }
90         List<Channel> channels = getChannelList(sceneCollections.size());
91         sceneCollections.stream().sorted().forEach(sceneCollection -> channels.add(createChannel(sceneCollection)));
92         return channels;
93     }
94
95     private Channel createChannel(SceneCollection sceneCollection) {
96         ChannelUID channelUid = new ChannelUID(channelGroupUid, Integer.toString(sceneCollection.id));
97         String description = translationProvider.getText("dynamic-channel.scene-group-activate.description",
98                 sceneCollection.getName());
99         return ChannelBuilder.create(channelUid, CoreItemFactory.SWITCH).withType(channelTypeUid)
100                 .withLabel(sceneCollection.getName()).withDescription(description).build();
101     }
102 }