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