]> git.basschouten.com Git - openhab-addons.git/blob
854b08eb04d13e8adb43dd42d82f288121554904
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.knx.internal.channel;
14
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.*;
17 import static org.junit.jupiter.api.Assertions.*;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.when;
20
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Objects;
24 import java.util.Set;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.junit.jupiter.api.Test;
28 import org.openhab.binding.knx.internal.KNXBindingConstants;
29 import org.openhab.binding.knx.internal.client.OutboundSpec;
30 import org.openhab.binding.knx.internal.dpt.ValueEncoder;
31 import org.openhab.core.config.core.Configuration;
32 import org.openhab.core.library.items.ColorItem;
33 import org.openhab.core.library.types.HSBType;
34 import org.openhab.core.library.types.PercentType;
35 import org.openhab.core.thing.Channel;
36 import org.openhab.core.thing.type.ChannelTypeUID;
37 import org.openhab.core.types.Command;
38 import org.openhab.core.types.UnDefType;
39
40 import tuwien.auto.calimero.GroupAddress;
41 import tuwien.auto.calimero.KNXFormatException;
42
43 /**
44  *
45  * @author Simon Kaufmann - Initial contribution
46  *
47  */
48 @NonNullByDefault
49 class KNXChannelTest {
50
51     @Test
52     public void invalidFails() {
53         GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<1/3/22+0/3/22+<0/8/15");
54         assertNull(res);
55     }
56
57     @Test
58     void testParseWithDptMultipleWithRead() throws KNXFormatException {
59         GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<1/3/22+0/3/22+<0/7/15");
60
61         if (res == null) {
62             fail();
63             return;
64         }
65
66         assertEquals("5.001", res.getDPT());
67         assertEquals(new GroupAddress("1/3/22"), res.getMainGA());
68         assertTrue(res.getReadGAs().contains(res.getMainGA()));
69         assertEquals(3, res.getListenGAs().size());
70         assertEquals(2, res.getReadGAs().size());
71     }
72
73     @Test
74     void testParseWithDptMultipleWithoutRead() throws KNXFormatException {
75         GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:1/3/22+0/3/22+0/7/15");
76
77         if (res == null) {
78             fail();
79             return;
80         }
81
82         assertEquals("5.001", res.getDPT());
83         assertEquals(new GroupAddress("1/3/22"), res.getMainGA());
84         assertFalse(res.getReadGAs().contains(res.getMainGA()));
85         assertEquals(3, res.getListenGAs().size());
86         assertEquals(0, res.getReadGAs().size());
87     }
88
89     @Test
90     void testParseWithoutDptSingleWithoutRead() throws KNXFormatException {
91         GroupAddressConfiguration res = GroupAddressConfiguration.parse("1/3/22");
92
93         if (res == null) {
94             fail();
95             return;
96         }
97
98         assertNull(res.getDPT());
99         assertEquals(new GroupAddress("1/3/22"), res.getMainGA());
100         assertFalse(res.getReadGAs().contains(res.getMainGA()));
101         assertEquals(1, res.getListenGAs().size());
102         assertEquals(0, res.getReadGAs().size());
103     }
104
105     @Test
106     void testParseWithoutDptSingleWithRead() throws KNXFormatException {
107         GroupAddressConfiguration res = GroupAddressConfiguration.parse("<1/3/22");
108
109         if (res == null) {
110             fail();
111             return;
112         }
113
114         assertNull(res.getDPT());
115         assertEquals(new GroupAddress("1/3/22"), res.getMainGA());
116         assertTrue(res.getReadGAs().contains(res.getMainGA()));
117         assertEquals(1, res.getListenGAs().size());
118         assertEquals(1, res.getReadGAs().size());
119     }
120
121     @Test
122     void testParseTwoLevel() throws KNXFormatException {
123         GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<3/1024+<4/1025");
124
125         if (res == null) {
126             fail();
127             return;
128         }
129
130         assertEquals(new GroupAddress("3/1024"), res.getMainGA());
131         assertTrue(res.getReadGAs().contains(res.getMainGA()));
132         assertEquals(2, res.getListenGAs().size());
133         assertEquals(2, res.getReadGAs().size());
134     }
135
136     @Test
137     void testParseFreeLevel() throws KNXFormatException {
138         GroupAddressConfiguration res = GroupAddressConfiguration.parse("5.001:<4610+<4611");
139
140         if (res == null) {
141             fail();
142             return;
143         }
144
145         assertEquals(new GroupAddress("4610"), res.getMainGA());
146         assertEquals(2, res.getListenGAs().size());
147         assertEquals(2, res.getReadGAs().size());
148     }
149
150     @Test
151     public void testChannelGaParsing() throws KNXFormatException {
152         Channel channel = Objects.requireNonNull(mock(Channel.class));
153         Configuration configuration = new Configuration(
154                 Map.of("key1", "5.001:<1/2/3+4/5/6+1/5/6", "key2", "1.001:7/1/9+1/1/2"));
155         when(channel.getChannelTypeUID()).thenReturn(new ChannelTypeUID("a:b:c"));
156         when(channel.getConfiguration()).thenReturn(configuration);
157         when(channel.getAcceptedItemType()).thenReturn("none");
158
159         MyKNXChannel knxChannel = new MyKNXChannel(channel);
160
161         Set<GroupAddress> listenAddresses = knxChannel.getAllGroupAddresses();
162         assertEquals(5, listenAddresses.size());
163         // we don't check the content since parsing has been checked before and the quantity is correct
164         Set<GroupAddress> writeAddresses = knxChannel.getWriteAddresses();
165         assertEquals(2, writeAddresses.size());
166         assertTrue(writeAddresses.contains(new GroupAddress("1/2/3")));
167         assertTrue(writeAddresses.contains(new GroupAddress("7/1/9")));
168     }
169
170     @Test
171     void testSubTypeMapping() throws KNXFormatException {
172         Channel channel = Objects.requireNonNull(mock(Channel.class));
173         Configuration configuration = new Configuration(Map.of("key1", "1.001:1/2/3"));
174         when(channel.getChannelTypeUID()).thenReturn(new ChannelTypeUID("a:b:c"));
175         when(channel.getConfiguration()).thenReturn(configuration);
176         when(channel.getAcceptedItemType()).thenReturn(ColorItem.class.getName());
177         MyKNXChannel knxChannel = new MyKNXChannel(channel);
178         assertNotNull(knxChannel.getCommandSpec(new HSBType("0,100,100")));
179         assertEquals(knxChannel.getCommandSpec(new HSBType("0,100,100")).getDPT(), "1.001");
180     }
181
182     @Test
183     void test5001PercentType() throws KNXFormatException {
184         Configuration configuration = new Configuration(Map.of("switch", "1.001:1/2/1", "position", "5.001:1/2/2"));
185         Channel channel = Objects.requireNonNull(mock(Channel.class));
186         when(channel.getChannelTypeUID())
187                 .thenReturn(new ChannelTypeUID(KNXBindingConstants.BINDING_ID, KNXBindingConstants.CHANNEL_DIMMER));
188         when(channel.getConfiguration()).thenReturn(configuration);
189
190         KNXChannel knxChannel = KNXChannelFactory.createKnxChannel(channel);
191         assertThat(knxChannel, instanceOf(TypeDimmer.class));
192
193         Command command = new PercentType("100");
194         OutboundSpec outboundSpec = knxChannel.getCommandSpec(command);
195         assertThat(outboundSpec, is(notNullValue()));
196
197         String mappedValue = ValueEncoder.encode(outboundSpec.getValue(), outboundSpec.getDPT());
198         assertThat(mappedValue, is("100"));
199         assertThat(outboundSpec.getValue(), is(instanceOf(PercentType.class)));
200     }
201
202     private static class MyKNXChannel extends KNXChannel {
203         public MyKNXChannel(Channel channel) {
204             super(Set.of("key1", "key2"), List.of(UnDefType.class), channel);
205         }
206
207         @Override
208         protected String getDefaultDPT(String gaConfigKey) {
209             return "";
210         }
211     }
212 }