]> git.basschouten.com Git - openhab-addons.git/blob
d4181ca4da47391d240de7cc0ffb1f7909cc260b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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, "", null, Set.of()),
68
69     ACCOUNT(FeatureArea.NONE, "", null, Set.of(), new ChannelGroup(ApiBridgeChannelHelper.class, GROUP_MONITORING)),
70
71     HOME(FeatureArea.NONE, "NAHome", 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", 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", 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", WELCOME, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
85             ChannelGroup.BATTERY, ChannelGroup.TIMESTAMP, new ChannelGroup(DoorTagChannelHelper.class, GROUP_TAG)),
86
87     SIREN(FeatureArea.SECURITY, "NIS", WELCOME, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
88             ChannelGroup.BATTERY, ChannelGroup.TIMESTAMP, new ChannelGroup(SirenChannelHelper.class, GROUP_SIREN)),
89
90     PRESENCE(FeatureArea.SECURITY, "NOC", 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", 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", 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", 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", WEATHER_STATION, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
114             ChannelGroup.TSTAMP_EXT, ChannelGroup.BATTERY, new ChannelGroup(WindChannelHelper.class, GROUP_WIND)),
115
116     RAIN(FeatureArea.WEATHER, "NAModule3", WEATHER_STATION,
117             Set.of(MeasureCapability.class, ChannelHelperCapability.class), ChannelGroup.SIGNAL,
118             ChannelGroup.TSTAMP_EXT, ChannelGroup.MEASURE, ChannelGroup.BATTERY,
119             new ChannelGroup(RainChannelHelper.class, MeasureClass.RAIN_QUANTITY, GROUP_RAIN)),
120
121     INDOOR(FeatureArea.WEATHER, "NAModule4", WEATHER_STATION,
122             Set.of(MeasureCapability.class, ChannelHelperCapability.class), ChannelGroup.SIGNAL,
123             ChannelGroup.TSTAMP_EXT, ChannelGroup.MEASURE, ChannelGroup.BATTERY, ChannelGroup.HUMIDITY,
124             ChannelGroup.TEMP_INSIDE_EXT, ChannelGroup.AIR_QUALITY),
125
126     HOME_COACH(FeatureArea.AIR_CARE, "NHC", ACCOUNT,
127             Set.of(DeviceCapability.class, AirCareCapability.class, MeasureCapability.class,
128                     ChannelHelperCapability.class),
129             ChannelGroup.LOCATION, ChannelGroup.SIGNAL, ChannelGroup.NOISE, ChannelGroup.HUMIDITY,
130             ChannelGroup.TEMP_INSIDE, ChannelGroup.MEASURE, ChannelGroup.TSTAMP_EXT,
131             new ChannelGroup(AirQualityChannelHelper.class, GROUP_TYPE_AIR_QUALITY_EXTENDED),
132             new ChannelGroup(PressureChannelHelper.class, MeasureClass.PRESSURE, GROUP_PRESSURE)),
133
134     PLUG(FeatureArea.ENERGY, "NAPlug", HOME, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL),
135
136     VALVE(FeatureArea.ENERGY, "NRV", PLUG, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
137             ChannelGroup.BATTERY_EXT),
138
139     THERMOSTAT(FeatureArea.ENERGY, "NATherm1", PLUG, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
140             ChannelGroup.BATTERY_EXT, new ChannelGroup(Therm1ChannelHelper.class, GROUP_TYPE_TH_PROPERTIES)),
141
142     ROOM(FeatureArea.ENERGY, "NARoom", HOME, Set.of(RoomCapability.class, ChannelHelperCapability.class),
143             new ChannelGroup(RoomChannelHelper.class, GROUP_TYPE_ROOM_PROPERTIES, GROUP_TYPE_ROOM_TEMPERATURE),
144             new ChannelGroup(SetpointChannelHelper.class, GROUP_SETPOINT)),
145
146     SMOKE_DETECTOR(FeatureArea.SECURITY, "NSD", HOME, Set.of(AlarmEventCapability.class, ChannelHelperCapability.class),
147             ChannelGroup.SIGNAL, ChannelGroup.TIMESTAMP, ChannelGroup.ALARM_LAST_EVENT),
148
149     CO_DETECTOR(FeatureArea.SECURITY, "NCO", HOME, Set.of(AlarmEventCapability.class, ChannelHelperCapability.class),
150             ChannelGroup.SIGNAL, ChannelGroup.TIMESTAMP, ChannelGroup.ALARM_LAST_EVENT);
151
152     public static final EnumSet<ModuleType> AS_SET = EnumSet.allOf(ModuleType.class);
153
154     private final Optional<ModuleType> bridgeType;
155     public final Set<ChannelGroup> channelGroups;
156     public final Set<Class<? extends Capability>> capabilities;
157     public final ThingTypeUID thingTypeUID;
158     public final FeatureArea feature;
159     public final String apiName;
160
161     ModuleType(FeatureArea feature, String apiName, @Nullable ModuleType bridge,
162             Set<Class<? extends Capability>> capabilities, ChannelGroup... channelGroups) {
163         this.bridgeType = Optional.ofNullable(bridge);
164         this.feature = feature;
165         this.capabilities = capabilities;
166         this.apiName = apiName;
167         this.channelGroups = Set.of(channelGroups);
168         this.thingTypeUID = new ThingTypeUID(BINDING_ID, name().toLowerCase().replace("_", "-"));
169     }
170
171     public boolean isLogical() {
172         return !channelGroups.contains(ChannelGroup.SIGNAL);
173     }
174
175     public boolean isABridge() { // I am a bridge if any module references me as being so
176         return AS_SET.stream().anyMatch(mt -> this.equals(mt.getBridge()));
177     }
178
179     public List<String> getExtensions() {
180         return channelGroups.stream().map(cg -> cg.extensions).flatMap(Set::stream).toList();
181     }
182
183     public List<String> getGroupTypes() {
184         return channelGroups.stream().map(cg -> cg.groupTypes).flatMap(Set::stream).toList();
185     }
186
187     public int[] getSignalLevels() {
188         if (!isLogical()) {
189             return (channelGroups.contains(ChannelGroup.BATTERY) || channelGroups.contains(ChannelGroup.BATTERY_EXT))
190                     ? RADIO_SIGNAL_LEVELS
191                     : WIFI_SIGNAL_LEVELS;
192         }
193         throw new IllegalArgumentException(
194                 "getSignalLevels should not be called for module type : '%s', please file a bug report."
195                         .formatted(name()));
196     }
197
198     public ModuleType getBridge() {
199         return bridgeType.orElse(UNKNOWN);
200     }
201
202     public URI getConfigDescription() {
203         return URI.create(BINDING_ID + ":"
204                 + (equals(ACCOUNT) ? "api_bridge"
205                         : equals(HOME) ? "home"
206                                 : (isLogical() ? "virtual" : UNKNOWN.equals(getBridge()) ? "configurable" : "device")));
207     }
208
209     public int getDepth() {
210         ModuleType parent = getBridge();
211         return parent == UNKNOWN ? 1 : parent.getDepth() + 1;
212     }
213
214     public static ModuleType from(ThingTypeUID thingTypeUID) {
215         return AS_SET.stream().filter(mt -> mt.thingTypeUID.equals(thingTypeUID)).findFirst()
216                 .orElseThrow(() -> new IllegalArgumentException(
217                         "No known ModuleType matched '%s'".formatted(thingTypeUID.toString())));
218     }
219 }