8000 Cast/Sonos: create config entry if manually configured by balloob · Pull Request #15630 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Cast/Sonos: create config entry if manually configured #15630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion homeassistant/components/cast/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Component to embed Google Cast."""
from homeassistant import data_entry_flow
from homeassistant.helpers import config_entry_flow


Expand All @@ -8,7 +9,14 @@

async def async_setup(hass, config):
"""Set up the Cast component."""
hass.data[DOMAIN] = config.get(DOMAIN, {})
conf = config.get(DOMAIN)

hass.data[DOMAIN] = conf or {}

if conf is not None:
hass.async_create_task(hass.config_entries.flow.async_init(
DOMAIN, source=data_entry_flow.SOURCE_IMPORT))

return True


Expand Down
10 changes: 9 additions & 1 deletion homeassistant/components/sonos/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Component to embed Sonos."""
from homeassistant import data_entry_flow
from homeassistant.helpers import config_entry_flow


Expand All @@ -8,7 +9,14 @@

async def async_setup(hass, config):
"""Set up the Sonos component."""
hass.data[DOMAIN] = config.get(DOMAIN, {})
conf = config.get(DOMAIN)

hass.data[DOMAIN] = conf or {}

if conf is not None:
hass.async_create_task(hass.config_entries.flow.async_init(
DOMAIN, source=data_entry_flow.SOURCE_IMPORT))

return True


Expand Down
12 changes: 12 additions & 0 deletions homeassistant/helpers/config_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ async def async_step_discovery(self, discovery_info):

return await self.async_step_confirm()

async def async_step_import(self, _):
"""Handle a flow initialized by import."""
if self._async_in_progress() or self._async_current_entries():
return self.async_abort(
reason='single_instance_allowed'
)

return self.async_create_entry(
title=self._title,
data={},
)

@callback
def _async_current_entries(self):
"""Return current entries."""
Expand Down
31 changes: 31 additions & 0 deletions tests/components/cast/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest.mock import patch

from homeassistant import data_entry_flow
from homeassistant.setup import async_setup_component
from homeassistant.components import cast

from tests.common import MockDependency, mock_coro
Expand All @@ -20,3 +21,33 @@ async def test_creating_entry_sets_up_media_player(hass):
await hass.async_block_till_done()

assert len(mock_setup.mock_calls) == 1


async def test_configuring_cast_creates_entry(hass):
"""Test that specifying config will create an entry."""
with patch('homeassistant.components.cast.async_setup_entry',
return_value=mock_coro(True)) as mock_setup, \
MockDependency('pychromecast', 'discovery'), \
patch('pychromecast.discovery.discover_chromecasts',
return_value=True):
await async_setup_component(hass, cast.DOMAIN, {
'cast': {
'some_config': 'to_trigger_import'
}
})
await hass.async_block_till_done()

assert len(mock_setup.mock_calls) == 1


async def test_not_configuring_cast_not_creates_entry(hass):
"""Test that no config will not create an entry."""
with patch('homeassistant.components.cast.async_setup_entry',
return_value=mock_coro(True)) as mock_setup, \
MockDependency('pychromecast', 'discovery'), \
patch('pychromecast.discovery.discover_chromecasts',
return_value=True):
await async_setup_component(hass, cast.DOMAIN, {})
await hass.async_block_till_done()

assert len(mock_setup.mock_calls) == 0
27 changes: 27 additions & 0 deletions tests/components/sonos/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest.mock import patch

from homeassistant import data_entry_flow
from homeassistant.setup import async_setup_component
from homeassistant.components import sonos

from tests.common import mock_coro
Expand All @@ -18,3 +19,29 @@ async def test_creating_entry_sets_up_media_player(hass):
await hass.async_block_till_done()

assert len(mock_setup.mock_calls) == 1


async def test_configuring_sonos_creates_entry(hass):
"""Test that specifying config will create an entry."""
with patch('homeassistant.components.sonos.async_setup_entry',
return_value=mock_coro(True)) as mock_setup, \
patch('soco.discover', return_value=True):
await async_setup_component(hass, sonos.DOMAIN, {
'sonos': {
'some_config': 'to_trigger_import'
}
})
await hass.async_block_till_done()

assert len(mock_setup.mock_calls) == 1


async def test_not_configuring_sonos_not_creates_entry(hass):
"""Test that no config will not create an entry."""
with patch('homeassistant.components.sonos.async_setup_entry',
return_value=mock_coro(True)) as mock_setup, \
patch('soco.discover', return_value=True):
await async_setup_component(hass, sonos.DOMAIN, {})
await hass.async_block_till_done()

assert len(mock_setup.mock_calls) == 0
21 changes: 21 additions & 0 deletions tests/helpers/test_config_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,24 @@ async def test_user_init_trumps_discovery(hass, flow_conf):

# Discovery flow has been aborted
assert len(hass.config_entries.flow.async_progress()) == 0


async def test_import_no_confirmation(hass, flow_conf):
"""Test import requires no confirmation to setup."""
flow = config_entries.HANDLERS['test']()
flow.hass = hass
flow_conf['discovered'] = True

result = await flow.async_step_import(None)
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY


async def test_import_single_instance(hass, flow_conf):
"""Test import doesn't create second instance."""
flow = config_entries.HANDLERS['test']()
flow.hass = hass
flow_conf['discovered'] = True
MockConfigEntry(domain='test').add_to_hass(hass)

result = await flow.async_step_import(None)
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
0