From 5cc5ee6f7bcdbaf6fffb2a0213d86c5659672bcc Mon Sep 17 00:00:00 2001 From: lsiepel Date: Sun, 27 Aug 2023 23:53:02 +0200 Subject: [PATCH] [yamahareceiver] Show correct status detail on failure (#15510) * Partial fix #7667 * java 17 instanceof * Checkstyle Signed-off-by: Leo Siepel --- .../discovery/ZoneDiscoveryService.java | 6 ++-- .../internal/handler/YamahaBridgeHandler.java | 5 ++- .../handler/YamahaZoneThingHandler.java | 36 +++++++++---------- .../internal/protocol/InputConverter.java | 2 +- .../protocol/xml/DeviceInformationXML.java | 2 +- .../protocol/xml/InputConverterXML.java | 2 +- .../xml/InputWithNavigationControlXML.java | 2 +- .../protocol/xml/InputWithPlayControlXML.java | 2 +- .../xml/InputWithPresetControlXML.java | 2 +- .../internal/protocol/xml/XMLUtils.java | 14 ++++---- 10 files changed, 35 insertions(+), 38 deletions(-) diff --git a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/discovery/ZoneDiscoveryService.java b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/discovery/ZoneDiscoveryService.java index 3684dd720..0f79e46f9 100644 --- a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/discovery/ZoneDiscoveryService.java +++ b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/discovery/ZoneDiscoveryService.java @@ -101,9 +101,9 @@ public class ZoneDiscoveryService extends AbstractDiscoveryService implements Di @Override public void setThingHandler(@Nullable ThingHandler handler) { - if (handler instanceof YamahaBridgeHandler) { - this.handler = (YamahaBridgeHandler) handler; - this.handler.setZoneDiscoveryService(this); + if (handler instanceof YamahaBridgeHandler bridgeHandler) { + bridgeHandler.setZoneDiscoveryService(this); + this.handler = bridgeHandler; } } diff --git a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/handler/YamahaBridgeHandler.java b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/handler/YamahaBridgeHandler.java index d450c17a5..3a8b150e5 100644 --- a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/handler/YamahaBridgeHandler.java +++ b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/handler/YamahaBridgeHandler.java @@ -167,9 +167,8 @@ public class YamahaBridgeHandler extends BaseBridgeHandler systemControl.setPartyModeMute(((OnOffType) command) == OnOffType.ON); break; case CHANNEL_PARTY_MODE_VOLUME: - if (command instanceof IncreaseDecreaseType) { - systemControl - .setPartyModeVolume(((IncreaseDecreaseType) command) == IncreaseDecreaseType.INCREASE); + if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) { + systemControl.setPartyModeVolume(increaseDecreaseCommand == IncreaseDecreaseType.INCREASE); } else { logger.warn("Only {} and {} commands are supported for {}", IncreaseDecreaseType.DECREASE, IncreaseDecreaseType.DECREASE, id); diff --git a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/handler/YamahaZoneThingHandler.java b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/handler/YamahaZoneThingHandler.java index 2d3a0ca21..011252dea 100644 --- a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/handler/YamahaZoneThingHandler.java +++ b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/handler/YamahaZoneThingHandler.java @@ -87,7 +87,7 @@ import org.slf4j.LoggerFactory; * class {@link ZoneControlXML}, {@link InputWithPlayControlXML} and {@link InputWithNavigationControlXML} * for communication. * - * @author David Graeff + * @author David Graeff - Initial contribution * @author Tomasz Maruszak - [yamaha] Tuner band selection and preset feature for dual band models (RX-S601D), added * config object */ @@ -287,11 +287,11 @@ public class YamahaZoneThingHandler extends BaseThingHandler zoneControl.setVolumeDB(((DecimalType) command).floatValue()); break; case CHANNEL_VOLUME: - if (command instanceof DecimalType) { - zoneControl.setVolume(((DecimalType) command).floatValue()); - } else if (command instanceof IncreaseDecreaseType) { + if (command instanceof DecimalType decimalCommand) { + zoneControl.setVolume(decimalCommand.floatValue()); + } else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) { zoneControl.setVolumeRelative(zoneState, - (((IncreaseDecreaseType) command) == IncreaseDecreaseType.INCREASE ? 1 : -1) + (increaseDecreaseCommand == IncreaseDecreaseType.INCREASE ? 1 : -1) * zoneConfig.getVolumeRelativeChangeFactor()); } break; @@ -377,11 +377,11 @@ public class YamahaZoneThingHandler extends BaseThingHandler return; } - if (command instanceof DecimalType) { - inputWithPresetControl.selectItemByPresetNumber(((DecimalType) command).intValue()); - } else if (command instanceof StringType) { + if (command instanceof DecimalType decimalCommand) { + inputWithPresetControl.selectItemByPresetNumber(decimalCommand.intValue()); + } else if (command instanceof StringType stringCommand) { try { - int v = Integer.valueOf(((StringType) command).toString()); + int v = Integer.valueOf(stringCommand.toString()); inputWithPresetControl.selectItemByPresetNumber(v); } catch (NumberFormatException e) { logger.warn("Provide a number for {}", id); @@ -408,8 +408,7 @@ public class YamahaZoneThingHandler extends BaseThingHandler return; } - if (command instanceof PlayPauseType) { - PlayPauseType t = ((PlayPauseType) command); + if (command instanceof PlayPauseType t) { switch (t) { case PAUSE: inputWithPlayControl.pause(); @@ -418,8 +417,7 @@ public class YamahaZoneThingHandler extends BaseThingHandler inputWithPlayControl.play(); break; } - } else if (command instanceof NextPreviousType) { - NextPreviousType t = ((NextPreviousType) command); + } else if (command instanceof NextPreviousType t) { switch (t) { case NEXT: inputWithPlayControl.nextTrack(); @@ -428,15 +426,15 @@ public class YamahaZoneThingHandler extends BaseThingHandler inputWithPlayControl.previousTrack(); break; } - } else if (command instanceof DecimalType) { - int v = ((DecimalType) command).intValue(); + } else if (command instanceof DecimalType decimalCommand) { + int v = decimalCommand.intValue(); if (v < 0) { inputWithPlayControl.skipREV(); } else if (v > 0) { inputWithPlayControl.skipFF(); } - } else if (command instanceof StringType) { - String v = ((StringType) command).toFullString(); + } else if (command instanceof StringType stringCommand) { + String v = stringCommand.toFullString(); switch (v) { case "Play": inputWithPlayControl.play(); @@ -496,7 +494,7 @@ public class YamahaZoneThingHandler extends BaseThingHandler } else if (id.equals(grpZone(CHANNEL_SURROUND))) { updateState(channelUID, new StringType(zoneState.surroundProgram)); } else if (id.equals(grpZone(CHANNEL_SCENE))) { - // no state updates available + logger.debug("No state updates available"); } else if (id.equals(grpZone(CHANNEL_DIALOGUE_LEVEL))) { updateState(channelUID, new DecimalType(zoneState.dialogueLevel)); } else if (id.equals(grpZone(CHANNEL_HDMI1OUT))) { @@ -739,7 +737,7 @@ public class YamahaZoneThingHandler extends BaseThingHandler stateUpdatable.update(); } catch (IOException e) { logger.debug("State update error. Changing thing to offline", e); - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage()); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage()); } catch (ReceivedMessageParseException e) { String message = e.getMessage(); updateProperty(PROPERTY_LAST_PARSE_ERROR, message != null ? message : ""); diff --git a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/InputConverter.java b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/InputConverter.java index 5bc70f8b3..bd2a8d619 100644 --- a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/InputConverter.java +++ b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/InputConverter.java @@ -18,7 +18,7 @@ package org.openhab.binding.yamahareceiver.internal.protocol; * For example, AVRs when setting input 'AUDIO_X' (or HDMI_X) need the input to be sent in this form. * However, what comes back in the status update from the AVR is 'AUDIOX' (and 'HDMIX') respectively. * - * @author Tomasz Maruszak + * @author Tomasz Maruszak - Initial contribution */ public interface InputConverter { diff --git a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/DeviceInformationXML.java b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/DeviceInformationXML.java index 9f68db839..9603243f9 100644 --- a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/DeviceInformationXML.java +++ b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/DeviceInformationXML.java @@ -145,7 +145,7 @@ public class DeviceInformationXML implements DeviceInformation { private boolean isFeatureSupported(Node node, String name) { String value = getNodeContentOrEmpty(node, name); - boolean supported = value.equals("1") || value.equals("Available"); + boolean supported = "1".equals(value) || "Available".equals(value); return supported; } diff --git a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputConverterXML.java b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputConverterXML.java index 3b3e42bbc..83b515237 100644 --- a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputConverterXML.java +++ b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputConverterXML.java @@ -32,7 +32,7 @@ import org.slf4j.LoggerFactory; /** * XML implementation of {@link InputConverter}. * - * @author Tomasz Maruszak - Initial contribution. + * @author Tomasz Maruszak - Initial contribution * */ public class InputConverterXML implements InputConverter { diff --git a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputWithNavigationControlXML.java b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputWithNavigationControlXML.java index 74e9e06a7..e4756630e 100644 --- a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputWithNavigationControlXML.java +++ b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputWithNavigationControlXML.java @@ -43,8 +43,8 @@ import org.w3c.dom.Node; * menu.goToPath(menuDir); * menu.selectItem(stationName); * + * @author Dennis Frommknecht - Initial contribution * @author David Graeff - Completely refactored class - * @author Dennis Frommknecht - Initial idea and implementaton * @author Tomasz Maruszak - Refactor */ public class InputWithNavigationControlXML extends AbstractInputControlXML implements InputWithNavigationControl { diff --git a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputWithPlayControlXML.java b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputWithPlayControlXML.java index 9decf1ad7..25b1fe441 100644 --- a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputWithPlayControlXML.java +++ b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputWithPlayControlXML.java @@ -45,7 +45,7 @@ import org.w3c.dom.Node; * No state will be saved in here, but in {@link PlayInfoState} and * {@link PresetInfoState} instead. * - * @author David Graeff + * @author David Graeff - Initial contribution * @author Tomasz Maruszak - Spotify support, refactoring */ public class InputWithPlayControlXML extends AbstractInputControlXML implements InputWithPlayControl { diff --git a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputWithPresetControlXML.java b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputWithPresetControlXML.java index 9251cca84..04305656f 100644 --- a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputWithPresetControlXML.java +++ b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputWithPresetControlXML.java @@ -43,7 +43,7 @@ import org.w3c.dom.Node; * No state will be saved in here, but in {@link PlayInfoState} and * {@link PresetInfoState} instead. * - * @author David Graeff + * @author David Graeff - Initial contribution * @author Tomasz Maruszak - Compatibility fixes */ public class InputWithPresetControlXML extends AbstractInputControlXML implements InputWithPresetControl { diff --git a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/XMLUtils.java b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/XMLUtils.java index 41a8fc43f..447d9ebd3 100644 --- a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/XMLUtils.java +++ b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/XMLUtils.java @@ -42,7 +42,7 @@ public class XMLUtils { private static final Logger LOG = LoggerFactory.getLogger(XMLUtils.class); // We need a lot of xml parsing. Create a document builder beforehand. - static final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + static final DocumentBuilderFactory DBF = DocumentBuilderFactory.newInstance(); static Node getNode(Node parent, String[] nodePath, int offset) { if (parent == null) { @@ -174,12 +174,12 @@ public class XMLUtils { try { // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html - dbf.setFeature("http://xml.org/sax/features/external-general-entities", false); - dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false); - dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); - dbf.setXIncludeAware(false); - dbf.setExpandEntityReferences(false); - return dbf.newDocumentBuilder().parse(new InputSource(new StringReader(response))); + DBF.setFeature("http://xml.org/sax/features/external-general-entities", false); + DBF.setFeature("http://xml.org/sax/features/external-parameter-entities", false); + DBF.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); + DBF.setXIncludeAware(false); + DBF.setExpandEntityReferences(false); + return DBF.newDocumentBuilder().parse(new InputSource(new StringReader(response))); } catch (SAXException | ParserConfigurationException e) { throw new ReceivedMessageParseException(e); }