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