mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-02-09 13:57:00 +01:00
[knx] Fix dimmer channels (#16364)
* [knx] Fix dimmer channels Signed-off-by: Jan N. Klug <github@klug.nrw> Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
parent
1521e25ab9
commit
15fa99e138
@ -123,13 +123,18 @@ public abstract class KNXChannel {
|
|||||||
String dpt = Objects.requireNonNullElse(entry.getValue().getDPT(), getDefaultDPT(entry.getKey()));
|
String dpt = Objects.requireNonNullElse(entry.getValue().getDPT(), getDefaultDPT(entry.getKey()));
|
||||||
Set<Class<? extends Type>> expectedTypeClasses = DPTUtil.getAllowedTypes(dpt);
|
Set<Class<? extends Type>> expectedTypeClasses = DPTUtil.getAllowedTypes(dpt);
|
||||||
// find the first matching type that is assignable from the command
|
// find the first matching type that is assignable from the command
|
||||||
for (Class<? extends Type> expectedTypeClass : expectedTypeClasses) {
|
if (expectedTypeClasses.contains(command.getClass())) {
|
||||||
if (expectedTypeClass.equals(command.getClass())) {
|
logger.trace(
|
||||||
logger.trace("getCommandSpec command class matches expected type class");
|
"getCommandSpec key '{}' has one of the expectedTypeClasses '{}', matching command '{}' and dpt '{}'",
|
||||||
|
entry.getKey(), expectedTypeClasses, command, dpt);
|
||||||
return new WriteSpecImpl(entry.getValue(), dpt, command);
|
return new WriteSpecImpl(entry.getValue(), dpt, command);
|
||||||
} else if (command instanceof State state && State.class.isAssignableFrom(expectedTypeClass)) {
|
} else {
|
||||||
|
for (Class<? extends Type> expectedTypeClass : expectedTypeClasses) {
|
||||||
|
if (command instanceof State state && State.class.isAssignableFrom(expectedTypeClass)) {
|
||||||
if (state.as(expectedTypeClass.asSubclass(State.class)) != null) {
|
if (state.as(expectedTypeClass.asSubclass(State.class)) != null) {
|
||||||
logger.trace("getCommandSpec command class is a sub-class of the expected type class");
|
logger.trace(
|
||||||
|
"getCommandSpec command class '{}' is a sub-class of the expectedTypeClass '{}' for key '{}'",
|
||||||
|
command.getClass(), expectedTypeClass, entry.getKey());
|
||||||
Class<? extends State> expectedTypeAsStateClass = expectedTypeClass.asSubclass(State.class);
|
Class<? extends State> expectedTypeAsStateClass = expectedTypeClass.asSubclass(State.class);
|
||||||
State convertedState = state.as(expectedTypeAsStateClass);
|
State convertedState = state.as(expectedTypeAsStateClass);
|
||||||
if (convertedState != null) {
|
if (convertedState != null) {
|
||||||
@ -139,7 +144,10 @@ public abstract class KNXChannel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger.trace("getCommandSpec no Spec found!");
|
}
|
||||||
|
logger.trace(
|
||||||
|
"getCommandSpec could not match command class '{}' with expectedTypeClasses for any of the checked keys '{}', discarding command",
|
||||||
|
command.getClass(), gaKeys);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.openhab.binding.knx.internal.channel;
|
package org.openhab.binding.knx.internal.channel;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.*;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
@ -23,11 +25,16 @@ import java.util.Set;
|
|||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.openhab.binding.knx.internal.KNXBindingConstants;
|
||||||
|
import org.openhab.binding.knx.internal.client.OutboundSpec;
|
||||||
|
import org.openhab.binding.knx.internal.dpt.ValueEncoder;
|
||||||
import org.openhab.core.config.core.Configuration;
|
import org.openhab.core.config.core.Configuration;
|
||||||
import org.openhab.core.library.items.ColorItem;
|
import org.openhab.core.library.items.ColorItem;
|
||||||
import org.openhab.core.library.types.HSBType;
|
import org.openhab.core.library.types.HSBType;
|
||||||
|
import org.openhab.core.library.types.PercentType;
|
||||||
import org.openhab.core.thing.Channel;
|
import org.openhab.core.thing.Channel;
|
||||||
import org.openhab.core.thing.type.ChannelTypeUID;
|
import org.openhab.core.thing.type.ChannelTypeUID;
|
||||||
|
import org.openhab.core.types.Command;
|
||||||
import org.openhab.core.types.UnDefType;
|
import org.openhab.core.types.UnDefType;
|
||||||
|
|
||||||
import tuwien.auto.calimero.GroupAddress;
|
import tuwien.auto.calimero.GroupAddress;
|
||||||
@ -172,6 +179,26 @@ class KNXChannelTest {
|
|||||||
assertEquals(knxChannel.getCommandSpec(new HSBType("0,100,100")).getDPT(), "1.001");
|
assertEquals(knxChannel.getCommandSpec(new HSBType("0,100,100")).getDPT(), "1.001");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void test5001PercentType() throws KNXFormatException {
|
||||||
|
Configuration configuration = new Configuration(Map.of("switch", "1.001:1/2/1", "position", "5.001:1/2/2"));
|
||||||
|
Channel channel = Objects.requireNonNull(mock(Channel.class));
|
||||||
|
when(channel.getChannelTypeUID())
|
||||||
|
.thenReturn(new ChannelTypeUID(KNXBindingConstants.BINDING_ID, KNXBindingConstants.CHANNEL_DIMMER));
|
||||||
|
when(channel.getConfiguration()).thenReturn(configuration);
|
||||||
|
|
||||||
|
KNXChannel knxChannel = KNXChannelFactory.createKnxChannel(channel);
|
||||||
|
assertThat(knxChannel, instanceOf(TypeDimmer.class));
|
||||||
|
|
||||||
|
Command command = new PercentType("100");
|
||||||
|
OutboundSpec outboundSpec = knxChannel.getCommandSpec(command);
|
||||||
|
assertThat(outboundSpec, is(notNullValue()));
|
||||||
|
|
||||||
|
String mappedValue = ValueEncoder.encode(outboundSpec.getValue(), outboundSpec.getDPT());
|
||||||
|
assertThat(mappedValue, is("100"));
|
||||||
|
assertThat(outboundSpec.getValue(), is(instanceOf(PercentType.class)));
|
||||||
|
}
|
||||||
|
|
||||||
private static class MyKNXChannel extends KNXChannel {
|
private static class MyKNXChannel extends KNXChannel {
|
||||||
public MyKNXChannel(Channel channel) {
|
public MyKNXChannel(Channel channel) {
|
||||||
super(Set.of("key1", "key2"), List.of(UnDefType.class), channel);
|
super(Set.of("key1", "key2"), List.of(UnDefType.class), channel);
|
||||||
|
Loading…
Reference in New Issue
Block a user