2 * Copyright (c) 2010-2021 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;
24 import java.util.Collections;
28 import org.eclipse.jdt.annotation.NonNullByDefault;
29 import org.openhab.binding.hue.internal.FullSensor;
30 import org.openhab.binding.hue.internal.SensorConfigUpdate;
31 import org.openhab.binding.hue.internal.handler.HueSensorHandler;
32 import org.openhab.core.config.core.Configuration;
33 import org.openhab.core.library.types.DecimalType;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingTypeUID;
40 * @author Samuel Leisering - Initial contribution
41 * @author Christoph Weitkamp - Initial contribution
44 public class DimmerSwitchHandler extends HueSensorHandler {
46 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_DIMMER_SWITCH);
48 public DimmerSwitchHandler(Thing thing) {
53 protected SensorConfigUpdate doConfigurationUpdate(Map<String, Object> configurationParameters) {
54 return new SensorConfigUpdate();
58 protected void doSensorStateChanged(FullSensor sensor, Configuration config) {
59 ZoneId zoneId = ZoneId.systemDefault();
60 ZonedDateTime now = ZonedDateTime.now(zoneId), timestamp = now;
62 Object lastUpdated = sensor.getState().get(FullSensor.STATE_LAST_UPDATED);
63 if (lastUpdated != null) {
65 timestamp = ZonedDateTime.ofInstant(
66 LocalDateTime.parse(String.valueOf(lastUpdated), DateTimeFormatter.ISO_LOCAL_DATE_TIME),
67 ZoneOffset.UTC, zoneId);
68 } catch (DateTimeParseException e) {
73 Object buttonState = sensor.getState().get(FullSensor.STATE_BUTTON_EVENT);
74 if (buttonState != null) {
75 String value = String.valueOf(buttonState);
76 updateState(CHANNEL_DIMMER_SWITCH, new DecimalType(value));
77 // Avoid dispatching events if "lastupdated" is older than now minus 3 seconds (e.g. during restart)
78 Instant then = timestamp.toInstant();
79 Instant someSecondsEarlier = now.minusSeconds(3).toInstant();
80 if (then.isAfter(someSecondsEarlier) && then.isBefore(now.toInstant())) {
81 triggerChannel(EVENT_DIMMER_SWITCH, value);