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