mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-10 13:21:53 +01: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:
parent
0df4857b11
commit
a8a5e86fb9
@ -94,27 +94,31 @@ 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.contains(UnitUtils.UNIT_PLACEHOLDER)) {
|
||||
logger.warn(
|
||||
"{}: State description contains unit place-holder '%unit%', check if 'unit' metadata is needed!",
|
||||
itemName);
|
||||
} else {
|
||||
Unit<?> stateDescriptionUnit = UnitUtils.parseUnit(pattern);
|
||||
if (stateDescriptionUnit != null) {
|
||||
String unit = stateDescriptionUnit.toString();
|
||||
MetadataKey defaultUnitMetadataKey = new MetadataKey(NumberItem.UNIT_METADATA_NAMESPACE,
|
||||
if (pattern != null) {
|
||||
if (pattern.contains(UnitUtils.UNIT_PLACEHOLDER)) {
|
||||
logger.warn(
|
||||
"{}: State description contains unit place-holder '%unit%', check if 'unit' metadata is needed!",
|
||||
itemName);
|
||||
Metadata defaultUnitMetadata = new Metadata(defaultUnitMetadataKey, unit, null);
|
||||
metadataStorage.put(defaultUnitMetadataKey.toString(), defaultUnitMetadata);
|
||||
logger.info("{}: Wrote 'unit={}' to metadata.", itemName, unit);
|
||||
} else {
|
||||
Unit<?> stateDescriptionUnit = UnitUtils.parseUnit(pattern);
|
||||
if (stateDescriptionUnit != null) {
|
||||
String unit = stateDescriptionUnit.toString();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user