]> git.basschouten.com Git - openhab-addons.git/blob
58df996a74dc9ceddafc2cecf854c4b0a21ab0e2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.tado.internal.handler;
14
15 import java.io.IOException;
16 import java.time.Instant;
17 import java.time.temporal.ChronoUnit;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Objects;
21 import java.util.Optional;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.binding.tado.internal.api.ApiException;
25 import org.openhab.binding.tado.internal.api.model.ControlDevice;
26 import org.openhab.binding.tado.internal.api.model.Zone;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.types.State;
29 import org.openhab.core.types.UnDefType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * The {@link TadoBatteryChecker} checks the battery state of Tado control
35  * devices.
36  *
37  * @author Andrew Fiddian-Green - Initial contribution
38  *
39  */
40 @NonNullByDefault
41 public class TadoBatteryChecker {
42     private final Logger logger = LoggerFactory.getLogger(TadoBatteryChecker.class);
43
44     private final TadoHomeHandler homeHandler;
45     private Map<Long, Zone> zones = new HashMap<>();
46     private Instant refreshTime = Instant.MIN;
47
48     public TadoBatteryChecker(TadoHomeHandler homeHandler) {
49         this.homeHandler = homeHandler;
50     }
51
52     private void refreshZoneList() {
53         if (refreshTime.isAfter(Instant.now())) {
54             return;
55         }
56         // only refresh the battery state hourly
57         refreshTime = Instant.now().plus(1, ChronoUnit.HOURS);
58         Long homeId = homeHandler.getHomeId();
59         if (homeId != null) {
60             logger.debug("Fetching (battery state) zone list for HomeId {}", homeId);
61             try {
62                 Map<Long, Zone> zones = new HashMap<>();
63                 homeHandler.getApi().listZones(homeId).stream().filter(Objects::nonNull)
64                         .forEach(zone -> zones.put((long) zone.getId(), zone));
65                 this.zones = zones;
66             } catch (IOException | ApiException e) {
67                 logger.debug("Fetch (battery state) zone list exception");
68             }
69         }
70     }
71
72     public synchronized Optional<Zone> getZone(long zoneId) {
73         refreshZoneList();
74         return Optional.ofNullable(zones.get(zoneId));
75     }
76
77     public State getBatteryLowAlarm(long zoneId) {
78         Optional<Zone> zone = getZone(zoneId);
79         if (zone.isPresent()) {
80             boolean batteryOk = zone.get().getDevices().stream().map(ControlDevice::getBatteryState)
81                     .filter(Objects::nonNull).allMatch(batteryState -> "NORMAL".equals(batteryState));
82             return OnOffType.from(!batteryOk);
83         }
84         return UnDefType.UNDEF;
85     }
86 }