]> git.basschouten.com Git - openhab-addons.git/blob
757f02b10458258bb005f25e63dc6ac03837f811
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.thermostat;
14
15 import static org.openhab.binding.netatmo.internal.ChannelTypeUtils.*;
16 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
17
18 import java.time.ZonedDateTime;
19 import java.time.temporal.TemporalAdjusters;
20 import java.util.Optional;
21
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;
29
30 import io.swagger.client.model.NAPlug;
31 import io.swagger.client.model.NAYearMonth;
32
33 /**
34  * {@link NAPlugHandler} is the class used to handle the plug
35  * device of a thermostat set
36  *
37  * @author GaĆ«l L'hopital - Initial contribution OH2 version
38  *
39  */
40 @NonNullByDefault
41 public class NAPlugHandler extends NetatmoDeviceHandler<NAPlug> {
42
43     public NAPlugHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
44         super(thing, timeZoneProvider);
45     }
46
47     @Override
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));
54         });
55         return result;
56     }
57
58     @Override
59     protected void updateProperties(NAPlug deviceData) {
60         updateProperties(deviceData.getFirmware(), deviceData.getType());
61     }
62
63     @Override
64     protected State getNAThingProperty(String channelId) {
65         switch (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());
73         }
74         return super.getNAThingProperty(channelId);
75     }
76
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());
83         }
84         return null;
85     }
86
87     @Override
88     protected Optional<Integer> getDataTimestamp() {
89         return getDevice().map(d -> d.getLastStatusStore());
90     }
91 }