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.converter.type;
15 import org.openhab.binding.homematic.internal.converter.ConverterException;
16 import org.openhab.binding.homematic.internal.model.HmDatapoint;
17 import org.openhab.binding.homematic.internal.model.HmDatapointInfo;
18 import org.openhab.core.library.types.StringType;
19 import org.openhab.core.types.Type;
22 * Converts between a Homematic datapoint value and an openHAB StringType.
24 * @author Gerhard Riegler - Initial contribution
26 public class StringTypeConverter extends AbstractTypeConverter<StringType> {
28 protected boolean toBindingValidation(HmDatapoint dp, Class<? extends Type> typeClass) {
29 return (dp.isStringType() || dp.isEnumType()) && typeClass.isAssignableFrom(StringType.class);
33 protected Object toBinding(StringType type, HmDatapoint dp) throws ConverterException {
34 if (dp.isStringType()) {
35 return type.toString();
37 int idx = dp.getOptionIndex(type.toString());
39 throw new ConverterException(String.format("Option value '%s' not found in datapoint '%s'",
40 type.toString(), new HmDatapointInfo(dp)));
47 protected boolean fromBindingValidation(HmDatapoint dp) {
48 return (dp.isStringType()) || (dp.isEnumType() && dp.getValue() instanceof Number);
52 protected StringType fromBinding(HmDatapoint dp) throws ConverterException {
53 if (dp.isStringType()) {
54 return new StringType(String.valueOf(dp.getValue()));
56 String value = dp.getOptionValue();
58 throw new ConverterException(String.format("Option for value '%s' not found in datapoint '%s'",
59 dp.getValue(), new HmDatapointInfo(dp)));
61 return new StringType(value);