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