]> git.basschouten.com Git - openhab-addons.git/blob
524b46d8943e0d8418cf108fd0d7b2f5eff6be13
[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.neeo.internal.handler;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.neeo.internal.NeeoConstants;
20 import org.openhab.binding.neeo.internal.UidUtils;
21 import org.openhab.binding.neeo.internal.models.NeeoDevice;
22 import org.openhab.binding.neeo.internal.models.NeeoMacro;
23 import org.openhab.binding.neeo.internal.models.NeeoRecipe;
24 import org.openhab.binding.neeo.internal.models.NeeoRecipes;
25 import org.openhab.binding.neeo.internal.models.NeeoRoom;
26 import org.openhab.binding.neeo.internal.models.NeeoScenario;
27 import org.openhab.binding.neeo.internal.models.NeeoScenarios;
28 import org.openhab.core.thing.Channel;
29 import org.openhab.core.thing.ThingUID;
30 import org.openhab.core.thing.binding.builder.ChannelBuilder;
31
32 /**
33  * Utility class for generating channels
34  *
35  * @author Tim Roberts - Initial Contribution
36  */
37 @NonNullByDefault
38 class ChannelUtils {
39     /**
40      * Generates a list of {@link Channel} s for a specific device. This implementation will generate a channel for each
41      * macro found on the device.
42      *
43      * @param thingUid a non-null thingUID
44      * @param device a non-null device
45      * @return a non-null but possibly empty list of {@link Channel} s
46      */
47     static List<Channel> generateChannels(ThingUID thingUid, NeeoDevice device) {
48         final List<Channel> channels = new ArrayList<>();
49         for (NeeoMacro macro : device.getMacros().getMacros()) {
50             final String key = macro.getKey();
51             if (key != null && !key.isEmpty()) {
52                 String name = macro.getName();
53                 final String label = (name == null || name.isEmpty()) ? macro.getLabel() : name;
54                 channels.add(ChannelBuilder
55                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.DEVICE_GROUP_MACROS_ID,
56                                 NeeoConstants.DEVICE_CHANNEL_STATUS, key), "Switch")
57                         .withLabel((label == null || label.isEmpty()) ? key : label)
58                         .withType(NeeoConstants.DEVICE_MACRO_STATUS_UID).build());
59             }
60         }
61         return channels;
62     }
63
64     /**
65      * Generates a list of {@link Channel} s for a specific room. This implementation will generate multiple channels
66      * for each scenario and recipe in the room (in addition to the current step channel)
67      *
68      * @param thingUid a non-null thingUID
69      * @param room a non-null room
70      * @return a non-null but possibly empty list of {@link Channel} s
71      */
72     static List<Channel> generateChannels(ThingUID thingUid, NeeoRoom room) {
73         final List<Channel> channels = new ArrayList<>();
74         channels.addAll(generateStateChannels(thingUid));
75         channels.addAll(generateScenarioChannels(thingUid, room.getScenarios()));
76         channels.addAll(generateRecipeChannels(thingUid, room.getRecipes()));
77         return channels;
78     }
79
80     /**
81      * Helper method to generate state channels (the current step)
82      *
83      * @param thingUid a non-null thingUID
84      * @return a non-null but possibly empty list of {@link Channel} s
85      */
86     private static List<Channel> generateStateChannels(ThingUID thingUid) {
87         final List<Channel> channels = new ArrayList<>();
88         channels.add(ChannelBuilder
89                 .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_STATE_ID,
90                         NeeoConstants.ROOM_CHANNEL_CURRENTSTEP), "String")
91                 .withType(NeeoConstants.ROOM_STATE_CURRENTSTEP_UID).build());
92         return channels;
93     }
94
95     /**
96      * Helper method to generate scenario channels
97      *
98      * @param thingUid a non-null thingUID
99      * @param scenarios the non-null scenarios
100      * @return a non-null but possibly empty list of {@link Channel} s
101      */
102     private static List<Channel> generateScenarioChannels(ThingUID thingUid, NeeoScenarios scenarios) {
103         final List<Channel> channels = new ArrayList<>();
104         for (NeeoScenario scenario : scenarios.getScenarios()) {
105             final String key = scenario.getKey();
106             if (key != null && !key.isEmpty()) {
107                 final String name = scenario.getName();
108                 final String scenarioLabel = (name == null || name.isEmpty()) ? key : name;
109                 final String nameLabel = scenarioLabel + " Name";
110                 final String configuredLabel = scenarioLabel + " Configured";
111                 final String statusLabel = scenarioLabel + " Status";
112
113                 channels.add(ChannelBuilder
114                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_SCENARIO_ID,
115                                 NeeoConstants.ROOM_CHANNEL_NAME, key), "String")
116                         .withLabel(nameLabel).withType(NeeoConstants.ROOM_SCENARIO_NAME_UID).build());
117                 channels.add(ChannelBuilder
118                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_SCENARIO_ID,
119                                 NeeoConstants.ROOM_CHANNEL_CONFIGURED, key), "Switch")
120                         .withLabel(configuredLabel).withType(NeeoConstants.ROOM_SCENARIO_CONFIGURED_UID).build());
121                 channels.add(ChannelBuilder
122                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_SCENARIO_ID,
123                                 NeeoConstants.ROOM_CHANNEL_STATUS, key), "Switch")
124                         .withLabel(statusLabel).withType(NeeoConstants.ROOM_SCENARIO_STATUS_UID).build());
125             }
126         }
127         return channels;
128     }
129
130     /**
131      * Helper method to generate recipe channels
132      *
133      * @param thingUid a non-null thingUID
134      * @param recipes the non-null recipes
135      * @return a non-null but possibly empty list of {@link Channel} s
136      */
137     private static List<Channel> generateRecipeChannels(ThingUID thingUid, NeeoRecipes recipes) {
138         final List<Channel> channels = new ArrayList<>();
139         for (NeeoRecipe recipe : recipes.getRecipes()) {
140             final String key = recipe.getKey();
141             if (key != null && !key.isEmpty()) {
142                 final String name = recipe.getName();
143                 final String recipeLabel = (name == null || name.isEmpty()) ? key : name;
144                 final String nameLabel = recipeLabel + " Name (" + recipe.getType() + ")";
145                 final String typeLabel = recipeLabel + " Type (" + recipe.getType() + ")";
146                 final String enabledLabel = recipeLabel + " Enabled (" + recipe.getType() + ")";
147                 final String statusLabel = recipeLabel + " Status (" + recipe.getType() + ")";
148
149                 channels.add(ChannelBuilder
150                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_RECIPE_ID,
151                                 NeeoConstants.ROOM_CHANNEL_NAME, key), "String")
152                         .withLabel(nameLabel).withType(NeeoConstants.ROOM_RECIPE_NAME_UID).build());
153                 channels.add(ChannelBuilder
154                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_RECIPE_ID,
155                                 NeeoConstants.ROOM_CHANNEL_TYPE, key), "String")
156                         .withLabel(enabledLabel).withType(NeeoConstants.ROOM_RECIPE_TYPE_UID).build());
157                 channels.add(ChannelBuilder
158                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_RECIPE_ID,
159                                 NeeoConstants.ROOM_CHANNEL_ENABLED, key), "Switch")
160                         .withLabel(typeLabel).withType(NeeoConstants.ROOM_RECIPE_ENABLED_UID).build());
161                 channels.add(ChannelBuilder
162                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_RECIPE_ID,
163                                 NeeoConstants.ROOM_CHANNEL_STATUS, key), "Switch")
164                         .withLabel(statusLabel).withType(NeeoConstants.ROOM_RECIPE_STATUS_UID).build());
165             }
166         }
167         return channels;
168     }
169 }