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

[knx] Correctly support state sub-types for DPTs #16337

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
*/
package org.openhab.binding.knx.internal.channel;

import static java.util.stream.Collectors.*;
import static org.openhab.binding.knx.internal.KNXBindingConstants.CONTROL_CHANNEL_TYPES;
import static org.openhab.binding.knx.internal.KNXBindingConstants.GA;
import static java.util.stream.Collectors.toList;
import static org.openhab.binding.knx.internal.KNXBindingConstants.*;

import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -31,6 +30,7 @@
import org.openhab.core.config.core.Configuration;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.types.State;
import org.openhab.core.types.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -121,11 +121,22 @@ public final Set<GroupAddress> getWriteAddresses() {
logger.trace("getCommandSpec checking keys '{}' for command '{}' ({})", gaKeys, command, command.getClass());
for (Map.Entry<String, GroupAddressConfiguration> entry : groupAddressConfigurations.entrySet()) {
String dpt = Objects.requireNonNullElse(entry.getValue().getDPT(), getDefaultDPT(entry.getKey()));
Set<Class<? extends Type>> expectedTypeClass = DPTUtil.getAllowedTypes(dpt);
if (expectedTypeClass.contains(command.getClass())) {
logger.trace("getCommandSpec key '{}' has expectedTypeClass '{}', matching command '{}' and dpt '{}'",
entry.getKey(), expectedTypeClass, command, dpt);
return new WriteSpecImpl(entry.getValue(), dpt, command);
Set<Class<? extends Type>> expectedTypeClasses = DPTUtil.getAllowedTypes(dpt);
// find the first matching type that is assignable from the command
for (Class<? extends Type> expectedTypeClass : expectedTypeClasses) {
if (expectedTypeClass.equals(command.getClass())) {
logger.trace("getCommandSpec command class matches expected type class");
return new WriteSpecImpl(entry.getValue(), dpt, command);
} else if (command instanceof State state && State.class.isAssignableFrom(expectedTypeClass)) {
if (state.as(expectedTypeClass.asSubclass(State.class)) != null) {
logger.trace("getCommandSpec command class is a sub-class of the expected type class");
Class<? extends State> expectedTypeAsStateClass = expectedTypeClass.asSubclass(State.class);
State convertedState = state.as(expectedTypeAsStateClass);
if (convertedState != null) {
return new WriteSpecImpl(entry.getValue(), dpt, convertedState);
}
}
}
}
}
logger.trace("getCommandSpec no Spec found!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.library.items.ColorItem;
import org.openhab.core.library.types.HSBType;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.types.UnDefType;
Expand Down Expand Up @@ -158,6 +160,18 @@ public void testChannelGaParsing() throws KNXFormatException {
assertTrue(writeAddresses.contains(new GroupAddress("7/1/9")));
}

@Test
void testSubTypeMapping() throws KNXFormatException {
Channel channel = Objects.requireNonNull(mock(Channel.class));
Configuration configuration = new Configuration(Map.of("key1", "1.001:1/2/3"));
when(channel.getChannelTypeUID()).thenReturn(new ChannelTypeUID("a:b:c"));
when(channel.getConfiguration()).thenReturn(configuration);
when(channel.getAcceptedItemType()).thenReturn(ColorItem.class.getName());
MyKNXChannel knxChannel = new MyKNXChannel(channel);
assertNotNull(knxChannel.getCommandSpec(new HSBType("0,100,100")));
assertEquals(knxChannel.getCommandSpec(new HSBType("0,100,100")).getDPT(), "1.001");
}

private static class MyKNXChannel extends KNXChannel {
public MyKNXChannel(Channel channel) {
super(Set.of("key1", "key2"), List.of(UnDefType.class), channel);
Expand Down