mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 01:37:08 +00:00
Start of using hass state for tests rather than direct object (#29377)
This commit is contained in:
parent
8a5bce81c8
commit
84a7115435
@ -16,6 +16,7 @@ MOCK_TURN_ON = {
|
|||||||
"data": {"entity_id": "switch.test"},
|
"data": {"entity_id": "switch.test"},
|
||||||
}
|
}
|
||||||
MOCK_NAME = "dummy"
|
MOCK_NAME = "dummy"
|
||||||
|
MOCK_ENTITY_ID = "media_player.arcam_fmj_1"
|
||||||
MOCK_CONFIG = DEVICE_SCHEMA({CONF_HOST: MOCK_HOST, CONF_PORT: MOCK_PORT})
|
MOCK_CONFIG = DEVICE_SCHEMA({CONF_HOST: MOCK_HOST, CONF_PORT: MOCK_PORT})
|
||||||
|
|
||||||
|
|
||||||
@ -41,6 +42,10 @@ def state_fixture(client):
|
|||||||
state.client = client
|
state.client = client
|
||||||
state.zn = 1
|
state.zn = 1
|
||||||
state.get_power.return_value = True
|
state.get_power.return_value = True
|
||||||
|
state.get_volume.return_value = 0.0
|
||||||
|
state.get_source_list.return_value = []
|
||||||
|
state.get_incoming_audio_format.return_value = (0, 0)
|
||||||
|
state.get_mute.return_value = None
|
||||||
return state
|
return state
|
||||||
|
|
||||||
|
|
||||||
@ -48,5 +53,7 @@ def state_fixture(client):
|
|||||||
def player_fixture(hass, state):
|
def player_fixture(hass, state):
|
||||||
"""Get standard player."""
|
"""Get standard player."""
|
||||||
player = ArcamFmj(state, MOCK_NAME, None)
|
player = ArcamFmj(state, MOCK_NAME, None)
|
||||||
|
player.entity_id = MOCK_ENTITY_ID
|
||||||
|
player.hass = hass
|
||||||
player.async_schedule_update_ha_state = Mock()
|
player.async_schedule_update_ha_state = Mock()
|
||||||
return player
|
return player
|
||||||
|
@ -5,18 +5,10 @@ from arcam.fmj import DecodeMode2CH, DecodeModeMCH, IncomingAudioFormat, SourceC
|
|||||||
from asynctest.mock import ANY, MagicMock, Mock, PropertyMock, patch
|
from asynctest.mock import ANY, MagicMock, Mock, PropertyMock, patch
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.arcam_fmj.const import (
|
|
||||||
DOMAIN,
|
|
||||||
SIGNAL_CLIENT_DATA,
|
|
||||||
SIGNAL_CLIENT_STARTED,
|
|
||||||
SIGNAL_CLIENT_STOPPED,
|
|
||||||
)
|
|
||||||
from homeassistant.components.arcam_fmj.media_player import ArcamFmj
|
|
||||||
from homeassistant.components.media_player.const import MEDIA_TYPE_MUSIC
|
from homeassistant.components.media_player.const import MEDIA_TYPE_MUSIC
|
||||||
from homeassistant.const import STATE_OFF, STATE_ON
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .conftest import MOCK_HOST, MOCK_NAME, MOCK_PORT
|
from .conftest import MOCK_HOST, MOCK_NAME, MOCK_PORT, MOCK_ENTITY_ID
|
||||||
|
|
||||||
MOCK_TURN_ON = {
|
MOCK_TURN_ON = {
|
||||||
"service": "switch.turn_on",
|
"service": "switch.turn_on",
|
||||||
@ -24,50 +16,69 @@ MOCK_TURN_ON = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def update(player, force_refresh=False):
|
||||||
|
"""Force a update of player and return current state data."""
|
||||||
|
await player.async_update_ha_state(force_refresh=force_refresh)
|
||||||
|
return player.hass.states.get(player.entity_id)
|
||||||
|
|
||||||
|
|
||||||
async def test_properties(player, state):
|
async def test_properties(player, state):
|
||||||
"""Test standard properties."""
|
"""Test standard properties."""
|
||||||
assert player.unique_id is None
|
assert player.unique_id is None
|
||||||
assert player.device_info == {
|
assert player.device_info == {
|
||||||
"identifiers": {(DOMAIN, MOCK_HOST, MOCK_PORT)},
|
"identifiers": {("arcam_fmj", MOCK_HOST, MOCK_PORT)},
|
||||||
"model": "FMJ",
|
"model": "FMJ",
|
||||||
"manufacturer": "Arcam",
|
"manufacturer": "Arcam",
|
||||||
}
|
}
|
||||||
assert not player.should_poll
|
assert not player.should_poll
|
||||||
|
|
||||||
|
|
||||||
async def test_powered_off(player, state):
|
async def test_powered_off(hass, player, state):
|
||||||
"""Test properties in powered off state."""
|
"""Test properties in powered off state."""
|
||||||
state.get_source.return_value = None
|
state.get_source.return_value = None
|
||||||
state.get_power.return_value = None
|
state.get_power.return_value = None
|
||||||
assert player.source is None
|
|
||||||
assert player.state == STATE_OFF
|
data = await update(player)
|
||||||
|
assert "source" not in data.attributes
|
||||||
|
assert data.state == "off"
|
||||||
|
|
||||||
|
|
||||||
async def test_powered_on(player, state):
|
async def test_powered_on(player, state):
|
||||||
"""Test properties in powered on state."""
|
"""Test properties in powered on state."""
|
||||||
state.get_source.return_value = SourceCodes.PVR
|
state.get_source.return_value = SourceCodes.PVR
|
||||||
state.get_power.return_value = True
|
state.get_power.return_value = True
|
||||||
assert player.source == "PVR"
|
|
||||||
assert player.state == STATE_ON
|
data = await update(player)
|
||||||
|
assert data.attributes["source"] == "PVR"
|
||||||
|
assert data.state == "on"
|
||||||
|
|
||||||
|
|
||||||
async def test_supported_features_no_service(player, state):
|
async def test_supported_features_no_service(player, state):
|
||||||
"""Test support when turn on service exist."""
|
"""Test support when turn on service exist."""
|
||||||
state.get_power.return_value = None
|
state.get_power.return_value = None
|
||||||
assert player.supported_features == 68876
|
data = await update(player)
|
||||||
|
assert data.attributes["supported_features"] == 68876
|
||||||
|
|
||||||
state.get_power.return_value = False
|
state.get_power.return_value = False
|
||||||
assert player.supported_features == 69004
|
data = await update(player)
|
||||||
|
assert data.attributes["supported_features"] == 69004
|
||||||
|
|
||||||
|
|
||||||
async def test_supported_features_service(hass, state):
|
async def test_supported_features_service(hass, state):
|
||||||
"""Test support when turn on service exist."""
|
"""Test support when turn on service exist."""
|
||||||
|
from homeassistant.components.arcam_fmj.media_player import ArcamFmj
|
||||||
|
|
||||||
player = ArcamFmj(state, "dummy", MOCK_TURN_ON)
|
player = ArcamFmj(state, "dummy", MOCK_TURN_ON)
|
||||||
|
player.hass = hass
|
||||||
|
player.entity_id = MOCK_ENTITY_ID
|
||||||
|
|
||||||
state.get_power.return_value = None
|
state.get_power.return_value = None
|
||||||
assert player.supported_features == 69004
|
data = await update(player)
|
||||||
|
assert data.attributes["supported_features"] == 69004
|
||||||
|
|
||||||
state.get_power.return_value = False
|
state.get_power.return_value = False
|
||||||
assert player.supported_features == 69004
|
data = await update(player)
|
||||||
|
assert data.attributes["supported_features"] == 69004
|
||||||
|
|
||||||
|
|
||||||
async def test_turn_on_without_service(player, state):
|
async def test_turn_on_without_service(player, state):
|
||||||
@ -83,8 +94,11 @@ async def test_turn_on_without_service(player, state):
|
|||||||
|
|
||||||
async def test_turn_on_with_service(hass, state):
|
async def test_turn_on_with_service(hass, state):
|
||||||
"""Test support when turn on service exist."""
|
"""Test support when turn on service exist."""
|
||||||
|
from homeassistant.components.arcam_fmj.media_player import ArcamFmj
|
||||||
|
|
||||||
player = ArcamFmj(state, "dummy", MOCK_TURN_ON)
|
player = ArcamFmj(state, "dummy", MOCK_TURN_ON)
|
||||||
player.hass = Mock(HomeAssistant)
|
player.hass = Mock(HomeAssistant)
|
||||||
|
player.entity_id = MOCK_ENTITY_ID
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.arcam_fmj.media_player.async_call_from_config"
|
"homeassistant.components.arcam_fmj.media_player.async_call_from_config"
|
||||||
) as async_call_from_config:
|
) as async_call_from_config:
|
||||||
@ -122,7 +136,7 @@ async def test_name(player):
|
|||||||
|
|
||||||
async def test_update(player, state):
|
async def test_update(player, state):
|
||||||
"""Test update."""
|
"""Test update."""
|
||||||
await player.async_update()
|
await update(player, force_refresh=True)
|
||||||
state.update.assert_called_with()
|
state.update.assert_called_with()
|
||||||
|
|
||||||
|
|
||||||
@ -157,7 +171,8 @@ async def test_select_source(player, state, source, value):
|
|||||||
async def test_source_list(player, state):
|
async def test_source_list(player, state):
|
||||||
"""Test source list."""
|
"""Test source list."""
|
||||||
state.get_source_list.return_value = [SourceCodes.BD]
|
state.get_source_list.return_value = [SourceCodes.BD]
|
||||||
assert player.source_list == ["BD"]
|
data = await update(player)
|
||||||
|
assert data.attributes["source_list"] == ["BD"]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
@ -317,16 +332,28 @@ async def test_media_artist(player, state, source, dls, artist):
|
|||||||
)
|
)
|
||||||
async def test_media_title(player, state, source, channel, title):
|
async def test_media_title(player, state, source, channel, title):
|
||||||
"""Test media title."""
|
"""Test media title."""
|
||||||
|
from homeassistant.components.arcam_fmj.media_player import ArcamFmj
|
||||||
|
|
||||||
state.get_source.return_value = source
|
state.get_source.return_value = source
|
||||||
with patch.object(
|
with patch.object(
|
||||||
ArcamFmj, "media_channel", new_callable=PropertyMock
|
ArcamFmj, "media_channel", new_callable=PropertyMock
|
||||||
) as media_channel:
|
) as media_channel:
|
||||||
media_channel.return_value = channel
|
media_channel.return_value = channel
|
||||||
assert player.media_title == title
|
data = await update(player)
|
||||||
|
if title is None:
|
||||||
|
assert "media_title" not in data.attributes
|
||||||
|
else:
|
||||||
|
assert data.attributes["media_title"] == title
|
||||||
|
|
||||||
|
|
||||||
async def test_added_to_hass(player, state):
|
async def test_added_to_hass(player, state):
|
||||||
"""Test addition to hass."""
|
"""Test addition to hass."""
|
||||||
|
from homeassistant.components.arcam_fmj.const import (
|
||||||
|
SIGNAL_CLIENT_DATA,
|
||||||
|
SIGNAL_CLIENT_STARTED,
|
||||||
|
SIGNAL_CLIENT_STOPPED,
|
||||||
|
)
|
||||||
|
|
||||||
connectors = {}
|
connectors = {}
|
||||||
|
|
||||||
def _connect(signal, fun):
|
def _connect(signal, fun):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user