2 * Copyright (c) 2010-2021 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;
27 import com.google.gson.annotations.SerializedName;
30 * A MQTT sensor, following the https://www.home-assistant.io/components/sensor.mqtt/ specification.
32 * @author David Graeff - Initial contribution
35 public class Sensor extends AbstractComponent<Sensor.ChannelConfiguration> {
36 public static final String SENSOR_CHANNEL_ID = "sensor"; // Randomly chosen channel "ID"
37 private static final Pattern TRIGGER_ICONS = Pattern.compile("^mdi:(toggle|gesture).*$");
40 * Configuration class for MQTT component
42 static class ChannelConfiguration extends AbstractChannelConfiguration {
43 ChannelConfiguration() {
47 @SerializedName("unit_of_measurement")
48 protected @Nullable String unitOfMeasurement;
49 @SerializedName("device_class")
50 protected @Nullable String deviceClass;
51 @SerializedName("force_update")
52 protected boolean forceUpdate = false;
53 @SerializedName("expire_after")
54 protected @Nullable Integer expireAfter;
56 @SerializedName("state_topic")
57 protected String stateTopic = "";
59 @SerializedName("json_attributes_topic")
60 protected @Nullable String jsonAttributesTopic;
61 @SerializedName("json_attributes_template")
62 protected @Nullable String jsonAttributesTemplate;
63 @SerializedName("json_attributes")
64 protected @Nullable List<String> jsonAttributes;
67 public Sensor(ComponentFactory.ComponentConfiguration componentConfiguration) {
68 super(componentConfiguration, ChannelConfiguration.class);
71 String uom = channelConfiguration.unitOfMeasurement;
73 if (uom != null && !uom.isBlank()) {
74 value = new NumberValue(null, null, null, uom);
76 value = new TextValue();
79 String icon = channelConfiguration.getIcon();
81 boolean trigger = TRIGGER_ICONS.matcher(icon).matches();
83 buildChannel(SENSOR_CHANNEL_ID, value, channelConfiguration.getName(),
84 getListener(componentConfiguration, value))
85 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())//
86 .trigger(trigger).build();
89 private ChannelStateUpdateListener getListener(ComponentFactory.ComponentConfiguration componentConfiguration,
91 ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();
93 if (channelConfiguration.expireAfter != null) {
94 updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expireAfter, value,
95 componentConfiguration.getTracker(), componentConfiguration.getScheduler());
97 return updateListener;