Move SamsungTV test constants to fixture files (#144086)

This commit is contained in:
epenet 2025-05-02 08:32:44 +02:00 committed by GitHub
parent 4e8d68a2ef
commit fca62f1ae8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 283 additions and 219 deletions

View File

@ -19,9 +19,11 @@ from samsungtvws.event import ED_INSTALLED_APP_EVENT
from samsungtvws.exceptions import ResponseError
from samsungtvws.remote import ChannelEmitCommand
from homeassistant.components.samsungtv.const import WEBSOCKET_SSL_PORT
from homeassistant.components.samsungtv.const import DOMAIN, WEBSOCKET_SSL_PORT
from .const import SAMPLE_DEVICE_INFO_UE48JU6400, SAMPLE_DEVICE_INFO_WIFI
from .const import SAMPLE_DEVICE_INFO_WIFI
from tests.common import load_json_object_fixture
@pytest.fixture
@ -186,7 +188,7 @@ def rest_api_fixture_non_ssl_only() -> Generator[None]:
"""Mock rest_device_info to fail for ssl and work for non-ssl."""
if self.port == WEBSOCKET_SSL_PORT:
raise ResponseError
return SAMPLE_DEVICE_INFO_UE48JU6400
return load_json_object_fixture("device_info_UE48JU6400.json", DOMAIN)
with patch(
"homeassistant.components.samsungtv.bridge.SamsungTVAsyncRest",

View File

@ -1,7 +1,5 @@
"""Constants for the samsungtv tests."""
from samsungtvws.event import ED_INSTALLED_APP_EVENT
from homeassistant.components.samsungtv.const import (
CONF_SESSION_ID,
METHOD_LEGACY,
@ -94,102 +92,3 @@ SAMPLE_DEVICE_INFO_WIFI = {
"networkType": "wireless",
},
}
SAMPLE_DEVICE_INFO_FRAME = {
"device": {
"FrameTVSupport": "true",
"GamePadSupport": "true",
"ImeSyncedSupport": "true",
"OS": "Tizen",
"TokenAuthSupport": "true",
"VoiceSupport": "true",
"countryCode": "FR",
"description": "Samsung DTV RCR",
"developerIP": "0.0.0.0",
"developerMode": "0",
"duid": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
"firmwareVersion": "Unknown",
"id": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
"ip": "1.2.3.4",
"model": "17_KANTM_UHD",
"modelName": "UE43LS003",
"name": "[TV] Samsung Frame (43)",
"networkType": "wired",
"resolution": "3840x2160",
"smartHubAgreement": "true",
"type": "Samsung SmartTV",
"udn": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
"wifiMac": "aa:ee:tt:hh:ee:rr",
},
"id": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
"isSupport": (
'{"DMP_DRM_PLAYREADY":"false","DMP_DRM_WIDEVINE":"false","DMP_available":"true",'
'"EDEN_available":"true","FrameTVSupport":"true","ImeSyncedSupport":"true",'
'"TokenAuthSupport":"true","remote_available":"true","remote_fourDirections":"true",'
'"remote_touchPad":"true","remote_voiceControl":"true"}\n'
),
"name": "[TV] Samsung Frame (43)",
"remote": "1.0",
"type": "Samsung SmartTV",
"uri": "https://1.2.3.4:8002/api/v2/",
"version": "2.0.25",
}
SAMPLE_DEVICE_INFO_UE48JU6400 = {
"id": "uuid:223da676-497a-4e06-9507-5e27ec4f0fb3",
"name": "[TV] TV-UE48JU6470",
"version": "2.0.25",
"device": {
"type": "Samsung SmartTV",
"duid": "uuid:223da676-497a-4e06-9507-5e27ec4f0fb3",
"model": "15_HAWKM_UHD_2D",
"modelName": "UE48JU6400",
"description": "Samsung DTV RCR",
"networkType": "wired",
"ssid": "",
"ip": "1.2.3.4",
"firmwareVersion": "Unknown",
"name": "[TV] TV-UE48JU6470",
"id": "uuid:223da676-497a-4e06-9507-5e27ec4f0fb3",
"udn": "uuid:223da676-497a-4e06-9507-5e27ec4f0fb3",
"resolution": "1920x1080",
"countryCode": "AT",
"msfVersion": "2.0.25",
"smartHubAgreement": "true",
"wifiMac": "aa:bb:aa:aa:aa:aa",
"developerMode": "0",
"developerIP": "",
},
"type": "Samsung SmartTV",
"uri": "https://1.2.3.4:8002/api/v2/",
}
SAMPLE_EVENT_ED_INSTALLED_APP = {
"event": ED_INSTALLED_APP_EVENT,
"from": "host",
"data": {
"data": [
{
"appId": "111299001912",
"app_type": 2,
"icon": "/opt/share/webappservice/apps_icon/FirstScreen/111299001912/250x250.png",
"is_lock": 0,
"name": "YouTube",
},
{
"appId": "3201608010191",
"app_type": 2,
"icon": "/opt/share/webappservice/apps_icon/FirstScreen/3201608010191/250x250.png",
"is_lock": 0,
"name": "Deezer",
},
{
"appId": "3201606009684",
"app_type": 2,
"icon": "/opt/share/webappservice/apps_icon/FirstScreen/3201606009684/250x250.png",
"is_lock": 0,
"name": "Spotify - Music and Podcasts",
},
]
},
}

View File

@ -0,0 +1,34 @@
{
"device": {
"FrameTVSupport": "true",
"GamePadSupport": "true",
"ImeSyncedSupport": "true",
"OS": "Tizen",
"TokenAuthSupport": "true",
"VoiceSupport": "true",
"countryCode": "FR",
"description": "Samsung DTV RCR",
"developerIP": "0.0.0.0",
"developerMode": "0",
"duid": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
"firmwareVersion": "Unknown",
"id": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
"ip": "1.2.3.4",
"model": "17_KANTM_UHD",
"modelName": "UE43LS003",
"name": "[TV] Samsung Frame (43)",
"networkType": "wired",
"resolution": "3840x2160",
"smartHubAgreement": "true",
"type": "Samsung SmartTV",
"udn": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
"wifiMac": "aa:ee:tt:hh:ee:rr"
},
"id": "uuid:be9554b9-c9fb-41f4-8920-22da015376a4",
"isSupport": "{\"DMP_DRM_PLAYREADY\":\"false\",\"DMP_DRM_WIDEVINE\":\"false\",\"DMP_available\":\"true\",\"EDEN_available\":\"true\",\"FrameTVSupport\":\"true\",\"ImeSyncedSupport\":\"true\",\"TokenAuthSupport\":\"true\",\"remote_available\":\"true\",\"remote_fourDirections\":\"true\",\"remote_touchPad\":\"true\",\"remote_voiceControl\":\"true\"}\n",
"name": "[TV] Samsung Frame (43)",
"remote": "1.0",
"type": "Samsung SmartTV",
"uri": "https://1.2.3.4:8002/api/v2/",
"version": "2.0.25"
}

View File

@ -0,0 +1,28 @@
{
"id": "uuid:223da676-497a-4e06-9507-5e27ec4f0fb3",
"name": "[TV] TV-UE48JU6470",
"version": "2.0.25",
"device": {
"type": "Samsung SmartTV",
"duid": "uuid:223da676-497a-4e06-9507-5e27ec4f0fb3",
"model": "15_HAWKM_UHD_2D",
"modelName": "UE48JU6400",
"description": "Samsung DTV RCR",
"networkType": "wired",
"ssid": "",
"ip": "1.2.3.4",
"firmwareVersion": "Unknown",
"name": "[TV] TV-UE48JU6470",
"id": "uuid:223da676-497a-4e06-9507-5e27ec4f0fb3",
"udn": "uuid:223da676-497a-4e06-9507-5e27ec4f0fb3",
"resolution": "1920x1080",
"countryCode": "AT",
"msfVersion": "2.0.25",
"smartHubAgreement": "true",
"wifiMac": "aa:bb:aa:aa:aa:aa",
"developerMode": "0",
"developerIP": ""
},
"type": "Samsung SmartTV",
"uri": "https://1.2.3.4:8002/api/v2/"
}

View File

@ -0,0 +1,29 @@
{
"event": "ed.installedApp.get",
"from": "host",
"data": {
"data": [
{
"appId": "111299001912",
"app_type": 2,
"icon": "/opt/share/webappservice/apps_icon/FirstScreen/111299001912/250x250.png",
"is_lock": 0,
"name": "YouTube"
},
{
"appId": "3201608010191",
"app_type": 2,
"icon": "/opt/share/webappservice/apps_icon/FirstScreen/3201608010191/250x250.png",
"is_lock": 0,
"name": "Deezer"
},
{
"appId": "3201606009684",
"app_type": 2,
"icon": "/opt/share/webappservice/apps_icon/FirstScreen/3201606009684/250x250.png",
"is_lock": 0,
"name": "Spotify - Music and Podcasts"
}
]
}
}

View File

@ -0,0 +1,137 @@
# serializer version: 1
# name: test_entry_diagnostics
dict({
'device_info': dict({
'device': dict({
'modelName': '82GXARRS',
'name': '[TV] Living Room',
'networkType': 'wireless',
'type': 'Samsung SmartTV',
'wifiMac': 'aa:bb:aa:aa:aa:aa',
}),
'id': 'uuid:be9554b9-c9fb-41f4-8920-22da015376a4',
}),
'entry': dict({
'data': dict({
'host': 'fake_host',
'ip_address': 'test',
'mac': 'aa:bb:cc:dd:ee:ff',
'method': 'websocket',
'model': '82GXARRS',
'name': 'fake',
'port': 8002,
'token': '**REDACTED**',
}),
'disabled_by': None,
'discovery_keys': dict({
}),
'domain': 'samsungtv',
'entry_id': '123456',
'minor_version': 2,
'options': dict({
}),
'pref_disable_new_entities': False,
'pref_disable_polling': False,
'source': 'user',
'subentries': list([
]),
'title': 'Mock Title',
'unique_id': 'be9554b9-c9fb-41f4-8920-22da015376a4',
'version': 2,
}),
})
# ---
# name: test_entry_diagnostics_encrypte_offline
dict({
'device_info': None,
'entry': dict({
'data': dict({
'host': 'fake_host',
'ip_address': 'test',
'mac': 'aa:bb:cc:dd:ee:ff',
'method': 'encrypted',
'name': 'fake',
'port': 8000,
'session_id': '**REDACTED**',
'token': '**REDACTED**',
}),
'disabled_by': None,
'discovery_keys': dict({
}),
'domain': 'samsungtv',
'entry_id': '123456',
'minor_version': 2,
'options': dict({
}),
'pref_disable_new_entities': False,
'pref_disable_polling': False,
'source': 'user',
'subentries': list([
]),
'title': 'Mock Title',
'unique_id': 'be9554b9-c9fb-41f4-8920-22da015376a4',
'version': 2,
}),
})
# ---
# name: test_entry_diagnostics_encrypted
dict({
'device_info': dict({
'device': dict({
'countryCode': 'AT',
'description': 'Samsung DTV RCR',
'developerIP': '',
'developerMode': '0',
'duid': 'uuid:223da676-497a-4e06-9507-5e27ec4f0fb3',
'firmwareVersion': 'Unknown',
'id': 'uuid:223da676-497a-4e06-9507-5e27ec4f0fb3',
'ip': '1.2.3.4',
'model': '15_HAWKM_UHD_2D',
'modelName': 'UE48JU6400',
'msfVersion': '2.0.25',
'name': '[TV] TV-UE48JU6470',
'networkType': 'wired',
'resolution': '1920x1080',
'smartHubAgreement': 'true',
'ssid': '',
'type': 'Samsung SmartTV',
'udn': 'uuid:223da676-497a-4e06-9507-5e27ec4f0fb3',
'wifiMac': 'aa:bb:aa:aa:aa:aa',
}),
'id': 'uuid:223da676-497a-4e06-9507-5e27ec4f0fb3',
'name': '[TV] TV-UE48JU6470',
'type': 'Samsung SmartTV',
'uri': 'https://1.2.3.4:8002/api/v2/',
'version': '2.0.25',
}),
'entry': dict({
'data': dict({
'host': 'fake_host',
'ip_address': 'test',
'mac': 'aa:bb:cc:dd:ee:ff',
'method': 'encrypted',
'model': 'UE48JU6400',
'name': 'fake',
'port': 8000,
'session_id': '**REDACTED**',
'token': '**REDACTED**',
}),
'disabled_by': None,
'discovery_keys': dict({
}),
'domain': 'samsungtv',
'entry_id': '123456',
'minor_version': 2,
'options': dict({
}),
'pref_disable_new_entities': False,
'pref_disable_polling': False,
'source': 'user',
'subentries': list([
]),
'title': 'Mock Title',
'unique_id': 'be9554b9-c9fb-41f4-8920-22da015376a4',
'version': 2,
}),
})
# ---

View File

@ -63,10 +63,9 @@ from .const import (
MOCK_ENTRYDATA_WS,
MOCK_SSDP_DATA_MAIN_TV_AGENT_ST,
MOCK_SSDP_DATA_RENDERING_CONTROL_ST,
SAMPLE_DEVICE_INFO_FRAME,
)
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, load_json_object_fixture
RESULT_ALREADY_CONFIGURED = "already_configured"
RESULT_ALREADY_IN_PROGRESS = "already_in_progress"
@ -956,7 +955,9 @@ async def test_dhcp_wireless(hass: HomeAssistant) -> None:
async def test_dhcp_wired(hass: HomeAssistant, rest_api: Mock) -> None:
"""Test starting a flow from dhcp."""
# Even though it is named "wifiMac", it matches the mac of the wired connection
rest_api.rest_device_info.return_value = SAMPLE_DEVICE_INFO_FRAME
rest_api.rest_device_info.return_value = load_json_object_fixture(
"device_info_UE43LS003.json", DOMAIN
)
# confirm to add the entry
result = await hass.config_entries.flow.async_init(
DOMAIN,

View File

@ -4,138 +4,63 @@ from unittest.mock import Mock
import pytest
from samsungtvws.exceptions import HttpApiError
from syrupy.assertion import SnapshotAssertion
from syrupy.filters import props
from homeassistant.components.diagnostics import REDACTED
from homeassistant.components.samsungtv.const import DOMAIN
from homeassistant.core import HomeAssistant
from . import setup_samsungtv_entry
from .const import (
MOCK_ENTRY_WS_WITH_MAC,
MOCK_ENTRYDATA_ENCRYPTED_WS,
SAMPLE_DEVICE_INFO_UE48JU6400,
SAMPLE_DEVICE_INFO_WIFI,
)
from .const import MOCK_ENTRY_WS_WITH_MAC, MOCK_ENTRYDATA_ENCRYPTED_WS
from tests.common import ANY
from tests.common import load_json_object_fixture
from tests.components.diagnostics import get_diagnostics_for_config_entry
from tests.typing import ClientSessionGenerator
@pytest.mark.usefixtures("remotews", "rest_api")
async def test_entry_diagnostics(
hass: HomeAssistant, hass_client: ClientSessionGenerator
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
snapshot: SnapshotAssertion,
) -> None:
"""Test config entry diagnostics."""
config_entry = await setup_samsungtv_entry(hass, MOCK_ENTRY_WS_WITH_MAC)
assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == {
"entry": {
"created_at": ANY,
"data": {
"host": "fake_host",
"ip_address": "test",
"mac": "aa:bb:cc:dd:ee:ff",
"method": "websocket",
"model": "82GXARRS",
"name": "fake",
"port": 8002,
"token": REDACTED,
},
"disabled_by": None,
"discovery_keys": {},
"domain": "samsungtv",
"entry_id": "123456",
"minor_version": 2,
"modified_at": ANY,
"options": {},
"pref_disable_new_entities": False,
"pref_disable_polling": False,
"source": "user",
"subentries": [],
"title": "Mock Title",
"unique_id": "be9554b9-c9fb-41f4-8920-22da015376a4",
"version": 2,
},
"device_info": SAMPLE_DEVICE_INFO_WIFI,
}
assert await get_diagnostics_for_config_entry(
hass, hass_client, config_entry
) == snapshot(exclude=props("created_at", "modified_at"))
@pytest.mark.usefixtures("remoteencws")
async def test_entry_diagnostics_encrypted(
hass: HomeAssistant, rest_api: Mock, hass_client: ClientSessionGenerator
hass: HomeAssistant,
rest_api: Mock,
hass_client: ClientSessionGenerator,
snapshot: SnapshotAssertion,
) -> None:
"""Test config entry diagnostics."""
rest_api.rest_device_info.return_value = SAMPLE_DEVICE_INFO_UE48JU6400
rest_api.rest_device_info.return_value = load_json_object_fixture(
"device_info_UE48JU6400.json", DOMAIN
)
config_entry = await setup_samsungtv_entry(hass, MOCK_ENTRYDATA_ENCRYPTED_WS)
assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == {
"entry": {
"created_at": ANY,
"data": {
"host": "fake_host",
"ip_address": "test",
"mac": "aa:bb:cc:dd:ee:ff",
"method": "encrypted",
"model": "UE48JU6400",
"name": "fake",
"port": 8000,
"token": REDACTED,
"session_id": REDACTED,
},
"disabled_by": None,
"discovery_keys": {},
"domain": "samsungtv",
"entry_id": "123456",
"minor_version": 2,
"modified_at": ANY,
"options": {},
"pref_disable_new_entities": False,
"pref_disable_polling": False,
"source": "user",
"subentries": [],
"title": "Mock Title",
"unique_id": "be9554b9-c9fb-41f4-8920-22da015376a4",
"version": 2,
},
"device_info": SAMPLE_DEVICE_INFO_UE48JU6400,
}
assert await get_diagnostics_for_config_entry(
hass, hass_client, config_entry
) == snapshot(exclude=props("created_at", "modified_at"))
@pytest.mark.usefixtures("remoteencws")
async def test_entry_diagnostics_encrypte_offline(
hass: HomeAssistant, rest_api: Mock, hass_client: ClientSessionGenerator
hass: HomeAssistant,
rest_api: Mock,
hass_client: ClientSessionGenerator,
snapshot: SnapshotAssertion,
) -> None:
"""Test config entry diagnostics."""
rest_api.rest_device_info.side_effect = HttpApiError
config_entry = await setup_samsungtv_entry(hass, MOCK_ENTRYDATA_ENCRYPTED_WS)
assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == {
"entry": {
"created_at": ANY,
"data": {
"host": "fake_host",
"ip_address": "test",
"mac": "aa:bb:cc:dd:ee:ff",
"method": "encrypted",
"name": "fake",
"port": 8000,
"token": REDACTED,
"session_id": REDACTED,
},
"disabled_by": None,
"discovery_keys": {},
"domain": "samsungtv",
"entry_id": "123456",
"minor_version": 2,
"modified_at": ANY,
"options": {},
"pref_disable_new_entities": False,
"pref_disable_polling": False,
"source": "user",
"subentries": [],
"title": "Mock Title",
"unique_id": "be9554b9-c9fb-41f4-8920-22da015376a4",
"version": 2,
},
"device_info": None,
}
assert await get_diagnostics_for_config_entry(
hass, hass_client, config_entry
) == snapshot(exclude=props("created_at", "modified_at"))

View File

@ -45,10 +45,9 @@ from .const import (
MOCK_ENTRYDATA_WS,
MOCK_SSDP_DATA_MAIN_TV_AGENT_ST,
MOCK_SSDP_DATA_RENDERING_CONTROL_ST,
SAMPLE_DEVICE_INFO_UE48JU6400,
)
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, load_json_object_fixture
ENTITY_ID = f"{MP_DOMAIN}.fake_name"
MOCK_CONFIG = {
@ -117,7 +116,9 @@ async def test_setup_h_j_model(
hass: HomeAssistant, rest_api: Mock, caplog: pytest.LogCaptureFixture
) -> None:
"""Test Samsung TV integration is setup."""
rest_api.rest_device_info.return_value = SAMPLE_DEVICE_INFO_UE48JU6400
rest_api.rest_device_info.return_value = load_json_object_fixture(
"device_info_UE48JU6400.json", DOMAIN
)
await setup_samsungtv_entry(hass, MOCK_CONFIG)
await hass.async_block_till_done()
state = hass.states.get(ENTITY_ID)

View File

@ -85,12 +85,14 @@ from .const import (
MOCK_CONFIG,
MOCK_ENTRY_WS_WITH_MAC,
MOCK_ENTRYDATA_ENCRYPTED_WS,
SAMPLE_DEVICE_INFO_FRAME,
SAMPLE_DEVICE_INFO_WIFI,
SAMPLE_EVENT_ED_INSTALLED_APP,
)
from tests.common import MockConfigEntry, async_fire_time_changed
from tests.common import (
MockConfigEntry,
async_fire_time_changed,
load_json_object_fixture,
)
ENTITY_ID = f"{MP_DOMAIN}.fake"
MOCK_CONFIGWS = {
@ -689,7 +691,9 @@ async def test_turn_off_websocket(
hass: HomeAssistant, remotews: Mock, caplog: pytest.LogCaptureFixture
) -> None:
"""Test for turn_off."""
remotews.app_list_data = SAMPLE_EVENT_ED_INSTALLED_APP
remotews.app_list_data = load_json_object_fixture(
"ws_installed_app_event.json", DOMAIN
)
with patch(
"homeassistant.components.samsungtv.bridge.Remote",
side_effect=[OSError("Boom"), DEFAULT_MOCK],
@ -728,7 +732,9 @@ async def test_turn_off_websocket_frame(
hass: HomeAssistant, remotews: Mock, rest_api: Mock
) -> None:
"""Test for turn_off."""
rest_api.rest_device_info.return_value = SAMPLE_DEVICE_INFO_FRAME
rest_api.rest_device_info.return_value = load_json_object_fixture(
"device_info_UE43LS003.json", DOMAIN
)
with patch(
"homeassistant.components.samsungtv.bridge.Remote",
side_effect=[OSError("Boom"), DEFAULT_MOCK],
@ -1136,7 +1142,9 @@ async def test_play_media_app(hass: HomeAssistant, remotews: Mock) -> None:
@pytest.mark.usefixtures("rest_api")
async def test_select_source_app(hass: HomeAssistant, remotews: Mock) -> None:
"""Test for select_source."""
remotews.app_list_data = SAMPLE_EVENT_ED_INSTALLED_APP
remotews.app_list_data = load_json_object_fixture(
"ws_installed_app_event.json", DOMAIN
)
await setup_samsungtv_entry(hass, MOCK_CONFIGWS)
remotews.send_commands.reset_mock()