2 * Copyright (c) 2010-2023 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.api2.ShellyBluApi.buildBluServiceName;
17 import static org.openhab.binding.shelly.internal.discovery.ShellyThingCreator.*;
18 import static org.openhab.binding.shelly.internal.util.ShellyUtils.*;
19 import static org.openhab.core.thing.Thing.PROPERTY_MODEL_ID;
22 import java.util.TreeMap;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.eclipse.jetty.client.HttpClient;
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;
36 * {@link ShellyBluSensorHandler} implements the thing handler for the BLU devices
38 * @author Markus Michels - Initial contribution
40 public class ShellyBluSensorHandler extends ShellyBaseHandler {
41 private static final Logger logger = LoggerFactory.getLogger(ShellyBluSensorHandler.class);
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);
50 public void initialize() {
51 logger.debug("Thing is using {}", this.getClass());
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.replace(":", "");
59 logger.debug("{}: Create thing for new BLU device {}: {} / {}", gateway, e.data.name, model, mac);
62 case SHELLYDT_BLUBUTTON:
63 ttype = THING_TYPE_SHELLYBLUBUTTON_STR;
64 tuid = THING_TYPE_SHELLYBLUBUTTON;
67 ttype = THING_TYPE_SHELLYBLUDW_STR;
68 tuid = THING_TYPE_SHELLYBLUDW;
70 case SHELLYDT_BLUMOTION:
71 ttype = THING_TYPE_SHELLYBLUMOTION_STR;
72 tuid = THING_TYPE_SHELLYBLUMOTION;
75 logger.debug("{}: Unsupported BLU device model {}, MAC={}", gateway, model, mac);
78 String serviceName = buildBluServiceName(model, mac);
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);
89 if (thingTable != null) {
90 thingTable.discoveredResult(tuid, model, serviceName, mac, properties);
94 private static void addProperty(Map<String, Object> properties, String key, @Nullable String value) {
95 properties.put(key, value != null ? value : "");