Skip to content

Commit a48f72f

Browse files
authored
[sensibo] Fix channel provider throwing exception (openhab#17030)
* Fix channel provider throwing exception * Add case insensitive mac address comparison Signed-off-by: Arne Seime <[email protected]>
1 parent 8e570c5 commit a48f72f

File tree

6 files changed

+470
-5
lines changed

6 files changed

+470
-5
lines changed

bundles/org.openhab.binding.sensibo/src/main/java/org/openhab/binding/sensibo/internal/CallbackChannelsTypeProvider.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.util.Collection;
1616
import java.util.Collections;
17+
import java.util.List;
1718
import java.util.Locale;
1819

1920
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -44,12 +45,12 @@ public class CallbackChannelsTypeProvider
4445

4546
@Override
4647
public Collection<ChannelType> getChannelTypes(@Nullable final Locale locale) {
47-
return handler.getChannelTypes(locale);
48+
return handler != null ? handler.getChannelTypes(locale) : List.of();
4849
}
4950

5051
@Override
5152
public @Nullable ChannelType getChannelType(final ChannelTypeUID channelTypeUID, @Nullable final Locale locale) {
52-
return handler.getChannelType(channelTypeUID, locale);
53+
return handler != null ? handler.getChannelType(channelTypeUID, locale) : null;
5354
}
5455

5556
@Override

bundles/org.openhab.binding.sensibo/src/main/java/org/openhab/binding/sensibo/internal/handler/SensiboSkyHandler.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,21 @@ public Collection<Class<? extends ThingHandlerService>> getServices() {
297297
public void initialize() {
298298
config = Optional.ofNullable(getConfigAs(SensiboSkyConfiguration.class));
299299
logger.debug("Initializing SensiboSky using config {}", config);
300-
getSensiboModel().findSensiboSkyByMacAddress(getMacAddress()).ifPresent(pod -> {
300+
Optional<SensiboSky> optionalDevice = getSensiboModel().findSensiboSkyByMacAddress(getMacAddress());
301301

302+
if (optionalDevice.isPresent()) {
303+
SensiboSky pod = optionalDevice.get();
302304
if (pod.isAlive()) {
303305
addDynamicChannelsAndProperties(pod);
304306
updateStatus(ThingStatus.ONLINE);
305307
} else {
306308
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
307309
"Unreachable by Sensibo servers");
308310
}
309-
});
311+
} else {
312+
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
313+
String.format("Device with mac address %s not found", getMacAddress()));
314+
}
310315
}
311316

312317
private boolean isDynamicChannel(final ChannelTypeUID uid) {

bundles/org.openhab.binding.sensibo/src/main/java/org/openhab/binding/sensibo/internal/model/SensiboModel.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public long getLastUpdated() {
4646

4747
public Optional<SensiboSky> findSensiboSkyByMacAddress(final String macAddress) {
4848
final String macAddressWithoutColons = macAddress.replace(":", "");
49-
return pods.stream().filter(pod -> macAddressWithoutColons.equals(pod.getMacAddress())).findFirst();
49+
return pods.stream().filter(pod -> macAddressWithoutColons.equalsIgnoreCase(pod.getMacAddress())).findFirst();
5050
}
5151

5252
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.sensibo.internal;
14+
15+
import static org.junit.jupiter.api.Assertions.assertFalse;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
17+
18+
import java.io.IOException;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.openhab.binding.sensibo.internal.dto.poddetails.PodDetailsDTO;
22+
import org.openhab.binding.sensibo.internal.model.SensiboModel;
23+
import org.openhab.binding.sensibo.internal.model.SensiboSky;
24+
25+
/**
26+
* @author Arne Seime - Initial contribution
27+
*
28+
*/
29+
public class SensiboModelTest {
30+
31+
private final WireHelper wireHelper = new WireHelper();
32+
33+
@Test
34+
public void testCaseInsensitiveMacAddress() throws IOException {
35+
36+
final PodDetailsDTO rsp = wireHelper.deSerializeResponse("/get_pod_details_response.json", PodDetailsDTO.class);
37+
SensiboSky sky = new SensiboSky(rsp);
38+
39+
SensiboModel model = new SensiboModel(0);
40+
model.addPod(sky);
41+
42+
assertFalse(model.findSensiboSkyByMacAddress("MA:C:AD:DR:ES:XX").isPresent());
43+
assertTrue(model.findSensiboSkyByMacAddress("MA:C:AD:DR:ES:S0").isPresent());
44+
assertTrue(model.findSensiboSkyByMacAddress("MA:C:AD:DR:ES:S0".toLowerCase()).isPresent());
45+
}
46+
}

bundles/org.openhab.binding.sensibo/src/test/java/org/openhab/binding/sensibo/internal/handler/SensiboSkyHandlerTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public void testAddDynamicChannels() throws IOException, SensiboCommunicationExc
126126
testAddDynamicChannels("/get_pod_details_response.json");
127127
}
128128

129+
@Test
130+
public void testAddDynamicChannelsValter() throws IOException, SensiboCommunicationException {
131+
testAddDynamicChannels("/get_pod_details_response_valter.json");
132+
}
133+
129134
private void testAddDynamicChannels(String podDetailsResponse) throws IOException, SensiboCommunicationException {
130135
final PodDetailsDTO rsp = wireHelper.deSerializeResponse(podDetailsResponse, PodDetailsDTO.class);
131136
SensiboSky sky = new SensiboSky(rsp);

0 commit comments

Comments
 (0)