]> git.basschouten.com Git - openhab-addons.git/blob
26b6c704d6ae89f0f5c1d95d4478b34197751d94
[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.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("state_class")
53         protected @Nullable String stateClass;
54         @SerializedName("force_update")
55         protected boolean forceUpdate = false;
56         @SerializedName("expire_after")
57         protected @Nullable Integer expireAfter;
58
59         @SerializedName("state_topic")
60         protected String stateTopic = "";
61
62         @SerializedName("json_attributes_topic")
63         protected @Nullable String jsonAttributesTopic;
64         @SerializedName("json_attributes_template")
65         protected @Nullable String jsonAttributesTemplate;
66         @SerializedName("json_attributes")
67         protected @Nullable List<String> jsonAttributes;
68     }
69
70     public Sensor(ComponentFactory.ComponentConfiguration componentConfiguration) {
71         super(componentConfiguration, ChannelConfiguration.class);
72
73         Value value;
74         String uom = channelConfiguration.unitOfMeasurement;
75         String sc = channelConfiguration.stateClass;
76
77         if (uom != null && !uom.isBlank()) {
78             value = new NumberValue(null, null, null, UnitUtils.parseUnit(uom));
79         } else if (sc != null && !sc.isBlank()) {
80             // see state_class at https://developers.home-assistant.io/docs/core/entity/sensor#properties
81             // > If not None, the sensor is assumed to be numerical
82             value = new NumberValue(null, null, null, null);
83         } else {
84             value = new TextValue();
85         }
86
87         String icon = channelConfiguration.getIcon();
88
89         boolean trigger = TRIGGER_ICONS.matcher(icon).matches();
90
91         buildChannel(SENSOR_CHANNEL_ID, value, getName(), getListener(componentConfiguration, value))
92                 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())//
93                 .trigger(trigger).build();
94     }
95
96     private ChannelStateUpdateListener getListener(ComponentFactory.ComponentConfiguration componentConfiguration,
97             Value value) {
98         ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();
99
100         if (channelConfiguration.expireAfter != null) {
101             updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expireAfter, value,
102                     componentConfiguration.getTracker(), componentConfiguration.getScheduler());
103         }
104         return updateListener;
105     }
106 }