[miio] Implement alternative MiIoQuantityTypes (#9203)
* [miio] Implement alternative MiIoQuantityTypes * Improved way avoiding multiple entries for Unit * Test cases for the MiIoQuantityTypes class * Apply suggestions from code review Signed-off-by: Marcel Verpaalen <marcel@verpaalen.com> Co-authored-by: Connor Petty <mistercpp2000+gitsignoff@gmail.com>
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
package org.openhab.binding.miio.internal;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -24,6 +25,8 @@ import org.openhab.core.library.unit.ImperialUnits;
|
||||
import org.openhab.core.library.unit.SIUnits;
|
||||
import org.openhab.core.library.unit.SmartHomeUnits;
|
||||
|
||||
import tec.uom.se.unit.Units;
|
||||
|
||||
/**
|
||||
* Enum of the units used in the miio protocol
|
||||
* Used to find the right {@link javax.measure.Unit} given the string of the unit
|
||||
@@ -33,31 +36,50 @@ import org.openhab.core.library.unit.SmartHomeUnits;
|
||||
@NonNullByDefault
|
||||
public enum MiIoQuantiyTypes {
|
||||
|
||||
CELCIUS(SIUnits.CELSIUS),
|
||||
CELCIUS(SIUnits.CELSIUS, "C"),
|
||||
FAHRENHEIT(ImperialUnits.FAHRENHEIT),
|
||||
SECOND(SmartHomeUnits.SECOND),
|
||||
MINUTE(SmartHomeUnits.MINUTE),
|
||||
HOUR(SmartHomeUnits.HOUR),
|
||||
SECONDS(SmartHomeUnits.SECOND),
|
||||
MINUTES(SmartHomeUnits.MINUTE),
|
||||
HOURS(SmartHomeUnits.HOUR),
|
||||
SECOND(SmartHomeUnits.SECOND, "seconds"),
|
||||
MINUTE(SmartHomeUnits.MINUTE, "minutes"),
|
||||
HOUR(SmartHomeUnits.HOUR, "hours"),
|
||||
AMPERE(SmartHomeUnits.AMPERE),
|
||||
WATT(SmartHomeUnits.WATT);
|
||||
WATT(SmartHomeUnits.WATT),
|
||||
SQUARE_METRE(Units.SQUARE_METRE, "square_meter", "squaremeter"),
|
||||
PERCENT(SmartHomeUnits.PERCENT);
|
||||
|
||||
private final Unit<?> unit;
|
||||
private final String[] aliasses;
|
||||
|
||||
private static Map<String, Unit<?>> stringMap = Arrays.stream(values())
|
||||
.collect(Collectors.toMap(Enum::toString, MiIoQuantiyTypes::getUnit));
|
||||
|
||||
private MiIoQuantiyTypes(Unit<?> unit) {
|
||||
private static Map<String, Unit<?>> aliasMap() {
|
||||
Map<String, Unit<?>> aliassesMap = new HashMap<>();
|
||||
for (MiIoQuantiyTypes miIoQuantiyType : values()) {
|
||||
for (String alias : miIoQuantiyType.getAliasses()) {
|
||||
aliassesMap.put(alias.toLowerCase(), miIoQuantiyType.getUnit());
|
||||
}
|
||||
}
|
||||
return aliassesMap;
|
||||
}
|
||||
|
||||
private MiIoQuantiyTypes(Unit<?> unit, String... aliasses) {
|
||||
this.unit = unit;
|
||||
this.aliasses = aliasses;
|
||||
}
|
||||
|
||||
public Unit<?> getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public String[] getAliasses() {
|
||||
return aliasses;
|
||||
}
|
||||
|
||||
public static @Nullable Unit<?> get(String unitName) {
|
||||
return stringMap.get(unitName.toUpperCase());
|
||||
Unit<?> unit = stringMap.get(unitName.toUpperCase());
|
||||
if (unit == null) {
|
||||
unit = aliasMap().get(unitName.toLowerCase());
|
||||
}
|
||||
return unit;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user