2 * Copyright (c) 2010-2024 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.shelly.internal.handler;
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;
21 import java.util.TreeMap;
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;
37 * {@link ShellyBluSensorHandler} implements the thing handler for the BLU devices
39 * @author Markus Michels - Initial contribution
42 public class ShellyBluSensorHandler extends ShellyBaseHandler {
43 private static final Logger logger = LoggerFactory.getLogger(ShellyBluSensorHandler.class);
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);
52 public void initialize() {
53 logger.debug("Thing is using {}", this.getClass());
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(":", "");
61 logger.debug("{}: Create thing for new BLU device {}: {} / {}", gateway, e.data.name, model, mac);
64 case SHELLYDT_BLUBUTTON:
65 ttype = THING_TYPE_SHELLYBLUBUTTON_STR;
66 tuid = THING_TYPE_SHELLYBLUBUTTON;
69 ttype = THING_TYPE_SHELLYBLUDW_STR;
70 tuid = THING_TYPE_SHELLYBLUDW;
72 case SHELLYDT_BLUMOTION:
73 ttype = THING_TYPE_SHELLYBLUMOTION_STR;
74 tuid = THING_TYPE_SHELLYBLUMOTION;
77 ttype = THING_TYPE_SHELLYBLUHT_STR;
78 tuid = THING_TYPE_SHELLYBLUHT;
81 logger.debug("{}: Unsupported BLU device model {}, MAC={}", gateway, model, mac);
84 String serviceName = ShellyDeviceProfile.buildBluServiceName(getString(e.data.name), mac);
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);
95 if (thingTable != null) {
96 thingTable.discoveredResult(tuid, model, serviceName, mac, properties);
100 private static void addProperty(Map<String, Object> properties, String key, @Nullable String value) {
101 properties.put(key, value != null ? value : "");