Fix SamsungTVWS mocking in samsungtv tests (#66650)
Co-authored-by: epenet <epenet@users.noreply.github.com>pull/66691/head
parent
41c43fe639
commit
14e48bac3a
|
@ -2,6 +2,8 @@
|
||||||
from unittest.mock import Mock, patch
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from samsungctl import Remote
|
||||||
|
from samsungtvws import SamsungTVWS
|
||||||
|
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
|
@ -20,10 +22,9 @@ def fake_host_fixture() -> None:
|
||||||
def remote_fixture():
|
def remote_fixture():
|
||||||
"""Patch the samsungctl Remote."""
|
"""Patch the samsungctl Remote."""
|
||||||
with patch("homeassistant.components.samsungtv.bridge.Remote") as remote_class:
|
with patch("homeassistant.components.samsungtv.bridge.Remote") as remote_class:
|
||||||
remote = Mock()
|
remote = Mock(Remote)
|
||||||
remote.__enter__ = Mock()
|
remote.__enter__ = Mock()
|
||||||
remote.__exit__ = Mock()
|
remote.__exit__ = Mock()
|
||||||
remote.port.return_value = 55000
|
|
||||||
remote_class.return_value = remote
|
remote_class.return_value = remote
|
||||||
yield remote
|
yield remote
|
||||||
|
|
||||||
|
@ -34,10 +35,9 @@ def remotews_fixture():
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.samsungtv.bridge.SamsungTVWS"
|
"homeassistant.components.samsungtv.bridge.SamsungTVWS"
|
||||||
) as remotews_class:
|
) as remotews_class:
|
||||||
remotews = Mock()
|
remotews = Mock(SamsungTVWS)
|
||||||
remotews.__enter__ = Mock()
|
remotews.__enter__ = Mock(return_value=remotews)
|
||||||
remotews.__exit__ = Mock()
|
remotews.__exit__ = Mock()
|
||||||
remotews.port.return_value = 8002
|
|
||||||
remotews.rest_device_info.return_value = {
|
remotews.rest_device_info.return_value = {
|
||||||
"id": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
|
"id": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
|
||||||
"device": {
|
"device": {
|
||||||
|
@ -48,8 +48,8 @@ def remotews_fixture():
|
||||||
"networkType": "wireless",
|
"networkType": "wireless",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
remotews.token = "FAKE_TOKEN"
|
||||||
remotews_class.return_value = remotews
|
remotews_class.return_value = remotews
|
||||||
remotews_class().__enter__().token = "FAKE_TOKEN"
|
|
||||||
yield remotews
|
yield remotews
|
||||||
|
|
||||||
|
|
||||||
|
@ -59,12 +59,12 @@ def remotews_no_device_info_fixture():
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.samsungtv.bridge.SamsungTVWS"
|
"homeassistant.components.samsungtv.bridge.SamsungTVWS"
|
||||||
) as remotews_class:
|
) as remotews_class:
|
||||||
remotews = Mock()
|
remotews = Mock(SamsungTVWS)
|
||||||
remotews.__enter__ = Mock()
|
remotews.__enter__ = Mock(return_value=remotews)
|
||||||
remotews.__exit__ = Mock()
|
remotews.__exit__ = Mock()
|
||||||
remotews.rest_device_info.return_value = None
|
remotews.rest_device_info.return_value = None
|
||||||
|
remotews.token = "FAKE_TOKEN"
|
||||||
remotews_class.return_value = remotews
|
remotews_class.return_value = remotews
|
||||||
remotews_class().__enter__().token = "FAKE_TOKEN"
|
|
||||||
yield remotews
|
yield remotews
|
||||||
|
|
||||||
|
|
||||||
|
@ -74,8 +74,8 @@ def remotews_soundbar_fixture():
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.samsungtv.bridge.SamsungTVWS"
|
"homeassistant.components.samsungtv.bridge.SamsungTVWS"
|
||||||
) as remotews_class:
|
) as remotews_class:
|
||||||
remotews = Mock()
|
remotews = Mock(SamsungTVWS)
|
||||||
remotews.__enter__ = Mock()
|
remotews.__enter__ = Mock(return_value=remotews)
|
||||||
remotews.__exit__ = Mock()
|
remotews.__exit__ = Mock()
|
||||||
remotews.rest_device_info.return_value = {
|
remotews.rest_device_info.return_value = {
|
||||||
"id": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
|
"id": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
|
||||||
|
@ -87,8 +87,8 @@ def remotews_soundbar_fixture():
|
||||||
"type": "Samsung SoundBar",
|
"type": "Samsung SoundBar",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
remotews.token = "FAKE_TOKEN"
|
||||||
remotews_class.return_value = remotews
|
remotews_class.return_value = remotews
|
||||||
remotews_class().__enter__().token = "FAKE_TOKEN"
|
|
||||||
yield remotews
|
yield remotews
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
"""Tests for Samsung TV config flow."""
|
"""Tests for Samsung TV config flow."""
|
||||||
import socket
|
import socket
|
||||||
from unittest.mock import Mock, PropertyMock, call, patch
|
from unittest.mock import Mock, call, patch
|
||||||
|
|
||||||
from samsungctl.exceptions import AccessDenied, UnhandledResponse
|
from samsungctl.exceptions import AccessDenied, UnhandledResponse
|
||||||
|
from samsungtvws import SamsungTVWS
|
||||||
from samsungtvws.exceptions import ConnectionFailure, HttpApiError
|
from samsungtvws.exceptions import ConnectionFailure, HttpApiError
|
||||||
from websocket import WebSocketException, WebSocketProtocolException
|
from websocket import WebSocketException, WebSocketProtocolException
|
||||||
|
|
||||||
|
@ -799,10 +800,8 @@ async def test_autodetect_websocket(hass: HomeAssistant):
|
||||||
"homeassistant.components.samsungtv.bridge.Remote",
|
"homeassistant.components.samsungtv.bridge.Remote",
|
||||||
side_effect=OSError("Boom"),
|
side_effect=OSError("Boom"),
|
||||||
), patch("homeassistant.components.samsungtv.bridge.SamsungTVWS") as remotews:
|
), patch("homeassistant.components.samsungtv.bridge.SamsungTVWS") as remotews:
|
||||||
enter = Mock()
|
remote = Mock(SamsungTVWS)
|
||||||
type(enter).token = PropertyMock(return_value="123456789")
|
remote.__enter__ = Mock(return_value=remote)
|
||||||
remote = Mock()
|
|
||||||
remote.__enter__ = Mock(return_value=enter)
|
|
||||||
remote.__exit__ = Mock(return_value=False)
|
remote.__exit__ = Mock(return_value=False)
|
||||||
remote.rest_device_info.return_value = {
|
remote.rest_device_info.return_value = {
|
||||||
"id": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
|
"id": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
|
||||||
|
@ -816,6 +815,7 @@ async def test_autodetect_websocket(hass: HomeAssistant):
|
||||||
"type": "Samsung SmartTV",
|
"type": "Samsung SmartTV",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
remote.token = "123456789"
|
||||||
remotews.return_value = remote
|
remotews.return_value = remote
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
@ -846,10 +846,8 @@ async def test_websocket_no_mac(hass: HomeAssistant):
|
||||||
) as remotews, patch(
|
) as remotews, patch(
|
||||||
"getmac.get_mac_address", return_value="gg:hh:ii:ll:mm:nn"
|
"getmac.get_mac_address", return_value="gg:hh:ii:ll:mm:nn"
|
||||||
):
|
):
|
||||||
enter = Mock()
|
remote = Mock(SamsungTVWS)
|
||||||
type(enter).token = PropertyMock(return_value="123456789")
|
remote.__enter__ = Mock(return_value=remote)
|
||||||
remote = Mock()
|
|
||||||
remote.__enter__ = Mock(return_value=enter)
|
|
||||||
remote.__exit__ = Mock(return_value=False)
|
remote.__exit__ = Mock(return_value=False)
|
||||||
remote.rest_device_info.return_value = {
|
remote.rest_device_info.return_value = {
|
||||||
"id": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
|
"id": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
|
||||||
|
@ -861,6 +859,7 @@ async def test_websocket_no_mac(hass: HomeAssistant):
|
||||||
"type": "Samsung SmartTV",
|
"type": "Samsung SmartTV",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
remote.token = "123456789"
|
||||||
remotews.return_value = remote
|
remotews.return_value = remote
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
|
|
@ -2,10 +2,11 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
from unittest.mock import DEFAULT as DEFAULT_MOCK, Mock, PropertyMock, call, patch
|
from unittest.mock import DEFAULT as DEFAULT_MOCK, Mock, call, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from samsungctl import exceptions
|
from samsungctl import exceptions
|
||||||
|
from samsungtvws import SamsungTVWS
|
||||||
from samsungtvws.exceptions import ConnectionFailure
|
from samsungtvws.exceptions import ConnectionFailure
|
||||||
from websocket import WebSocketException
|
from websocket import WebSocketException
|
||||||
|
|
||||||
|
@ -151,10 +152,8 @@ async def test_setup_without_turnon(hass, remote):
|
||||||
async def test_setup_websocket(hass, remotews):
|
async def test_setup_websocket(hass, remotews):
|
||||||
"""Test setup of platform."""
|
"""Test setup of platform."""
|
||||||
with patch("homeassistant.components.samsungtv.bridge.SamsungTVWS") as remote_class:
|
with patch("homeassistant.components.samsungtv.bridge.SamsungTVWS") as remote_class:
|
||||||
enter = Mock()
|
remote = Mock(SamsungTVWS)
|
||||||
type(enter).token = PropertyMock(return_value="987654321")
|
remote.__enter__ = Mock(return_value=remote)
|
||||||
remote = Mock()
|
|
||||||
remote.__enter__ = Mock(return_value=enter)
|
|
||||||
remote.__exit__ = Mock()
|
remote.__exit__ = Mock()
|
||||||
remote.rest_device_info.return_value = {
|
remote.rest_device_info.return_value = {
|
||||||
"id": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
|
"id": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
|
||||||
|
@ -166,6 +165,7 @@ async def test_setup_websocket(hass, remotews):
|
||||||
"networkType": "wireless",
|
"networkType": "wireless",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
remote.token = "987654321"
|
||||||
remote_class.return_value = remote
|
remote_class.return_value = remote
|
||||||
|
|
||||||
await setup_samsungtv(hass, MOCK_CONFIGWS)
|
await setup_samsungtv(hass, MOCK_CONFIGWS)
|
||||||
|
@ -200,10 +200,8 @@ async def test_setup_websocket_2(hass, mock_now):
|
||||||
assert entry is config_entries[0]
|
assert entry is config_entries[0]
|
||||||
|
|
||||||
with patch("homeassistant.components.samsungtv.bridge.SamsungTVWS") as remote_class:
|
with patch("homeassistant.components.samsungtv.bridge.SamsungTVWS") as remote_class:
|
||||||
enter = Mock()
|
remote = Mock(SamsungTVWS)
|
||||||
type(enter).token = PropertyMock(return_value="987654321")
|
remote.__enter__ = Mock(return_value=remote)
|
||||||
remote = Mock()
|
|
||||||
remote.__enter__ = Mock(return_value=enter)
|
|
||||||
remote.__exit__ = Mock()
|
remote.__exit__ = Mock()
|
||||||
remote.rest_device_info.return_value = {
|
remote.rest_device_info.return_value = {
|
||||||
"id": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
|
"id": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
|
||||||
|
@ -215,6 +213,7 @@ async def test_setup_websocket_2(hass, mock_now):
|
||||||
"networkType": "wireless",
|
"networkType": "wireless",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
remote.token = "987654321"
|
||||||
remote_class.return_value = remote
|
remote_class.return_value = remote
|
||||||
assert await async_setup_component(hass, SAMSUNGTV_DOMAIN, {})
|
assert await async_setup_component(hass, SAMSUNGTV_DOMAIN, {})
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
Loading…
Reference in New Issue