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