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.homematic.internal.type;
15 import static org.openhab.binding.homematic.internal.HomematicBindingConstants.*;
16 import static org.openhab.binding.homematic.internal.misc.HomematicConstants.*;
18 import java.math.BigDecimal;
20 import java.net.URISyntaxException;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.List;
28 import org.apache.commons.lang.ObjectUtils;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.commons.lang.WordUtils;
31 import org.openhab.binding.homematic.internal.model.HmChannel;
32 import org.openhab.binding.homematic.internal.model.HmDatapoint;
33 import org.openhab.binding.homematic.internal.model.HmDevice;
34 import org.openhab.binding.homematic.internal.model.HmParamsetType;
35 import org.openhab.binding.homematic.internal.type.MetadataUtils.OptionsBuilder;
36 import org.openhab.core.config.core.ConfigDescriptionBuilder;
37 import org.openhab.core.config.core.ConfigDescriptionParameter;
38 import org.openhab.core.config.core.ConfigDescriptionParameterBuilder;
39 import org.openhab.core.config.core.ConfigDescriptionParameterGroup;
40 import org.openhab.core.config.core.ConfigDescriptionParameterGroupBuilder;
41 import org.openhab.core.config.core.ParameterOption;
42 import org.openhab.core.thing.DefaultSystemChannelTypeProvider;
43 import org.openhab.core.thing.Thing;
44 import org.openhab.core.thing.ThingTypeUID;
45 import org.openhab.core.thing.type.ChannelDefinition;
46 import org.openhab.core.thing.type.ChannelDefinitionBuilder;
47 import org.openhab.core.thing.type.ChannelGroupDefinition;
48 import org.openhab.core.thing.type.ChannelGroupType;
49 import org.openhab.core.thing.type.ChannelGroupTypeBuilder;
50 import org.openhab.core.thing.type.ChannelGroupTypeUID;
51 import org.openhab.core.thing.type.ChannelType;
52 import org.openhab.core.thing.type.ChannelTypeBuilder;
53 import org.openhab.core.thing.type.ChannelTypeUID;
54 import org.openhab.core.thing.type.ThingType;
55 import org.openhab.core.thing.type.ThingTypeBuilder;
56 import org.openhab.core.types.EventDescription;
57 import org.openhab.core.types.EventOption;
58 import org.openhab.core.types.StateDescriptionFragmentBuilder;
59 import org.openhab.core.types.StateOption;
60 import org.osgi.service.component.annotations.Activate;
61 import org.osgi.service.component.annotations.Component;
62 import org.osgi.service.component.annotations.Reference;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
67 * Generates ThingTypes based on metadata from a Homematic gateway.
69 * @author Gerhard Riegler - Initial contribution
72 public class HomematicTypeGeneratorImpl implements HomematicTypeGenerator {
73 private final Logger logger = LoggerFactory.getLogger(HomematicTypeGeneratorImpl.class);
74 private static URI configDescriptionUriChannel;
76 private HomematicThingTypeProvider thingTypeProvider;
77 private HomematicChannelTypeProvider channelTypeProvider;
78 private HomematicChannelGroupTypeProvider channelGroupTypeProvider;
79 private HomematicConfigDescriptionProvider configDescriptionProvider;
80 private final Map<String, Set<String>> firmwaresByType = new HashMap<>();
82 private static final String[] IGNORE_DATAPOINT_NAMES = new String[] { DATAPOINT_NAME_AES_KEY,
83 VIRTUAL_DATAPOINT_NAME_RELOAD_FROM_GATEWAY };
85 public HomematicTypeGeneratorImpl() {
87 configDescriptionUriChannel = new URI(CONFIG_DESCRIPTION_URI_CHANNEL);
88 } catch (Exception ex) {
89 logger.warn("Can't create ConfigDescription URI '{}', ConfigDescription for channels not avilable!",
90 CONFIG_DESCRIPTION_URI_CHANNEL);
95 protected void setThingTypeProvider(HomematicThingTypeProvider thingTypeProvider) {
96 this.thingTypeProvider = thingTypeProvider;
99 protected void unsetThingTypeProvider(HomematicThingTypeProvider thingTypeProvider) {
100 this.thingTypeProvider = null;
104 protected void setChannelTypeProvider(HomematicChannelTypeProvider channelTypeProvider) {
105 this.channelTypeProvider = channelTypeProvider;
108 protected void unsetChannelTypeProvider(HomematicChannelTypeProvider channelTypeProvider) {
109 this.channelTypeProvider = null;
113 protected void setChannelGroupTypeProvider(HomematicChannelGroupTypeProvider channelGroupTypeProvider) {
114 this.channelGroupTypeProvider = channelGroupTypeProvider;
117 protected void unsetChannelGroupTypeProvider(HomematicChannelGroupTypeProvider channelGroupTypeProvider) {
118 this.channelGroupTypeProvider = null;
122 protected void setConfigDescriptionProvider(HomematicConfigDescriptionProvider configDescriptionProvider) {
123 this.configDescriptionProvider = configDescriptionProvider;
126 protected void unsetConfigDescriptionProvider(HomematicConfigDescriptionProvider configDescriptionProvider) {
127 this.configDescriptionProvider = null;
132 public void initialize() {
133 MetadataUtils.initialize();
137 public void generate(HmDevice device) {
138 if (thingTypeProvider != null) {
139 ThingTypeUID thingTypeUID = UidUtils.generateThingTypeUID(device);
140 ThingType tt = thingTypeProvider.getInternalThingType(thingTypeUID);
142 if (tt == null || device.isGatewayExtras()) {
143 logger.debug("Generating ThingType for device '{}' with {} datapoints", device.getType(),
144 device.getDatapointCount());
146 List<ChannelGroupType> groupTypes = new ArrayList<>();
147 for (HmChannel channel : device.getChannels()) {
148 List<ChannelDefinition> channelDefinitions = new ArrayList<>();
149 // Omit thing channel definitions for reconfigurable channels;
150 // those will be populated dynamically during thing initialization
151 if (!channel.isReconfigurable()) {
153 for (HmDatapoint dp : channel.getDatapoints()) {
154 if (!isIgnoredDatapoint(dp) && dp.getParamsetType() == HmParamsetType.VALUES) {
155 ChannelTypeUID channelTypeUID = UidUtils.generateChannelTypeUID(dp);
156 ChannelType channelType = channelTypeProvider.getInternalChannelType(channelTypeUID);
157 if (channelType == null) {
158 channelType = createChannelType(dp, channelTypeUID);
159 channelTypeProvider.addChannelType(channelType);
162 ChannelDefinition channelDef = new ChannelDefinitionBuilder(dp.getName(),
163 channelType.getUID()).build();
164 channelDefinitions.add(channelDef);
170 ChannelGroupTypeUID groupTypeUID = UidUtils.generateChannelGroupTypeUID(channel);
171 ChannelGroupType groupType = channelGroupTypeProvider.getInternalChannelGroupType(groupTypeUID);
172 if (groupType == null || device.isGatewayExtras()) {
173 String groupLabel = String.format("%s",
174 WordUtils.capitalizeFully(StringUtils.replace(channel.getType(), "_", " ")));
175 groupType = ChannelGroupTypeBuilder.instance(groupTypeUID, groupLabel)
176 .withChannelDefinitions(channelDefinitions).build();
177 channelGroupTypeProvider.addChannelGroupType(groupType);
178 groupTypes.add(groupType);
182 tt = createThingType(device, groupTypes);
183 thingTypeProvider.addThingType(tt);
190 public void validateFirmwares() {
191 for (String deviceType : firmwaresByType.keySet()) {
192 Set<String> firmwares = firmwaresByType.get(deviceType);
193 if (firmwares.size() > 1) {
195 "Multiple firmware versions for device type '{}' found ({}). "
196 + "Make sure, all devices of the same type have the same firmware version, "
197 + "otherwise you MAY have channel and/or datapoint errors in the logfile",
198 deviceType, StringUtils.join(firmwares, ", "));
204 * Adds the firmware version for validation.
206 private void addFirmware(HmDevice device) {
207 if (!StringUtils.equals(device.getFirmware(), "?") && !DEVICE_TYPE_VIRTUAL.equals(device.getType())
208 && !DEVICE_TYPE_VIRTUAL_WIRED.equals(device.getType())) {
209 Set<String> firmwares = firmwaresByType.get(device.getType());
210 if (firmwares == null) {
211 firmwares = new HashSet<>();
212 firmwaresByType.put(device.getType(), firmwares);
214 firmwares.add(device.getFirmware());
219 * Creates the ThingType for the given device.
221 private ThingType createThingType(HmDevice device, List<ChannelGroupType> groupTypes) {
222 String label = MetadataUtils.getDeviceName(device);
223 String description = String.format("%s (%s)", label, device.getType());
225 List<String> supportedBridgeTypeUids = new ArrayList<>();
226 supportedBridgeTypeUids.add(THING_TYPE_BRIDGE.toString());
227 ThingTypeUID thingTypeUID = UidUtils.generateThingTypeUID(device);
229 Map<String, String> properties = new HashMap<>();
230 properties.put(Thing.PROPERTY_VENDOR, PROPERTY_VENDOR_NAME);
231 properties.put(Thing.PROPERTY_MODEL_ID, device.getType());
233 URI configDescriptionURI = getConfigDescriptionURI(device);
234 if (configDescriptionProvider.getInternalConfigDescription(configDescriptionURI) == null) {
235 generateConfigDescription(device, configDescriptionURI);
238 List<ChannelGroupDefinition> groupDefinitions = new ArrayList<>();
239 for (ChannelGroupType groupType : groupTypes) {
240 String id = StringUtils.substringAfterLast(groupType.getUID().getId(), "_");
241 groupDefinitions.add(new ChannelGroupDefinition(id, groupType.getUID()));
244 return ThingTypeBuilder.instance(thingTypeUID, label).withSupportedBridgeTypeUIDs(supportedBridgeTypeUids)
245 .withDescription(description).withChannelGroupDefinitions(groupDefinitions).withProperties(properties)
246 .withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER).withConfigDescriptionURI(configDescriptionURI)
251 * Creates the ChannelType for the given datapoint.
253 private ChannelType createChannelType(HmDatapoint dp, ChannelTypeUID channelTypeUID) {
254 ChannelType channelType;
255 if (dp.getName().equals(DATAPOINT_NAME_LOWBAT) || dp.getName().equals(DATAPOINT_NAME_LOWBAT_IP)) {
256 channelType = DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_LOW_BATTERY;
257 } else if (dp.getName().equals(VIRTUAL_DATAPOINT_NAME_SIGNAL_STRENGTH)) {
258 channelType = DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_SIGNAL_STRENGTH;
259 } else if (dp.getName().equals(VIRTUAL_DATAPOINT_NAME_BUTTON)) {
260 channelType = DefaultSystemChannelTypeProvider.SYSTEM_BUTTON;
262 String itemType = MetadataUtils.getItemType(dp);
263 String category = MetadataUtils.getCategory(dp, itemType);
264 String label = MetadataUtils.getLabel(dp);
265 String description = MetadataUtils.getDatapointDescription(dp);
267 List<StateOption> options = null;
268 if (dp.isEnumType()) {
269 options = MetadataUtils.generateOptions(dp, new OptionsBuilder<StateOption>() {
271 public StateOption createOption(String value, String description) {
272 return new StateOption(value, description);
277 StateDescriptionFragmentBuilder stateFragment = StateDescriptionFragmentBuilder.create();
278 if (dp.isNumberType()) {
279 BigDecimal min = MetadataUtils.createBigDecimal(dp.getMinValue());
280 BigDecimal max = MetadataUtils.createBigDecimal(dp.getMaxValue());
281 BigDecimal step = MetadataUtils.createBigDecimal(dp.getStep());
282 if (ITEM_TYPE_DIMMER.equals(itemType) && dp.getMaxValue().doubleValue() == 1.01d) {
283 // For dimmers with a max value of 1.01 the values must be corrected
284 min = MetadataUtils.createBigDecimal(0);
285 max = MetadataUtils.createBigDecimal(100);
286 step = MetadataUtils.createBigDecimal(1);
290 .createBigDecimal(dp.isFloatType() ? Float.valueOf(0.1f) : Long.valueOf(1L));
293 stateFragment.withMinimum(min).withMaximum(max).withStep(step)
294 .withPattern(MetadataUtils.getStatePattern(dp)).withReadOnly(dp.isReadOnly());
296 stateFragment.withPattern(MetadataUtils.getStatePattern(dp)).withReadOnly(dp.isReadOnly());
298 if (options != null) {
299 stateFragment.withOptions(options);
302 ChannelTypeBuilder channelTypeBuilder;
303 EventDescription eventDescription = null;
304 if (dp.isTrigger()) {
305 eventDescription = new EventDescription(
306 MetadataUtils.generateOptions(dp, new OptionsBuilder<EventOption>() {
308 public EventOption createOption(String value, String description) {
309 return new EventOption(value, description);
312 channelTypeBuilder = ChannelTypeBuilder.trigger(channelTypeUID, label)
313 .withEventDescription(eventDescription);
315 channelTypeBuilder = ChannelTypeBuilder.state(channelTypeUID, label, itemType)
316 .withStateDescriptionFragment(stateFragment.build());
318 channelType = channelTypeBuilder.isAdvanced(!MetadataUtils.isStandard(dp)).withDescription(description)
319 .withCategory(category).withConfigDescriptionURI(configDescriptionUriChannel).build();
324 private void generateConfigDescription(HmDevice device, URI configDescriptionURI) {
325 List<ConfigDescriptionParameter> parms = new ArrayList<>();
326 List<ConfigDescriptionParameterGroup> groups = new ArrayList<>();
328 for (HmChannel channel : device.getChannels()) {
329 String groupName = "HMG_" + channel.getNumber();
330 String groupLabel = MetadataUtils.getDescription("CHANNEL_NAME") + " " + channel.getNumber();
331 groups.add(ConfigDescriptionParameterGroupBuilder.create(groupName).withLabel(groupLabel).build());
333 for (HmDatapoint dp : channel.getDatapoints()) {
334 if (dp.getParamsetType() == HmParamsetType.MASTER) {
335 ConfigDescriptionParameterBuilder builder = ConfigDescriptionParameterBuilder.create(
336 MetadataUtils.getParameterName(dp), MetadataUtils.getConfigDescriptionParameterType(dp));
338 builder.withLabel(MetadataUtils.getLabel(dp));
339 builder.withDefault(ObjectUtils.toString(dp.getDefaultValue()));
340 builder.withDescription(MetadataUtils.getDatapointDescription(dp));
342 if (dp.isEnumType()) {
343 builder.withLimitToOptions(dp.isEnumType());
344 List<ParameterOption> options = MetadataUtils.generateOptions(dp,
345 new OptionsBuilder<ParameterOption>() {
347 public ParameterOption createOption(String value, String description) {
348 return new ParameterOption(value, description);
351 builder.withOptions(options);
354 if (dp.isNumberType()) {
355 builder.withMinimum(MetadataUtils.createBigDecimal(dp.getMinValue()));
356 builder.withMaximum(MetadataUtils.createBigDecimal(dp.getMaxValue()));
357 builder.withStepSize(MetadataUtils
358 .createBigDecimal(dp.isFloatType() ? Float.valueOf(0.1f) : Long.valueOf(1L)));
359 builder.withUnitLabel(MetadataUtils.getUnit(dp));
362 builder.withGroupName(groupName);
363 parms.add(builder.build());
367 configDescriptionProvider.addConfigDescription(ConfigDescriptionBuilder.create(configDescriptionURI)
368 .withParameters(parms).withParameterGroups(groups).build());
371 private URI getConfigDescriptionURI(HmDevice device) {
374 String.format("%s:%s", CONFIG_DESCRIPTION_URI_THING_PREFIX, UidUtils.generateThingTypeUID(device)));
375 } catch (URISyntaxException ex) {
376 logger.warn("Can't create configDescriptionURI for device type {}", device.getType());
382 * Returns true, if the given datapoint can be ignored for metadata generation.
384 public static boolean isIgnoredDatapoint(HmDatapoint dp) {
385 return StringUtils.indexOfAny(dp.getName(), IGNORE_DATAPOINT_NAMES) != -1;