]> git.basschouten.com Git - openhab-addons.git/commitdiff
[yamahareceiver] Show correct status detail on failure (#15510)
authorlsiepel <leosiepel@gmail.com>
Sun, 27 Aug 2023 21:53:02 +0000 (23:53 +0200)
committerGitHub <noreply@github.com>
Sun, 27 Aug 2023 21:53:02 +0000 (23:53 +0200)
* Partial fix #7667
* java 17 instanceof
* Checkstyle

Signed-off-by: Leo Siepel <leosiepel@gmail.com>
bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/discovery/ZoneDiscoveryService.java
bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/handler/YamahaBridgeHandler.java
bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/handler/YamahaZoneThingHandler.java
bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/InputConverter.java
bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/DeviceInformationXML.java
bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputConverterXML.java
bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputWithNavigationControlXML.java
bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputWithPlayControlXML.java
bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/InputWithPresetControlXML.java
bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/protocol/xml/XMLUtils.java

index 3684dd7206234484ecea99da565a3abc53d240d5..0f79e46f9ab591f5a866710fedd216485d68eb67 100644 (file)
@@ -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;
         }
     }
 
index d450c17a51ca62ff4b7ca01eb3598902fa64c001..3a8b150e598e9bc324e4b1f7be2cf6d59ca5ea12 100644 (file)
@@ -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);
index 2d3a0ca21161030d8c82ea171637b3df6f87bc95..011252dea6ec5ea4e5533a2f46f7fd2d9943c718 100644 (file)
@@ -87,7 +87,7 @@ import org.slf4j.LoggerFactory;
  * class {@link ZoneControlXML}, {@link InputWithPlayControlXML} and {@link InputWithNavigationControlXML}
  * for communication.
  *
- * @author David Graeff <david.graeff@web.de>
+ * @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 : "");
index 5bc70f8b343933db7d2ce27abc53f3a30fddb6a9..bd2a8d6196af09b160aa27c203966bcff992d1cd 100644 (file)
@@ -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 {
 
index 9f68db839759ba67bcde6b146209556b402e9b1e..9603243f91a5ad8db4b5537291c35ab5ae0b0b03 100644 (file)
@@ -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;
     }
 
index 3b3e42bbc2c56e1ea9c924b6c681285e0ea78314..83b5152373e567c0afe6e4d3c3a1e0fbfc3bcc57 100644 (file)
@@ -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 {
index 74e9e06a7821bbee4bed0b87bf595f6a4b892921..e4756630e7f16dc0f65936a039095ab3ef95b17f 100644 (file)
@@ -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 {
index 9decf1ad7057a2529337241e8b0c5963505991ea..25b1fe4410935487a107478d9991e0ea7ca2b0ed 100644 (file)
@@ -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 {
index 9251cca84602f3159640974533656853a12a20bf..04305656f093fdadb09688effea2602d20c29299 100644 (file)
@@ -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 {
index 41a8fc43f10a26d3a83acc9480e44c5b24cdf734..447d9ebd30c42635c963268da47f518223a22af4 100644 (file)
@@ -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);
         }