2 * Copyright (c) 2010-2020 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.gardena.internal.util;
15 import static org.openhab.binding.gardena.internal.GardenaBindingConstants.BINDING_ID;
17 import java.util.ArrayList;
18 import java.util.List;
20 import org.openhab.binding.gardena.internal.handler.GardenaDeviceConfig;
21 import org.openhab.binding.gardena.internal.model.Device;
22 import org.openhab.core.thing.Bridge;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingTypeUID;
25 import org.openhab.core.thing.ThingUID;
28 * Utility class for converting between a Thing and a Gardena device.
30 * @author Gerhard Riegler - Initial contribution
32 public class UidUtils {
35 * Generates the ThingUID for the given device in the given account.
37 public static ThingUID generateThingUID(Device device, Bridge account) {
38 ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID, device.getCategory());
39 return new ThingUID(thingTypeUID, account.getUID(), device.getId());
43 * Returns all ThingUIDs for a given device.
45 public static List<ThingUID> getThingUIDs(Device device, Bridge account) {
46 List<ThingUID> thingUIDs = new ArrayList<>();
47 for (Thing thing : account.getThings()) {
48 String deviceId = thing.getConfiguration().as(GardenaDeviceConfig.class).deviceId;
49 if (deviceId == null) {
50 deviceId = thing.getUID().getId();
52 if (deviceId.equals(device.getId())) {
53 thingUIDs.add(thing.getUID());
60 * Returns the device id of the Gardena device from the given thing.
62 public static String getGardenaDeviceId(Thing thing) {
63 String deviceId = thing.getConfiguration().as(GardenaDeviceConfig.class).deviceId;
64 if (deviceId != null) {
68 return thing.getUID().getId();