Allow platform unloading (#13784)

* Allow platform unloading

* Add tests

* Add last test
This commit is contained in:
Paulus Schoutsen authored and Pascal Vizeli committed 2018-04-12 14:28:54 +02:00
1 parent dd7e6edf61
commit f47572d3c0
12 files changed
+218 -14

No files matched your search

+59 -2
View File
@@ -18,8 +18,8 @@ async def test_bridge_setup():
assert await hue_bridge.async_setup() is True
assert hue_bridge.api is api
assert len(hass.config_entries.async_forward_entry.mock_calls) == 1
assert hass.config_entries.async_forward_entry.mock_calls[0][1] == \
assert len(hass.config_entries.async_forward_entry_setup.mock_calls) == 1
assert hass.config_entries.async_forward_entry_setup.mock_calls[0][1] == \
(entry, 'light')
@@ -54,3 +54,60 @@ async def test_bridge_setup_timeout(hass):
assert len(hass.helpers.event.async_call_later.mock_calls) == 1
# Assert we are going to wait 2 seconds
assert hass.helpers.event.async_call_later.mock_calls[0][1][0] == 2
async def test_reset_cancels_retry_setup():
"""Test resetting a bridge while we're waiting to retry setup."""
hass = Mock()
entry = Mock()
entry.data = {'host': '1.2.3.4', 'username': 'mock-username'}
hue_bridge = bridge.HueBridge(hass, entry, False, False)
with patch.object(bridge, 'get_bridge', side_effect=errors.CannotConnect):
assert await hue_bridge.async_setup() is False
mock_call_later = hass.helpers.event.async_call_later
assert len(mock_call_later.mock_calls) == 1
assert await hue_bridge.async_reset()
assert len(mock_call_later.mock_calls) == 2
assert len(mock_call_later.return_value.mock_calls) == 1
async def test_reset_if_entry_had_wrong_auth():
"""Test calling reset when the entry contained wrong auth."""
hass = Mock()
entry = Mock()
entry.data = {'host': '1.2.3.4', 'username': 'mock-username'}
hue_bridge = bridge.HueBridge(hass, entry, False, False)
with patch.object(bridge, 'get_bridge',
side_effect=errors.AuthenticationRequired):
assert await hue_bridge.async_setup() is False
assert len(hass.async_add_job.mock_calls) == 1
assert await hue_bridge.async_reset()
async def test_reset_unloads_entry_if_setup():
"""Test calling reset while the entry has been setup."""
hass = Mock()
entry = Mock()
entry.data = {'host': '1.2.3.4', 'username': 'mock-username'}
hue_bridge = bridge.HueBridge(hass, entry, False, False)
with patch.object(bridge, 'get_bridge', return_value=mock_coro(Mock())):
assert await hue_bridge.async_setup() is True
assert len(hass.services.async_register.mock_calls) == 1
assert len(hass.config_entries.async_forward_entry_setup.mock_calls) == 1
hass.config_entries.async_forward_entry_unload.return_value = \
mock_coro(True)
assert await hue_bridge.async_reset()
assert len(hass.config_entries.async_forward_entry_unload.mock_calls) == 1
assert len(hass.services.async_remove.mock_calls) == 1
+19
View File
@@ -167,3 +167,22 @@ async def test_config_passed_to_config_entry(hass):
assert p_entry is entry
assert p_allow_unreachable is True
assert p_allow_groups is False
async def test_unload_entry(hass):
"""Test being able to unload an entry."""
entry = MockConfigEntry(domain=hue.DOMAIN, data={
'host': '0.0.0.0',
})
entry.add_to_hass(hass)
with patch.object(hue, 'HueBridge') as mock_bridge:
mock_bridge.return_value.async_setup.return_value = mock_coro(True)
assert await async_setup_component(hass, hue.DOMAIN, {}) is True
assert len(mock_bridge.return_value.mock_calls) == 1
mock_bridge.return_value.async_reset.return_value = mock_coro(True)
assert await hue.async_unload_entry(hass, entry)
assert len(mock_bridge.return_value.async_reset.mock_calls) == 1
assert hass.data[hue.DOMAIN] == {}
+1 -1
View File
@@ -200,7 +200,7 @@ async def setup_bridge(hass, mock_bridge):
config_entry = config_entries.ConfigEntry(1, hue.DOMAIN, 'Mock Title', {
'host': 'mock-host'
}, 'test')
await hass.config_entries.async_forward_entry(config_entry, 'light')
await hass.config_entries.async_forward_entry_setup(config_entry, 'light')
# To flush out the service call to update the group
await hass.async_block_till_done()