Refactor DirecTV Integration to Async (#33114)
* switch to directv==0.1.1 * work on directv async. * Update const.py * Update __init__.py * Update media_player.py * Update __init__.py * Update __init__.py * Update __init__.py * Update media_player.py * Update test_config_flow.py * Update media_player.py * Update media_player.py * work on tests and coverage. * Update __init__.py * Update __init__.py * squash.
This commit is contained in:
1 parent
3566803d2e
commit
b892dbc6ea
19 files changed
+749
-845
No files matched your search
@@ -4,7 +4,6 @@ from typing import Optional
|
||||
|
||||
from asynctest import patch
|
||||
from pytest import fixture
|
||||
from requests import RequestException
|
||||
|
||||
from homeassistant.components.directv.media_player import (
|
||||
ATTR_MEDIA_CURRENTLY_RECORDING,
|
||||
@@ -24,6 +23,7 @@ from homeassistant.components.media_player.const import (
|
||||
ATTR_MEDIA_SERIES_TITLE,
|
||||
ATTR_MEDIA_TITLE,
|
||||
DOMAIN as MP_DOMAIN,
|
||||
MEDIA_TYPE_MOVIE,
|
||||
MEDIA_TYPE_TVSHOW,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
SUPPORT_NEXT_TRACK,
|
||||
@@ -44,7 +44,6 @@ from homeassistant.const import (
|
||||
SERVICE_MEDIA_STOP,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
STATE_OFF,
|
||||
STATE_PAUSED,
|
||||
STATE_PLAYING,
|
||||
STATE_UNAVAILABLE,
|
||||
@@ -52,18 +51,13 @@ from homeassistant.const import (
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
from tests.components.directv import (
|
||||
DOMAIN,
|
||||
MOCK_GET_LOCATIONS_MULTIPLE,
|
||||
RECORDING,
|
||||
MockDirectvClass,
|
||||
setup_integration,
|
||||
)
|
||||
from tests.components.directv import setup_integration
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
|
||||
ATTR_UNIQUE_ID = "unique_id"
|
||||
CLIENT_ENTITY_ID = f"{MP_DOMAIN}.bedroom_client"
|
||||
MAIN_ENTITY_ID = f"{MP_DOMAIN}.main_dvr"
|
||||
CLIENT_ENTITY_ID = f"{MP_DOMAIN}.client"
|
||||
MAIN_ENTITY_ID = f"{MP_DOMAIN}.host"
|
||||
UNAVAILABLE_ENTITY_ID = f"{MP_DOMAIN}.unavailable_client"
|
||||
|
||||
# pylint: disable=redefined-outer-name
|
||||
|
||||
@@ -74,29 +68,6 @@ def mock_now() -> datetime:
|
||||
return dt_util.utcnow()
|
||||
|
||||
|
||||
async def setup_directv(hass: HomeAssistantType) -> MockConfigEntry:
|
||||
"""Set up mock DirecTV integration."""
|
||||
with patch(
|
||||
"homeassistant.components.directv.DIRECTV", new=MockDirectvClass,
|
||||
):
|
||||
return await setup_integration(hass)
|
||||
|
||||
|
||||
async def setup_directv_with_locations(hass: HomeAssistantType) -> MockConfigEntry:
|
||||
"""Set up mock DirecTV integration."""
|
||||
with patch(
|
||||
"tests.components.directv.test_media_player.MockDirectvClass.get_locations",
|
||||
return_value=MOCK_GET_LOCATIONS_MULTIPLE,
|
||||
):
|
||||
with patch(
|
||||
"homeassistant.components.directv.DIRECTV", new=MockDirectvClass,
|
||||
), patch(
|
||||
"homeassistant.components.directv.media_player.DIRECTV",
|
||||
new=MockDirectvClass,
|
||||
):
|
||||
return await setup_integration(hass)
|
||||
|
||||
|
||||
async def async_turn_on(
|
||||
hass: HomeAssistantType, entity_id: Optional[str] = None
|
||||
) -> None:
|
||||
@@ -172,23 +143,21 @@ async def async_play_media(
|
||||
await hass.services.async_call(MP_DOMAIN, SERVICE_PLAY_MEDIA, data)
|
||||
|
||||
|
||||
async def test_setup(hass: HomeAssistantType) -> None:
|
||||
async def test_setup(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test setup with basic config."""
|
||||
await setup_directv(hass)
|
||||
assert hass.states.get(MAIN_ENTITY_ID)
|
||||
|
||||
|
||||
async def test_setup_with_multiple_locations(hass: HomeAssistantType) -> None:
|
||||
"""Test setup with basic config with client location."""
|
||||
await setup_directv_with_locations(hass)
|
||||
|
||||
await setup_integration(hass, aioclient_mock)
|
||||
assert hass.states.get(MAIN_ENTITY_ID)
|
||||
assert hass.states.get(CLIENT_ENTITY_ID)
|
||||
assert hass.states.get(UNAVAILABLE_ENTITY_ID)
|
||||
|
||||
|
||||
async def test_unique_id(hass: HomeAssistantType) -> None:
|
||||
async def test_unique_id(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test unique id."""
|
||||
await setup_directv_with_locations(hass)
|
||||
await setup_integration(hass, aioclient_mock)
|
||||
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
@@ -198,10 +167,15 @@ async def test_unique_id(hass: HomeAssistantType) -> None:
|
||||
client = entity_registry.async_get(CLIENT_ENTITY_ID)
|
||||
assert client.unique_id == "2CA17D1CD30X"
|
||||
|
||||
unavailable_client = entity_registry.async_get(UNAVAILABLE_ENTITY_ID)
|
||||
assert unavailable_client.unique_id == "9XXXXXXXXXX9"
|
||||
|
||||
async def test_supported_features(hass: HomeAssistantType) -> None:
|
||||
|
||||
async def test_supported_features(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test supported features."""
|
||||
await setup_directv_with_locations(hass)
|
||||
await setup_integration(hass, aioclient_mock)
|
||||
|
||||
# Features supported for main DVR
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
@@ -231,168 +205,123 @@ async def test_supported_features(hass: HomeAssistantType) -> None:
|
||||
|
||||
|
||||
async def test_check_attributes(
|
||||
hass: HomeAssistantType, mock_now: dt_util.dt.datetime
|
||||
hass: HomeAssistantType,
|
||||
mock_now: dt_util.dt.datetime,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
) -> None:
|
||||
"""Test attributes."""
|
||||
await setup_directv_with_locations(hass)
|
||||
await setup_integration(hass, aioclient_mock)
|
||||
|
||||
next_update = mock_now + timedelta(minutes=5)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=next_update):
|
||||
async_fire_time_changed(hass, next_update)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
assert state.state == STATE_PLAYING
|
||||
|
||||
# Start playing TV
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=next_update):
|
||||
await async_media_play(hass, CLIENT_ENTITY_ID)
|
||||
await hass.async_block_till_done()
|
||||
assert state.attributes.get(ATTR_MEDIA_CONTENT_ID) == "17016356"
|
||||
assert state.attributes.get(ATTR_MEDIA_CONTENT_TYPE) == MEDIA_TYPE_MOVIE
|
||||
assert state.attributes.get(ATTR_MEDIA_DURATION) == 7200
|
||||
assert state.attributes.get(ATTR_MEDIA_POSITION) == 4437
|
||||
assert state.attributes.get(ATTR_MEDIA_POSITION_UPDATED_AT)
|
||||
assert state.attributes.get(ATTR_MEDIA_TITLE) == "Snow Bride"
|
||||
assert state.attributes.get(ATTR_MEDIA_SERIES_TITLE) is None
|
||||
assert state.attributes.get(ATTR_MEDIA_CHANNEL) == "{} ({})".format("HALLHD", "312")
|
||||
assert state.attributes.get(ATTR_INPUT_SOURCE) == "312"
|
||||
assert not state.attributes.get(ATTR_MEDIA_CURRENTLY_RECORDING)
|
||||
assert state.attributes.get(ATTR_MEDIA_RATING) == "TV-G"
|
||||
assert not state.attributes.get(ATTR_MEDIA_RECORDED)
|
||||
assert state.attributes.get(ATTR_MEDIA_START_TIME) == datetime(
|
||||
2020, 3, 21, 13, 0, tzinfo=dt_util.UTC
|
||||
)
|
||||
|
||||
state = hass.states.get(CLIENT_ENTITY_ID)
|
||||
assert state.state == STATE_PLAYING
|
||||
|
||||
assert state.attributes.get(ATTR_MEDIA_CONTENT_ID) == RECORDING["programId"]
|
||||
assert state.attributes.get(ATTR_MEDIA_CONTENT_ID) == "4405732"
|
||||
assert state.attributes.get(ATTR_MEDIA_CONTENT_TYPE) == MEDIA_TYPE_TVSHOW
|
||||
assert state.attributes.get(ATTR_MEDIA_DURATION) == RECORDING["duration"]
|
||||
assert state.attributes.get(ATTR_MEDIA_POSITION) == 2
|
||||
assert state.attributes.get(ATTR_MEDIA_POSITION_UPDATED_AT) == next_update
|
||||
assert state.attributes.get(ATTR_MEDIA_TITLE) == RECORDING["title"]
|
||||
assert state.attributes.get(ATTR_MEDIA_SERIES_TITLE) == RECORDING["episodeTitle"]
|
||||
assert state.attributes.get(ATTR_MEDIA_CHANNEL) == "{} ({})".format(
|
||||
RECORDING["callsign"], RECORDING["major"]
|
||||
)
|
||||
assert state.attributes.get(ATTR_INPUT_SOURCE) == RECORDING["major"]
|
||||
assert (
|
||||
state.attributes.get(ATTR_MEDIA_CURRENTLY_RECORDING) == RECORDING["isRecording"]
|
||||
)
|
||||
assert state.attributes.get(ATTR_MEDIA_RATING) == RECORDING["rating"]
|
||||
assert state.attributes.get(ATTR_MEDIA_DURATION) == 1791
|
||||
assert state.attributes.get(ATTR_MEDIA_POSITION) == 263
|
||||
assert state.attributes.get(ATTR_MEDIA_POSITION_UPDATED_AT)
|
||||
assert state.attributes.get(ATTR_MEDIA_TITLE) == "Tyler's Ultimate"
|
||||
assert state.attributes.get(ATTR_MEDIA_SERIES_TITLE) == "Spaghetti and Clam Sauce"
|
||||
assert state.attributes.get(ATTR_MEDIA_CHANNEL) == "{} ({})".format("FOODHD", "231")
|
||||
assert state.attributes.get(ATTR_INPUT_SOURCE) == "231"
|
||||
assert not state.attributes.get(ATTR_MEDIA_CURRENTLY_RECORDING)
|
||||
assert state.attributes.get(ATTR_MEDIA_RATING) == "No Rating"
|
||||
assert state.attributes.get(ATTR_MEDIA_RECORDED)
|
||||
assert state.attributes.get(ATTR_MEDIA_START_TIME) == datetime(
|
||||
2018, 11, 10, 19, 0, tzinfo=dt_util.UTC
|
||||
2010, 7, 5, 15, 0, 8, tzinfo=dt_util.UTC
|
||||
)
|
||||
|
||||
state = hass.states.get(UNAVAILABLE_ENTITY_ID)
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
|
||||
async def test_attributes_paused(
|
||||
hass: HomeAssistantType,
|
||||
mock_now: dt_util.dt.datetime,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
):
|
||||
"""Test attributes while paused."""
|
||||
await setup_integration(hass, aioclient_mock)
|
||||
|
||||
state = hass.states.get(CLIENT_ENTITY_ID)
|
||||
last_updated = state.attributes.get(ATTR_MEDIA_POSITION_UPDATED_AT)
|
||||
|
||||
# Test to make sure that ATTR_MEDIA_POSITION_UPDATED_AT is not
|
||||
# updated if TV is paused.
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=next_update + timedelta(minutes=5)
|
||||
"homeassistant.util.dt.utcnow", return_value=mock_now + timedelta(minutes=5)
|
||||
):
|
||||
await async_media_pause(hass, CLIENT_ENTITY_ID)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(CLIENT_ENTITY_ID)
|
||||
assert state.state == STATE_PAUSED
|
||||
assert state.attributes.get(ATTR_MEDIA_POSITION_UPDATED_AT) == next_update
|
||||
assert state.attributes.get(ATTR_MEDIA_POSITION_UPDATED_AT) == last_updated
|
||||
|
||||
|
||||
async def test_main_services(
|
||||
hass: HomeAssistantType, mock_now: dt_util.dt.datetime
|
||||
hass: HomeAssistantType,
|
||||
mock_now: dt_util.dt.datetime,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
) -> None:
|
||||
"""Test the different services."""
|
||||
await setup_directv(hass)
|
||||
await setup_integration(hass, aioclient_mock)
|
||||
|
||||
next_update = mock_now + timedelta(minutes=5)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=next_update):
|
||||
async_fire_time_changed(hass, next_update)
|
||||
with patch("directv.DIRECTV.remote") as remote_mock:
|
||||
await async_turn_off(hass, MAIN_ENTITY_ID)
|
||||
await hass.async_block_till_done()
|
||||
# DVR starts in off state.
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
assert state.state == STATE_OFF
|
||||
remote_mock.assert_called_once_with("poweroff", "0")
|
||||
|
||||
# Turn main DVR on. When turning on DVR is playing.
|
||||
await async_turn_on(hass, MAIN_ENTITY_ID)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
assert state.state == STATE_PLAYING
|
||||
|
||||
# Pause live TV.
|
||||
await async_media_pause(hass, MAIN_ENTITY_ID)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
assert state.state == STATE_PAUSED
|
||||
|
||||
# Start play again for live TV.
|
||||
await async_media_play(hass, MAIN_ENTITY_ID)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
assert state.state == STATE_PLAYING
|
||||
|
||||
# Change channel, currently it should be 202
|
||||
assert state.attributes.get("source") == 202
|
||||
await async_play_media(hass, "channel", 7, MAIN_ENTITY_ID)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
assert state.attributes.get("source") == 7
|
||||
|
||||
# Stop live TV.
|
||||
await async_media_stop(hass, MAIN_ENTITY_ID)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
assert state.state == STATE_PAUSED
|
||||
|
||||
# Turn main DVR off.
|
||||
await async_turn_off(hass, MAIN_ENTITY_ID)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
|
||||
async def test_available(
|
||||
hass: HomeAssistantType, mock_now: dt_util.dt.datetime
|
||||
) -> None:
|
||||
"""Test available status."""
|
||||
entry = await setup_directv(hass)
|
||||
|
||||
next_update = mock_now + timedelta(minutes=5)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=next_update):
|
||||
async_fire_time_changed(hass, next_update)
|
||||
with patch("directv.DIRECTV.remote") as remote_mock:
|
||||
await async_turn_on(hass, MAIN_ENTITY_ID)
|
||||
await hass.async_block_till_done()
|
||||
remote_mock.assert_called_once_with("poweron", "0")
|
||||
|
||||
# Confirm service is currently set to available.
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
|
||||
assert hass.data[DOMAIN]
|
||||
assert hass.data[DOMAIN][entry.entry_id]
|
||||
assert hass.data[DOMAIN][entry.entry_id]["client"]
|
||||
|
||||
main_dtv = hass.data[DOMAIN][entry.entry_id]["client"]
|
||||
|
||||
# Make update fail 1st time
|
||||
next_update = next_update + timedelta(minutes=5)
|
||||
with patch.object(main_dtv, "get_standby", side_effect=RequestException), patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=next_update
|
||||
):
|
||||
async_fire_time_changed(hass, next_update)
|
||||
with patch("directv.DIRECTV.remote") as remote_mock:
|
||||
await async_media_pause(hass, MAIN_ENTITY_ID)
|
||||
await hass.async_block_till_done()
|
||||
remote_mock.assert_called_once_with("pause", "0")
|
||||
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
|
||||
# Make update fail 2nd time within 1 minute
|
||||
next_update = next_update + timedelta(seconds=30)
|
||||
with patch.object(main_dtv, "get_standby", side_effect=RequestException), patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=next_update
|
||||
):
|
||||
async_fire_time_changed(hass, next_update)
|
||||
with patch("directv.DIRECTV.remote") as remote_mock:
|
||||
await async_media_play(hass, MAIN_ENTITY_ID)
|
||||
await hass.async_block_till_done()
|
||||
remote_mock.assert_called_once_with("play", "0")
|
||||
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
|
||||
# Make update fail 3rd time more then a minute after 1st failure
|
||||
next_update = next_update + timedelta(minutes=1)
|
||||
with patch.object(main_dtv, "get_standby", side_effect=RequestException), patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=next_update
|
||||
):
|
||||
async_fire_time_changed(hass, next_update)
|
||||
with patch("directv.DIRECTV.remote") as remote_mock:
|
||||
await async_media_next_track(hass, MAIN_ENTITY_ID)
|
||||
await hass.async_block_till_done()
|
||||
remote_mock.assert_called_once_with("ffwd", "0")
|
||||
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
# Recheck state, update should work again.
|
||||
next_update = next_update + timedelta(minutes=5)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=next_update):
|
||||
async_fire_time_changed(hass, next_update)
|
||||
with patch("directv.DIRECTV.remote") as remote_mock:
|
||||
await async_media_previous_track(hass, MAIN_ENTITY_ID)
|
||||
await hass.async_block_till_done()
|
||||
remote_mock.assert_called_once_with("rew", "0")
|
||||
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
with patch("directv.DIRECTV.remote") as remote_mock:
|
||||
await async_media_stop(hass, MAIN_ENTITY_ID)
|
||||
await hass.async_block_till_done()
|
||||
remote_mock.assert_called_once_with("stop", "0")
|
||||
|
||||
with patch("directv.DIRECTV.tune") as tune_mock:
|
||||
await async_play_media(hass, "channel", 312, MAIN_ENTITY_ID)
|
||||
await hass.async_block_till_done()
|
||||
tune_mock.assert_called_once_with("312", "0")
|
||||
Reference in new issue
Block a user