mirror of
https://github.com/danieldemus/openhab-core.git
synced 2026-07-29 12:34:22 +02:00
Fix NPE in UpgradeTool when no pattern present (#3630)
Reported on the forum. State descriptions do no necessarily contain a pattern and thus `.get("pattern")` can return null.
Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
@@ -94,13 +94,14 @@ public class Upgrader {
|
||||
ManagedItemProvider.PersistedItem item = itemStorage.get(itemName);
|
||||
if (item != null && item.itemType.startsWith("Number:")) {
|
||||
if (metadataStorage.containsKey(NumberItem.UNIT_METADATA_NAMESPACE + ":" + itemName)) {
|
||||
logger.debug("{}: already contains a 'unit' metadata, skipping it", itemName);
|
||||
logger.debug("{}: Already contains a 'unit' metadata, skipping it", itemName);
|
||||
} else {
|
||||
Metadata metadata = metadataStorage.get("stateDescription:" + itemName);
|
||||
if (metadata == null) {
|
||||
logger.debug("{}: Nothing to do, no state description found.", itemName);
|
||||
} else {
|
||||
String pattern = (String) metadata.getConfiguration().get("pattern");
|
||||
if (pattern != null) {
|
||||
if (pattern.contains(UnitUtils.UNIT_PLACEHOLDER)) {
|
||||
logger.warn(
|
||||
"{}: State description contains unit place-holder '%unit%', check if 'unit' metadata is needed!",
|
||||
@@ -109,13 +110,16 @@ public class Upgrader {
|
||||
Unit<?> stateDescriptionUnit = UnitUtils.parseUnit(pattern);
|
||||
if (stateDescriptionUnit != null) {
|
||||
String unit = stateDescriptionUnit.toString();
|
||||
MetadataKey defaultUnitMetadataKey = new MetadataKey(NumberItem.UNIT_METADATA_NAMESPACE,
|
||||
itemName);
|
||||
MetadataKey defaultUnitMetadataKey = new MetadataKey(
|
||||
NumberItem.UNIT_METADATA_NAMESPACE, itemName);
|
||||
Metadata defaultUnitMetadata = new Metadata(defaultUnitMetadataKey, unit, null);
|
||||
metadataStorage.put(defaultUnitMetadataKey.toString(), defaultUnitMetadata);
|
||||
logger.info("{}: Wrote 'unit={}' to metadata.", itemName, unit);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
logger.debug("{}: Nothing to do, no pattern found.", itemName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user