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.core.library.CoreItemFactory.*;
17 import static org.openhab.core.library.unit.MetricPrefix.*;
20 import java.util.Arrays;
21 import java.util.EnumSet;
22 import java.util.HashMap;
25 import java.util.stream.Collectors;
26 import java.util.stream.Stream;
28 import javax.measure.Unit;
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.openhab.core.library.unit.SIUnits;
32 import org.openhab.core.library.unit.Units;
33 import org.openhab.core.types.StateDescriptionFragment;
34 import org.openhab.core.types.StateDescriptionFragmentBuilder;
35 import org.openhab.core.types.util.UnitUtils;
37 import com.google.gson.annotations.SerializedName;
40 * This class holds various definitions and settings provided by the Netatmo
43 * @author Gaƫl L'hopital - Initial contribution
46 public class NetatmoConstants {
47 public static class Measure {
48 public final double minValue;
49 public final double maxValue;
50 public final int scale;
51 public final Unit<?> unit;
53 private Measure(double minValue, double maxValue, double precision, Unit<?> unit) {
54 this.minValue = minValue;
55 this.maxValue = maxValue;
57 String[] splitter = Double.valueOf(precision).toString().split("\\.");
58 if (splitter.length > 1) {
59 int dec = Integer.parseInt(splitter[1]);
60 this.scale = dec > 0 ? Integer.toString(dec).length() : 0;
67 public static class MeasureChannelDetails {
68 private static final StateDescriptionFragmentBuilder BUILDER = StateDescriptionFragmentBuilder.create();
69 public final URI configURI;
70 public final String itemType;
71 public final StateDescriptionFragment stateDescriptionFragment;
73 private MeasureChannelDetails(String measureType, String itemType, String pattern) {
74 this.configURI = URI.create(String.join(":", BINDING_ID, measureType, "config"));
75 this.itemType = itemType;
76 this.stateDescriptionFragment = BUILDER.withReadOnly(true).withPattern(pattern).build();
80 public enum MeasureClass {
81 INSIDE_TEMPERATURE(0, 50, 0.3, SIUnits.CELSIUS, "temp", "measure", true),
82 OUTSIDE_TEMPERATURE(-40, 65, 0.3, SIUnits.CELSIUS, "temp", "measure", true),
83 HEAT_INDEX(-40, 65, 1, SIUnits.CELSIUS, "", "", false),
84 PRESSURE(260, 1260, 1, HECTO(SIUnits.PASCAL), "pressure", "measure", true),
85 CO2(0, 5000, 50, Units.PARTS_PER_MILLION, "co2", "measure", true),
86 NOISE(35, 120, 1, Units.DECIBEL, "noise", "measure", true),
87 RAIN_QUANTITY(0, 150, 0.1, MILLI(SIUnits.METRE), "sum_rain", "sum_rain", false),
88 RAIN_INTENSITY(0, 150, 0.1, Units.MILLIMETRE_PER_HOUR, "", "", false),
89 WIND_SPEED(0, 160, 1.8, SIUnits.KILOMETRE_PER_HOUR, "", "", false),
90 WIND_ANGLE(0, 360, 5, Units.DEGREE_ANGLE, "", "", false),
91 HUMIDITY(0, 100, 3, Units.PERCENT, "hum", "measure", true);
93 public static final EnumSet<MeasureClass> AS_SET = EnumSet.allOf(MeasureClass.class);
95 public final Measure measureDefinition;
96 public final String apiDescriptor;
97 public final Map<String, MeasureChannelDetails> channels = new HashMap<>(2);
99 MeasureClass(double min, double max, double precision, Unit<?> unit, String apiDescriptor, String confFragment,
101 this.measureDefinition = new Measure(min, max, precision, unit);
102 this.apiDescriptor = apiDescriptor;
103 if (!apiDescriptor.isBlank()) {
104 String dimension = UnitUtils.getDimensionName(unit);
106 channels.put(String.join("-", apiDescriptor, "measurement"),
107 new MeasureChannelDetails(confFragment, String.join(":", NUMBER, dimension),
108 String.format("%%.%df %s", measureDefinition.scale, UnitUtils.UNIT_PLACEHOLDER)));
110 channels.put(String.join("-", apiDescriptor, GROUP_TIMESTAMP), new MeasureChannelDetails(
111 GROUP_TIMESTAMP, DATETIME, "@text/extensible-channel-type.timestamp.pattern"));
118 public static final String URL_API = "https://api.netatmo.com/";
119 public static final String URL_APP = "https://app.netatmo.net/";
120 public static final String PATH_OAUTH = "oauth2";
121 public static final String SUB_PATH_TOKEN = "token";
122 public static final String SUB_PATH_AUTHORIZE = "authorize";
123 public static final String PATH_API = "api";
124 public static final String PATH_COMMAND = "command";
125 public static final String PATH_STATE = "setstate";
126 public static final String SUB_PATH_PERSON_AWAY = "setpersonsaway";
127 public static final String SUB_PATH_PERSON_HOME = "setpersonshome";
128 public static final String SUB_PATH_HOMES_DATA = "homesdata";
129 public static final String SUB_PATH_ADD_WEBHOOK = "addwebhook";
130 public static final String SUB_PATH_DROP_WEBHOOK = "dropwebhook";
131 public static final String SUB_PATH_SET_ROOM_THERMPOINT = "setroomthermpoint";
132 public static final String SUB_PATH_SET_THERM_MODE = "setthermmode";
133 public static final String SUB_PATH_SWITCH_SCHEDULE = "switchschedule";
134 public static final String SUB_PATH_GET_STATION = "getstationsdata";
135 public static final String SUB_PATH_GET_MEASURE = "getmeasure";
136 public static final String SUB_PATH_HOMESTATUS = "homestatus";
137 public static final String SUB_PATH_HOMECOACH = "gethomecoachsdata";
138 public static final String SUB_PATH_GET_EVENTS = "getevents";
139 public static final String SUB_PATH_PING = "ping";
140 public static final String SUB_PATH_CHANGESTATUS = "changestatus";
141 public static final String PARAM_DEVICE_ID = "device_id";
142 public static final String PARAM_MODULE_ID = "module_id";
143 public static final String PARAM_HOME_ID = "home_id";
144 public static final String PARAM_ROOM_ID = "room_id";
145 public static final String PARAM_PERSON_ID = "person_id";
146 public static final String PARAM_SCHEDULE_ID = "schedule_id";
147 public static final String PARAM_OFFSET = "offset";
148 public static final String PARAM_GATEWAY_TYPE = "gateway_types";
149 public static final String PARAM_MODE = "mode";
150 public static final String PARAM_URL = "url";
151 public static final String PARAM_FAVORITES = "get_favorites";
152 public static final String PARAM_STATUS = "status";
153 public static final String PARAM_DEVICES_TYPE = "device_types";
155 // Autentication process params
156 public static final String PARAM_ERROR = "error";
159 public static final int THERM_MAX_SETPOINT = 30;
162 public static enum Scope {
163 @SerializedName("read_station")
165 @SerializedName("read_thermostat")
167 @SerializedName("write_thermostat")
169 @SerializedName("read_camera")
171 @SerializedName("write_camera")
173 @SerializedName("access_camera")
175 @SerializedName("read_presence")
177 @SerializedName("write_presence")
179 @SerializedName("access_presence")
181 @SerializedName("read_smokedetector")
183 @SerializedName("read_homecoach")
185 @SerializedName("read_doorbell")
187 @SerializedName("write_doorbell")
189 @SerializedName("access_doorbell")
194 private static final Scope[] SMOKE_SCOPES = { Scope.READ_SMOKEDETECTOR };
195 private static final Scope[] AIR_CARE_SCOPES = { Scope.READ_HOMECOACH };
196 private static final Scope[] WEATHER_SCOPES = { Scope.READ_STATION };
197 private static final Scope[] THERMOSTAT_SCOPES = { Scope.READ_THERMOSTAT, Scope.WRITE_THERMOSTAT };
198 private static final Scope[] WELCOME_SCOPES = { Scope.READ_CAMERA, Scope.WRITE_CAMERA, Scope.ACCESS_CAMERA };
199 private static final Scope[] DOORBELL_SCOPES = { Scope.READ_DOORBELL, Scope.WRITE_DOORBELL, Scope.ACCESS_DOORBELL };
200 private static final Scope[] PRESENCE_SCOPES = { Scope.READ_PRESENCE, Scope.WRITE_PRESENCE, Scope.ACCESS_PRESENCE };
202 public static enum FeatureArea {
203 AIR_CARE(AIR_CARE_SCOPES),
204 WEATHER(WEATHER_SCOPES),
205 ENERGY(THERMOSTAT_SCOPES),
206 SECURITY(WELCOME_SCOPES, PRESENCE_SCOPES, SMOKE_SCOPES, DOORBELL_SCOPES),
209 public static String ALL_SCOPES = EnumSet.allOf(FeatureArea.class).stream().map(fa -> fa.scopes)
210 .flatMap(Set::stream).map(s -> s.name().toLowerCase()).collect(Collectors.joining(" "));
212 public final Set<Scope> scopes;
214 FeatureArea(Scope[]... scopeArrays) {
215 this.scopes = Stream.of(scopeArrays).flatMap(Arrays::stream).collect(Collectors.toSet());
219 // Radio signal quality thresholds
220 static final int[] WIFI_SIGNAL_LEVELS = new int[] { 99, 84, 69, 54 }; // Resp : bad, average, good, full
221 static final int[] RADIO_SIGNAL_LEVELS = new int[] { 90, 80, 70, 60 }; // Resp : low, medium, high, full
223 // Thermostat definitions
224 public static enum SetpointMode {
225 @SerializedName("program")
227 @SerializedName("away")
229 @SerializedName("hg")
231 @SerializedName("manual")
233 @SerializedName("off")
235 @SerializedName("max")
237 @SerializedName("schedule")
238 SCHEDULE("schedule"),
242 public final String apiDescriptor;
244 SetpointMode(String descriptor) {
245 this.apiDescriptor = descriptor;
249 public static enum ThermostatZoneType {
266 public final String zoneId;
268 private ThermostatZoneType(String id) {
273 public enum FloodLightMode {
274 @SerializedName("on")
276 @SerializedName("off")
278 @SerializedName("auto")
283 public enum EventCategory {
284 @SerializedName("human")
286 @SerializedName("animal")
288 @SerializedName("vehicle")
293 public enum TrendDescription {
294 @SerializedName("up")
296 @SerializedName("stable")
298 @SerializedName("down")
303 public enum VideoStatus {
304 @SerializedName("recording")
306 @SerializedName("available")
308 @SerializedName("deleted")
313 public enum SdCardStatus {
325 SD_CARD_INCOMPATIBLE_SPEED,
327 SD_CARD_INSUFFICIENT_SPACE,
331 public enum AlimentationStatus {
333 ALIM_INCORRECT_POWER,
339 public enum BatteryState {
340 @SerializedName("full")
342 @SerializedName("high")
344 @SerializedName("medium")
346 @SerializedName("low")
350 public final int level;
352 BatteryState(int i) {
357 public enum ServiceError {
358 @SerializedName("99")
360 @SerializedName("-2")
361 UNKNOWN_ERROR_IN_OAUTH,
362 @SerializedName("-1")
365 ACCESS_TOKEN_MISSING,
367 INVALID_TOKEN_MISSING,
369 ACCESS_TOKEN_EXPIRED,
371 APPLICATION_DEACTIVATED,
376 @SerializedName("10")
378 @SerializedName("13")
380 @SerializedName("19")
382 @SerializedName("21")
384 @SerializedName("22")
385 APPLICATION_NOT_FOUND,
386 @SerializedName("23")
388 @SerializedName("25")
390 @SerializedName("26")
391 MAXIMUM_USAGE_REACHED,
392 @SerializedName("30")
393 INVALID_REFRESH_TOKEN,
394 @SerializedName("31")
396 @SerializedName("35")
398 @SerializedName("36")
400 @SerializedName("37")
401 NO_MORE_SPACE_AVAILABLE_ON_THE_CAMERA,
402 @SerializedName("40")
403 JSON_GIVEN_HAS_AN_INVALID_ENCODING,
404 @SerializedName("41")
405 DEVICE_IS_UNREACHABLE;