2 * Copyright (c) 2010-2022 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.homematic.internal.type;
15 import static org.openhab.binding.homematic.internal.HomematicBindingConstants.BINDING_ID;
17 import org.openhab.binding.homematic.internal.model.HmChannel;
18 import org.openhab.binding.homematic.internal.model.HmDatapoint;
19 import org.openhab.binding.homematic.internal.model.HmDatapointInfo;
20 import org.openhab.binding.homematic.internal.model.HmDevice;
21 import org.openhab.binding.homematic.internal.model.HmGatewayInfo;
22 import org.openhab.binding.homematic.internal.model.HmParamsetType;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.openhab.core.thing.type.ChannelGroupTypeUID;
29 import org.openhab.core.thing.type.ChannelTypeUID;
32 * Utility class for generating some UIDs.
34 * @author Gerhard Riegler - Initial contribution
36 public class UidUtils {
39 * Generates the ThingTypeUID for the given device. If it's a Homegear device, add a prefix because a Homegear
40 * device has more datapoints.
42 public static ThingTypeUID generateThingTypeUID(HmDevice device) {
43 if (!device.isGatewayExtras() && device.getGatewayId().equals(HmGatewayInfo.ID_HOMEGEAR)) {
44 return new ThingTypeUID(BINDING_ID, String.format("HG-%s", device.getType()));
46 return new ThingTypeUID(BINDING_ID, device.getType());
51 * Generates the ChannelTypeUID for the given datapoint with deviceType, channelNumber and datapointName.
53 public static ChannelTypeUID generateChannelTypeUID(HmDatapoint dp) {
54 return new ChannelTypeUID(BINDING_ID, String.format("%s_%s_%s", dp.getChannel().getDevice().getType(),
55 dp.getChannel().getNumber(), dp.getName()));
59 * Generates the ChannelTypeUID for the given datapoint with deviceType and channelNumber.
61 public static ChannelGroupTypeUID generateChannelGroupTypeUID(HmChannel channel) {
62 return new ChannelGroupTypeUID(BINDING_ID,
63 String.format("%s_%s", channel.getDevice().getType(), channel.getNumber()));
67 * Generates the ThingUID for the given device in the given bridge.
69 public static ThingUID generateThingUID(HmDevice device, Bridge bridge) {
70 ThingTypeUID thingTypeUID = generateThingTypeUID(device);
71 return new ThingUID(thingTypeUID, bridge.getUID(), device.getAddress());
75 * Generates the ChannelUID for the given datapoint with channelNumber and datapointName.
77 public static ChannelUID generateChannelUID(HmDatapoint dp, ThingUID thingUID) {
78 return new ChannelUID(thingUID, String.valueOf(dp.getChannel().getNumber()), dp.getName());
82 * Generates the HmDatapointInfo for the given thing and channelUID.
84 public static HmDatapointInfo createHmDatapointInfo(ChannelUID channelUID) {
87 String groupID = channelUID.getGroupId();
88 value = groupID == null ? 0 : Integer.parseInt(groupID);
89 } catch (NumberFormatException e) {
92 return new HmDatapointInfo(channelUID.getThingUID().getId(), HmParamsetType.VALUES, value,
93 channelUID.getIdWithoutGroup());
97 * Returns the address of the Homematic device from the given thing.
99 public static String getHomematicAddress(Thing thing) {
100 return thing.getUID().getId();