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