Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set a default presentation pattern for String/Number/Datetime items (#…
…4175) * Set a default presentation pattern for String/Number/Datetime items A default state pattern was previously provided by ChannelStateDescriptionProvider only for String and Number items linked to a channel. It is now the class DefaultStateDescriptionFragmentProvider which is responsible for providing the default state pattern for items, whether the item is linked to a channel or not. This new class is the lowest ranked StateDescriptionFragmentProvider so that all other providers have priority in setting the state pattern. Default pattern for string item: %s Default pattern for datetime item or group with datetime state: %1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS Default pattern for number item or group with number state: %.0f Default pattern for number+dimension item or group with number+dimension state: %.0f %unit% Closes #4071 Closes #3835 Signed-off-by: Laurent Garnier <lg.hc@free.fr>
- Loading branch information
Showing
5 changed files
with
142 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...rc/main/java/org/openhab/core/internal/items/DefaultStateDescriptionFragmentProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* Copyright (c) 2010-2024 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.core.internal.items; | ||
|
||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.items.GroupItem; | ||
import org.openhab.core.items.Item; | ||
import org.openhab.core.library.CoreItemFactory; | ||
import org.openhab.core.types.StateDescriptionFragment; | ||
import org.openhab.core.types.StateDescriptionFragmentBuilder; | ||
import org.openhab.core.types.StateDescriptionFragmentProvider; | ||
import org.osgi.framework.Constants; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Deactivate; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A {@link StateDescriptionFragment} provider providing a default state pattern for items of type String, | ||
* DateTime and Number (with or without dimension). | ||
* | ||
* @author Laurent Garnier - initial contribution | ||
* | ||
*/ | ||
@NonNullByDefault | ||
@Component(service = { StateDescriptionFragmentProvider.class, | ||
DefaultStateDescriptionFragmentProvider.class }, immediate = true, property = { "service.ranking:Integer=-2" }) | ||
public class DefaultStateDescriptionFragmentProvider implements StateDescriptionFragmentProvider { | ||
|
||
private static final StateDescriptionFragment DEFAULT_STRING = StateDescriptionFragmentBuilder.create() | ||
.withPattern("%s").build(); | ||
private static final StateDescriptionFragment DEFAULT_DATETIME = StateDescriptionFragmentBuilder.create() | ||
.withPattern("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS").build(); | ||
private static final StateDescriptionFragment DEFAULT_NUMBER = StateDescriptionFragmentBuilder.create() | ||
.withPattern("%.0f").build(); | ||
private static final StateDescriptionFragment DEFAULT_NUMBER_WITH_DIMENSION = StateDescriptionFragmentBuilder | ||
.create().withPattern("%.0f %unit%").build(); | ||
|
||
private final Logger logger = LoggerFactory.getLogger(DefaultStateDescriptionFragmentProvider.class); | ||
|
||
private final Map<String, StateDescriptionFragment> stateDescriptionFragments = new ConcurrentHashMap<>(); | ||
|
||
private Integer rank = -2; // takes less precedence than all other providers | ||
|
||
@Activate | ||
public DefaultStateDescriptionFragmentProvider(Map<String, Object> properties) { | ||
Object serviceRanking = properties.get(Constants.SERVICE_RANKING); | ||
if (serviceRanking instanceof Integer rankValue) { | ||
rank = rankValue; | ||
} | ||
} | ||
|
||
@Deactivate | ||
protected void deactivate() { | ||
stateDescriptionFragments.clear(); | ||
} | ||
|
||
public void onItemAdded(Item item) { | ||
logger.trace("onItemAdded {} {}", item.getName(), item.getType()); | ||
if (item instanceof GroupItem group) { | ||
Item baseItem = group.getBaseItem(); | ||
if (baseItem != null) { | ||
onItemAdded(baseItem); | ||
} | ||
} else if (item.getType().startsWith(CoreItemFactory.NUMBER + ":")) { | ||
stateDescriptionFragments.put(item.getName(), DEFAULT_NUMBER_WITH_DIMENSION); | ||
} else { | ||
switch (item.getType()) { | ||
case CoreItemFactory.STRING: | ||
stateDescriptionFragments.put(item.getName(), DEFAULT_STRING); | ||
break; | ||
case CoreItemFactory.DATETIME: | ||
stateDescriptionFragments.put(item.getName(), DEFAULT_DATETIME); | ||
break; | ||
case CoreItemFactory.NUMBER: | ||
stateDescriptionFragments.put(item.getName(), DEFAULT_NUMBER); | ||
break; | ||
default: | ||
stateDescriptionFragments.remove(item.getName()); | ||
} | ||
} | ||
} | ||
|
||
public void onItemRemoved(Item item) { | ||
logger.trace("onItemRemoved {}", item.getName()); | ||
stateDescriptionFragments.remove(item.getName()); | ||
} | ||
|
||
@Override | ||
public @Nullable StateDescriptionFragment getStateDescriptionFragment(String itemName, @Nullable Locale locale) { | ||
return stateDescriptionFragments.get(itemName); | ||
} | ||
|
||
@Override | ||
public Integer getRank() { | ||
return rank; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters