2 * Copyright (c) 2010-2020 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.homecoach;
15 import static org.openhab.binding.netatmo.internal.ChannelTypeUtils.*;
16 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
18 import java.util.Optional;
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;
27 import io.swagger.client.model.NADashboardData;
28 import io.swagger.client.model.NAHealthyHomeCoach;
31 * {@link NAHealthyHomeCoachHandler} is the class used to handle the Health Home Coach device
33 * @author Michael Svinth - Initial contribution OH2 version
37 public class NAHealthyHomeCoachHandler extends NetatmoDeviceHandler<NAHealthyHomeCoach> {
39 public NAHealthyHomeCoachHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
40 super(thing, timeZoneProvider);
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));
51 protected void updateProperties(NAHealthyHomeCoach deviceData) {
52 updateProperties(deviceData.getFirmware(), deviceData.getType());
56 protected State getNAThingProperty(String channelId) {
57 NADashboardData dashboardData = getDevice().map(d -> d.getDashboardData()).orElse(null);
58 if (dashboardData != null) {
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());
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);
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);
90 return super.getNAThingProperty(channelId);
93 private @Nullable String toHealthIndexString(@Nullable Integer healthIndex) {
94 if (healthIndex == null) {
97 switch (healthIndex) {
109 return healthIndex.toString();
114 protected Optional<Integer> getDataTimestamp() {
115 return getDevice().map(d -> d.getLastStatusStore());