[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:
parent
ffe252ccd5
commit
35528eda3b
@ -13,6 +13,7 @@
|
|||||||
package org.openhab.binding.miio.internal;
|
package org.openhab.binding.miio.internal;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
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.SIUnits;
|
||||||
import org.openhab.core.library.unit.SmartHomeUnits;
|
import org.openhab.core.library.unit.SmartHomeUnits;
|
||||||
|
|
||||||
|
import tec.uom.se.unit.Units;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enum of the units used in the miio protocol
|
* Enum of the units used in the miio protocol
|
||||||
* Used to find the right {@link javax.measure.Unit} given the string of the unit
|
* 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
|
@NonNullByDefault
|
||||||
public enum MiIoQuantiyTypes {
|
public enum MiIoQuantiyTypes {
|
||||||
|
|
||||||
CELCIUS(SIUnits.CELSIUS),
|
CELCIUS(SIUnits.CELSIUS, "C"),
|
||||||
FAHRENHEIT(ImperialUnits.FAHRENHEIT),
|
FAHRENHEIT(ImperialUnits.FAHRENHEIT),
|
||||||
SECOND(SmartHomeUnits.SECOND),
|
SECOND(SmartHomeUnits.SECOND, "seconds"),
|
||||||
MINUTE(SmartHomeUnits.MINUTE),
|
MINUTE(SmartHomeUnits.MINUTE, "minutes"),
|
||||||
HOUR(SmartHomeUnits.HOUR),
|
HOUR(SmartHomeUnits.HOUR, "hours"),
|
||||||
SECONDS(SmartHomeUnits.SECOND),
|
|
||||||
MINUTES(SmartHomeUnits.MINUTE),
|
|
||||||
HOURS(SmartHomeUnits.HOUR),
|
|
||||||
AMPERE(SmartHomeUnits.AMPERE),
|
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 Unit<?> unit;
|
||||||
|
private final String[] aliasses;
|
||||||
|
|
||||||
private static Map<String, Unit<?>> stringMap = Arrays.stream(values())
|
private static Map<String, Unit<?>> stringMap = Arrays.stream(values())
|
||||||
.collect(Collectors.toMap(Enum::toString, MiIoQuantiyTypes::getUnit));
|
.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.unit = unit;
|
||||||
|
this.aliasses = aliasses;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Unit<?> getUnit() {
|
public Unit<?> getUnit() {
|
||||||
return unit;
|
return unit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String[] getAliasses() {
|
||||||
|
return aliasses;
|
||||||
|
}
|
||||||
|
|
||||||
public static @Nullable Unit<?> get(String unitName) {
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,60 @@
|
|||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static tec.uom.se.unit.Units.SQUARE_METRE;
|
||||||
|
|
||||||
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.openhab.core.library.unit.SmartHomeUnits;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case for {@link MiIoQuantityTypes}
|
||||||
|
*
|
||||||
|
* @author Marcel Verpaalen - Initial contribution
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@NonNullByDefault
|
||||||
|
public class MiIoQuantityTypesTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void UnknownUnitTest() {
|
||||||
|
|
||||||
|
String unitName = "some none existent unit";
|
||||||
|
assertNull(MiIoQuantiyTypes.get(unitName));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void regularsUnitTest() {
|
||||||
|
|
||||||
|
String unitName = "minute";
|
||||||
|
assertEquals(SmartHomeUnits.MINUTE, MiIoQuantiyTypes.get(unitName));
|
||||||
|
|
||||||
|
unitName = "Minute";
|
||||||
|
assertEquals(SmartHomeUnits.MINUTE, MiIoQuantiyTypes.get(unitName));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void aliasUnitsTest() {
|
||||||
|
|
||||||
|
String unitName = "square_meter";
|
||||||
|
assertEquals(SQUARE_METRE, MiIoQuantiyTypes.get(unitName));
|
||||||
|
|
||||||
|
unitName = "Square_meter";
|
||||||
|
assertEquals(SQUARE_METRE, MiIoQuantiyTypes.get(unitName));
|
||||||
|
|
||||||
|
unitName = "squaremeter";
|
||||||
|
assertEquals(SQUARE_METRE, MiIoQuantiyTypes.get(unitName));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user