]> git.basschouten.com Git - openhab-addons.git/commitdiff
[roku] Improve TV discovery model name and add timeout (#16210)
authormlobstein <michael.lobstein@gmail.com>
Mon, 8 Jan 2024 19:18:05 +0000 (13:18 -0600)
committerGitHub <noreply@github.com>
Mon, 8 Jan 2024 19:18:05 +0000 (20:18 +0100)
Signed-off-by: Michael Lobstein <michael.lobstein@gmail.com>
bundles/org.openhab.binding.roku/README.md
bundles/org.openhab.binding.roku/src/main/java/org/openhab/binding/roku/internal/communication/RokuCommunicator.java
bundles/org.openhab.binding.roku/src/main/java/org/openhab/binding/roku/internal/discovery/RokuDiscoveryService.java
bundles/org.openhab.binding.roku/src/main/java/org/openhab/binding/roku/internal/handler/RokuHandler.java

index fa3f7836700bf20b974a1652e3cb869dab0af16d..1257eabf6df17edf71fc0943d5ce4c39ea89e7dc 100644 (file)
@@ -54,7 +54,7 @@ The following channels are available:
 
 Some Notes:
 
-- The values for `activeApp`, `playMode`, `timeElapsed`, `timeTotal`, `activeChannel`, `signalMode`, `signalQuality`, `channelName`, `programTitle`, `programDescription` & `programRating` refresh automatically per the configured `refresh` interval (10 seconds minimum).
+- The values for `activeApp`, `activeAppName`, `playMode`, `timeElapsed`, `timeTotal`, `activeChannel`, `signalMode`, `signalQuality`, `channelName`, `programTitle`, `programDescription`, `programRating`, `power` & `powerState` refresh automatically per the configured `refresh` interval.
 
 **List of available button commands for Roku streaming devices:**  
 Home  
@@ -109,7 +109,7 @@ roku:roku_tv:mytv1 "My Roku TV" [ hostName="192.168.10.1", refresh=10 ]
 
 String Player_ActiveApp        "Current App: [%s]"         { channel="roku:roku_player:myplayer1:activeApp" }
 String Player_ActiveAppName    "Current App Name: [%s]"    { channel="roku:roku_player:myplayer1:activeAppName" }
-String Player_Button           "Send Command to Roku"      { channel="roku:roku_player:myplayer1:button" }
+String Player_Button           "Send Command to Roku"      { channel="roku:roku_player:myplayer1:button", autoupdate="false" }
 Player Player_Control          "Control"                   { channel="roku:roku_player:myplayer1:control" }
 String Player_PlayMode         "Status: [%s]"              { channel="roku:roku_player:myplayer1:playMode" }
 Number:Time Player_TimeElapsed "Elapsed Time: [%d %unit%]" { channel="roku:roku_player:myplayer1:timeElapsed" }
@@ -117,9 +117,11 @@ Number:Time Player_TimeTotal   "Total Time: [%d %unit%]"   { channel="roku:roku_
 
 // Roku TV items:
 
+Switch Player_Power              "Power: [%s]"               { channel="roku:roku_tv:mytv1:power" }
+String Player_PowerState         "Power State: [%s]          { channel="roku:roku_tv:mytv1:powerState" }
 String Player_ActiveApp          "Current App: [%s]"         { channel="roku:roku_tv:mytv1:activeApp" }
 String Player_ActiveAppName      "Current App Name: [%s]"    { channel="roku:roku_tv:mytv1:activeAppName" }
-String Player_Button             "Send Command to Roku"      { channel="roku:roku_tv:mytv1:button" }
+String Player_Button             "Send Command to Roku"      { channel="roku:roku_tv:mytv1:button", autoupdate="false" }
 Player Player_Control            "Control"                   { channel="roku:roku_tv:mytv1:control" }
 String Player_PlayMode           "Status: [%s]"              { channel="roku:roku_tv:mytv1:playMode" }
 Number:Time Player_TimeElapsed   "Elapsed Time: [%d %unit%]" { channel="roku:roku_tv:mytv1:timeElapsed" }
@@ -131,9 +133,6 @@ String Player_ChannelName        "Channel Name: [%s]"        { channel="roku:rok
 String Player_ProgramTitle       "Program Title: [%s]"       { channel="roku:roku_tv:mytv1:programTitle" }
 String Player_ProgramDescription "Program Description: [%s]" { channel="roku:roku_tv:mytv1:programDescription" }
 String Player_ProgramRating      "Program Rating: [%s]"      { channel="roku:roku_tv:mytv1:programRating" }
-Switch Player_Power              "Power: [%s]"               { channel="roku:roku_tv:mytv1:power" }
-String Player_PowerState         "Power State: [%s]          { channel="roku:roku_tv:mytv1:powerState" }
-
 ```
 
 ### roku.sitemap:
@@ -149,6 +148,8 @@ sitemap roku label="Roku" {
         Text item=Player_TimeElapsed icon="time"
         Text item=Player_TimeTotal icon="time"
         // The following items apply to Roku TVs only
+        Switch item=Player_Power
+        Text item=Player_PowerState
         Selection item=Player_ActiveChannel icon="screen"
         Text item=Player_SignalMode
         Text item=Player_SignalQuality
@@ -156,8 +157,6 @@ sitemap roku label="Roku" {
         Text item=Player_ProgramTitle
         Text item=Player_ProgramDescription
         Text item=Player_ProgramRating
-        Switch item=Player_Power
-        Text item=Player_PowerState
     }
 }
 ```
index 586460858f8f9d0605219d5769bbc39a6932360e..91c80dade6dd34f741c9ed66093bd534362e614d 100644 (file)
@@ -15,6 +15,7 @@ package org.openhab.binding.roku.internal.communication;
 import java.io.StringReader;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
 import javax.xml.bind.JAXBContext;
@@ -43,6 +44,8 @@ import org.openhab.binding.roku.internal.dto.TvChannels.Channel;
  */
 @NonNullByDefault
 public class RokuCommunicator {
+    private static final int REQUEST_TIMEOUT = 5000;
+
     private final HttpClient httpClient;
 
     private final String urlKeyPress;
@@ -265,7 +268,8 @@ public class RokuCommunicator {
      */
     private String getCommand(String url) throws RokuHttpException {
         try {
-            return httpClient.GET(url).getContentAsString();
+            return httpClient.newRequest(url).method(HttpMethod.GET).timeout(REQUEST_TIMEOUT, TimeUnit.MILLISECONDS)
+                    .send().getContentAsString();
         } catch (TimeoutException | ExecutionException e) {
             throw new RokuHttpException("Error executing GET command for URL: " + url, e);
         } catch (InterruptedException e) {
@@ -282,7 +286,7 @@ public class RokuCommunicator {
      */
     private void postCommand(String url) throws RokuHttpException {
         try {
-            httpClient.POST(url).method(HttpMethod.POST).send();
+            httpClient.POST(url).method(HttpMethod.POST).timeout(REQUEST_TIMEOUT, TimeUnit.MILLISECONDS).send();
         } catch (TimeoutException | ExecutionException e) {
             throw new RokuHttpException("Error executing POST command, URL: " + url, e);
         } catch (InterruptedException e) {
index 4de31f229d873afaccaeda2133d582e6440ca638..a08592e49b98d1b97d57138196acc4e1cafab069 100644 (file)
@@ -253,7 +253,10 @@ public class RokuDiscoveryService extends AbstractDiscoveryService {
         try {
             RokuCommunicator communicator = new RokuCommunicator(httpClient, host, port);
             DeviceInfo device = communicator.getDeviceInfo();
-            label = device.getModelName() + " " + device.getModelNumber();
+
+            // replace extraneous characters with spaces and remove any consecutive spaces
+            label = (device.getFriendlyModelName() + " " + device.getUserDeviceLocation())
+                    .replaceAll("[^a-zA-Z0-9\\-_]", " ").trim().replaceAll("  +", " ");
             if (device.isTv()) {
                 thingUid = new ThingUID(THING_TYPE_ROKU_TV, uuid);
             }
index cd55019f3ca0309e05e8d91de910d1d94e2bf43d..9df1b33ea06c318f3e2acabb672a49c57a090830 100644 (file)
@@ -114,6 +114,7 @@ public class RokuHandler extends BaseThingHandler {
             thing.setProperty(PROPERTY_SERIAL_NUMBER, deviceInfo.getSerialNumber());
             thing.setProperty(PROPERTY_DEVICE_ID, deviceInfo.getDeviceId());
             thing.setProperty(PROPERTY_SOFTWARE_VERSION, deviceInfo.getSoftwareVersion());
+            thing.setProperty(PROPERTY_UUID, deviceInfo.getSerialNumber().toLowerCase());
             updateStatus(ThingStatus.ONLINE);
         } catch (RokuHttpException e) {
             logger.debug("Unable to retrieve Roku device-info. Exception: {}", e.getMessage(), e);