]> git.basschouten.com Git - openhab-addons.git/blob
217301cc364d7da4cf7a8a6c896210cbf0e4db0b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.hue.internal.handler.sensors;
14
15 import static org.openhab.binding.hue.internal.HueBindingConstants.*;
16
17 import java.time.Instant;
18 import java.time.LocalDateTime;
19 import java.time.ZoneId;
20 import java.time.ZoneOffset;
21 import java.time.ZonedDateTime;
22 import java.time.format.DateTimeFormatter;
23 import java.time.format.DateTimeParseException;
24 import java.util.Map;
25 import java.util.Set;
26
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.openhab.binding.hue.internal.dto.FullSensor;
29 import org.openhab.binding.hue.internal.dto.SensorConfigUpdate;
30 import org.openhab.binding.hue.internal.handler.HueSensorHandler;
31 import org.openhab.core.config.core.Configuration;
32 import org.openhab.core.library.types.DecimalType;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingTypeUID;
35
36 /**
37  * Hue Tap Switch
38  *
39  * @author Christoph Weitkamp - Initial contribution
40  */
41 @NonNullByDefault
42 public class TapSwitchHandler extends HueSensorHandler {
43
44     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_TAP_SWITCH);
45
46     public TapSwitchHandler(Thing thing) {
47         super(thing);
48     }
49
50     @Override
51     protected SensorConfigUpdate doConfigurationUpdate(Map<String, Object> configurationParameters) {
52         return new SensorConfigUpdate();
53     }
54
55     @Override
56     protected void doSensorStateChanged(FullSensor sensor, Configuration config) {
57         ZoneId zoneId = ZoneId.systemDefault();
58         ZonedDateTime now = ZonedDateTime.now(zoneId), timestamp = now;
59
60         Object lastUpdated = sensor.getState().get(FullSensor.STATE_LAST_UPDATED);
61         if (lastUpdated != null) {
62             try {
63                 timestamp = ZonedDateTime.ofInstant(
64                         LocalDateTime.parse(String.valueOf(lastUpdated), DateTimeFormatter.ISO_LOCAL_DATE_TIME),
65                         ZoneOffset.UTC, zoneId);
66             } catch (DateTimeParseException e) {
67                 // do nothing
68             }
69         }
70
71         Object buttonState = sensor.getState().get(FullSensor.STATE_BUTTON_EVENT);
72         if (buttonState != null) {
73             String value = String.valueOf(buttonState);
74             updateState(CHANNEL_TAP_SWITCH, new DecimalType(value));
75             // Avoid dispatching events if "lastupdated" is older than now minus 3 seconds (e.g. during restart)
76             Instant then = timestamp.toInstant();
77             Instant someSecondsEarlier = now.minusSeconds(3).toInstant();
78             if (then.isAfter(someSecondsEarlier) && then.isBefore(now.toInstant())) {
79                 triggerChannel(EVENT_TAP_SWITCH, value);
80             }
81         }
82     }
83 }