diff --git a/homeassistant/components/samsungtv/bridge.py b/homeassistant/components/samsungtv/bridge.py index 4e53b0fd0a5..fd258f562b4 100644 --- a/homeassistant/components/samsungtv/bridge.py +++ b/homeassistant/components/samsungtv/bridge.py @@ -534,16 +534,21 @@ class SamsungTVWSBridge(SamsungTVBridge): ) try: await self._remote.start_listening(self._remote_event) - except ConnectionClosedError as err: - # This is only happening when the auth was switched to DENY - # A removed auth will lead to socket timeout because waiting - # for auth popup is just an open socket + except UnauthorizedError as err: LOGGER.info( "Failed to get remote for %s, re-authentication required: %s", self.host, err.__repr__(), ) self._notify_reauth_callback() + self._remote = None + except ConnectionClosedError as err: + LOGGER.info( + "Failed to get remote for %s: %s", + self.host, + err.__repr__(), + ) + self._remote = None except ConnectionFailure as err: LOGGER.warning( "Unexpected ConnectionFailure trying to get remote for %s, " diff --git a/tests/components/samsungtv/test_media_player.py b/tests/components/samsungtv/test_media_player.py index 346d04e9a65..f40777ccff5 100644 --- a/tests/components/samsungtv/test_media_player.py +++ b/tests/components/samsungtv/test_media_player.py @@ -12,7 +12,7 @@ from samsungtvws.encrypted.remote import ( SamsungTVEncryptedCommand, SamsungTVEncryptedWSAsyncRemote, ) -from samsungtvws.exceptions import ConnectionFailure, HttpApiError +from samsungtvws.exceptions import ConnectionFailure, HttpApiError, UnauthorizedError from samsungtvws.remote import ChannelEmitCommand, SendRemoteKey from websockets.exceptions import ConnectionClosedError, WebSocketException @@ -478,6 +478,24 @@ async def test_update_ws_connection_closed( async_fire_time_changed(hass, next_update) await hass.async_block_till_done() + state = hass.states.get(ENTITY_ID) + assert state.state == STATE_OFF + + +async def test_update_ws_unauthorized_error( + hass: HomeAssistant, mock_now: datetime, remotews: Mock +) -> None: + """Testing update tv unauthorized failure exception.""" + await setup_samsungtv(hass, MOCK_CONFIGWS) + + with patch.object( + remotews, "start_listening", side_effect=UnauthorizedError + ), patch.object(remotews, "is_alive", return_value=False): + next_update = mock_now + timedelta(minutes=5) + with patch("homeassistant.util.dt.utcnow", return_value=next_update): + async_fire_time_changed(hass, next_update) + await hass.async_block_till_done() + assert [ flow for flow in hass.config_entries.flow.async_progress()