]> git.basschouten.com Git - openhab-addons.git/blob
7c98243e498f1b6ad456718b8d1ff13be8c1eb3d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.shelly.internal.handler;
14
15 import static org.openhab.binding.shelly.internal.ShellyBindingConstants.*;
16 import static org.openhab.binding.shelly.internal.discovery.ShellyThingCreator.*;
17 import static org.openhab.binding.shelly.internal.util.ShellyUtils.*;
18 import static org.openhab.core.thing.Thing.PROPERTY_MODEL_ID;
19
20 import java.util.Map;
21 import java.util.TreeMap;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.eclipse.jetty.client.HttpClient;
26 import org.openhab.binding.shelly.internal.api.ShellyDeviceProfile;
27 import org.openhab.binding.shelly.internal.api1.Shelly1CoapServer;
28 import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2NotifyEvent;
29 import org.openhab.binding.shelly.internal.config.ShellyBindingConfiguration;
30 import org.openhab.binding.shelly.internal.provider.ShellyTranslationProvider;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * {@link ShellyBluSensorHandler} implements the thing handler for the BLU devices
38  *
39  * @author Markus Michels - Initial contribution
40  */
41 @NonNullByDefault
42 public class ShellyBluSensorHandler extends ShellyBaseHandler {
43     private static final Logger logger = LoggerFactory.getLogger(ShellyBluSensorHandler.class);
44
45     public ShellyBluSensorHandler(final Thing thing, final ShellyTranslationProvider translationProvider,
46             final ShellyBindingConfiguration bindingConfig, final ShellyThingTable thingTable,
47             final Shelly1CoapServer coapServer, final HttpClient httpClient) {
48         super(thing, translationProvider, bindingConfig, thingTable, coapServer, httpClient);
49     }
50
51     @Override
52     public void initialize() {
53         logger.debug("Thing is using  {}", this.getClass());
54         super.initialize();
55     }
56
57     public static void addBluThing(String gateway, Shelly2NotifyEvent e, @Nullable ShellyThingTable thingTable) {
58         String model = substringBefore(getString(e.data.name), "-").toUpperCase();
59         String mac = e.data.addr.replaceAll(":", "");
60         String ttype = "";
61         logger.debug("{}: Create thing for new BLU device {}: {} / {}", gateway, e.data.name, model, mac);
62         ThingTypeUID tuid;
63         switch (model) {
64             case SHELLYDT_BLUBUTTON:
65                 ttype = THING_TYPE_SHELLYBLUBUTTON_STR;
66                 tuid = THING_TYPE_SHELLYBLUBUTTON;
67                 break;
68             case SHELLYDT_BLUDW:
69                 ttype = THING_TYPE_SHELLYBLUDW_STR;
70                 tuid = THING_TYPE_SHELLYBLUDW;
71                 break;
72             case SHELLYDT_BLUMOTION:
73                 ttype = THING_TYPE_SHELLYBLUMOTION_STR;
74                 tuid = THING_TYPE_SHELLYBLUMOTION;
75                 break;
76             case SHELLYDT_BLUHT:
77                 ttype = THING_TYPE_SHELLYBLUHT_STR;
78                 tuid = THING_TYPE_SHELLYBLUHT;
79                 break;
80             default:
81                 logger.debug("{}: Unsupported BLU device model {}, MAC={}", gateway, model, mac);
82                 return;
83         }
84         String serviceName = ShellyDeviceProfile.buildBluServiceName(getString(e.data.name), mac);
85
86         Map<String, Object> properties = new TreeMap<>();
87         addProperty(properties, PROPERTY_MODEL_ID, model);
88         addProperty(properties, PROPERTY_SERVICE_NAME, serviceName);
89         addProperty(properties, PROPERTY_DEV_NAME, e.data.name);
90         addProperty(properties, PROPERTY_DEV_TYPE, ttype);
91         addProperty(properties, PROPERTY_DEV_GEN, "BLU");
92         addProperty(properties, PROPERTY_GW_DEVICE, gateway);
93         addProperty(properties, CONFIG_DEVICEADDRESS, mac);
94
95         if (thingTable != null) {
96             thingTable.discoveredResult(tuid, model, serviceName, mac, properties);
97         }
98     }
99
100     private static void addProperty(Map<String, Object> properties, String key, @Nullable String value) {
101         properties.put(key, value != null ? value : "");
102     }
103 }