From ac8c889b0fe99d1cf91b91beb76eb35d7dc06841 Mon Sep 17 00:00:00 2001 From: Paolo Tuninetto Date: Tue, 17 Mar 2020 00:37:10 +0100 Subject: [PATCH] Add default port to samsung tv (#32820) * Default port for websocket tv * Update config entry * move bridge creation * fix indent * remove loop --- homeassistant/components/samsungtv/bridge.py | 2 ++ .../components/samsungtv/media_player.py | 26 ++++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/samsungtv/bridge.py b/homeassistant/components/samsungtv/bridge.py index 31f102a62a4..b582f6269e4 100644 --- a/homeassistant/components/samsungtv/bridge.py +++ b/homeassistant/components/samsungtv/bridge.py @@ -46,6 +46,7 @@ class SamsungTVBridge(ABC): self.method = method self.host = host self.token = None + self.default_port = None self._remote = None self._callback = None @@ -191,6 +192,7 @@ class SamsungTVWSBridge(SamsungTVBridge): """Initialize Bridge.""" super().__init__(method, host, port) self.token = token + self.default_port = 8001 def try_connect(self): """Try to connect to the Websocket TV.""" diff --git a/homeassistant/components/samsungtv/media_player.py b/homeassistant/components/samsungtv/media_player.py index 8fa6a93088a..8f12341ee4a 100644 --- a/homeassistant/components/samsungtv/media_player.py +++ b/homeassistant/components/samsungtv/media_player.py @@ -71,13 +71,27 @@ async def async_setup_entry(hass, config_entry, async_add_entities): ): turn_on_action = hass.data[DOMAIN][ip_address][CONF_ON_ACTION] on_script = Script(hass, turn_on_action) - async_add_entities([SamsungTVDevice(config_entry, on_script)]) + + # Initialize bridge + data = config_entry.data.copy() + bridge = SamsungTVBridge.get_bridge( + data[CONF_METHOD], data[CONF_HOST], data[CONF_PORT], data.get(CONF_TOKEN), + ) + if bridge.port is None and bridge.default_port is not None: + # For backward compat, set default port for websocket tv + data[CONF_PORT] = bridge.default_port + hass.config_entries.async_update_entry(config_entry, data=data) + bridge = SamsungTVBridge.get_bridge( + data[CONF_METHOD], data[CONF_HOST], data[CONF_PORT], data.get(CONF_TOKEN), + ) + + async_add_entities([SamsungTVDevice(bridge, config_entry, on_script)]) class SamsungTVDevice(MediaPlayerDevice): """Representation of a Samsung TV.""" - def __init__(self, config_entry, on_script): + def __init__(self, bridge, config_entry, on_script): """Initialize the Samsung device.""" self._config_entry = config_entry self._manufacturer = config_entry.data.get(CONF_MANUFACTURER) @@ -93,13 +107,7 @@ class SamsungTVDevice(MediaPlayerDevice): # Mark the end of a shutdown command (need to wait 15 seconds before # sending the next command to avoid turning the TV back ON). self._end_of_power_off = None - # Initialize bridge - self._bridge = SamsungTVBridge.get_bridge( - config_entry.data[CONF_METHOD], - config_entry.data[CONF_HOST], - config_entry.data[CONF_PORT], - config_entry.data.get(CONF_TOKEN), - ) + self._bridge = bridge self._bridge.register_reauth_callback(self.access_denied) def access_denied(self):