2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.netatmo.internal.api.data;
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
16 import static org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.*;
19 import java.util.EnumSet;
20 import java.util.List;
22 import java.util.stream.Collectors;
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.WeatherCapability;
40 import org.openhab.binding.netatmo.internal.handler.channelhelper.AirQualityChannelHelper;
41 import org.openhab.binding.netatmo.internal.handler.channelhelper.CameraChannelHelper;
42 import org.openhab.binding.netatmo.internal.handler.channelhelper.EnergyChannelHelper;
43 import org.openhab.binding.netatmo.internal.handler.channelhelper.EventDoorbellChannelHelper;
44 import org.openhab.binding.netatmo.internal.handler.channelhelper.EventPersonChannelHelper;
45 import org.openhab.binding.netatmo.internal.handler.channelhelper.PersonChannelHelper;
46 import org.openhab.binding.netatmo.internal.handler.channelhelper.PresenceChannelHelper;
47 import org.openhab.binding.netatmo.internal.handler.channelhelper.PressureChannelHelper;
48 import org.openhab.binding.netatmo.internal.handler.channelhelper.RainChannelHelper;
49 import org.openhab.binding.netatmo.internal.handler.channelhelper.RoomChannelHelper;
50 import org.openhab.binding.netatmo.internal.handler.channelhelper.SecurityChannelHelper;
51 import org.openhab.binding.netatmo.internal.handler.channelhelper.SetpointChannelHelper;
52 import org.openhab.binding.netatmo.internal.handler.channelhelper.SirenChannelHelper;
53 import org.openhab.binding.netatmo.internal.handler.channelhelper.Therm1ChannelHelper;
54 import org.openhab.binding.netatmo.internal.handler.channelhelper.TimestampChannelHelper;
55 import org.openhab.binding.netatmo.internal.handler.channelhelper.WindChannelHelper;
56 import org.openhab.core.thing.ThingTypeUID;
59 * This enum all handled Netatmo modules and devices along with their capabilities
61 * @author Gaƫl L'hopital - Initial contribution
64 public enum ModuleType {
65 UNKNOWN(FeatureArea.NONE, "", null, Set.of()),
66 ACCOUNT(FeatureArea.NONE, "", null, Set.of()),
68 HOME(FeatureArea.NONE, "NAHome", ACCOUNT,
69 Set.of(DeviceCapability.class, EventCapability.class, HomeCapability.class, ChannelHelperCapability.class),
70 new ChannelGroup(SecurityChannelHelper.class, GROUP_SECURITY),
71 new ChannelGroup(EnergyChannelHelper.class, GROUP_ENERGY)),
73 PERSON(FeatureArea.SECURITY, "NAPerson", HOME,
74 Set.of(EventCapability.class, PersonCapability.class, ChannelHelperCapability.class),
75 new ChannelGroup(PersonChannelHelper.class, GROUP_PERSON),
76 new ChannelGroup(EventPersonChannelHelper.class, GROUP_PERSON_LAST_EVENT)),
78 WELCOME(FeatureArea.SECURITY, "NACamera", HOME,
79 Set.of(EventCapability.class, CameraCapability.class, ChannelHelperCapability.class), ChannelGroup.SIGNAL,
80 ChannelGroup.EVENT, new ChannelGroup(CameraChannelHelper.class, GROUP_CAM_STATUS, GROUP_CAM_LIVE)),
82 SIREN(FeatureArea.SECURITY, "NIS", WELCOME, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
83 ChannelGroup.BATTERY, new ChannelGroup(TimestampChannelHelper.class, GROUP_TIMESTAMP),
84 new ChannelGroup(SirenChannelHelper.class, GROUP_SIREN)),
86 PRESENCE(FeatureArea.SECURITY, "NOC", HOME,
87 Set.of(EventCapability.class, PresenceCapability.class, ChannelHelperCapability.class), ChannelGroup.SIGNAL,
89 new ChannelGroup(PresenceChannelHelper.class, GROUP_CAM_STATUS, GROUP_CAM_LIVE, GROUP_PRESENCE)),
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)),
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)),
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),
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)),
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)),
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),
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)),
128 PLUG(FeatureArea.ENERGY, "NAPlug", HOME, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL),
130 VALVE(FeatureArea.ENERGY, "NRV", PLUG, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
131 ChannelGroup.BATTERY_EXT),
133 THERMOSTAT(FeatureArea.ENERGY, "NATherm1", PLUG, Set.of(ChannelHelperCapability.class), ChannelGroup.SIGNAL,
134 ChannelGroup.BATTERY_EXT, new ChannelGroup(Therm1ChannelHelper.class, GROUP_TYPE_TH_PROPERTIES)),
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));
140 public static final EnumSet<ModuleType> AS_SET = EnumSet.allOf(ModuleType.class);
142 private final @Nullable ModuleType bridgeType;
143 public final Set<ChannelGroup> channelGroups;
144 public final Set<Class<? extends Capability>> capabilities;
145 public final ThingTypeUID thingTypeUID;
146 public final FeatureArea feature;
147 public final String apiName;
149 ModuleType(FeatureArea feature, String apiName, @Nullable ModuleType bridge,
150 Set<Class<? extends Capability>> capabilities, ChannelGroup... channelGroups) {
151 this.bridgeType = bridge;
152 this.feature = feature;
153 this.capabilities = capabilities;
154 this.apiName = apiName;
155 this.channelGroups = Set.of(channelGroups);
156 this.thingTypeUID = new ThingTypeUID(BINDING_ID, name().toLowerCase().replace("_", "-"));
159 public boolean isLogical() {
160 return !channelGroups.contains(ChannelGroup.SIGNAL);
163 public boolean isABridge() {
164 for (ModuleType mt : ModuleType.values()) {
165 if (this.equals(mt.bridgeType)) {
172 public List<String> getExtensions() {
173 return channelGroups.stream().map(cg -> cg.extensions).flatMap(Set::stream).collect(Collectors.toList());
176 public Set<String> getGroupTypes() {
177 return channelGroups.stream().map(cg -> cg.groupTypes).flatMap(Set::stream).collect(Collectors.toSet());
180 public int[] getSignalLevels() {
182 return (channelGroups.contains(ChannelGroup.BATTERY) || channelGroups.contains(ChannelGroup.BATTERY_EXT))
183 ? RADIO_SIGNAL_LEVELS
184 : WIFI_SIGNAL_LEVELS;
186 throw new IllegalArgumentException(
187 "This should not be called for module type : " + name() + ", please file a bug report.");
190 public ModuleType getBridge() {
191 ModuleType bridge = bridgeType;
192 return bridge != null ? bridge : ModuleType.UNKNOWN;
195 public URI getConfigDescription() {
196 return URI.create(BINDING_ID + ":"
197 + (equals(ACCOUNT) ? "api_bridge"
198 : equals(HOME) ? "home"
199 : (isLogical() ? "virtual"
200 : ModuleType.UNKNOWN.equals(getBridge()) ? "configurable" : "device")));
203 public static ModuleType from(ThingTypeUID thingTypeUID) {
204 return ModuleType.AS_SET.stream().filter(mt -> mt.thingTypeUID.equals(thingTypeUID)).findFirst()
205 .orElseThrow(() -> new IllegalArgumentException());
208 public static ModuleType from(String apiName) {
209 return ModuleType.AS_SET.stream().filter(mt -> apiName.equals(mt.apiName)).findFirst()
210 .orElseThrow(() -> new IllegalArgumentException());