]> git.basschouten.com Git - openhab-addons.git/blob
918f6c16ad5240b7697fd5e6e910c942f49d9990
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.homecoach;
14
15 import static org.openhab.binding.netatmo.internal.ChannelTypeUtils.*;
16 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
17
18 import java.util.Optional;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.i18n.TimeZoneProvider;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.types.State;
25 import org.openhab.binding.netatmo.internal.handler.NetatmoDeviceHandler;
26
27 import io.swagger.client.model.NADashboardData;
28 import io.swagger.client.model.NAHealthyHomeCoach;
29
30 /**
31  * {@link NAHealthyHomeCoachHandler} is the class used to handle the Health Home Coach device
32  *
33  * @author Michael Svinth - Initial contribution OH2 version
34  *
35  */
36 @NonNullByDefault
37 public class NAHealthyHomeCoachHandler extends NetatmoDeviceHandler<NAHealthyHomeCoach> {
38
39     public NAHealthyHomeCoachHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
40         super(thing, timeZoneProvider);
41     }
42
43     @Override
44     protected Optional<NAHealthyHomeCoach> updateReadings() {
45         return getBridgeHandler().flatMap(handler -> handler.getHomecoachDataBody(getId()))
46                 .map(dataBody -> dataBody.getDevices().stream()
47                         .filter(device -> device.getId().equalsIgnoreCase(getId())).findFirst().orElse(null));
48     }
49
50     @Override
51     protected void updateProperties(NAHealthyHomeCoach deviceData) {
52         updateProperties(deviceData.getFirmware(), deviceData.getType());
53     }
54
55     @Override
56     protected State getNAThingProperty(String channelId) {
57         NADashboardData dashboardData = getDevice().map(d -> d.getDashboardData()).orElse(null);
58         if (dashboardData != null) {
59             switch (channelId) {
60                 case CHANNEL_CO2:
61                     return toQuantityType(dashboardData.getCO2(), API_CO2_UNIT);
62                 case CHANNEL_TEMPERATURE:
63                     return toQuantityType(dashboardData.getTemperature(), API_TEMPERATURE_UNIT);
64                 case CHANNEL_HEALTH_INDEX:
65                     return toStringType(toHealthIndexString(dashboardData.getHealthIdx()));
66                 case CHANNEL_MIN_TEMP:
67                     return toQuantityType(dashboardData.getMinTemp(), API_TEMPERATURE_UNIT);
68                 case CHANNEL_MAX_TEMP:
69                     return toQuantityType(dashboardData.getMaxTemp(), API_TEMPERATURE_UNIT);
70                 case CHANNEL_TEMP_TREND:
71                     return toStringType(dashboardData.getTempTrend());
72                 case CHANNEL_NOISE:
73                     return toQuantityType(dashboardData.getNoise(), API_NOISE_UNIT);
74                 case CHANNEL_PRESSURE:
75                     return toQuantityType(dashboardData.getPressure(), API_PRESSURE_UNIT);
76                 case CHANNEL_PRESS_TREND:
77                     return toStringType(dashboardData.getPressureTrend());
78                 case CHANNEL_ABSOLUTE_PRESSURE:
79                     return toQuantityType(dashboardData.getAbsolutePressure(), API_PRESSURE_UNIT);
80                 case CHANNEL_TIMEUTC:
81                     return toDateTimeType(dashboardData.getTimeUtc(), timeZoneProvider.getTimeZone());
82                 case CHANNEL_DATE_MIN_TEMP:
83                     return toDateTimeType(dashboardData.getDateMinTemp(), timeZoneProvider.getTimeZone());
84                 case CHANNEL_DATE_MAX_TEMP:
85                     return toDateTimeType(dashboardData.getDateMaxTemp(), timeZoneProvider.getTimeZone());
86                 case CHANNEL_HUMIDITY:
87                     return toQuantityType(dashboardData.getHumidity(), API_HUMIDITY_UNIT);
88             }
89         }
90         return super.getNAThingProperty(channelId);
91     }
92
93     private @Nullable String toHealthIndexString(@Nullable Integer healthIndex) {
94         if (healthIndex == null) {
95             return null;
96         }
97         switch (healthIndex) {
98             case 0:
99                 return "healthy";
100             case 1:
101                 return "fine";
102             case 2:
103                 return "fair";
104             case 3:
105                 return "poor";
106             case 4:
107                 return "unhealthy";
108             default:
109                 return healthIndex.toString();
110         }
111     }
112
113     @Override
114     protected Optional<Integer> getDataTimestamp() {
115         return getDevice().map(d -> d.getLastStatusStore());
116     }
117 }