[samsungtv] Fix NullPointerException (#17454)

* Fix NPE

Signed-off-by: Leo Siepel <leosiepel@gmail.com>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
lsiepel 2024-09-23 22:43:59 +02:00 committed by Ciprian Pascu
parent 1940e22a46
commit 000d5329f7

View File

@ -147,6 +147,10 @@ public class SmartThingsApiService implements SamsungTvService {
class TvChannel {
Values tvChannel;
Values tvChannelName;
public String getChannelNum() {
return Optional.ofNullable(tvChannel).map(a -> a.value).orElse("");
}
}
class Values {
@ -246,8 +250,7 @@ public class SmartThingsApiService implements SamsungTvService {
}
public Number getTvChannel() {
return Optional.ofNullable(tvChannel).map(a -> a.tvChannel).map(a -> a.value).filter(i -> !i.isBlank())
.map(j -> parseTVChannel(j)).orElse(-1f);
return Optional.ofNullable(tvChannel).map(a -> a.getChannelNum()).map(j -> parseTVChannel(j)).orElse(-1f);
}
public String getTvChannelName() {
@ -456,10 +459,9 @@ public class SmartThingsApiService implements SamsungTvService {
public Number getTvChannel() {
if (getCapabilityAttribute("tvChannel", "tvChannel")) {
return Optional.ofNullable(deviceEvent).map(a -> a.getValue()).filter(i -> !i.isBlank())
.map(j -> parseTVChannel(j)).orElse(-1f);
return Optional.ofNullable(deviceEvent).map(a -> a.getValue()).map(j -> parseTVChannel(j)).orElse(-1f);
}
return -1;
return -1f;
}
public String getTvChannelName() {
@ -470,15 +472,14 @@ public class SmartThingsApiService implements SamsungTvService {
}
}
public Number parseTVChannel(@Nullable String channel) {
public static Number parseTVChannel(@Nullable String channel) {
try {
return channel != null
? Float.parseFloat(
channel.replaceAll("\\D+", ".").replaceFirst("^\\D*((\\d+\\.\\d+)|(\\d+)).*", "$1"))
: -1f;
return (channel == null || channel.isBlank()) ? -1f
: Float.parseFloat(
channel.replaceAll("\\D+", ".").replaceFirst("^\\D*((\\d+\\.\\d+)|(\\d+)).*", "$1"));
} catch (NumberFormatException ignore) {
}
return -1;
return -1f;
}
public void updateTV() {
@ -790,10 +791,12 @@ public class SmartThingsApiService implements SamsungTvService {
}
Number tvChannel = d.getTvChannel();
if (tvChannel.intValue() != -1) {
logger.trace("{}: SSE Got TV Channel: {}", host, tvChannel);
updateState(CHANNEL, tvChannel);
String tvChannelName = d.getTvChannelName();
logger.trace("{}: SSE Got TV Channel Name: {} Channel: {}", host, tvChannelName,
tvChannel);
}
String tvChannelName = d.getTvChannelName();
if (!tvChannelName.isBlank()) {
logger.trace("{}: SSE Got TV Channel Name: {}", host, tvChannelName);
updateState(CHANNEL_NAME, tvChannelName);
}
String Power = d.getSwitch();