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