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.thermostat;
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.time.ZonedDateTime;
20 import java.time.temporal.TemporalAdjusters;
21 import java.util.Optional;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.netatmo.internal.handler.NetatmoDeviceHandler;
26 import org.openhab.core.i18n.TimeZoneProvider;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.types.State;
29 import org.openhab.core.types.UnDefType;
31 import io.swagger.client.model.NAPlug;
32 import io.swagger.client.model.NAYearMonth;
35 * {@link NAPlugHandler} is the class used to handle the plug
36 * device of a thermostat set
38 * @author Gaƫl L'hopital - Initial contribution OH2 version
42 public class NAPlugHandler extends NetatmoDeviceHandler<NAPlug> {
44 public NAPlugHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
45 super(thing, timeZoneProvider);
49 protected Optional<NAPlug> updateReadings() {
50 Optional<NAPlug> result = getBridgeHandler().flatMap(handler -> handler.getThermostatsDataBody(getId()))
51 .map(dataBody -> nonNullStream(dataBody.getDevices())
52 .filter(device -> device.getId().equalsIgnoreCase(getId())).findFirst().orElse(null));
53 result.ifPresent(device -> {
54 nonNullList(device.getModules()).forEach(child -> childs.put(child.getId(), child));
60 protected void updateProperties(NAPlug deviceData) {
61 updateProperties(deviceData.getFirmware(), deviceData.getType());
65 protected State getNAThingProperty(String channelId) {
67 case CHANNEL_CONNECTED_BOILER:
68 return getDevice().map(d -> toOnOffType(d.getPlugConnectedBoiler())).orElse(UnDefType.UNDEF);
69 case CHANNEL_LAST_PLUG_SEEN:
70 return getDevice().map(d -> toDateTimeType(d.getLastPlugSeen(), timeZoneProvider.getTimeZone()))
71 .orElse(UnDefType.UNDEF);
72 case CHANNEL_LAST_BILAN:
73 return toDateTimeType(getLastBilan());
75 return super.getNAThingProperty(channelId);
78 public @Nullable ZonedDateTime getLastBilan() {
79 Optional<NAYearMonth> lastBilan = getDevice().map(d -> d.getLastBilan());
80 if (lastBilan.isPresent()) {
81 ZonedDateTime zonedDT = ZonedDateTime.of(lastBilan.get().getY(), lastBilan.get().getM(), 1, 0, 0, 0, 0,
82 ZonedDateTime.now().getZone());
83 return zonedDT.with(TemporalAdjusters.lastDayOfMonth());
89 protected Optional<Integer> getDataTimestamp() {
90 return getDevice().map(d -> d.getLastStatusStore());