]> git.basschouten.com Git - openhab-addons.git/commitdiff
[kodi] Reduce polling by checking linkage first (#8912)
authorChristoph Weitkamp <github@christophweitkamp.de>
Sun, 1 Nov 2020 21:29:18 +0000 (22:29 +0100)
committerGitHub <noreply@github.com>
Sun, 1 Nov 2020 21:29:18 +0000 (22:29 +0100)
* Reduce polling by checking linkage first

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
bundles/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/handler/KodiHandler.java
bundles/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/protocol/KodiConnection.java

index 5ef7b0da807fd7808e2d260801a196b3c464c5a0..a6e7c34348812ecb8709c0468b4ea1ca1ba7a42a 100644 (file)
@@ -91,6 +91,9 @@ public class KodiHandler extends BaseThingHandler implements KodiEventListener {
     private final KodiDynamicCommandDescriptionProvider commandDescriptionProvider;
     private final KodiDynamicStateDescriptionProvider stateDescriptionProvider;
 
+    private final ChannelUID volumeChannelUID;
+    private final ChannelUID mutedChannelUID;
+    private final ChannelUID favoriteChannelUID;
     private final ChannelUID profileChannelUID;
 
     private ScheduledFuture<?> connectionCheckerFuture;
@@ -105,6 +108,9 @@ public class KodiHandler extends BaseThingHandler implements KodiEventListener {
         this.commandDescriptionProvider = commandDescriptionProvider;
         this.stateDescriptionProvider = stateDescriptionProvider;
 
+        volumeChannelUID = new ChannelUID(getThing().getUID(), CHANNEL_VOLUME);
+        mutedChannelUID = new ChannelUID(getThing().getUID(), CHANNEL_MUTE);
+        favoriteChannelUID = new ChannelUID(getThing().getUID(), CHANNEL_PLAYFAVORITE);
         profileChannelUID = new ChannelUID(getThing().getUID(), CHANNEL_PROFILE);
     }
 
@@ -208,9 +214,9 @@ public class KodiHandler extends BaseThingHandler implements KodiEventListener {
             case CHANNEL_PLAYFAVORITE:
                 if (command instanceof StringType) {
                     playFavorite(command);
-                    updateState(CHANNEL_PLAYFAVORITE, UnDefType.UNDEF);
+                    updateState(favoriteChannelUID, UnDefType.UNDEF);
                 } else if (RefreshType.REFRESH == command) {
-                    updateState(CHANNEL_PLAYFAVORITE, UnDefType.UNDEF);
+                    updateState(favoriteChannelUID, UnDefType.UNDEF);
                 }
                 break;
             case CHANNEL_PVR_OPEN_TV:
@@ -267,6 +273,8 @@ public class KodiHandler extends BaseThingHandler implements KodiEventListener {
             case CHANNEL_PROFILE:
                 if (command instanceof StringType) {
                     connection.profile(command.toString());
+                } else if (RefreshType.REFRESH == command) {
+                    connection.updateCurrentProfile();
                 }
                 break;
             case CHANNEL_ARTIST:
@@ -644,13 +652,12 @@ public class KodiHandler extends BaseThingHandler implements KodiEventListener {
     }
 
     private void updateFavoriteChannelStateDescription() {
-        if (isLinked(CHANNEL_PLAYFAVORITE)) {
+        if (isLinked(favoriteChannelUID)) {
             List<StateOption> options = new ArrayList<>();
             for (KodiFavorite favorite : connection.getFavorites()) {
                 options.add(new StateOption(favorite.getTitle(), favorite.getTitle()));
             }
-            stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), CHANNEL_PLAYFAVORITE),
-                    options);
+            stateDescriptionProvider.setStateOptions(favoriteChannelUID, options);
         }
     }
 
@@ -707,7 +714,9 @@ public class KodiHandler extends BaseThingHandler implements KodiEventListener {
         if (connected) {
             updateStatus(ThingStatus.ONLINE);
             scheduler.schedule(() -> connection.getSystemProperties(), 1, TimeUnit.SECONDS);
-            scheduler.schedule(() -> connection.updateVolume(), 1, TimeUnit.SECONDS);
+            if (isLinked(volumeChannelUID) || isLinked(mutedChannelUID)) {
+                scheduler.schedule(() -> connection.updateVolume(), 1, TimeUnit.SECONDS);
+            }
             if (isLinked(profileChannelUID)) {
                 scheduler.schedule(() -> connection.updateCurrentProfile(), 1, TimeUnit.SECONDS);
             }
@@ -732,7 +741,7 @@ public class KodiHandler extends BaseThingHandler implements KodiEventListener {
 
     @Override
     public void updateVolume(int volume) {
-        updateState(CHANNEL_VOLUME, new PercentType(volume));
+        updateState(volumeChannelUID, new PercentType(volume));
     }
 
     @Override
@@ -764,11 +773,7 @@ public class KodiHandler extends BaseThingHandler implements KodiEventListener {
 
     @Override
     public void updateMuted(boolean muted) {
-        if (muted) {
-            updateState(CHANNEL_MUTE, OnOffType.ON);
-        } else {
-            updateState(CHANNEL_MUTE, OnOffType.OFF);
-        }
+        updateState(mutedChannelUID, OnOffType.from(muted));
     }
 
     @Override
@@ -933,11 +938,7 @@ public class KodiHandler extends BaseThingHandler implements KodiEventListener {
 
     @Override
     public void updateSubtitleEnabled(boolean enabled) {
-        if (enabled) {
-            updateState(CHANNEL_SUBTITLE_ENABLED, OnOffType.ON);
-        } else {
-            updateState(CHANNEL_SUBTITLE_ENABLED, OnOffType.OFF);
-        }
+        updateState(CHANNEL_SUBTITLE_ENABLED, OnOffType.from(enabled));
     }
 
     @Override
index a1afeb163b3635bffb1fbc9471bb8be5725be2e9..3da014712bfb867a4c99e94afab119b9217b9414 100644 (file)
@@ -1117,8 +1117,8 @@ public class KodiConnection implements KodiClientSocketEventListener {
                 }
             }
         } else {
-            listener.updateMuted(false);
             listener.updateVolume(100);
+            listener.updateMuted(false);
         }
     }