]> git.basschouten.com Git - openhab-addons.git/blob
82a2c7e7b63e13bed61e819196f965a67f8a330b
[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.netatmo.internal.api.data;
14
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.BINDING_ID;
16 import static org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.*;
17
18 import java.net.URI;
19 import java.util.EnumSet;
20 import java.util.LinkedList;
21 import java.util.List;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FeatureArea;
26 import org.openhab.binding.netatmo.internal.handler.capability.AirCareCapability;
27 import org.openhab.binding.netatmo.internal.handler.capability.CameraCapability;
28 import org.openhab.binding.netatmo.internal.handler.capability.Capability;
29 import org.openhab.binding.netatmo.internal.handler.capability.ChannelHelperCapability;
30 import org.openhab.binding.netatmo.internal.handler.capability.DeviceCapability;
31 import org.openhab.binding.netatmo.internal.handler.capability.EventCapability;
32 import org.openhab.binding.netatmo.internal.handler.capability.HomeCapability;
33 import org.openhab.binding.netatmo.internal.handler.capability.MeasureCapability;
34 import org.openhab.binding.netatmo.internal.handler.capability.PersonCapability;
35 import org.openhab.binding.netatmo.internal.handler.capability.PresenceCapability;
36 import org.openhab.binding.netatmo.internal.handler.capability.RoomCapability;
37 import org.openhab.binding.netatmo.internal.handler.capability.WeatherCapability;
38 import org.openhab.binding.netatmo.internal.handler.channelhelper.AirQualityChannelHelper;
39 import org.openhab.binding.netatmo.internal.handler.channelhelper.AirQualityExtChannelHelper;
40 import org.openhab.binding.netatmo.internal.handler.channelhelper.BatteryChannelHelper;
41 import org.openhab.binding.netatmo.internal.handler.channelhelper.BatteryExtChannelHelper;
42 import org.openhab.binding.netatmo.internal.handler.channelhelper.CameraChannelHelper;
43 import org.openhab.binding.netatmo.internal.handler.channelhelper.ChannelHelper;
44 import org.openhab.binding.netatmo.internal.handler.channelhelper.EventChannelHelper;
45 import org.openhab.binding.netatmo.internal.handler.channelhelper.EventPersonChannelHelper;
46 import org.openhab.binding.netatmo.internal.handler.channelhelper.HomeEnergyChannelHelper;
47 import org.openhab.binding.netatmo.internal.handler.channelhelper.HomeSecurityChannelHelper;
48 import org.openhab.binding.netatmo.internal.handler.channelhelper.HumidityChannelHelper;
49 import org.openhab.binding.netatmo.internal.handler.channelhelper.LocationChannelHelper;
50 import org.openhab.binding.netatmo.internal.handler.channelhelper.MeasuresChannelHelper;
51 import org.openhab.binding.netatmo.internal.handler.channelhelper.NoiseChannelHelper;
52 import org.openhab.binding.netatmo.internal.handler.channelhelper.PersonChannelHelper;
53 import org.openhab.binding.netatmo.internal.handler.channelhelper.PresenceChannelHelper;
54 import org.openhab.binding.netatmo.internal.handler.channelhelper.PressureChannelHelper;
55 import org.openhab.binding.netatmo.internal.handler.channelhelper.PressureExtChannelHelper;
56 import org.openhab.binding.netatmo.internal.handler.channelhelper.RainChannelHelper;
57 import org.openhab.binding.netatmo.internal.handler.channelhelper.RoomChannelHelper;
58 import org.openhab.binding.netatmo.internal.handler.channelhelper.SetpointChannelHelper;
59 import org.openhab.binding.netatmo.internal.handler.channelhelper.SignalChannelHelper;
60 import org.openhab.binding.netatmo.internal.handler.channelhelper.TemperatureChannelHelper;
61 import org.openhab.binding.netatmo.internal.handler.channelhelper.TemperatureExtChannelHelper;
62 import org.openhab.binding.netatmo.internal.handler.channelhelper.TemperatureOutChannelHelper;
63 import org.openhab.binding.netatmo.internal.handler.channelhelper.Therm1ChannelHelper;
64 import org.openhab.binding.netatmo.internal.handler.channelhelper.TimestampChannelHelper;
65 import org.openhab.binding.netatmo.internal.handler.channelhelper.TimestampExtChannelHelper;
66 import org.openhab.binding.netatmo.internal.handler.channelhelper.WindChannelHelper;
67 import org.openhab.core.thing.ThingTypeUID;
68
69 import com.google.gson.annotations.SerializedName;
70
71 /**
72  * This enum all handled Netatmo modules and devices along with their capabilities
73  *
74  * @author GaĆ«l L'hopital - Initial contribution
75  */
76 @NonNullByDefault
77 public enum ModuleType {
78     UNKNOWN(FeatureArea.NONE, null, null, List.of(), List.of()),
79     ACCOUNT(FeatureArea.NONE, null, null, List.of(), List.of()),
80     @SerializedName("NAHome")
81     HOME(FeatureArea.NONE, "NAHome", ACCOUNT,
82             List.of(DeviceCapability.class, EventCapability.class, HomeCapability.class, ChannelHelperCapability.class),
83             List.of(HomeSecurityChannelHelper.class, HomeEnergyChannelHelper.class)),
84     @SerializedName("NAPerson")
85     PERSON(FeatureArea.SECURITY, "NAPerson", HOME,
86             List.of(EventCapability.class, PersonCapability.class, ChannelHelperCapability.class),
87             List.of(PersonChannelHelper.class, EventPersonChannelHelper.class)),
88     @SerializedName("NACamera")
89     WELCOME(FeatureArea.SECURITY, "NACamera", HOME,
90             List.of(EventCapability.class, CameraCapability.class, ChannelHelperCapability.class),
91             List.of(CameraChannelHelper.class, SignalChannelHelper.class, EventChannelHelper.class)),
92     @SerializedName("NOC")
93     PRESENCE(FeatureArea.SECURITY, "NOC", HOME,
94             List.of(EventCapability.class, PresenceCapability.class, ChannelHelperCapability.class),
95             List.of(CameraChannelHelper.class, PresenceChannelHelper.class, SignalChannelHelper.class,
96                     EventChannelHelper.class)),
97     @SerializedName("NIS")
98     SIREN(FeatureArea.SECURITY, "NIS", HOME, List.of(ChannelHelperCapability.class),
99             List.of(BatteryChannelHelper.class, TimestampChannelHelper.class, SignalChannelHelper.class)),
100     @SerializedName("NDB")
101     DOORBELL(FeatureArea.SECURITY, "NDB", HOME, List.of(ChannelHelperCapability.class),
102             List.of(SignalChannelHelper.class)),
103     @SerializedName("NAMain")
104     WEATHER_STATION(FeatureArea.WEATHER, "NAMain", ACCOUNT,
105             List.of(DeviceCapability.class, WeatherCapability.class, MeasureCapability.class,
106                     ChannelHelperCapability.class),
107             List.of(PressureExtChannelHelper.class, NoiseChannelHelper.class, HumidityChannelHelper.class,
108                     TemperatureExtChannelHelper.class, AirQualityChannelHelper.class, LocationChannelHelper.class,
109                     TimestampExtChannelHelper.class, MeasuresChannelHelper.class, SignalChannelHelper.class)),
110     @SerializedName("NAModule1")
111     OUTDOOR(FeatureArea.WEATHER, "NAModule1", WEATHER_STATION,
112             List.of(MeasureCapability.class, ChannelHelperCapability.class),
113             List.of(HumidityChannelHelper.class, TemperatureOutChannelHelper.class, BatteryChannelHelper.class,
114                     MeasuresChannelHelper.class, TimestampExtChannelHelper.class, SignalChannelHelper.class)),
115     @SerializedName("NAModule2")
116     WIND(FeatureArea.WEATHER, "NAModule2", WEATHER_STATION, List.of(ChannelHelperCapability.class),
117             List.of(WindChannelHelper.class, BatteryChannelHelper.class, TimestampExtChannelHelper.class,
118                     SignalChannelHelper.class)),
119     @SerializedName("NAModule3")
120     RAIN(FeatureArea.WEATHER, "NAModule3", WEATHER_STATION,
121             List.of(MeasureCapability.class, ChannelHelperCapability.class),
122             List.of(RainChannelHelper.class, BatteryChannelHelper.class, MeasuresChannelHelper.class,
123                     TimestampExtChannelHelper.class, SignalChannelHelper.class)),
124     @SerializedName("NAModule4")
125     INDOOR(FeatureArea.WEATHER, "NAModule4", WEATHER_STATION,
126             List.of(MeasureCapability.class, ChannelHelperCapability.class),
127             List.of(HumidityChannelHelper.class, TemperatureExtChannelHelper.class, AirQualityChannelHelper.class,
128                     BatteryChannelHelper.class, MeasuresChannelHelper.class, TimestampExtChannelHelper.class,
129                     SignalChannelHelper.class)),
130     @SerializedName("NHC")
131     HOME_COACH(FeatureArea.AIR_CARE, "NHC", ACCOUNT,
132             List.of(DeviceCapability.class, AirCareCapability.class, MeasureCapability.class,
133                     ChannelHelperCapability.class),
134             List.of(NoiseChannelHelper.class, HumidityChannelHelper.class, AirQualityExtChannelHelper.class,
135                     TemperatureChannelHelper.class, PressureChannelHelper.class, TimestampExtChannelHelper.class,
136                     SignalChannelHelper.class, MeasuresChannelHelper.class, LocationChannelHelper.class)),
137     @SerializedName("NAPlug")
138     PLUG(FeatureArea.ENERGY, "NAPlug", HOME, List.of(ChannelHelperCapability.class),
139             List.of(SignalChannelHelper.class)),
140     @SerializedName("NATherm1")
141     THERMOSTAT(FeatureArea.ENERGY, "NATherm1", HOME, List.of(ChannelHelperCapability.class),
142             List.of(Therm1ChannelHelper.class, BatteryExtChannelHelper.class, SignalChannelHelper.class)),
143     @SerializedName("NARoom")
144     ROOM(FeatureArea.ENERGY, "NARoom", HOME, List.of(RoomCapability.class, ChannelHelperCapability.class),
145             List.of(RoomChannelHelper.class, SetpointChannelHelper.class)),
146     @SerializedName("NRV")
147     VALVE(FeatureArea.ENERGY, "NRV", HOME, List.of(ChannelHelperCapability.class),
148             List.of(BatteryExtChannelHelper.class, SignalChannelHelper.class));
149
150     public static final EnumSet<ModuleType> AS_SET = EnumSet.allOf(ModuleType.class);
151
152     private final @Nullable ModuleType bridgeType;
153     public final List<String> groupTypes = new LinkedList<>();
154     public final List<String> extensions = new LinkedList<>();
155     public final List<Class<? extends ChannelHelper>> channelHelpers;
156     public final List<Class<? extends Capability>> capabilities;
157     public final ThingTypeUID thingTypeUID;
158     public final FeatureArea feature;
159     public final @Nullable String apiName;
160
161     ModuleType(FeatureArea feature, @Nullable String apiName, @Nullable ModuleType bridge,
162             List<Class<? extends Capability>> capabilities, List<Class<? extends ChannelHelper>> helpers) {
163         this.channelHelpers = helpers;
164         this.bridgeType = bridge;
165         this.feature = feature;
166         this.capabilities = capabilities;
167         this.apiName = apiName;
168         thingTypeUID = new ThingTypeUID(BINDING_ID, name().toLowerCase().replace("_", "-"));
169         try {
170             for (Class<? extends ChannelHelper> helperClass : helpers) {
171                 ChannelHelper helper = helperClass.getConstructor().newInstance();
172                 groupTypes.addAll(helper.getChannelGroupTypes());
173                 extensions.addAll(helper.getExtensibleChannels());
174             }
175         } catch (RuntimeException | ReflectiveOperationException e) {
176             throw new IllegalArgumentException(e);
177         }
178     }
179
180     public boolean isLogical() {
181         return !channelHelpers.contains(SignalChannelHelper.class);
182     }
183
184     public boolean isABridge() {
185         for (ModuleType mt : ModuleType.values()) {
186             if (this.equals(mt.bridgeType)) {
187                 return true;
188             }
189         }
190         return false;
191     }
192
193     public int[] getSignalLevels() {
194         if (!isLogical()) {
195             return (channelHelpers.contains(BatteryChannelHelper.class)
196                     || channelHelpers.contains(BatteryExtChannelHelper.class)) ? RADIO_SIGNAL_LEVELS
197                             : WIFI_SIGNAL_LEVELS;
198         }
199         throw new IllegalArgumentException(
200                 "This should not be called for module type : " + name() + ", please file a bug report.");
201     }
202
203     public ModuleType getBridge() {
204         ModuleType bridge = bridgeType;
205         return bridge != null ? bridge : ModuleType.UNKNOWN;
206     }
207
208     public URI getConfigDescription() {
209         return URI.create(BINDING_ID + ":"
210                 + (equals(ACCOUNT) ? "api_bridge"
211                         : equals(HOME) ? "home"
212                                 : (isLogical() ? "virtual"
213                                         : ModuleType.UNKNOWN.equals(getBridge()) ? "configurable" : "device")));
214     }
215
216     public static ModuleType from(ThingTypeUID thingTypeUID) {
217         return ModuleType.AS_SET.stream().filter(mt -> mt.thingTypeUID.equals(thingTypeUID)).findFirst()
218                 .orElseThrow(() -> new IllegalArgumentException());
219     }
220
221     public static ModuleType from(String apiName) {
222         return ModuleType.AS_SET.stream().filter(mt -> apiName.equals(mt.apiName)).findFirst()
223                 .orElseThrow(() -> new IllegalArgumentException());
224     }
225 }