2 * Copyright (c) 2010-2023 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.config.dto.AbstractChannelConfiguration;
25 import org.openhab.binding.mqtt.homeassistant.internal.listener.ExpireUpdateStateListener;
26 import org.openhab.core.types.util.UnitUtils;
28 import com.google.gson.annotations.SerializedName;
31 * A MQTT sensor, following the https://www.home-assistant.io/components/sensor.mqtt/ specification.
33 * @author David Graeff - Initial contribution
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).*$");
41 * Configuration class for MQTT component
43 static class ChannelConfiguration extends AbstractChannelConfiguration {
44 ChannelConfiguration() {
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;
59 @SerializedName("state_topic")
60 protected String stateTopic = "";
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;
70 public Sensor(ComponentFactory.ComponentConfiguration componentConfiguration) {
71 super(componentConfiguration, ChannelConfiguration.class);
74 String uom = channelConfiguration.unitOfMeasurement;
75 String sc = channelConfiguration.stateClass;
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);
84 value = new TextValue();
87 String icon = channelConfiguration.getIcon();
89 boolean trigger = TRIGGER_ICONS.matcher(icon).matches();
91 buildChannel(SENSOR_CHANNEL_ID, value, getName(), getListener(componentConfiguration, value))
92 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())//
93 .trigger(trigger).build();
96 private ChannelStateUpdateListener getListener(ComponentFactory.ComponentConfiguration componentConfiguration,
98 ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();
100 if (channelConfiguration.expireAfter != null) {
101 updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expireAfter, value,
102 componentConfiguration.getTracker(), componentConfiguration.getScheduler());
104 return updateListener;