From f382be4c15ef32f557ba6d40bb01c084c5cbc692 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 18 Jun 2019 13:59:40 -0700 Subject: [PATCH] Cloud: Make sure on_connect forwards platform only once (#24582) * Make sure on_connect forwards platform only once * Make sure right handler --- homeassistant/components/cloud/__init__.py | 8 ++++++++ tests/components/cloud/test_init.py | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/homeassistant/components/cloud/__init__.py b/homeassistant/components/cloud/__init__.py index 01e2b48559b..e874e4213bc 100644 --- a/homeassistant/components/cloud/__init__.py +++ b/homeassistant/components/cloud/__init__.py @@ -191,8 +191,16 @@ async def async_setup(hass, config): 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)) diff --git a/tests/components/cloud/test_init.py b/tests/components/cloud/test_init.py index ea611c29df1..c938a404964 100644 --- a/tests/components/cloud/test_init.py +++ b/tests/components/cloud/test_init.py @@ -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