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 private static final Pattern TRIGGER_ICONS = Pattern.compile("^mdi:(toggle|gesture).*$");
42 * Configuration class for MQTT component
44 static class ChannelConfiguration extends AbstractChannelConfiguration {
45 ChannelConfiguration() {
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;
60 @SerializedName("state_topic")
61 protected String stateTopic = "";
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;
71 public Sensor(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
72 super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);
75 String uom = channelConfiguration.unitOfMeasurement;
76 String sc = channelConfiguration.stateClass;
77 ComponentChannelType type;
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;
88 value = new TextValue();
89 type = ComponentChannelType.STRING;
92 String icon = channelConfiguration.getIcon();
94 boolean trigger = TRIGGER_ICONS.matcher(icon).matches();
96 buildChannel(SENSOR_CHANNEL_ID, type, value, getName(), getListener(componentConfiguration, value))
97 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())//
98 .trigger(trigger).build();
102 private ChannelStateUpdateListener getListener(ComponentFactory.ComponentConfiguration componentConfiguration,
104 ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();
106 if (channelConfiguration.expireAfter != null) {
107 updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expireAfter, value,
108 componentConfiguration.getTracker(), componentConfiguration.getScheduler());
110 return updateListener;