]> git.basschouten.com Git - openhab-addons.git/blob
2e18a3a2dd6f14848d129443b3766c8f441f95f7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.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.listener.ExpireUpdateStateListener;
25
26 /**
27  * A MQTT sensor, following the https://www.home-assistant.io/components/sensor.mqtt/ specification.
28  *
29  * @author David Graeff - Initial contribution
30  */
31 @NonNullByDefault
32 public class ComponentSensor extends AbstractComponent<ComponentSensor.ChannelConfiguration> {
33     public static final String sensorChannelID = "sensor"; // Randomly chosen channel "ID"
34     private static final Pattern triggerIcons = Pattern.compile("^mdi:(toggle|gesture).*$");
35
36     /**
37      * Configuration class for MQTT component
38      */
39     static class ChannelConfiguration extends BaseChannelConfiguration {
40         ChannelConfiguration() {
41             super("MQTT Sensor");
42         }
43
44         protected @Nullable String unit_of_measurement;
45         protected @Nullable String device_class;
46         protected boolean force_update = false;
47         protected @Nullable Integer expire_after;
48
49         protected String state_topic = "";
50
51         protected @Nullable String json_attributes_topic;
52         protected @Nullable String json_attributes_template;
53         protected @Nullable List<String> json_attributes;
54     }
55
56     public ComponentSensor(CFactory.ComponentConfiguration componentConfiguration) {
57         super(componentConfiguration, ChannelConfiguration.class);
58
59         Value value;
60
61         String uom = channelConfiguration.unit_of_measurement;
62
63         if (uom != null && !uom.isBlank()) {
64             value = new NumberValue(null, null, null, uom);
65         } else {
66             value = new TextValue();
67         }
68
69         String icon = channelConfiguration.icon;
70
71         boolean trigger = triggerIcons.matcher(icon).matches();
72
73         buildChannel(sensorChannelID, value, channelConfiguration.name, getListener(componentConfiguration, value))
74                 .stateTopic(channelConfiguration.state_topic, channelConfiguration.value_template)//
75                 .trigger(trigger).build();
76     }
77
78     private ChannelStateUpdateListener getListener(CFactory.ComponentConfiguration componentConfiguration,
79             Value value) {
80         ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();
81
82         if (channelConfiguration.expire_after != null) {
83             updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expire_after, value,
84                     componentConfiguration.getTracker(), componentConfiguration.getScheduler());
85         }
86         return updateListener;
87     }
88 }