]> git.basschouten.com Git - openhab-addons.git/blob
0451c276fa7c17e31682f591daa6ca2bd8d1f2dc
[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.mqtt.homeassistant.internal.component;
14
15 import java.util.List;
16 import java.util.regex.Pattern;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener;
21 import org.openhab.binding.mqtt.generic.values.NumberValue;
22 import org.openhab.binding.mqtt.generic.values.TextValue;
23 import org.openhab.binding.mqtt.generic.values.Value;
24 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
25 import org.openhab.binding.mqtt.homeassistant.internal.listener.ExpireUpdateStateListener;
26
27 import com.google.gson.annotations.SerializedName;
28
29 /**
30  * A MQTT sensor, following the https://www.home-assistant.io/components/sensor.mqtt/ specification.
31  *
32  * @author David Graeff - Initial contribution
33  */
34 @NonNullByDefault
35 public class Sensor extends AbstractComponent<Sensor.ChannelConfiguration> {
36     public static final String SENSOR_CHANNEL_ID = "sensor"; // Randomly chosen channel "ID"
37     private static final Pattern TRIGGER_ICONS = Pattern.compile("^mdi:(toggle|gesture).*$");
38
39     /**
40      * Configuration class for MQTT component
41      */
42     static class ChannelConfiguration extends AbstractChannelConfiguration {
43         ChannelConfiguration() {
44             super("MQTT Sensor");
45         }
46
47         @SerializedName("unit_of_measurement")
48         protected @Nullable String unitOfMeasurement;
49         @SerializedName("device_class")
50         protected @Nullable String deviceClass;
51         @SerializedName("force_update")
52         protected boolean forceUpdate = false;
53         @SerializedName("expire_after")
54         protected @Nullable Integer expireAfter;
55
56         @SerializedName("state_topic")
57         protected String stateTopic = "";
58
59         @SerializedName("json_attributes_topic")
60         protected @Nullable String jsonAttributesTopic;
61         @SerializedName("json_attributes_template")
62         protected @Nullable String jsonAttributesTemplate;
63         @SerializedName("json_attributes")
64         protected @Nullable List<String> jsonAttributes;
65     }
66
67     public Sensor(ComponentFactory.ComponentConfiguration componentConfiguration) {
68         super(componentConfiguration, ChannelConfiguration.class);
69
70         Value value;
71         String uom = channelConfiguration.unitOfMeasurement;
72
73         if (uom != null && !uom.isBlank()) {
74             value = new NumberValue(null, null, null, uom);
75         } else {
76             value = new TextValue();
77         }
78
79         String icon = channelConfiguration.getIcon();
80
81         boolean trigger = TRIGGER_ICONS.matcher(icon).matches();
82
83         buildChannel(SENSOR_CHANNEL_ID, value, channelConfiguration.getName(),
84                 getListener(componentConfiguration, value))
85                         .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())//
86                         .trigger(trigger).build();
87     }
88
89     private ChannelStateUpdateListener getListener(ComponentFactory.ComponentConfiguration componentConfiguration,
90             Value value) {
91         ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();
92
93         if (channelConfiguration.expireAfter != null) {
94             updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expireAfter, value,
95                     componentConfiguration.getTracker(), componentConfiguration.getScheduler());
96         }
97         return updateListener;
98     }
99 }