Skip to content
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

Cloud: Make sure on_connect forwards platform only once #24582

Merged
merged 2 commits into from
Jun 18, 2019
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
8 changes: 8 additions & 0 deletions homeassistant/components/cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,16 @@ async def _service_handler(service):
hass.helpers.service.async_register_admin_service(
DOMAIN, SERVICE_REMOTE_DISCONNECT, _service_handler)

loaded_binary_sensor = False

async def _on_connect():
"""Discover RemoteUI binary sensor."""
nonlocal loaded_binary_sensor

if loaded_binary_sensor:
return

loaded_binary_sensor = True
hass.async_create_task(hass.helpers.discovery.async_load_platform(
'binary_sensor', DOMAIN, {}, config))

Expand Down
22 changes: 22 additions & 0 deletions tests/components/cloud/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,25 @@ async def test_setup_setup_cloud_user(hass, hass_storage):

assert cloud_user
assert cloud_user.groups[0].id == GROUP_ID_ADMIN


async def test_on_connect(hass, mock_cloud_fixture):
"""Test cloud on connect triggers."""
cl = hass.data['cloud']

assert len(cl.iot._on_connect) == 4

assert len(hass.states.async_entity_ids('binary_sensor')) == 0

assert 'async_setup' in str(cl.iot._on_connect[-1])
await cl.iot._on_connect[-1]()
await hass.async_block_till_done()

assert len(hass.states.async_entity_ids('binary_sensor')) == 1

with patch('homeassistant.helpers.discovery.async_load_platform',
side_effect=mock_coro) as mock_load:
await cl.iot._on_connect[-1]()
await hass.async_block_till_done()

assert len(mock_load.mock_calls) == 0