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