2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.homeassistant.internal.component;
15 import java.util.List;
16 import java.util.regex.Pattern;
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;
29 import com.google.gson.annotations.SerializedName;
32 * A MQTT sensor, following the https://www.home-assistant.io/components/sensor.mqtt/ specification.
34 * @author David Graeff - Initial contribution
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";
41 private static final Pattern TRIGGER_ICONS = Pattern.compile("^mdi:(toggle|gesture).*$");
44 * Configuration class for MQTT component
46 static class ChannelConfiguration extends AbstractChannelConfiguration {
47 ChannelConfiguration() {
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;
62 @SerializedName("state_topic")
63 protected String stateTopic = "";
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;
73 public Sensor(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
74 super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);
77 String uom = channelConfiguration.unitOfMeasurement;
78 String sc = channelConfiguration.stateClass;
79 ComponentChannelType type;
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;
90 value = new TextValue();
91 type = ComponentChannelType.STRING;
94 String icon = channelConfiguration.getIcon();
96 boolean trigger = TRIGGER_ICONS.matcher(icon).matches();
98 buildChannel(SENSOR_CHANNEL_ID, type, value, getName(), getListener(componentConfiguration, value))
99 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())//
100 .trigger(trigger).build();
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)
111 private ChannelStateUpdateListener getListener(ComponentFactory.ComponentConfiguration componentConfiguration,
113 ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();
115 if (channelConfiguration.expireAfter != null) {
116 updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expireAfter, value,
117 componentConfiguration.getTracker(), componentConfiguration.getScheduler());
119 return updateListener;