Skip to content

YAML configuration: add support for items/metadata/channel links #4776

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

Merged
merged 2 commits into from
May 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -160,7 +160,7 @@ public class FileFormatResource implements RESTResource {
MyItem:
type: Switch
label: Label
category: icon
icon: icon
groups:
- Group1
- Group2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.openhab.core.model.yaml.YamlElementName;
import org.openhab.core.model.yaml.YamlModelListener;
import org.openhab.core.model.yaml.YamlModelRepository;
import org.openhab.core.model.yaml.internal.items.YamlItemDTO;
import org.openhab.core.model.yaml.internal.semantics.YamlSemanticTagDTO;
import org.openhab.core.model.yaml.internal.things.YamlThingDTO;
import org.openhab.core.service.WatchService;
Expand Down Expand Up @@ -88,7 +89,8 @@ public class YamlModelRepositoryImpl implements WatchService.WatchEventListener,
private static final String READ_ONLY = "readOnly";
private static final Set<String> KNOWN_ELEMENTS = Set.of( //
getElementName(YamlSemanticTagDTO.class), // "tags"
getElementName(YamlThingDTO.class) // "things"
getElementName(YamlThingDTO.class), // "things"
getElementName(YamlItemDTO.class) // "items"
);

private static final String UNWANTED_EXCEPTION_TEXT = "at [Source: UNKNOWN; byte offset: #UNKNOWN] ";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright (c) 2010-2025 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.model.yaml.internal.items;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.common.registry.AbstractProvider;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.items.ItemProvider;
import org.openhab.core.model.yaml.internal.util.YamlElementUtils;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.link.ItemChannelLink;
import org.openhab.core.thing.link.ItemChannelLinkProvider;
import org.openhab.core.thing.profiles.ProfileTypeUID;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This class serves as a provider for all item channel links that is found within YAML files.
* It is filled with content by the {@link YamlItemProvider}, which cannot itself implement the
* {@link ItemChannelLinkProvider} interface as it already implements {@link ItemProvider},
* which would lead to duplicate methods.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
@Component(immediate = true, service = { ItemChannelLinkProvider.class, YamlChannelLinkProvider.class })
public class YamlChannelLinkProvider extends AbstractProvider<ItemChannelLink> implements ItemChannelLinkProvider {

private final Logger logger = LoggerFactory.getLogger(YamlChannelLinkProvider.class);

// Map the channel links to each channel UID and then to each item name and finally to each model name
private Map<String, Map<String, Map<ChannelUID, ItemChannelLink>>> itemsChannelLinksMap = new ConcurrentHashMap<>();

@Override
public Collection<ItemChannelLink> getAll() {
return itemsChannelLinksMap.values().stream().flatMap(m -> m.values().stream())
.flatMap(m -> m.values().stream()).toList();
}

public Collection<ItemChannelLink> getAllFromModel(String modelName) {
return itemsChannelLinksMap.getOrDefault(modelName, Map.of()).values().stream()
.flatMap(m -> m.values().stream()).toList();
}

public void updateItemChannelLinks(String modelName, String itemName, Map<String, Configuration> channelLinks) {
Map<String, Map<ChannelUID, ItemChannelLink>> channelLinksMap = Objects
.requireNonNull(itemsChannelLinksMap.computeIfAbsent(modelName, k -> new ConcurrentHashMap<>()));
// Create a HashMap with an initial capacity of 2 (the default is 16) to save memory because most items have
// only one channel. A capacity of 2 is enough to avoid resizing the HashMap in most cases, whereas 1 would
// trigger a resize as soon as one element is added.
Map<ChannelUID, ItemChannelLink> links = Objects
.requireNonNull(channelLinksMap.computeIfAbsent(itemName, k -> new ConcurrentHashMap<>(2)));

Set<ChannelUID> linksToBeRemoved = new HashSet<>(links.keySet());

for (Map.Entry<String, Configuration> entry : channelLinks.entrySet()) {
String channelUID = entry.getKey();
Configuration configuration = entry.getValue();

ChannelUID channelUIDObject;
try {
channelUIDObject = new ChannelUID(channelUID);
} catch (IllegalArgumentException e) {
logger.warn("Invalid channel UID '{}' in channel link for item '{}'!", channelUID, itemName, e);
continue;
}

// Fix the configuration in case a profile is defined without any scope
if (configuration.containsKey("profile") && configuration.get("profile") instanceof String profile
&& profile.indexOf(":") == -1) {
String fullProfile = ProfileTypeUID.SYSTEM_SCOPE + ":" + profile;
configuration.put("profile", fullProfile);
logger.info(
"Profile '{}' for channel '{}' is missing the scope prefix, assuming the correct UID is '{}'. Check your configuration.",
profile, channelUID, fullProfile);
}

ItemChannelLink itemChannelLink = new ItemChannelLink(itemName, channelUIDObject, configuration);

linksToBeRemoved.remove(channelUIDObject);
ItemChannelLink oldLink = links.get(channelUIDObject);
if (oldLink == null) {
links.put(channelUIDObject, itemChannelLink);
logger.debug("notify added item channel link {}", itemChannelLink.getUID());
notifyListenersAboutAddedElement(itemChannelLink);
} else if (!YamlElementUtils.equalsConfig(configuration.getProperties(),
oldLink.getConfiguration().getProperties())) {
links.put(channelUIDObject, itemChannelLink);
logger.debug("notify updated item channel link {}", itemChannelLink.getUID());
notifyListenersAboutUpdatedElement(oldLink, itemChannelLink);
}
}

linksToBeRemoved.forEach(uid -> {
ItemChannelLink link = links.remove(uid);
if (link != null) {
logger.debug("notify removed item channel link {}", link.getUID());
notifyListenersAboutRemovedElement(link);
}
});
if (links.isEmpty()) {
channelLinksMap.remove(itemName);
}
if (channelLinksMap.isEmpty()) {
itemsChannelLinksMap.remove(modelName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2010-2025 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.model.yaml.internal.items;

import java.util.List;
import java.util.Objects;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.model.yaml.internal.util.YamlElementUtils;

/**
* The {@link YamlGroupDTO} is a data transfer object used to serialize the details of a group item
* in a YAML configuration file.
*
* @author Laurent Garnier - Initial contribution
*/
public class YamlGroupDTO {

private static final String DEFAULT_FUNCTION = "EQUALITY";
private static final Set<String> VALID_FUNCTIONS = Set.of("AND", "OR", "NAND", "NOR", "XOR", "COUNT", "AVG",
"MEDIAN", "SUM", "MIN", "MAX", "LATEST", "EARLIEST", DEFAULT_FUNCTION);

public String type;
public String dimension;
public String function;
public List<@NonNull String> parameters;

public YamlGroupDTO() {
}

public boolean isValid(@NonNull List<@NonNull String> errors, @NonNull List<@NonNull String> warnings) {
boolean ok = true;
if (!YamlElementUtils.isValidItemType(type)) {
errors.add("invalid value \"%s\" for \"type\" field in group".formatted(type));
ok = false;
} else if (YamlElementUtils.isNumberItemType(type)) {
if (!YamlElementUtils.isValidItemDimension(dimension)) {
errors.add("invalid value \"%s\" for \"dimension\" field in group".formatted(dimension));
ok = false;
}
} else if (dimension != null) {
warnings.add("\"dimension\" field in group ignored as type is not Number");
}
if (!VALID_FUNCTIONS.contains(getFunction())) {
errors.add("invalid value \"%s\" for \"function\" field".formatted(function));
ok = false;
}
return ok;
}

public @Nullable String getBaseType() {
return YamlElementUtils.getItemTypeWithDimension(type, dimension);
}

public String getFunction() {
return function != null ? function.toUpperCase() : DEFAULT_FUNCTION;
}

@Override
public int hashCode() {
return Objects.hash(getBaseType(), getFunction());
}

@Override
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
} else if (obj == null || getClass() != obj.getClass()) {
return false;
}
YamlGroupDTO other = (YamlGroupDTO) obj;
return Objects.equals(getBaseType(), other.getBaseType()) && Objects.equals(getFunction(), other.getFunction())
&& YamlElementUtils.equalsListStrings(parameters, other.parameters);
}
}
Loading