2 * Copyright (c) 2010-2021 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.APIUtils.*;
16 import static org.openhab.binding.netatmo.internal.ChannelTypeUtils.*;
17 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
19 import java.util.Optional;
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;
28 import io.swagger.client.model.NADashboardData;
29 import io.swagger.client.model.NAHealthyHomeCoach;
32 * {@link NAHealthyHomeCoachHandler} is the class used to handle the Health Home Coach device
34 * @author Michael Svinth - Initial contribution OH2 version
38 public class NAHealthyHomeCoachHandler extends NetatmoDeviceHandler<NAHealthyHomeCoach> {
40 public NAHealthyHomeCoachHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
41 super(thing, timeZoneProvider);
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));
52 protected void updateProperties(NAHealthyHomeCoach deviceData) {
53 updateProperties(deviceData.getFirmware(), deviceData.getType());
57 protected State getNAThingProperty(String channelId) {
58 NADashboardData dashboardData = getDevice().map(d -> d.getDashboardData()).orElse(null);
59 if (dashboardData != null) {
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());
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);
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);
91 return super.getNAThingProperty(channelId);
94 private @Nullable String toHealthIndexString(@Nullable Integer healthIndex) {
95 if (healthIndex == null) {
98 switch (healthIndex) {
110 return healthIndex.toString();
115 protected Optional<Integer> getDataTimestamp() {
116 return getDevice().map(d -> d.getLastStatusStore());