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