]> git.basschouten.com Git - openhab-addons.git/blob
e1e17e269858e5c90330cfdd456101f28e8f726e
[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.gardena.internal.util;
14
15 import static org.openhab.binding.gardena.internal.GardenaBindingConstants.BINDING_ID;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.gardena.internal.handler.GardenaDeviceConfig;
22 import org.openhab.binding.gardena.internal.model.dto.Device;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.ThingUID;
27
28 /**
29  * Utility class for converting between a Thing and a Gardena device.
30  *
31  * @author Gerhard Riegler - Initial contribution
32  */
33 @NonNullByDefault
34 public class UidUtils {
35
36     /**
37      * Generates the ThingUID for the given device in the given account.
38      */
39     public static ThingUID generateThingUID(Device device, Bridge account) {
40         ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID, device.deviceType);
41         return new ThingUID(thingTypeUID, account.getUID(), device.id);
42     }
43
44     /**
45      * Returns all ThingUIDs for a given device.
46      */
47     public static List<ThingUID> getThingUIDs(Device device, Bridge account) {
48         List<ThingUID> thingUIDs = new ArrayList<>();
49         for (Thing thing : account.getThings()) {
50             String deviceId = thing.getConfiguration().as(GardenaDeviceConfig.class).deviceId;
51             if (deviceId == null) {
52                 deviceId = thing.getUID().getId();
53             }
54             if (deviceId.equals(device.id)) {
55                 thingUIDs.add(thing.getUID());
56             }
57         }
58         return thingUIDs;
59     }
60
61     /**
62      * Returns the device id of the Gardena device from the given thing.
63      */
64     public static String getGardenaDeviceId(Thing thing) {
65         String deviceId = thing.getConfiguration().as(GardenaDeviceConfig.class).deviceId;
66         if (deviceId != null) {
67             return deviceId;
68         }
69
70         return thing.getUID().getId();
71     }
72 }