Fix cast config (#15143)

This commit is contained in:
Paulus Schoutsen 2018-06-25 15:59:05 -04:00 committed by GitHub
parent c8458fd7c5
commit 46ea28a4f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 3 deletions

View File

@ -4,6 +4,7 @@ Provide functionality to interact with Cast devices on the network.
For more details about this platform, please refer to the documentation at For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/media_player.cast/ https://home-assistant.io/components/media_player.cast/
""" """
import asyncio
import logging import logging
import threading import threading
from typing import Optional, Tuple from typing import Optional, Tuple
@ -199,9 +200,13 @@ async def async_setup_platform(hass: HomeAssistantType, config: ConfigType,
async def async_setup_entry(hass, config_entry, async_add_devices): async def async_setup_entry(hass, config_entry, async_add_devices):
"""Set up Cast from a config entry.""" """Set up Cast from a config entry."""
await _async_setup_platform( config = hass.data[CAST_DOMAIN].get('media_player', {})
hass, hass.data[CAST_DOMAIN].get('media_player', {}), if not isinstance(config, list):
async_add_devices, None) config = [config]
await asyncio.wait([
_async_setup_platform(hass, cfg, async_add_devices, None)
for cfg in config])
async def _async_setup_platform(hass: HomeAssistantType, config: ConfigType, async def _async_setup_platform(hass: HomeAssistantType, config: ConfigType,

View File

@ -17,6 +17,8 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect, \
from homeassistant.components.media_player import cast from homeassistant.components.media_player import cast
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry, mock_coro
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def cast_mock(): def cast_mock():
@ -359,3 +361,56 @@ async def test_disconnect_on_stop(hass: HomeAssistantType):
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP) hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
await hass.async_block_till_done() await hass.async_block_till_done()
assert chromecast.disconnect.call_count == 1 assert chromecast.disconnect.call_count == 1
async def test_entry_setup_no_config(hass: HomeAssistantType):
"""Test setting up entry with no config.."""
await async_setup_component(hass, 'cast', {})
with patch(
'homeassistant.components.media_player.cast._async_setup_platform',
return_value=mock_coro()) as mock_setup:
await cast.async_setup_entry(hass, MockConfigEntry(), None)
assert len(mock_setup.mock_calls) == 1
assert mock_setup.mock_calls[0][1][1] == {}
async def test_entry_setup_single_config(hass: HomeAssistantType):
"""Test setting up entry and having a single config option."""
await async_setup_component(hass, 'cast', {
'cast': {
'media_player': {
'host': 'bla'
}
}
})
with patch(
'homeassistant.components.media_player.cast._async_setup_platform',
return_value=mock_coro()) as mock_setup:
await cast.async_setup_entry(hass, MockConfigEntry(), None)
assert len(mock_setup.mock_calls) == 1
assert mock_setup.mock_calls[0][1][1] == {'host': 'bla'}
async def test_entry_setup_list_config(hass: HomeAssistantType):
"""Test setting up entry and having multiple config options."""
await async_setup_component(hass, 'cast', {
'cast': {
'media_player': [
{'host': 'bla'},
{'host': 'blu'},
]
}
})
with patch(
'homeassistant.components.media_player.cast._async_setup_platform',
return_value=mock_coro()) as mock_setup:
await cast.async_setup_entry(hass, MockConfigEntry(), None)
assert len(mock_setup.mock_calls) == 2
assert mock_setup.mock_calls[0][1][1] == {'host': 'bla'}
assert mock_setup.mock_calls[1][1][1] == {'host': 'blu'}