]> git.basschouten.com Git - openhab-addons.git/blob
f999e6a1e1684c80760f34b7147b94249efea282
[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.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, 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, Set.of(PersonCapability.class, ChannelHelperCapability.class),
75             new ChannelGroup(PersonChannelHelper.class, GROUP_PERSON),
76             new ChannelGroup(EventPersonChannelHelper.class, GROUP_PERSON_LAST_EVENT)),
77
78     WELCOME(FeatureArea.SECURITY, "NACamera", HOME, Set.of(CameraCapability.class, ChannelHelperCapability.class),
79             ChannelGroup.SIGNAL, ChannelGroup.EVENT,
80             new ChannelGroup(CameraChannelHelper.class, GROUP_CAM_STATUS, GROUP_CAM_LIVE)),
81
82     SIREN(FeatureArea.SECURITY, "NIS", WELCOME, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
83             ChannelGroup.BATTERY, ChannelGroup.TIMESTAMP, new ChannelGroup(SirenChannelHelper.class, GROUP_SIREN)),
84
85     PRESENCE(FeatureArea.SECURITY, "NOC", HOME, Set.of(PresenceCapability.class, ChannelHelperCapability.class),
86             ChannelGroup.SIGNAL, ChannelGroup.EVENT,
87             new ChannelGroup(PresenceChannelHelper.class, GROUP_CAM_STATUS, GROUP_CAM_LIVE, GROUP_PRESENCE)),
88
89     DOORBELL(FeatureArea.SECURITY, "NDB", HOME, Set.of(DoorbellCapability.class, ChannelHelperCapability.class),
90             ChannelGroup.SIGNAL,
91             new ChannelGroup(CameraChannelHelper.class, GROUP_DOORBELL_STATUS, GROUP_DOORBELL_LIVE),
92             new ChannelGroup(EventDoorbellChannelHelper.class, GROUP_DOORBELL_LAST_EVENT, GROUP_DOORBELL_SUB_EVENT)),
93
94     WEATHER_STATION(FeatureArea.WEATHER, "NAMain", ACCOUNT,
95             Set.of(DeviceCapability.class, WeatherCapability.class, MeasureCapability.class,
96                     ChannelHelperCapability.class),
97             ChannelGroup.SIGNAL, ChannelGroup.HUMIDITY, ChannelGroup.TSTAMP_EXT, ChannelGroup.MEASURE,
98             ChannelGroup.AIR_QUALITY, ChannelGroup.LOCATION, ChannelGroup.NOISE, ChannelGroup.TEMP_INSIDE_EXT,
99             new ChannelGroup(PressureChannelHelper.class, MeasureClass.PRESSURE, GROUP_TYPE_PRESSURE_EXTENDED)),
100
101     OUTDOOR(FeatureArea.WEATHER, "NAModule1", WEATHER_STATION,
102             Set.of(MeasureCapability.class, ChannelHelperCapability.class), ChannelGroup.SIGNAL, ChannelGroup.HUMIDITY,
103             ChannelGroup.TSTAMP_EXT, ChannelGroup.MEASURE, ChannelGroup.BATTERY, ChannelGroup.TEMP_OUTSIDE_EXT),
104
105     WIND(FeatureArea.WEATHER, "NAModule2", WEATHER_STATION, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
106             ChannelGroup.TSTAMP_EXT, ChannelGroup.BATTERY, new ChannelGroup(WindChannelHelper.class, GROUP_WIND)),
107
108     RAIN(FeatureArea.WEATHER, "NAModule3", WEATHER_STATION,
109             Set.of(MeasureCapability.class, ChannelHelperCapability.class), ChannelGroup.SIGNAL,
110             ChannelGroup.TSTAMP_EXT, ChannelGroup.MEASURE, ChannelGroup.BATTERY,
111             new ChannelGroup(RainChannelHelper.class, MeasureClass.RAIN_QUANTITY, GROUP_RAIN)),
112
113     INDOOR(FeatureArea.WEATHER, "NAModule4", WEATHER_STATION,
114             Set.of(MeasureCapability.class, ChannelHelperCapability.class), ChannelGroup.SIGNAL,
115             ChannelGroup.TSTAMP_EXT, ChannelGroup.MEASURE, ChannelGroup.BATTERY, ChannelGroup.HUMIDITY,
116             ChannelGroup.TEMP_INSIDE_EXT, ChannelGroup.AIR_QUALITY),
117
118     HOME_COACH(FeatureArea.AIR_CARE, "NHC", ACCOUNT,
119             Set.of(DeviceCapability.class, AirCareCapability.class, MeasureCapability.class,
120                     ChannelHelperCapability.class),
121             ChannelGroup.LOCATION, ChannelGroup.SIGNAL, ChannelGroup.NOISE, ChannelGroup.HUMIDITY,
122             ChannelGroup.TEMP_INSIDE, ChannelGroup.MEASURE, ChannelGroup.TSTAMP_EXT,
123             new ChannelGroup(AirQualityChannelHelper.class, GROUP_TYPE_AIR_QUALITY_EXTENDED),
124             new ChannelGroup(PressureChannelHelper.class, MeasureClass.PRESSURE, GROUP_PRESSURE)),
125
126     PLUG(FeatureArea.ENERGY, "NAPlug", HOME, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL),
127
128     VALVE(FeatureArea.ENERGY, "NRV", PLUG, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
129             ChannelGroup.BATTERY_EXT),
130
131     THERMOSTAT(FeatureArea.ENERGY, "NATherm1", PLUG, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
132             ChannelGroup.BATTERY_EXT, new ChannelGroup(Therm1ChannelHelper.class, GROUP_TYPE_TH_PROPERTIES)),
133
134     ROOM(FeatureArea.ENERGY, "NARoom", HOME, Set.of(RoomCapability.class, ChannelHelperCapability.class),
135             new ChannelGroup(RoomChannelHelper.class, GROUP_TYPE_ROOM_PROPERTIES, GROUP_TYPE_ROOM_TEMPERATURE),
136             new ChannelGroup(SetpointChannelHelper.class, GROUP_SETPOINT)),
137
138     SMOKE_DETECTOR(FeatureArea.SECURITY, "NSD", HOME, Set.of(SmokeCapability.class, ChannelHelperCapability.class),
139             ChannelGroup.SIGNAL, ChannelGroup.TIMESTAMP,
140             new ChannelGroup(EventChannelHelper.class, GROUP_SMOKE_LAST_EVENT));
141
142     public static final EnumSet<ModuleType> AS_SET = EnumSet.allOf(ModuleType.class);
143
144     private final @Nullable ModuleType bridgeType;
145     public final Set<ChannelGroup> channelGroups;
146     public final Set<Class<? extends Capability>> capabilities;
147     public final ThingTypeUID thingTypeUID;
148     public final FeatureArea feature;
149     public final String apiName;
150
151     ModuleType(FeatureArea feature, String apiName, @Nullable ModuleType bridge,
152             Set<Class<? extends Capability>> capabilities, ChannelGroup... channelGroups) {
153         this.bridgeType = bridge;
154         this.feature = feature;
155         this.capabilities = capabilities;
156         this.apiName = apiName;
157         this.channelGroups = Set.of(channelGroups);
158         this.thingTypeUID = new ThingTypeUID(BINDING_ID, name().toLowerCase().replace("_", "-"));
159     }
160
161     public boolean isLogical() {
162         return !channelGroups.contains(ChannelGroup.SIGNAL);
163     }
164
165     public boolean isABridge() {
166         for (ModuleType mt : ModuleType.values()) {
167             if (this.equals(mt.bridgeType)) {
168                 return true;
169             }
170         }
171         return false;
172     }
173
174     public List<String> getExtensions() {
175         return channelGroups.stream().map(cg -> cg.extensions).flatMap(Set::stream).collect(Collectors.toList());
176     }
177
178     public Set<String> getGroupTypes() {
179         return channelGroups.stream().map(cg -> cg.groupTypes).flatMap(Set::stream).collect(Collectors.toSet());
180     }
181
182     public int[] getSignalLevels() {
183         if (!isLogical()) {
184             return (channelGroups.contains(ChannelGroup.BATTERY) || channelGroups.contains(ChannelGroup.BATTERY_EXT))
185                     ? RADIO_SIGNAL_LEVELS
186                     : WIFI_SIGNAL_LEVELS;
187         }
188         throw new IllegalArgumentException(
189                 "This should not be called for module type : " + name() + ", please file a bug report.");
190     }
191
192     public ModuleType getBridge() {
193         ModuleType bridge = bridgeType;
194         return bridge != null ? bridge : ModuleType.UNKNOWN;
195     }
196
197     public URI getConfigDescription() {
198         return URI.create(BINDING_ID + ":"
199                 + (equals(ACCOUNT) ? "api_bridge"
200                         : equals(HOME) ? "home"
201                                 : (isLogical() ? "virtual"
202                                         : ModuleType.UNKNOWN.equals(getBridge()) ? "configurable" : "device")));
203     }
204
205     public static ModuleType from(ThingTypeUID thingTypeUID) {
206         return ModuleType.AS_SET.stream().filter(mt -> mt.thingTypeUID.equals(thingTypeUID)).findFirst()
207                 .orElseThrow(() -> new IllegalArgumentException());
208     }
209 }