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