]> git.basschouten.com Git - openhab-addons.git/blob
fc313af17f5198d7518f8b285b0220a0003b90fc
[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.*;
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.List;
21 import java.util.Set;
22 import java.util.stream.Collectors;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FeatureArea;
27 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.MeasureClass;
28 import org.openhab.binding.netatmo.internal.handler.capability.AirCareCapability;
29 import org.openhab.binding.netatmo.internal.handler.capability.CameraCapability;
30 import org.openhab.binding.netatmo.internal.handler.capability.Capability;
31 import org.openhab.binding.netatmo.internal.handler.capability.ChannelHelperCapability;
32 import org.openhab.binding.netatmo.internal.handler.capability.DeviceCapability;
33 import org.openhab.binding.netatmo.internal.handler.capability.DoorbellCapability;
34 import org.openhab.binding.netatmo.internal.handler.capability.HomeCapability;
35 import org.openhab.binding.netatmo.internal.handler.capability.MeasureCapability;
36 import org.openhab.binding.netatmo.internal.handler.capability.PersonCapability;
37 import org.openhab.binding.netatmo.internal.handler.capability.PresenceCapability;
38 import org.openhab.binding.netatmo.internal.handler.capability.RoomCapability;
39 import org.openhab.binding.netatmo.internal.handler.capability.SmokeCapability;
40 import org.openhab.binding.netatmo.internal.handler.capability.WeatherCapability;
41 import org.openhab.binding.netatmo.internal.handler.channelhelper.AirQualityChannelHelper;
42 import org.openhab.binding.netatmo.internal.handler.channelhelper.ApiBridgeChannelHelper;
43 import org.openhab.binding.netatmo.internal.handler.channelhelper.CameraChannelHelper;
44 import org.openhab.binding.netatmo.internal.handler.channelhelper.EnergyChannelHelper;
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.PersonChannelHelper;
49 import org.openhab.binding.netatmo.internal.handler.channelhelper.PresenceChannelHelper;
50 import org.openhab.binding.netatmo.internal.handler.channelhelper.PressureChannelHelper;
51 import org.openhab.binding.netatmo.internal.handler.channelhelper.RainChannelHelper;
52 import org.openhab.binding.netatmo.internal.handler.channelhelper.RoomChannelHelper;
53 import org.openhab.binding.netatmo.internal.handler.channelhelper.SecurityChannelHelper;
54 import org.openhab.binding.netatmo.internal.handler.channelhelper.SetpointChannelHelper;
55 import org.openhab.binding.netatmo.internal.handler.channelhelper.SirenChannelHelper;
56 import org.openhab.binding.netatmo.internal.handler.channelhelper.Therm1ChannelHelper;
57 import org.openhab.binding.netatmo.internal.handler.channelhelper.WindChannelHelper;
58 import org.openhab.core.thing.ThingTypeUID;
59
60 /**
61  * This enum all handled Netatmo modules and devices along with their capabilities
62  *
63  * @author GaĆ«l L'hopital - Initial contribution
64  */
65 @NonNullByDefault
66 public enum ModuleType {
67     UNKNOWN(FeatureArea.NONE, "", null, Set.of()),
68     ACCOUNT(FeatureArea.NONE, "", null, Set.of(), new ChannelGroup(ApiBridgeChannelHelper.class, GROUP_MONITORING)),
69
70     HOME(FeatureArea.NONE, "NAHome", ACCOUNT,
71             Set.of(DeviceCapability.class, HomeCapability.class, ChannelHelperCapability.class),
72             new ChannelGroup(SecurityChannelHelper.class, GROUP_SECURITY),
73             new ChannelGroup(EnergyChannelHelper.class, GROUP_ENERGY)),
74
75     PERSON(FeatureArea.SECURITY, "NAPerson", HOME, Set.of(PersonCapability.class, ChannelHelperCapability.class),
76             new ChannelGroup(PersonChannelHelper.class, GROUP_PERSON),
77             new ChannelGroup(EventPersonChannelHelper.class, GROUP_PERSON_LAST_EVENT)),
78
79     WELCOME(FeatureArea.SECURITY, "NACamera", HOME, Set.of(CameraCapability.class, ChannelHelperCapability.class),
80             ChannelGroup.SIGNAL, ChannelGroup.EVENT,
81             new ChannelGroup(CameraChannelHelper.class, GROUP_CAM_STATUS, GROUP_CAM_LIVE)),
82
83     SIREN(FeatureArea.SECURITY, "NIS", WELCOME, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
84             ChannelGroup.BATTERY, ChannelGroup.TIMESTAMP, new ChannelGroup(SirenChannelHelper.class, GROUP_SIREN)),
85
86     PRESENCE(FeatureArea.SECURITY, "NOC", HOME, Set.of(PresenceCapability.class, ChannelHelperCapability.class),
87             ChannelGroup.SIGNAL, ChannelGroup.EVENT,
88             new ChannelGroup(PresenceChannelHelper.class, GROUP_CAM_STATUS, GROUP_CAM_LIVE, GROUP_PRESENCE)),
89
90     DOORBELL(FeatureArea.SECURITY, "NDB", HOME, Set.of(DoorbellCapability.class, ChannelHelperCapability.class),
91             ChannelGroup.SIGNAL,
92             new ChannelGroup(CameraChannelHelper.class, GROUP_DOORBELL_STATUS, GROUP_DOORBELL_LIVE),
93             new ChannelGroup(EventDoorbellChannelHelper.class, GROUP_DOORBELL_LAST_EVENT, GROUP_DOORBELL_SUB_EVENT)),
94
95     WEATHER_STATION(FeatureArea.WEATHER, "NAMain", ACCOUNT,
96             Set.of(DeviceCapability.class, WeatherCapability.class, MeasureCapability.class,
97                     ChannelHelperCapability.class),
98             ChannelGroup.SIGNAL, ChannelGroup.HUMIDITY, ChannelGroup.TSTAMP_EXT, ChannelGroup.MEASURE,
99             ChannelGroup.AIR_QUALITY, ChannelGroup.LOCATION, ChannelGroup.NOISE, ChannelGroup.TEMP_INSIDE_EXT,
100             new ChannelGroup(PressureChannelHelper.class, MeasureClass.PRESSURE, GROUP_TYPE_PRESSURE_EXTENDED)),
101
102     OUTDOOR(FeatureArea.WEATHER, "NAModule1", WEATHER_STATION,
103             Set.of(MeasureCapability.class, ChannelHelperCapability.class), ChannelGroup.SIGNAL, ChannelGroup.HUMIDITY,
104             ChannelGroup.TSTAMP_EXT, ChannelGroup.MEASURE, ChannelGroup.BATTERY, ChannelGroup.TEMP_OUTSIDE_EXT),
105
106     WIND(FeatureArea.WEATHER, "NAModule2", WEATHER_STATION, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
107             ChannelGroup.TSTAMP_EXT, ChannelGroup.BATTERY, new ChannelGroup(WindChannelHelper.class, GROUP_WIND)),
108
109     RAIN(FeatureArea.WEATHER, "NAModule3", WEATHER_STATION,
110             Set.of(MeasureCapability.class, ChannelHelperCapability.class), ChannelGroup.SIGNAL,
111             ChannelGroup.TSTAMP_EXT, ChannelGroup.MEASURE, ChannelGroup.BATTERY,
112             new ChannelGroup(RainChannelHelper.class, MeasureClass.RAIN_QUANTITY, GROUP_RAIN)),
113
114     INDOOR(FeatureArea.WEATHER, "NAModule4", WEATHER_STATION,
115             Set.of(MeasureCapability.class, ChannelHelperCapability.class), ChannelGroup.SIGNAL,
116             ChannelGroup.TSTAMP_EXT, ChannelGroup.MEASURE, ChannelGroup.BATTERY, ChannelGroup.HUMIDITY,
117             ChannelGroup.TEMP_INSIDE_EXT, ChannelGroup.AIR_QUALITY),
118
119     HOME_COACH(FeatureArea.AIR_CARE, "NHC", ACCOUNT,
120             Set.of(DeviceCapability.class, AirCareCapability.class, MeasureCapability.class,
121                     ChannelHelperCapability.class),
122             ChannelGroup.LOCATION, ChannelGroup.SIGNAL, ChannelGroup.NOISE, ChannelGroup.HUMIDITY,
123             ChannelGroup.TEMP_INSIDE, ChannelGroup.MEASURE, ChannelGroup.TSTAMP_EXT,
124             new ChannelGroup(AirQualityChannelHelper.class, GROUP_TYPE_AIR_QUALITY_EXTENDED),
125             new ChannelGroup(PressureChannelHelper.class, MeasureClass.PRESSURE, GROUP_PRESSURE)),
126
127     PLUG(FeatureArea.ENERGY, "NAPlug", HOME, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL),
128
129     VALVE(FeatureArea.ENERGY, "NRV", PLUG, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
130             ChannelGroup.BATTERY_EXT),
131
132     THERMOSTAT(FeatureArea.ENERGY, "NATherm1", PLUG, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
133             ChannelGroup.BATTERY_EXT, new ChannelGroup(Therm1ChannelHelper.class, GROUP_TYPE_TH_PROPERTIES)),
134
135     ROOM(FeatureArea.ENERGY, "NARoom", HOME, Set.of(RoomCapability.class, ChannelHelperCapability.class),
136             new ChannelGroup(RoomChannelHelper.class, GROUP_TYPE_ROOM_PROPERTIES, GROUP_TYPE_ROOM_TEMPERATURE),
137             new ChannelGroup(SetpointChannelHelper.class, GROUP_SETPOINT)),
138
139     SMOKE_DETECTOR(FeatureArea.SECURITY, "NSD", HOME, Set.of(SmokeCapability.class, ChannelHelperCapability.class),
140             ChannelGroup.SIGNAL, ChannelGroup.TIMESTAMP,
141             new ChannelGroup(EventChannelHelper.class, GROUP_SMOKE_LAST_EVENT));
142
143     public static final EnumSet<ModuleType> AS_SET = EnumSet.allOf(ModuleType.class);
144
145     private final @Nullable ModuleType bridgeType;
146     public final Set<ChannelGroup> channelGroups;
147     public final Set<Class<? extends Capability>> capabilities;
148     public final ThingTypeUID thingTypeUID;
149     public final FeatureArea feature;
150     public final String apiName;
151
152     ModuleType(FeatureArea feature, String apiName, @Nullable ModuleType bridge,
153             Set<Class<? extends Capability>> capabilities, ChannelGroup... channelGroups) {
154         this.bridgeType = bridge;
155         this.feature = feature;
156         this.capabilities = capabilities;
157         this.apiName = apiName;
158         this.channelGroups = Set.of(channelGroups);
159         this.thingTypeUID = new ThingTypeUID(BINDING_ID, name().toLowerCase().replace("_", "-"));
160     }
161
162     public boolean isLogical() {
163         return !channelGroups.contains(ChannelGroup.SIGNAL);
164     }
165
166     public boolean isABridge() {
167         for (ModuleType mt : ModuleType.values()) {
168             if (this.equals(mt.bridgeType)) {
169                 return true;
170             }
171         }
172         return false;
173     }
174
175     public List<String> getExtensions() {
176         return channelGroups.stream().map(cg -> cg.extensions).flatMap(Set::stream).collect(Collectors.toList());
177     }
178
179     public Set<String> getGroupTypes() {
180         return channelGroups.stream().map(cg -> cg.groupTypes).flatMap(Set::stream).collect(Collectors.toSet());
181     }
182
183     public int[] getSignalLevels() {
184         if (!isLogical()) {
185             return (channelGroups.contains(ChannelGroup.BATTERY) || channelGroups.contains(ChannelGroup.BATTERY_EXT))
186                     ? RADIO_SIGNAL_LEVELS
187                     : WIFI_SIGNAL_LEVELS;
188         }
189         throw new IllegalArgumentException(
190                 "This should not be called for module type : " + name() + ", please file a bug report.");
191     }
192
193     public ModuleType getBridge() {
194         ModuleType bridge = bridgeType;
195         return bridge != null ? bridge : ModuleType.UNKNOWN;
196     }
197
198     public URI getConfigDescription() {
199         return URI.create(BINDING_ID + ":"
200                 + (equals(ACCOUNT) ? "api_bridge"
201                         : equals(HOME) ? "home"
202                                 : (isLogical() ? "virtual"
203                                         : ModuleType.UNKNOWN.equals(getBridge()) ? "configurable" : "device")));
204     }
205
206     public static ModuleType from(ThingTypeUID thingTypeUID) {
207         return ModuleType.AS_SET.stream().filter(mt -> mt.thingTypeUID.equals(thingTypeUID)).findFirst()
208                 .orElseThrow(() -> new IllegalArgumentException());
209     }
210 }