2 * Copyright (c) 2010-2023 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.hue.internal.handler.sensors;
15 import static org.openhab.binding.hue.internal.HueBindingConstants.*;
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;
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;
39 * @author Christoph Weitkamp - Initial contribution
42 public class TapSwitchHandler extends HueSensorHandler {
44 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_TAP_SWITCH);
46 public TapSwitchHandler(Thing thing) {
51 protected SensorConfigUpdate doConfigurationUpdate(Map<String, Object> configurationParameters) {
52 return new SensorConfigUpdate();
56 protected void doSensorStateChanged(FullSensor sensor, Configuration config) {
57 ZoneId zoneId = ZoneId.systemDefault();
58 ZonedDateTime now = ZonedDateTime.now(zoneId), timestamp = now;
60 Object lastUpdated = sensor.getState().get(FullSensor.STATE_LAST_UPDATED);
61 if (lastUpdated != null) {
63 timestamp = ZonedDateTime.ofInstant(
64 LocalDateTime.parse(String.valueOf(lastUpdated), DateTimeFormatter.ISO_LOCAL_DATE_TIME),
65 ZoneOffset.UTC, zoneId);
66 } catch (DateTimeParseException e) {
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);