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 Samuel Leisering - Initial contribution
40 * @author Christoph Weitkamp - Initial contribution
43 public class DimmerSwitchHandler extends HueSensorHandler {
45 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_DIMMER_SWITCH);
47 public DimmerSwitchHandler(Thing thing) {
52 protected SensorConfigUpdate doConfigurationUpdate(Map<String, Object> configurationParameters) {
53 return new SensorConfigUpdate();
57 protected void doSensorStateChanged(FullSensor sensor, Configuration config) {
58 ZoneId zoneId = ZoneId.systemDefault();
59 ZonedDateTime now = ZonedDateTime.now(zoneId), timestamp = now;
61 Object lastUpdated = sensor.getState().get(FullSensor.STATE_LAST_UPDATED);
62 if (lastUpdated != null) {
64 timestamp = ZonedDateTime.ofInstant(
65 LocalDateTime.parse(String.valueOf(lastUpdated), DateTimeFormatter.ISO_LOCAL_DATE_TIME),
66 ZoneOffset.UTC, zoneId);
67 } catch (DateTimeParseException e) {
72 Object buttonState = sensor.getState().get(FullSensor.STATE_BUTTON_EVENT);
73 if (buttonState != null) {
74 String value = String.valueOf(buttonState);
75 updateState(CHANNEL_DIMMER_SWITCH, new DecimalType(value));
76 // Avoid dispatching events if "lastupdated" is older than now minus 3 seconds (e.g. during restart)
77 Instant then = timestamp.toInstant();
78 Instant someSecondsEarlier = now.minusSeconds(3).toInstant();
79 if (then.isAfter(someSecondsEarlier) && then.isBefore(now.toInstant())) {
80 triggerChannel(EVENT_DIMMER_SWITCH, value);