Catch samsungtv websocket exceptions (#28849)

* catch websocket exceptions

* fix syntax

* use exceptions from base class

* add comments

* add test
This commit is contained in:
escoand 2019-11-25 10:45:50 +01:00 committed by cgtobi
parent 5b64052d69
commit 430f7bdfd4
2 changed files with 21 additions and 5 deletions

View File

@ -6,6 +6,7 @@ import socket
from samsungctl import exceptions as samsung_exceptions, Remote as SamsungRemote
import voluptuous as vol
import wakeonlan
from websocket import WebSocketException
from homeassistant.components.media_player import (
MediaPlayerDevice,
@ -209,23 +210,26 @@ class SamsungTVDevice(MediaPlayerDevice):
try:
self.get_remote().control(key)
break
except (samsung_exceptions.ConnectionClosed, BrokenPipeError):
except (
samsung_exceptions.ConnectionClosed,
BrokenPipeError,
WebSocketException,
):
# BrokenPipe can occur when the commands is sent to fast
# WebSocketException can occur when timed out
self._remote = None
self._state = STATE_ON
except AttributeError:
# Auto-detect could not find working config yet
pass
except (
samsung_exceptions.UnhandledResponse,
samsung_exceptions.AccessDenied,
):
except (samsung_exceptions.UnhandledResponse, samsung_exceptions.AccessDenied):
# We got a response so it's on.
self._state = STATE_ON
self._remote = None
LOGGER.debug("Failed sending command %s", key, exc_info=True)
return
except OSError:
# Different reasons, e.g. hostname not resolveable
self._state = STATE_OFF
self._remote = None
if self._power_off_in_progress():

View File

@ -8,6 +8,7 @@ from asynctest import mock
import pytest
from samsungctl import exceptions
from tests.common import async_fire_time_changed
from websocket import WebSocketException
from homeassistant.components.media_player import DEVICE_CLASS_TV
from homeassistant.components.media_player.const import (
@ -387,6 +388,17 @@ async def test_send_key_unhandled_response(hass, remote):
assert state.state == STATE_ON
async def test_send_key_websocketexception(hass, remote):
"""Testing unhandled response exception."""
await setup_samsungtv(hass, MOCK_CONFIG)
remote.control = mock.Mock(side_effect=WebSocketException("Boom"))
assert await hass.services.async_call(
DOMAIN, SERVICE_VOLUME_UP, {ATTR_ENTITY_ID: ENTITY_ID}, True
)
state = hass.states.get(ENTITY_ID)
assert state.state == STATE_ON
async def test_send_key_os_error(hass, remote):
"""Testing broken pipe Exception."""
await setup_samsungtv(hass, MOCK_CONFIG)