]> git.basschouten.com Git - openhab-addons.git/blob
9060e063b5c1a1ef4692db898ef6582f97de2b05
[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     public static final String JSON_ATTRIBUTES_CHANNEL_ID = "json-attributes";
40
41     private static final Pattern TRIGGER_ICONS = Pattern.compile("^mdi:(toggle|gesture).*$");
42
43     /**
44      * Configuration class for MQTT component
45      */
46     static class ChannelConfiguration extends AbstractChannelConfiguration {
47         ChannelConfiguration() {
48             super("MQTT Sensor");
49         }
50
51         @SerializedName("unit_of_measurement")
52         protected @Nullable String unitOfMeasurement;
53         @SerializedName("device_class")
54         protected @Nullable String deviceClass;
55         @SerializedName("state_class")
56         protected @Nullable String stateClass;
57         @SerializedName("force_update")
58         protected boolean forceUpdate = false;
59         @SerializedName("expire_after")
60         protected @Nullable Integer expireAfter;
61
62         @SerializedName("state_topic")
63         protected String stateTopic = "";
64
65         @SerializedName("json_attributes_topic")
66         protected @Nullable String jsonAttributesTopic;
67         @SerializedName("json_attributes_template")
68         protected @Nullable String jsonAttributesTemplate;
69         @SerializedName("json_attributes")
70         protected @Nullable List<String> jsonAttributes;
71     }
72
73     public Sensor(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
74         super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);
75
76         Value value;
77         String uom = channelConfiguration.unitOfMeasurement;
78         String sc = channelConfiguration.stateClass;
79         ComponentChannelType type;
80
81         if (uom != null && !uom.isBlank()) {
82             value = new NumberValue(null, null, null, UnitUtils.parseUnit(uom));
83             type = ComponentChannelType.NUMBER;
84         } else if (sc != null && !sc.isBlank()) {
85             // see state_class at https://developers.home-assistant.io/docs/core/entity/sensor#properties
86             // > If not None, the sensor is assumed to be numerical
87             value = new NumberValue(null, null, null, null);
88             type = ComponentChannelType.NUMBER;
89         } else {
90             value = new TextValue();
91             type = ComponentChannelType.STRING;
92         }
93
94         String icon = channelConfiguration.getIcon();
95
96         boolean trigger = TRIGGER_ICONS.matcher(icon).matches();
97
98         buildChannel(SENSOR_CHANNEL_ID, type, value, getName(), getListener(componentConfiguration, value))
99                 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())//
100                 .trigger(trigger).build();
101
102         if (channelConfiguration.jsonAttributesTemplate != null) {
103             buildChannel(JSON_ATTRIBUTES_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(), "JSON Attributes",
104                     componentConfiguration.getUpdateListener())
105                     .stateTopic(channelConfiguration.jsonAttributesTopic, channelConfiguration.jsonAttributesTemplate)
106                     .build();
107         }
108         finalizeChannels();
109     }
110
111     private ChannelStateUpdateListener getListener(ComponentFactory.ComponentConfiguration componentConfiguration,
112             Value value) {
113         ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();
114
115         if (channelConfiguration.expireAfter != null) {
116             updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expireAfter, value,
117                     componentConfiguration.getTracker(), componentConfiguration.getScheduler());
118         }
119         return updateListener;
120     }
121 }