]> git.basschouten.com Git - openhab-addons.git/blob
575bc05219ceb4b80fad56c4edbdee058a8b15cb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.Optional;
22 import java.util.Set;
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.AlarmEventCapability;
30 import org.openhab.binding.netatmo.internal.handler.capability.CameraCapability;
31 import org.openhab.binding.netatmo.internal.handler.capability.Capability;
32 import org.openhab.binding.netatmo.internal.handler.capability.ChannelHelperCapability;
33 import org.openhab.binding.netatmo.internal.handler.capability.DeviceCapability;
34 import org.openhab.binding.netatmo.internal.handler.capability.DoorbellCapability;
35 import org.openhab.binding.netatmo.internal.handler.capability.HomeCapability;
36 import org.openhab.binding.netatmo.internal.handler.capability.MeasureCapability;
37 import org.openhab.binding.netatmo.internal.handler.capability.PersonCapability;
38 import org.openhab.binding.netatmo.internal.handler.capability.PresenceCapability;
39 import org.openhab.binding.netatmo.internal.handler.capability.RoomCapability;
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.DoorTagChannelHelper;
45 import org.openhab.binding.netatmo.internal.handler.channelhelper.EnergyChannelHelper;
46 import org.openhab.binding.netatmo.internal.handler.channelhelper.EventCameraChannelHelper;
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 describes all 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, "", 1, null, Set.of()),
68
69     ACCOUNT(FeatureArea.NONE, "", 1, null, Set.of(), new ChannelGroup(ApiBridgeChannelHelper.class, GROUP_MONITORING)),
70
71     HOME(FeatureArea.NONE, "NAHome", 1, ACCOUNT,
72             Set.of(DeviceCapability.class, HomeCapability.class, ChannelHelperCapability.class),
73             new ChannelGroup(SecurityChannelHelper.class, GROUP_SECURITY_EVENT, GROUP_SECURITY),
74             new ChannelGroup(EnergyChannelHelper.class, GROUP_ENERGY)),
75
76     PERSON(FeatureArea.SECURITY, "NAPerson", 1, HOME, Set.of(PersonCapability.class, ChannelHelperCapability.class),
77             new ChannelGroup(PersonChannelHelper.class, GROUP_PERSON),
78             new ChannelGroup(EventPersonChannelHelper.class, GROUP_PERSON_LAST_EVENT)),
79
80     WELCOME(FeatureArea.SECURITY, "NACamera", 1, HOME, Set.of(CameraCapability.class, ChannelHelperCapability.class),
81             ChannelGroup.SIGNAL, ChannelGroup.EVENT,
82             new ChannelGroup(CameraChannelHelper.class, GROUP_SECURITY_EVENT, GROUP_CAM_STATUS, GROUP_CAM_LIVE)),
83
84     TAG(FeatureArea.SECURITY, "NACamDoorTag", 1, WELCOME, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
85             ChannelGroup.BATTERY, ChannelGroup.TIMESTAMP, new ChannelGroup(DoorTagChannelHelper.class, GROUP_TAG)),
86
87     SIREN(FeatureArea.SECURITY, "NIS", 1, WELCOME, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
88             ChannelGroup.BATTERY, ChannelGroup.TIMESTAMP, new ChannelGroup(SirenChannelHelper.class, GROUP_SIREN)),
89
90     PRESENCE(FeatureArea.SECURITY, "NOC", 1, HOME, Set.of(PresenceCapability.class, ChannelHelperCapability.class),
91             ChannelGroup.SIGNAL, ChannelGroup.EVENT,
92             new ChannelGroup(PresenceChannelHelper.class, GROUP_SECURITY_EVENT, GROUP_CAM_STATUS, GROUP_CAM_LIVE,
93                     GROUP_PRESENCE),
94             new ChannelGroup(EventCameraChannelHelper.class, GROUP_SUB_EVENT)),
95
96     DOORBELL(FeatureArea.SECURITY, "NDB", 1, HOME, Set.of(DoorbellCapability.class, ChannelHelperCapability.class),
97             ChannelGroup.SIGNAL,
98             new ChannelGroup(CameraChannelHelper.class, GROUP_SECURITY_EVENT, GROUP_DOORBELL_STATUS,
99                     GROUP_DOORBELL_LIVE),
100             new ChannelGroup(EventCameraChannelHelper.class, GROUP_DOORBELL_LAST_EVENT, GROUP_DOORBELL_SUB_EVENT)),
101
102     WEATHER_STATION(FeatureArea.WEATHER, "NAMain", 1, ACCOUNT,
103             Set.of(DeviceCapability.class, WeatherCapability.class, MeasureCapability.class,
104                     ChannelHelperCapability.class),
105             ChannelGroup.SIGNAL, ChannelGroup.HUMIDITY, ChannelGroup.TSTAMP_EXT, ChannelGroup.MEASURE,
106             ChannelGroup.AIR_QUALITY, ChannelGroup.LOCATION, ChannelGroup.NOISE, ChannelGroup.TEMP_INSIDE_EXT,
107             new ChannelGroup(PressureChannelHelper.class, MeasureClass.PRESSURE, GROUP_TYPE_PRESSURE_EXTENDED)),
108
109     OUTDOOR(FeatureArea.WEATHER, "NAModule1", 1, WEATHER_STATION,
110             Set.of(MeasureCapability.class, ChannelHelperCapability.class), ChannelGroup.SIGNAL, ChannelGroup.HUMIDITY,
111             ChannelGroup.TSTAMP_EXT, ChannelGroup.MEASURE, ChannelGroup.BATTERY, ChannelGroup.TEMP_OUTSIDE_EXT),
112
113     WIND(FeatureArea.WEATHER, "NAModule2", 1, WEATHER_STATION, Set.of(ChannelHelperCapability.class),
114             ChannelGroup.SIGNAL, ChannelGroup.TSTAMP_EXT, ChannelGroup.BATTERY,
115             new ChannelGroup(WindChannelHelper.class, GROUP_WIND)),
116
117     RAIN(FeatureArea.WEATHER, "NAModule3", 1, WEATHER_STATION,
118             Set.of(MeasureCapability.class, ChannelHelperCapability.class), ChannelGroup.SIGNAL,
119             ChannelGroup.TSTAMP_EXT, ChannelGroup.MEASURE, ChannelGroup.BATTERY,
120             new ChannelGroup(RainChannelHelper.class, MeasureClass.RAIN_QUANTITY, GROUP_RAIN)),
121
122     INDOOR(FeatureArea.WEATHER, "NAModule4", 1, WEATHER_STATION,
123             Set.of(MeasureCapability.class, ChannelHelperCapability.class), ChannelGroup.SIGNAL,
124             ChannelGroup.TSTAMP_EXT, ChannelGroup.MEASURE, ChannelGroup.BATTERY, ChannelGroup.HUMIDITY,
125             ChannelGroup.TEMP_INSIDE_EXT, ChannelGroup.AIR_QUALITY),
126
127     HOME_COACH(FeatureArea.AIR_CARE, "NHC", 1, ACCOUNT,
128             Set.of(DeviceCapability.class, AirCareCapability.class, MeasureCapability.class,
129                     ChannelHelperCapability.class),
130             ChannelGroup.LOCATION, ChannelGroup.SIGNAL, ChannelGroup.NOISE, ChannelGroup.HUMIDITY,
131             ChannelGroup.TEMP_INSIDE, ChannelGroup.MEASURE, ChannelGroup.TSTAMP_EXT,
132             new ChannelGroup(AirQualityChannelHelper.class, GROUP_TYPE_AIR_QUALITY_EXTENDED),
133             new ChannelGroup(PressureChannelHelper.class, MeasureClass.PRESSURE, GROUP_PRESSURE)),
134
135     PLUG(FeatureArea.ENERGY, "NAPlug", 1, HOME, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL),
136
137     VALVE(FeatureArea.ENERGY, "NRV", 1, PLUG, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
138             ChannelGroup.BATTERY_EXT),
139
140     THERMOSTAT(FeatureArea.ENERGY, "NATherm1", 1, PLUG, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
141             ChannelGroup.BATTERY_EXT, new ChannelGroup(Therm1ChannelHelper.class, GROUP_TYPE_TH_PROPERTIES)),
142
143     ROOM(FeatureArea.ENERGY, "NARoom", 1, HOME, Set.of(RoomCapability.class, ChannelHelperCapability.class),
144             new ChannelGroup(RoomChannelHelper.class, GROUP_TYPE_ROOM_PROPERTIES, GROUP_TYPE_ROOM_TEMPERATURE),
145             new ChannelGroup(SetpointChannelHelper.class, GROUP_SETPOINT)),
146
147     SMOKE_DETECTOR(FeatureArea.SECURITY, "NSD", 1, HOME,
148             Set.of(AlarmEventCapability.class, ChannelHelperCapability.class), ChannelGroup.SIGNAL,
149             ChannelGroup.TIMESTAMP, ChannelGroup.ALARM_LAST_EVENT),
150
151     CO_DETECTOR(FeatureArea.SECURITY, "NCO", 1, HOME, Set.of(AlarmEventCapability.class, ChannelHelperCapability.class),
152             ChannelGroup.SIGNAL, ChannelGroup.TIMESTAMP, ChannelGroup.ALARM_LAST_EVENT);
153
154     public static final EnumSet<ModuleType> AS_SET = EnumSet.allOf(ModuleType.class);
155
156     private final Optional<ModuleType> bridgeType;
157     public final Set<ChannelGroup> channelGroups;
158     public final Set<Class<? extends Capability>> capabilities;
159     public final ThingTypeUID thingTypeUID;
160     public final FeatureArea feature;
161     public final String apiName;
162     public final String thingTypeVersion;
163
164     ModuleType(FeatureArea feature, String apiName, int thingTypeVersion, @Nullable ModuleType bridge,
165             Set<Class<? extends Capability>> capabilities, ChannelGroup... channelGroups) {
166         this.bridgeType = Optional.ofNullable(bridge);
167         this.feature = feature;
168         this.capabilities = capabilities;
169         this.apiName = apiName;
170         this.channelGroups = Set.of(channelGroups);
171         this.thingTypeUID = new ThingTypeUID(BINDING_ID, name().toLowerCase().replace("_", "-"));
172         this.thingTypeVersion = Integer.toString(thingTypeVersion);
173     }
174
175     public boolean isLogical() {
176         return !channelGroups.contains(ChannelGroup.SIGNAL);
177     }
178
179     public boolean isABridge() { // I am a bridge if any module references me as being so
180         return AS_SET.stream().anyMatch(mt -> this.equals(mt.getBridge()));
181     }
182
183     public List<String> getExtensions() {
184         return channelGroups.stream().map(cg -> cg.extensions).flatMap(Set::stream).toList();
185     }
186
187     public List<String> getGroupTypes() {
188         return channelGroups.stream().map(cg -> cg.groupTypes).flatMap(Set::stream).toList();
189     }
190
191     public int[] getSignalLevels() {
192         if (!isLogical()) {
193             return (channelGroups.contains(ChannelGroup.BATTERY) || channelGroups.contains(ChannelGroup.BATTERY_EXT))
194                     ? RADIO_SIGNAL_LEVELS
195                     : WIFI_SIGNAL_LEVELS;
196         }
197         throw new IllegalArgumentException(
198                 "getSignalLevels should not be called for module type: '%s', please file a bug report."
199                         .formatted(name()));
200     }
201
202     public ModuleType getBridge() {
203         return bridgeType.orElse(UNKNOWN);
204     }
205
206     public URI getConfigDescription() {
207         return URI.create(BINDING_ID + ":"
208                 + (equals(ACCOUNT) ? "api_bridge"
209                         : equals(HOME) ? "home"
210                                 : (isLogical() ? "virtual" : UNKNOWN.equals(getBridge()) ? "configurable" : "device")));
211     }
212
213     public int getDepth() {
214         ModuleType parent = getBridge();
215         return parent == UNKNOWN ? 1 : parent.getDepth() + 1;
216     }
217
218     public static ModuleType from(ThingTypeUID thingTypeUID) {
219         return AS_SET.stream().filter(mt -> mt.thingTypeUID.equals(thingTypeUID)).findFirst()
220                 .orElseThrow(() -> new IllegalArgumentException(
221                         "No known ModuleType matched '%s'".formatted(thingTypeUID.toString())));
222     }
223 }