forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[miio] dynamically generate channelTypes (openhab#9158)
* [miio] dynamically generate channelTypes Simplify the json database creation for new models and less chance for errors Related to openhab#7276 Signed-off-by: Marcel Verpaalen <marcel@verpaalen.com> Co-authored-by: Connor Petty <mistercpp2000+gitsignoff@gmail.com>
- Loading branch information
Showing
6 changed files
with
353 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
....miio/src/main/java/org/openhab/binding/miio/internal/basic/BasicChannelTypeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* Copyright (c) 2010-2020 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.miio.internal.basic; | ||
|
||
import static org.openhab.binding.miio.internal.MiIoBindingConstants.BINDING_ID; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.thing.type.ChannelType; | ||
import org.openhab.core.thing.type.ChannelTypeBuilder; | ||
import org.openhab.core.thing.type.ChannelTypeProvider; | ||
import org.openhab.core.thing.type.ChannelTypeUID; | ||
import org.openhab.core.thing.type.StateChannelTypeBuilder; | ||
import org.openhab.core.types.StateDescriptionFragmentBuilder; | ||
import org.openhab.core.types.StateOption; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Provide channelTypes for Mi IO Basic devices | ||
* | ||
* @author Marcel Verpaalen - Initial contribution | ||
*/ | ||
@Component(service = { ChannelTypeProvider.class, BasicChannelTypeProvider.class }) | ||
@NonNullByDefault | ||
public class BasicChannelTypeProvider implements ChannelTypeProvider { | ||
private final Map<String, ChannelType> channelTypes = new ConcurrentHashMap<>(); | ||
private final Logger logger = LoggerFactory.getLogger(BasicChannelTypeProvider.class); | ||
|
||
@Override | ||
public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) { | ||
return channelTypes.values(); | ||
} | ||
|
||
@Override | ||
public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) { | ||
if (channelTypes.containsKey(channelTypeUID.getAsString())) { | ||
return channelTypes.get(channelTypeUID.getAsString()); | ||
} | ||
return null; | ||
} | ||
|
||
public void addChannelType(MiIoBasicChannel miChannel, String model) { | ||
ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID, | ||
model.toUpperCase().replace(".", "_") + "_" + miChannel.getChannel()); | ||
logger.debug("Adding channel definitions for {} -> {}", channelTypeUID, miChannel.getFriendlyName()); | ||
try { | ||
final StateDescriptionDTO stateDescription = miChannel.getStateDescription(); | ||
StateChannelTypeBuilder channelTypeBuilder = ChannelTypeBuilder.state(channelTypeUID, | ||
miChannel.getFriendlyName(), miChannel.getType()); // | ||
if (stateDescription != null) { | ||
StateDescriptionFragmentBuilder sdf = StateDescriptionFragmentBuilder.create(); | ||
final BigDecimal maximum = stateDescription.getMaximum(); | ||
if (maximum != null) { | ||
sdf.withMaximum(maximum); | ||
} | ||
final BigDecimal minimum = stateDescription.getMinimum(); | ||
if (minimum != null) { | ||
sdf.withMinimum(minimum); | ||
} | ||
final BigDecimal step = stateDescription.getStep(); | ||
if (step != null) { | ||
sdf.withStep(step); | ||
} | ||
final String pattern = stateDescription.getPattern(); | ||
if (pattern != null) { | ||
sdf.withPattern(pattern); | ||
} | ||
final Boolean readOnly = stateDescription.getReadOnly(); | ||
if (readOnly != null) { | ||
sdf.withReadOnly(readOnly); | ||
} | ||
List<OptionsValueListDTO> optionList = stateDescription.getOptions(); | ||
if (optionList != null) { | ||
List<StateOption> options = new ArrayList<>(); | ||
for (OptionsValueListDTO option : optionList) { | ||
String value = option.getValue(); | ||
if (value != null) { | ||
options.add(new StateOption(value, option.getLabel())); | ||
} | ||
} | ||
sdf.withOptions(options); | ||
} | ||
channelTypeBuilder.withStateDescriptionFragment(sdf.build()); | ||
logger.debug("added stateDescription: {}", sdf); | ||
} | ||
final String category = miChannel.getCategory(); | ||
if (category != null) { | ||
channelTypeBuilder.withCategory(category); | ||
} | ||
final LinkedHashSet<String> tags = miChannel.getTags(); | ||
if (tags != null && tags.size() > 0) { | ||
channelTypeBuilder.withTags(tags); | ||
} | ||
channelTypes.put(channelTypeUID.getAsString(), channelTypeBuilder.build()); | ||
} catch (Exception e) { | ||
logger.warn("Failed creating channelType {}: {} ", channelTypeUID, e.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...nding.miio/src/main/java/org/openhab/binding/miio/internal/basic/OptionsValueListDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Copyright (c) 2010-2020 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.miio.internal.basic; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
import com.google.gson.annotations.Expose; | ||
import com.google.gson.annotations.SerializedName; | ||
|
||
/** | ||
* Mapping properties from json for channel options | ||
* | ||
* @author Marcel Verpaalen - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class OptionsValueListDTO { | ||
|
||
@SerializedName("value") | ||
@Expose | ||
public @Nullable String value; | ||
|
||
@SerializedName("label") | ||
@Expose | ||
public @Nullable String label; | ||
|
||
public @Nullable String getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(String value) { | ||
this.value = value; | ||
} | ||
|
||
public @Nullable String getLabel() { | ||
return label; | ||
} | ||
|
||
public void setLabel(String label) { | ||
this.label = label; | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
...nding.miio/src/main/java/org/openhab/binding/miio/internal/basic/StateDescriptionDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* Copyright (c) 2010-2020 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.miio.internal.basic; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
import com.google.gson.annotations.Expose; | ||
import com.google.gson.annotations.SerializedName; | ||
|
||
/** | ||
* Mapping properties from json for state descriptions | ||
* | ||
* @author Marcel Verpaalen - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class StateDescriptionDTO { | ||
|
||
@SerializedName("minimum") | ||
@Expose | ||
@Nullable | ||
private BigDecimal minimum; | ||
@SerializedName("maximum") | ||
@Expose | ||
@Nullable | ||
private BigDecimal maximum; | ||
@SerializedName("step") | ||
@Expose | ||
@Nullable | ||
private BigDecimal step; | ||
@SerializedName("pattern") | ||
@Expose | ||
@Nullable | ||
private String pattern; | ||
@SerializedName("readOnly") | ||
@Expose | ||
@Nullable | ||
private Boolean readOnly; | ||
@SerializedName("options") | ||
@Expose | ||
@Nullable | ||
public List<OptionsValueListDTO> options = null; | ||
|
||
@Nullable | ||
public BigDecimal getMinimum() { | ||
return minimum; | ||
} | ||
|
||
public void setMinimum(BigDecimal minimum) { | ||
this.minimum = minimum; | ||
} | ||
|
||
@Nullable | ||
public BigDecimal getMaximum() { | ||
return maximum; | ||
} | ||
|
||
public void setMaximum(BigDecimal maximum) { | ||
this.maximum = maximum; | ||
} | ||
|
||
@Nullable | ||
public BigDecimal getStep() { | ||
return step; | ||
} | ||
|
||
public void setStep(BigDecimal step) { | ||
this.step = step; | ||
} | ||
|
||
@Nullable | ||
public String getPattern() { | ||
return pattern; | ||
} | ||
|
||
public void setPattern(String pattern) { | ||
this.pattern = pattern; | ||
} | ||
|
||
@Nullable | ||
public Boolean getReadOnly() { | ||
return readOnly; | ||
} | ||
|
||
public void setReadOnly(Boolean readOnly) { | ||
this.readOnly = readOnly; | ||
} | ||
|
||
@Nullable | ||
public List<OptionsValueListDTO> getOptions() { | ||
return options; | ||
} | ||
|
||
public void setOptions(List<OptionsValueListDTO> options) { | ||
this.options = options; | ||
} | ||
} |
Oops, something went wrong.