2 * Copyright (c) 2010-2022 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("force_update")
53 protected boolean forceUpdate = false;
54 @SerializedName("expire_after")
55 protected @Nullable Integer expireAfter;
57 @SerializedName("state_topic")
58 protected String stateTopic = "";
60 @SerializedName("json_attributes_topic")
61 protected @Nullable String jsonAttributesTopic;
62 @SerializedName("json_attributes_template")
63 protected @Nullable String jsonAttributesTemplate;
64 @SerializedName("json_attributes")
65 protected @Nullable List<String> jsonAttributes;
68 public Sensor(ComponentFactory.ComponentConfiguration componentConfiguration) {
69 super(componentConfiguration, ChannelConfiguration.class);
72 String uom = channelConfiguration.unitOfMeasurement;
74 if (uom != null && !uom.isBlank()) {
75 value = new NumberValue(null, null, null, UnitUtils.parseUnit(uom));
77 value = new TextValue();
80 String icon = channelConfiguration.getIcon();
82 boolean trigger = TRIGGER_ICONS.matcher(icon).matches();
84 buildChannel(SENSOR_CHANNEL_ID, value, channelConfiguration.getName(),
85 getListener(componentConfiguration, value))
86 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())//
87 .trigger(trigger).build();
90 private ChannelStateUpdateListener getListener(ComponentFactory.ComponentConfiguration componentConfiguration,
92 ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();
94 if (channelConfiguration.expireAfter != null) {
95 updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expireAfter, value,
96 componentConfiguration.getTracker(), componentConfiguration.getScheduler());
98 return updateListener;