]> git.basschouten.com Git - openhab-addons.git/blob
8adc3ed736a1140931d3111edaf68b48eaafc638
[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 Dimmer Switch
38  *
39  * @author Samuel Leisering - Initial contribution
40  * @author Christoph Weitkamp - Initial contribution
41  */
42 @NonNullByDefault
43 public class DimmerSwitchHandler extends HueSensorHandler {
44
45     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_DIMMER_SWITCH);
46
47     public DimmerSwitchHandler(Thing thing) {
48         super(thing);
49     }
50
51     @Override
52     protected SensorConfigUpdate doConfigurationUpdate(Map<String, Object> configurationParameters) {
53         return new SensorConfigUpdate();
54     }
55
56     @Override
57     protected void doSensorStateChanged(FullSensor sensor, Configuration config) {
58         ZoneId zoneId = ZoneId.systemDefault();
59         ZonedDateTime now = ZonedDateTime.now(zoneId), timestamp = now;
60
61         Object lastUpdated = sensor.getState().get(FullSensor.STATE_LAST_UPDATED);
62         if (lastUpdated != null) {
63             try {
64                 timestamp = ZonedDateTime.ofInstant(
65                         LocalDateTime.parse(String.valueOf(lastUpdated), DateTimeFormatter.ISO_LOCAL_DATE_TIME),
66                         ZoneOffset.UTC, zoneId);
67             } catch (DateTimeParseException e) {
68                 // do nothing
69             }
70         }
71
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);
81             }
82         }
83     }
84 }