Add type hints in samsungtv tests (#66632)
Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""Tests for samsungtv component."""
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
from datetime import datetime, timedelta
|
||||
import logging
|
||||
from unittest.mock import DEFAULT as DEFAULT_MOCK, Mock, call, patch
|
||||
|
||||
@@ -56,6 +56,8 @@ from homeassistant.const import (
|
||||
STATE_ON,
|
||||
STATE_UNAVAILABLE,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
@@ -131,25 +133,28 @@ def delay_fixture():
|
||||
yield delay
|
||||
|
||||
|
||||
async def setup_samsungtv(hass, config):
|
||||
async def setup_samsungtv(hass: HomeAssistant, config: ConfigType) -> None:
|
||||
"""Set up mock Samsung TV."""
|
||||
await async_setup_component(hass, SAMSUNGTV_DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_setup_with_turnon(hass, remote):
|
||||
@pytest.mark.usefixtures("remote")
|
||||
async def test_setup_with_turnon(hass: HomeAssistant) -> None:
|
||||
"""Test setup of platform."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
assert hass.states.get(ENTITY_ID)
|
||||
|
||||
|
||||
async def test_setup_without_turnon(hass, remote):
|
||||
@pytest.mark.usefixtures("remote")
|
||||
async def test_setup_without_turnon(hass: HomeAssistant) -> None:
|
||||
"""Test setup of platform."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG_NOTURNON)
|
||||
assert hass.states.get(ENTITY_ID_NOTURNON)
|
||||
|
||||
|
||||
async def test_setup_websocket(hass, remotews):
|
||||
@pytest.mark.usefixtures("remotews")
|
||||
async def test_setup_websocket(hass: HomeAssistant) -> None:
|
||||
"""Test setup of platform."""
|
||||
with patch("homeassistant.components.samsungtv.bridge.SamsungTVWS") as remote_class:
|
||||
remote = Mock(SamsungTVWS)
|
||||
@@ -184,7 +189,7 @@ async def test_setup_websocket(hass, remotews):
|
||||
assert config_entries[0].data[CONF_MAC] == "aa:bb:cc:dd:ee:ff"
|
||||
|
||||
|
||||
async def test_setup_websocket_2(hass, mock_now):
|
||||
async def test_setup_websocket_2(hass: HomeAssistant, mock_now: datetime) -> None:
|
||||
"""Test setup of platform from config entry."""
|
||||
entity_id = f"{DOMAIN}.fake"
|
||||
|
||||
@@ -231,7 +236,8 @@ async def test_setup_websocket_2(hass, mock_now):
|
||||
assert remote_class.call_args_list[0] == call(**MOCK_CALLS_WS)
|
||||
|
||||
|
||||
async def test_update_on(hass, remote, mock_now):
|
||||
@pytest.mark.usefixtures("remote")
|
||||
async def test_update_on(hass: HomeAssistant, mock_now: datetime) -> None:
|
||||
"""Testing update tv on."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
|
||||
@@ -244,7 +250,8 @@ async def test_update_on(hass, remote, mock_now):
|
||||
assert state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_update_off(hass, remote, mock_now):
|
||||
@pytest.mark.usefixtures("remote")
|
||||
async def test_update_off(hass: HomeAssistant, mock_now: datetime) -> None:
|
||||
"""Testing update tv off."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
|
||||
@@ -262,7 +269,8 @@ async def test_update_off(hass, remote, mock_now):
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
|
||||
async def test_update_access_denied(hass, remote, mock_now):
|
||||
@pytest.mark.usefixtures("remote")
|
||||
async def test_update_access_denied(hass: HomeAssistant, mock_now: datetime) -> None:
|
||||
"""Testing update tv access denied exception."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
|
||||
@@ -288,7 +296,10 @@ async def test_update_access_denied(hass, remote, mock_now):
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
|
||||
async def test_update_connection_failure(hass, remotews, mock_now):
|
||||
@pytest.mark.usefixtures("remotews")
|
||||
async def test_update_connection_failure(
|
||||
hass: HomeAssistant, mock_now: datetime
|
||||
) -> None:
|
||||
"""Testing update tv connection failure exception."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.bridge.Remote",
|
||||
@@ -315,7 +326,10 @@ async def test_update_connection_failure(hass, remotews, mock_now):
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
|
||||
async def test_update_unhandled_response(hass, remote, mock_now):
|
||||
@pytest.mark.usefixtures("remote")
|
||||
async def test_update_unhandled_response(
|
||||
hass: HomeAssistant, mock_now: datetime
|
||||
) -> None:
|
||||
"""Testing update tv unhandled response exception."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
|
||||
@@ -333,7 +347,10 @@ async def test_update_unhandled_response(hass, remote, mock_now):
|
||||
assert state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_connection_closed_during_update_can_recover(hass, remote, mock_now):
|
||||
@pytest.mark.usefixtures("remote")
|
||||
async def test_connection_closed_during_update_can_recover(
|
||||
hass: HomeAssistant, mock_now: datetime
|
||||
) -> None:
|
||||
"""Testing update tv connection closed exception can recover."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
|
||||
@@ -359,7 +376,7 @@ async def test_connection_closed_during_update_can_recover(hass, remote, mock_no
|
||||
assert state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_send_key(hass, remote):
|
||||
async def test_send_key(hass: HomeAssistant, remote: Mock) -> None:
|
||||
"""Test for send key."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
assert await hass.services.async_call(
|
||||
@@ -374,7 +391,7 @@ async def test_send_key(hass, remote):
|
||||
assert state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_send_key_broken_pipe(hass, remote):
|
||||
async def test_send_key_broken_pipe(hass: HomeAssistant, remote: Mock) -> None:
|
||||
"""Testing broken pipe Exception."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
remote.control = Mock(side_effect=BrokenPipeError("Boom"))
|
||||
@@ -385,7 +402,9 @@ async def test_send_key_broken_pipe(hass, remote):
|
||||
assert state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_send_key_connection_closed_retry_succeed(hass, remote):
|
||||
async def test_send_key_connection_closed_retry_succeed(
|
||||
hass: HomeAssistant, remote: Mock
|
||||
) -> None:
|
||||
"""Test retry on connection closed."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
remote.control = Mock(
|
||||
@@ -406,7 +425,7 @@ async def test_send_key_connection_closed_retry_succeed(hass, remote):
|
||||
assert state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_send_key_unhandled_response(hass, remote):
|
||||
async def test_send_key_unhandled_response(hass: HomeAssistant, remote: Mock) -> None:
|
||||
"""Testing unhandled response exception."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
remote.control = Mock(side_effect=exceptions.UnhandledResponse("Boom"))
|
||||
@@ -417,7 +436,7 @@ async def test_send_key_unhandled_response(hass, remote):
|
||||
assert state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_send_key_websocketexception(hass, remote):
|
||||
async def test_send_key_websocketexception(hass: HomeAssistant, remote: Mock) -> None:
|
||||
"""Testing unhandled response exception."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
remote.control = Mock(side_effect=WebSocketException("Boom"))
|
||||
@@ -428,7 +447,7 @@ async def test_send_key_websocketexception(hass, remote):
|
||||
assert state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_send_key_os_error(hass, remote):
|
||||
async def test_send_key_os_error(hass: HomeAssistant, remote: Mock) -> None:
|
||||
"""Testing broken pipe Exception."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
remote.control = Mock(side_effect=OSError("Boom"))
|
||||
@@ -439,14 +458,16 @@ async def test_send_key_os_error(hass, remote):
|
||||
assert state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_name(hass, remote):
|
||||
@pytest.mark.usefixtures("remote")
|
||||
async def test_name(hass: HomeAssistant) -> None:
|
||||
"""Test for name property."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.attributes[ATTR_FRIENDLY_NAME] == "fake"
|
||||
|
||||
|
||||
async def test_state_with_turnon(hass, remote, delay):
|
||||
@pytest.mark.usefixtures("remote")
|
||||
async def test_state_with_turnon(hass: HomeAssistant, delay: Mock) -> None:
|
||||
"""Test for state property."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
assert await hass.services.async_call(
|
||||
@@ -463,7 +484,8 @@ async def test_state_with_turnon(hass, remote, delay):
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
|
||||
async def test_state_without_turnon(hass, remote):
|
||||
@pytest.mark.usefixtures("remote")
|
||||
async def test_state_without_turnon(hass: HomeAssistant) -> None:
|
||||
"""Test for state property."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG_NOTURNON)
|
||||
assert await hass.services.async_call(
|
||||
@@ -491,7 +513,8 @@ async def test_state_without_turnon(hass, remote):
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
|
||||
async def test_supported_features_with_turnon(hass, remote):
|
||||
@pytest.mark.usefixtures("remote")
|
||||
async def test_supported_features_with_turnon(hass: HomeAssistant) -> None:
|
||||
"""Test for supported_features property."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
@@ -500,21 +523,23 @@ async def test_supported_features_with_turnon(hass, remote):
|
||||
)
|
||||
|
||||
|
||||
async def test_supported_features_without_turnon(hass, remote):
|
||||
@pytest.mark.usefixtures("remote")
|
||||
async def test_supported_features_without_turnon(hass: HomeAssistant) -> None:
|
||||
"""Test for supported_features property."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG_NOTURNON)
|
||||
state = hass.states.get(ENTITY_ID_NOTURNON)
|
||||
assert state.attributes[ATTR_SUPPORTED_FEATURES] == SUPPORT_SAMSUNGTV
|
||||
|
||||
|
||||
async def test_device_class(hass, remote):
|
||||
@pytest.mark.usefixtures("remote")
|
||||
async def test_device_class(hass: HomeAssistant) -> None:
|
||||
"""Test for device_class property."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.attributes[ATTR_DEVICE_CLASS] is MediaPlayerDeviceClass.TV.value
|
||||
|
||||
|
||||
async def test_turn_off_websocket(hass, remotews):
|
||||
async def test_turn_off_websocket(hass: HomeAssistant, remotews: Mock) -> None:
|
||||
"""Test for turn_off."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.bridge.Remote",
|
||||
@@ -529,7 +554,7 @@ async def test_turn_off_websocket(hass, remotews):
|
||||
assert remotews.send_key.call_args_list == [call("KEY_POWER")]
|
||||
|
||||
|
||||
async def test_turn_off_legacy(hass, remote):
|
||||
async def test_turn_off_legacy(hass: HomeAssistant, remote: Mock) -> None:
|
||||
"""Test for turn_off."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG_NOTURNON)
|
||||
assert await hass.services.async_call(
|
||||
@@ -540,7 +565,9 @@ async def test_turn_off_legacy(hass, remote):
|
||||
assert remote.control.call_args_list == [call("KEY_POWEROFF")]
|
||||
|
||||
|
||||
async def test_turn_off_os_error(hass, remote, caplog):
|
||||
async def test_turn_off_os_error(
|
||||
hass: HomeAssistant, remote: Mock, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""Test for turn_off with OSError."""
|
||||
caplog.set_level(logging.DEBUG)
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
@@ -551,7 +578,7 @@ async def test_turn_off_os_error(hass, remote, caplog):
|
||||
assert "Could not establish connection" in caplog.text
|
||||
|
||||
|
||||
async def test_volume_up(hass, remote):
|
||||
async def test_volume_up(hass: HomeAssistant, remote: Mock) -> None:
|
||||
"""Test for volume_up."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
assert await hass.services.async_call(
|
||||
@@ -564,7 +591,7 @@ async def test_volume_up(hass, remote):
|
||||
assert remote.close.call_args_list == [call()]
|
||||
|
||||
|
||||
async def test_volume_down(hass, remote):
|
||||
async def test_volume_down(hass: HomeAssistant, remote: Mock) -> None:
|
||||
"""Test for volume_down."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
assert await hass.services.async_call(
|
||||
@@ -577,7 +604,7 @@ async def test_volume_down(hass, remote):
|
||||
assert remote.close.call_args_list == [call()]
|
||||
|
||||
|
||||
async def test_mute_volume(hass, remote):
|
||||
async def test_mute_volume(hass: HomeAssistant, remote: Mock) -> None:
|
||||
"""Test for mute_volume."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
assert await hass.services.async_call(
|
||||
@@ -593,7 +620,7 @@ async def test_mute_volume(hass, remote):
|
||||
assert remote.close.call_args_list == [call()]
|
||||
|
||||
|
||||
async def test_media_play(hass, remote):
|
||||
async def test_media_play(hass: HomeAssistant, remote: Mock) -> None:
|
||||
"""Test for media_play."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
assert await hass.services.async_call(
|
||||
@@ -615,7 +642,7 @@ async def test_media_play(hass, remote):
|
||||
assert remote.close.call_args_list == [call(), call()]
|
||||
|
||||
|
||||
async def test_media_pause(hass, remote):
|
||||
async def test_media_pause(hass: HomeAssistant, remote: Mock) -> None:
|
||||
"""Test for media_pause."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
assert await hass.services.async_call(
|
||||
@@ -637,7 +664,7 @@ async def test_media_pause(hass, remote):
|
||||
assert remote.close.call_args_list == [call(), call()]
|
||||
|
||||
|
||||
async def test_media_next_track(hass, remote):
|
||||
async def test_media_next_track(hass: HomeAssistant, remote: Mock) -> None:
|
||||
"""Test for media_next_track."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
assert await hass.services.async_call(
|
||||
@@ -650,7 +677,7 @@ async def test_media_next_track(hass, remote):
|
||||
assert remote.close.call_args_list == [call()]
|
||||
|
||||
|
||||
async def test_media_previous_track(hass, remote):
|
||||
async def test_media_previous_track(hass: HomeAssistant, remote: Mock) -> None:
|
||||
"""Test for media_previous_track."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
assert await hass.services.async_call(
|
||||
@@ -663,7 +690,8 @@ async def test_media_previous_track(hass, remote):
|
||||
assert remote.close.call_args_list == [call()]
|
||||
|
||||
|
||||
async def test_turn_on_with_turnon(hass, remote, delay):
|
||||
@pytest.mark.usefixtures("remote")
|
||||
async def test_turn_on_with_turnon(hass: HomeAssistant, delay: Mock) -> None:
|
||||
"""Test turn on."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
assert await hass.services.async_call(
|
||||
@@ -672,7 +700,8 @@ async def test_turn_on_with_turnon(hass, remote, delay):
|
||||
assert delay.call_count == 1
|
||||
|
||||
|
||||
async def test_turn_on_wol(hass, remotews):
|
||||
@pytest.mark.usefixtures("remotews")
|
||||
async def test_turn_on_wol(hass: HomeAssistant) -> None:
|
||||
"""Test turn on."""
|
||||
entry = MockConfigEntry(
|
||||
domain=SAMSUNGTV_DOMAIN,
|
||||
@@ -692,7 +721,7 @@ async def test_turn_on_wol(hass, remotews):
|
||||
assert mock_send_magic_packet.called
|
||||
|
||||
|
||||
async def test_turn_on_without_turnon(hass, remote):
|
||||
async def test_turn_on_without_turnon(hass: HomeAssistant, remote: Mock) -> None:
|
||||
"""Test turn on."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG_NOTURNON)
|
||||
assert await hass.services.async_call(
|
||||
@@ -702,7 +731,7 @@ async def test_turn_on_without_turnon(hass, remote):
|
||||
assert remote.control.call_count == 0
|
||||
|
||||
|
||||
async def test_play_media(hass, remote):
|
||||
async def test_play_media(hass: HomeAssistant, remote: Mock) -> None:
|
||||
"""Test for play_media."""
|
||||
asyncio_sleep = asyncio.sleep
|
||||
sleeps = []
|
||||
@@ -736,7 +765,7 @@ async def test_play_media(hass, remote):
|
||||
assert len(sleeps) == 3
|
||||
|
||||
|
||||
async def test_play_media_invalid_type(hass):
|
||||
async def test_play_media_invalid_type(hass: HomeAssistant) -> None:
|
||||
"""Test for play_media with invalid media type."""
|
||||
with patch("homeassistant.components.samsungtv.bridge.Remote") as remote:
|
||||
url = "https://example.com"
|
||||
@@ -758,7 +787,7 @@ async def test_play_media_invalid_type(hass):
|
||||
assert remote.call_count == 1
|
||||
|
||||
|
||||
async def test_play_media_channel_as_string(hass):
|
||||
async def test_play_media_channel_as_string(hass: HomeAssistant) -> None:
|
||||
"""Test for play_media with invalid channel as string."""
|
||||
with patch("homeassistant.components.samsungtv.bridge.Remote") as remote:
|
||||
url = "https://example.com"
|
||||
@@ -780,7 +809,7 @@ async def test_play_media_channel_as_string(hass):
|
||||
assert remote.call_count == 1
|
||||
|
||||
|
||||
async def test_play_media_channel_as_non_positive(hass):
|
||||
async def test_play_media_channel_as_non_positive(hass: HomeAssistant) -> None:
|
||||
"""Test for play_media with invalid channel as non positive integer."""
|
||||
with patch("homeassistant.components.samsungtv.bridge.Remote") as remote:
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
@@ -801,7 +830,7 @@ async def test_play_media_channel_as_non_positive(hass):
|
||||
assert remote.call_count == 1
|
||||
|
||||
|
||||
async def test_select_source(hass, remote):
|
||||
async def test_select_source(hass: HomeAssistant, remote: Mock) -> None:
|
||||
"""Test for select_source."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
assert await hass.services.async_call(
|
||||
@@ -817,7 +846,7 @@ async def test_select_source(hass, remote):
|
||||
assert remote.close.call_args_list == [call()]
|
||||
|
||||
|
||||
async def test_select_source_invalid_source(hass):
|
||||
async def test_select_source_invalid_source(hass: HomeAssistant) -> None:
|
||||
"""Test for select_source with invalid source."""
|
||||
with patch("homeassistant.components.samsungtv.bridge.Remote") as remote:
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
|
||||
Reference in New Issue
Block a user