]> git.basschouten.com Git - openhab-addons.git/blob
c4848b4ded47595cc211ae84852a453588f00fd3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mqtt.homeassistant.internal;
14
15 import java.util.List;
16 import java.util.regex.Pattern;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener;
22 import org.openhab.binding.mqtt.generic.values.NumberValue;
23 import org.openhab.binding.mqtt.generic.values.TextValue;
24 import org.openhab.binding.mqtt.generic.values.Value;
25 import org.openhab.binding.mqtt.homeassistant.internal.listener.ExpireUpdateStateListener;
26
27 /**
28  * A MQTT sensor, following the https://www.home-assistant.io/components/sensor.mqtt/ specification.
29  *
30  * @author David Graeff - Initial contribution
31  */
32 @NonNullByDefault
33 public class ComponentSensor extends AbstractComponent<ComponentSensor.ChannelConfiguration> {
34     public static final String sensorChannelID = "sensor"; // Randomly chosen channel "ID"
35     private static final Pattern triggerIcons = Pattern.compile("^mdi:(toggle|gesture).*$");
36
37     /**
38      * Configuration class for MQTT component
39      */
40     static class ChannelConfiguration extends BaseChannelConfiguration {
41         ChannelConfiguration() {
42             super("MQTT Sensor");
43         }
44
45         protected @Nullable String unit_of_measurement;
46         protected @Nullable String device_class;
47         protected boolean force_update = false;
48         protected @Nullable Integer expire_after;
49
50         protected String state_topic = "";
51
52         protected @Nullable String json_attributes_topic;
53         protected @Nullable String json_attributes_template;
54         protected @Nullable List<String> json_attributes;
55     }
56
57     public ComponentSensor(CFactory.ComponentConfiguration componentConfiguration) {
58         super(componentConfiguration, ChannelConfiguration.class);
59
60         Value value;
61
62         String uom = channelConfiguration.unit_of_measurement;
63
64         if (uom != null && StringUtils.isNotBlank(uom)) {
65             value = new NumberValue(null, null, null, uom);
66         } else {
67             value = new TextValue();
68         }
69
70         String icon = channelConfiguration.icon;
71
72         boolean trigger = triggerIcons.matcher(icon).matches();
73
74         buildChannel(sensorChannelID, value, channelConfiguration.name, getListener(componentConfiguration, value))
75                 .stateTopic(channelConfiguration.state_topic, channelConfiguration.value_template)//
76                 .trigger(trigger).build();
77     }
78
79     private ChannelStateUpdateListener getListener(CFactory.ComponentConfiguration componentConfiguration,
80             Value value) {
81         ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();
82
83         if (channelConfiguration.expire_after != null) {
84             updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expire_after, value,
85                     componentConfiguration.getTracker(), componentConfiguration.getScheduler());
86         }
87         return updateListener;
88     }
89 }