Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add defaultUnit metadata for NumberItem #3248

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
package org.openhab.core.automation.internal.module.handler;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.time.ZoneId;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

import javax.measure.quantity.Temperature;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -34,6 +37,7 @@
import org.openhab.core.automation.util.ConditionBuilder;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.i18n.UnitProvider;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemNotFoundException;
import org.openhab.core.items.ItemRegistry;
Expand Down Expand Up @@ -77,7 +81,9 @@ public ParameterSet(String itemType, String comparisonState, State itemState, bo
((NumberItem) item).setState(itemState);
break;
case "Number:Temperature":
item = new NumberItem("Number:Temperature", ITEM_NAME);
UnitProvider unitProviderMock = mock(UnitProvider.class);
when(unitProviderMock.getUnit(Temperature.class)).thenReturn(SIUnits.CELSIUS);
item = new NumberItem("Number:Temperature", ITEM_NAME, unitProviderMock);
((NumberItem) item).setState(itemState);
break;
case "Dimmer":
Expand All @@ -103,7 +109,7 @@ public static Collection<Object[]> equalsParameters() {
{ new ParameterSet("Number", "5", new DecimalType(5), true) }, //
{ new ParameterSet("Number:Temperature", "5 °C", new DecimalType(23), false) }, //
{ new ParameterSet("Number:Temperature", "5 °C", new DecimalType(5), false) }, //
{ new ParameterSet("Number:Temperature", "0", new QuantityType<>(), true) }, //
{ new ParameterSet("Number:Temperature", "0", new DecimalType(), true) }, //
{ new ParameterSet("Number:Temperature", "5", new QuantityType<>(23, SIUnits.CELSIUS), false) }, //
{ new ParameterSet("Number:Temperature", "5", new QuantityType<>(5, SIUnits.CELSIUS), false) }, //
{ new ParameterSet("Number:Temperature", "5 °C", new QuantityType<>(23, SIUnits.CELSIUS), false) }, //
Expand All @@ -119,7 +125,7 @@ public static Collection<Object[]> greaterThanParameters() {
{ new ParameterSet("Number", "5", new DecimalType(5), false) }, //
{ new ParameterSet("Number", "5 °C", new DecimalType(23), true) }, //
{ new ParameterSet("Number", "5 °C", new DecimalType(5), false) }, //
{ new ParameterSet("Number:Temperature", "0", new QuantityType<>(), false) }, //
{ new ParameterSet("Number:Temperature", "0", new DecimalType(), false) }, //
{ new ParameterSet("Number:Temperature", "5", new QuantityType<>(23, SIUnits.CELSIUS), true) }, //
{ new ParameterSet("Number:Temperature", "5", new QuantityType<>(5, SIUnits.CELSIUS), false) }, //
{ new ParameterSet("Number:Temperature", "5 °C", new QuantityType<>(23, SIUnits.CELSIUS), true) }, //
Expand All @@ -138,7 +144,7 @@ public static Collection<Object[]> greaterThanOrEqualsParameters() {
{ new ParameterSet("Number", "5 °C", new DecimalType(23), true) }, //
{ new ParameterSet("Number", "5 °C", new DecimalType(5), true) }, //
{ new ParameterSet("Number", "5 °C", new DecimalType(4), false) }, //
{ new ParameterSet("Number:Temperature", "0", new QuantityType<>(), true) }, //
{ new ParameterSet("Number:Temperature", "0", new DecimalType(), true) }, //
{ new ParameterSet("Number:Temperature", "5", new QuantityType<>(23, SIUnits.CELSIUS), true) }, //
{ new ParameterSet("Number:Temperature", "5", new QuantityType<>(5, SIUnits.CELSIUS), true) }, //
{ new ParameterSet("Number:Temperature", "5", new QuantityType<>(4, SIUnits.CELSIUS), false) }, //
Expand All @@ -159,7 +165,7 @@ public static Collection<Object[]> lessThanParameters() {
{ new ParameterSet("Number", "5", new DecimalType(4), true) }, //
{ new ParameterSet("Number", "5 °C", new DecimalType(23), false) }, //
{ new ParameterSet("Number", "5 °C", new DecimalType(4), true) }, //
{ new ParameterSet("Number:Temperature", "0", new QuantityType<>(), false) }, //
{ new ParameterSet("Number:Temperature", "0", new DecimalType(), false) }, //
{ new ParameterSet("Number:Temperature", "5", new QuantityType<>(23, SIUnits.CELSIUS), false) }, //
{ new ParameterSet("Number:Temperature", "5", new QuantityType<>(4, SIUnits.CELSIUS), true) }, //
{ new ParameterSet("Number:Temperature", "5 °C", new QuantityType<>(23, SIUnits.CELSIUS), false) }, //
Expand All @@ -179,7 +185,7 @@ public static Collection<Object[]> lessThanOrEqualsParameters() {
{ new ParameterSet("Number", "5 °C", new DecimalType(23), false) }, //
{ new ParameterSet("Number", "5 °C", new DecimalType(5), true) }, //
{ new ParameterSet("Number", "5 °C", new DecimalType(4), true) }, //
{ new ParameterSet("Number:Temperature", "0", new QuantityType<>(), true) }, //
{ new ParameterSet("Number:Temperature", "0", new DecimalType(), true) }, //
{ new ParameterSet("Number:Temperature", "5", new QuantityType<>(23, SIUnits.CELSIUS), false) }, //
{ new ParameterSet("Number:Temperature", "5", new QuantityType<>(5, SIUnits.CELSIUS), true) }, //
{ new ParameterSet("Number:Temperature", "5", new QuantityType<>(4, SIUnits.CELSIUS), true) }, //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.Mockito.mock;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.core.i18n.UnitProvider;
import org.openhab.core.items.GenericItem;
import org.openhab.core.items.GroupItem;
import org.openhab.core.library.CoreItemFactory;
Expand All @@ -38,7 +40,7 @@ public void setup() {

@Test
public void testFiltering() {
CoreItemFactory itemFactory = new CoreItemFactory();
CoreItemFactory itemFactory = new CoreItemFactory(mock(UnitProvider.class));

GroupItem group = new GroupItem("TestGroup");
GroupItem subGroup = new GroupItem("TestSubGroup");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.when;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.openhab.core.i18n.UnitProvider;
import org.openhab.core.items.GenericItem;
import org.openhab.core.items.GroupItem;
import org.openhab.core.items.ItemNotFoundException;
Expand All @@ -43,20 +45,22 @@
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@NonNullByDefault
public class SemanticsTest {

private @Mock ItemRegistry mockedItemRegistry;
private @Mock @NonNullByDefault({}) ItemRegistry itemRegistryMock;
private @Mock @NonNullByDefault({}) UnitProvider unitProviderMock;

private GroupItem indoorLocationItem;
private GroupItem bathroomLocationItem;
private GroupItem equipmentItem;
private GenericItem temperaturePointItem;
private GenericItem humidityPointItem;
private GenericItem subEquipmentItem;
private @NonNullByDefault({})GroupItem indoorLocationItem;
private @NonNullByDefault({})GroupItem bathroomLocationItem;
private @NonNullByDefault({})GroupItem equipmentItem;
private @NonNullByDefault({})GenericItem temperaturePointItem;
private @NonNullByDefault({})GenericItem humidityPointItem;
private @NonNullByDefault({})GenericItem subEquipmentItem;

@BeforeEach
public void setup() throws ItemNotFoundException {
CoreItemFactory itemFactory = new CoreItemFactory();
CoreItemFactory itemFactory = new CoreItemFactory(unitProviderMock);

indoorLocationItem = new GroupItem("TestHouse");
indoorLocationItem.addTag("Indoor");
Expand Down Expand Up @@ -94,13 +98,13 @@ public void setup() throws ItemNotFoundException {
equipmentItem.addMember(subEquipmentItem);
subEquipmentItem.addGroupName(equipmentItem.getName());

when(mockedItemRegistry.getItem("TestHouse")).thenReturn(indoorLocationItem);
when(mockedItemRegistry.getItem("TestBathRoom")).thenReturn(bathroomLocationItem);
when(mockedItemRegistry.getItem("Test08")).thenReturn(equipmentItem);
when(mockedItemRegistry.getItem("TestTemperature")).thenReturn(temperaturePointItem);
when(mockedItemRegistry.getItem("TestHumidity")).thenReturn(humidityPointItem);
when(itemRegistryMock.getItem("TestHouse")).thenReturn(indoorLocationItem);
when(itemRegistryMock.getItem("TestBathRoom")).thenReturn(bathroomLocationItem);
when(itemRegistryMock.getItem("Test08")).thenReturn(equipmentItem);
when(itemRegistryMock.getItem("TestTemperature")).thenReturn(temperaturePointItem);
when(itemRegistryMock.getItem("TestHumidity")).thenReturn(humidityPointItem);

new SemanticsActionService(mockedItemRegistry);
new SemanticsActionService(itemRegistryMock);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,10 @@ public class PersistenceExtensionsTest {
public void setUp() {
when(unitProviderMock.getUnit(Temperature.class)).thenReturn(SIUnits.CELSIUS);

CoreItemFactory itemFactory = new CoreItemFactory();
CoreItemFactory itemFactory = new CoreItemFactory(unitProviderMock);
numberItem = itemFactory.createItem(CoreItemFactory.NUMBER, TEST_NUMBER);
quantityItem = itemFactory.createItem(CoreItemFactory.NUMBER + ItemUtil.EXTENSION_SEPARATOR + "Temperature",
TEST_QUANTITY_NUMBER);
quantityItem.setUnitProvider(unitProviderMock);
switchItem = itemFactory.createItem(CoreItemFactory.SWITCH, TEST_SWITCH);

when(itemRegistryMock.get(TEST_NUMBER)).thenReturn(numberItem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
package org.openhab.core.semantics;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;

import java.util.Locale;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.core.i18n.UnitProvider;
import org.openhab.core.items.GenericItem;
import org.openhab.core.items.GroupItem;
import org.openhab.core.library.CoreItemFactory;
Expand All @@ -41,7 +43,7 @@ public class SemanticTagsTest {

@BeforeEach
public void setup() {
CoreItemFactory itemFactory = new CoreItemFactory();
CoreItemFactory itemFactory = new CoreItemFactory(mock(UnitProvider.class));

locationItem = new GroupItem("TestBathRoom");
locationItem.addTag("Bathroom");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
package org.openhab.core.semantics;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.core.i18n.UnitProvider;
import org.openhab.core.items.GenericItem;
import org.openhab.core.items.GroupItem;
import org.openhab.core.library.CoreItemFactory;
import org.openhab.core.semantics.model.property.Humidity;
import org.openhab.core.semantics.model.property.Temperature;

/**
* This are tests for {@link SemanticsPredicates}.
* These are tests for {@link SemanticsPredicates}.
*
* @author Christoph Weitkamp - Initial contribution
*/
Expand All @@ -37,7 +39,7 @@ public class SemanticsPredicatesTest {

@BeforeEach
public void setup() {
CoreItemFactory itemFactory = new CoreItemFactory();
CoreItemFactory itemFactory = new CoreItemFactory(mock(UnitProvider.class));

locationItem = new GroupItem("TestBathRoom");
locationItem.addTag("Bathroom");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.openhab.core.i18n.UnitProvider;
import org.openhab.core.items.GenericItem;
import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item;
Expand All @@ -43,6 +44,7 @@ public class SemanticsServiceImplTest {

private @Mock @NonNullByDefault({}) ItemRegistry itemRegistryMock;
private @Mock @NonNullByDefault({}) MetadataRegistry metadataRegistryMock;
private @Mock @NonNullByDefault({}) UnitProvider unitProviderMock;

private @NonNullByDefault({}) GroupItem locationItem;
private @NonNullByDefault({}) GroupItem equipmentItem;
Expand All @@ -52,7 +54,7 @@ public class SemanticsServiceImplTest {

@BeforeEach
public void setup() throws Exception {
CoreItemFactory itemFactory = new CoreItemFactory();
CoreItemFactory itemFactory = new CoreItemFactory(unitProviderMock);
locationItem = new GroupItem("TestBathRoom");
locationItem.addTag("Bathroom");
locationItem.setLabel("Joe's Room");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.function.Function;

import javax.measure.Quantity;
import javax.measure.Unit;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand All @@ -36,6 +37,7 @@
import org.openhab.core.events.EventFilter;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.events.EventSubscriber;
import org.openhab.core.i18n.UnitProvider;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemFactory;
import org.openhab.core.items.ItemRegistry;
Expand Down Expand Up @@ -129,6 +131,7 @@ public void onStateUpdateFromItem(State state) {
private final EventPublisher eventPublisher;
private final SafeCaller safeCaller;
private final ThingRegistry thingRegistry;
private final UnitProvider unitProvider;

private final ExpiringCacheMap<Integer, Profile> profileSafeCallCache = new ExpiringCacheMap<>(CACHE_EXPIRATION);

Expand All @@ -141,7 +144,7 @@ public CommunicationManager(final @Reference AutoUpdateManager autoUpdateManager
final @Reference ItemStateConverter itemStateConverter, //
final @Reference EventPublisher eventPublisher, //
final @Reference SafeCaller safeCaller, //
final @Reference ThingRegistry thingRegistry) {
final @Reference ThingRegistry thingRegistry, final @Reference UnitProvider unitProvider) {
this.autoUpdateManager = autoUpdateManager;
this.channelTypeRegistry = channelTypeRegistry;
this.defaultProfileFactory = defaultProfileFactory;
Expand All @@ -151,6 +154,7 @@ public CommunicationManager(final @Reference AutoUpdateManager autoUpdateManager
this.eventPublisher = eventPublisher;
this.safeCaller = safeCaller;
this.thingRegistry = thingRegistry;
this.unitProvider = unitProvider;

itemChannelLinkRegistry.addRegistryChangeListener(this);
}
Expand Down Expand Up @@ -509,22 +513,24 @@ private boolean hasDimension(Item item, @Nullable String acceptedItemType) {
|| getDimension(acceptedItemType) != null;
}

@SuppressWarnings("unchecked")
private @Nullable QuantityType<?> convertToQuantityType(DecimalType originalType, Item item,
@Nullable String acceptedItemType) {
NumberItem numberItem = (NumberItem) item;

// DecimalType command sent via a NumberItem with dimension:
Class<? extends Quantity<?>> dimension = numberItem.getDimension();
Unit<? extends Quantity<?>> unit = numberItem.getUnit();

if (dimension == null) {
if (unit == null) {
// DecimalType command sent via a plain NumberItem w/o dimension.
// We try to guess the correct unit from the channel-type's expected item dimension
// or from the item's state description.
dimension = getDimension(acceptedItemType);
Class<? extends Quantity<?>> dimension = getDimension(acceptedItemType);
unit = unitProvider.getUnit((Class<? extends Quantity>) dimension);
}

if (dimension != null) {
return numberItem.toQuantityType(originalType, dimension);
if (unit != null) {
return new QuantityType<>(originalType.toBigDecimal(), unit);
}

return null;
Expand Down
Loading