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.digitalstrom.internal.providers;
16 import java.net.URISyntaxException;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.LinkedList;
21 import java.util.List;
22 import java.util.Locale;
24 import org.openhab.binding.digitalstrom.internal.DigitalSTROMBindingConstants;
25 import org.openhab.binding.digitalstrom.internal.handler.CircuitHandler;
26 import org.openhab.binding.digitalstrom.internal.handler.DeviceHandler;
27 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.MeteringTypeEnum;
28 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.MeteringUnitsEnum;
29 import org.openhab.core.i18n.TranslationProvider;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.binding.ThingTypeProvider;
32 import org.openhab.core.thing.type.ChannelDefinition;
33 import org.openhab.core.thing.type.ChannelDefinitionBuilder;
34 import org.openhab.core.thing.type.ChannelTypeUID;
35 import org.openhab.core.thing.type.ThingType;
36 import org.openhab.core.thing.type.ThingTypeBuilder;
37 import org.osgi.service.component.ComponentContext;
38 import org.osgi.service.component.annotations.Activate;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Deactivate;
41 import org.osgi.service.component.annotations.Reference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * The {@link DsDeviceThingTypeProvider} creates the {@link ThingType}'s for the subclasses of the
47 * {@link GeneralDeviceInformations}. It also adds the {@link ThingTypeUID}'s to the related handlers. So only the
48 * {@link SupportedThingTypes} enum has to be adjusted, if new device types of digitalSTROM should be supported.
49 * Provided the new digitalSTROM devices uses the same mechanism like now.
51 * @author Michael Ochel - Initial contribution
52 * @author Matthias Siegele - Initial contribution
54 @Component(service = ThingTypeProvider.class)
55 public class DsDeviceThingTypeProvider extends BaseDsI18n implements ThingTypeProvider {
58 * Through the {@link SupportedThingTypes} the {@link ThingType}'s will be created. For that the enum name will be
59 * used as thing type id, the first field will set the responsible handler and the last enum field will set the
60 * supporting of the power sensor refresh configurations (config-description with refresh priority setting or not).
62 * @author Michael Ochel - Initial contribution
63 * @author Matthias Siegele - Initial contribution
65 public static enum SupportedThingTypes {
66 // ThingType, responsible ThingHanlder, Device config-description with power-sensors
67 GE(DeviceHandler.class.getSimpleName(), true),
68 GR(DeviceHandler.class.getSimpleName(), false),
69 SW(DeviceHandler.class.getSimpleName(), true),
70 BL(DeviceHandler.class.getSimpleName(), true),
71 dSiSens200(DeviceHandler.class.getSimpleName(), false),
72 circuit(CircuitHandler.class.getSimpleName(), false);
74 private final String handler;
75 private final boolean havePowerSensors;
77 private SupportedThingTypes(String handler, boolean havePowerSensors) {
78 this.handler = handler;
79 this.havePowerSensors = havePowerSensors;
83 private final Logger logger = LoggerFactory.getLogger(DsDeviceThingTypeProvider.class);
85 private static final String DEVICE_WITH_POWER_SENSORS = "thing-type:digitalstrom:deviceWithPowerSensors";
86 private static final String DEVICE_WITHOUT_POWER_SENSORS = "thing-type:digitalstrom:deviceWithoutPowerSensors";
90 protected void activate(ComponentContext componentContext) {
91 super.activate(componentContext);
96 protected void deactivate(ComponentContext componentContext) {
97 super.deactivate(componentContext);
102 protected void setTranslationProvider(TranslationProvider translationProvider) {
103 super.setTranslationProvider(translationProvider);
107 protected void unsetTranslationProvider(TranslationProvider translationProvider) {
108 super.unsetTranslationProvider(translationProvider);
112 protected void init() {
113 for (SupportedThingTypes supportedThingType : SupportedThingTypes.values()) {
114 if (supportedThingType.handler.equals(DeviceHandler.class.getSimpleName())) {
115 DeviceHandler.SUPPORTED_THING_TYPES
116 .add(new ThingTypeUID(DigitalSTROMBindingConstants.BINDING_ID, supportedThingType.toString()));
118 if (supportedThingType.handler.equals(CircuitHandler.class.getSimpleName())) {
119 CircuitHandler.SUPPORTED_THING_TYPES
120 .add(new ThingTypeUID(DigitalSTROMBindingConstants.BINDING_ID, supportedThingType.toString()));
126 public Collection<ThingType> getThingTypes(Locale locale) {
127 List<ThingType> thingTypes = new LinkedList<>();
128 for (SupportedThingTypes supportedThingType : SupportedThingTypes.values()) {
129 thingTypes.add(getThingType(
130 new ThingTypeUID(DigitalSTROMBindingConstants.BINDING_ID, supportedThingType.toString()), locale));
136 public ThingType getThingType(ThingTypeUID thingTypeUID, Locale locale) {
138 SupportedThingTypes supportedThingType = SupportedThingTypes.valueOf(thingTypeUID.getId());
139 ThingTypeBuilder thingTypeBuilder = ThingTypeBuilder
140 .instance(thingTypeUID, getLabelText(thingTypeUID.getId(), locale))
141 .withSupportedBridgeTypeUIDs(
142 Arrays.asList(DigitalSTROMBindingConstants.THING_TYPE_DSS_BRIDGE.getAsString()))
143 .withDescription(getDescText(thingTypeUID.getId(), locale));
145 if (supportedThingType.havePowerSensors) {
146 thingTypeBuilder.withConfigDescriptionURI(new URI(DEVICE_WITH_POWER_SENSORS));
148 thingTypeBuilder.withConfigDescriptionURI(new URI(DEVICE_WITHOUT_POWER_SENSORS));
150 } catch (URISyntaxException e) {
151 logger.debug("An URISyntaxException occurred: ", e);
153 if (SupportedThingTypes.GR.equals(supportedThingType)) {
154 thingTypeBuilder.withChannelDefinitions(Arrays.asList(new ChannelDefinitionBuilder(
155 DsChannelTypeProvider.SHADE,
156 new ChannelTypeUID(DigitalSTROMBindingConstants.BINDING_ID, DsChannelTypeProvider.SHADE))
159 if (SupportedThingTypes.circuit.equals(supportedThingType)) {
160 List<ChannelDefinition> channelDefinitions = new ArrayList<>(3);
161 for (MeteringTypeEnum meteringType : MeteringTypeEnum.values()) {
162 channelDefinitions.add(new ChannelDefinitionBuilder(
163 DsChannelTypeProvider.getMeteringChannelID(meteringType, MeteringUnitsEnum.WH, false),
164 new ChannelTypeUID(DigitalSTROMBindingConstants.BINDING_ID, DsChannelTypeProvider
165 .getMeteringChannelID(meteringType, MeteringUnitsEnum.WH, false))).build());
167 thingTypeBuilder.withChannelDefinitions(channelDefinitions);
169 return thingTypeBuilder.build();
170 } catch (IllegalArgumentException e) {