From ba9d00c7c47854b101d5262c5f0afd38b0eef888 Mon Sep 17 00:00:00 2001 From: Mattias Welponer Date: Sat, 23 Jun 2018 00:22:18 +0200 Subject: [PATCH] Further tests implemented --- .../components/homematicip_cloud/test_hap.py | 73 +++++++++++++++++++ .../components/homematicip_cloud/test_init.py | 1 - 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 tests/components/homematicip_cloud/test_hap.py diff --git a/tests/components/homematicip_cloud/test_hap.py b/tests/components/homematicip_cloud/test_hap.py new file mode 100644 index 00000000000..3f3be8bf422 --- /dev/null +++ b/tests/components/homematicip_cloud/test_hap.py @@ -0,0 +1,73 @@ +"""Test HomematicIP Cloud hap.""" + +from unittest.mock import Mock, patch + +from homeassistant.components import homematicip_cloud as hmipc + +from tests.common import mock_coro + +from homematicip.base.base_connection import HmipConnectionError + + +async def test_hap_init(aioclient_mock): + """Test a successful setup.""" + hass = Mock() + entry = Mock() + home = Mock() + entry.data = { + hmipc.HMIPC_HAPID: 'ABC123', + hmipc.HMIPC_AUTHTOKEN: '123', + hmipc.HMIPC_NAME: 'hmip', + } + hap = hmipc.HomematicipHAP(hass, entry) + with patch.object(hap, 'get_hap', return_value=mock_coro(home)): + assert await hap.async_setup() is True + + assert hap.home is home + assert len(hass.config_entries.async_forward_entry_setup.mock_calls) == 5 + assert hass.config_entries.async_forward_entry_setup.mock_calls[0][1] == \ + (entry, 'binary_sensor') + + +async def test_hap_setup_invalid_token(): + """Test we start config flow if username is no longer whitelisted.""" + hass = Mock() + entry = Mock() + entry.data = { + hmipc.HMIPC_HAPID: 'ABC123', + hmipc.HMIPC_AUTHTOKEN: '123', + hmipc.HMIPC_NAME: 'hmip', + } + hap = hmipc.HomematicipHAP(hass, entry) + + with patch.object(hap, 'get_hap', + side_effect=HmipConnectionError): + assert await hap.async_setup() is False + + assert len(hass.async_add_job.mock_calls) == 0 + assert len(hass.config_entries.flow.async_init.mock_calls) == 0 + + +async def test_reset_unloads_entry_if_setup(): + """Test calling reset while the entry has been setup.""" + hass = Mock() + entry = Mock() + home = Mock() + entry.data = { + hmipc.HMIPC_HAPID: 'ABC123', + hmipc.HMIPC_AUTHTOKEN: '123', + hmipc.HMIPC_NAME: 'hmip', + } + hap = hmipc.HomematicipHAP(hass, entry) + with patch.object(hap, 'get_hap', return_value=mock_coro(home)): + assert await hap.async_setup() is True + + assert hap.home is home + assert len(hass.services.async_register.mock_calls) == 0 + assert len(hass.config_entries.async_forward_entry_setup.mock_calls) == 5 + + hass.config_entries.async_forward_entry_unload.return_value = \ + mock_coro(True) + await hap.async_reset() + + assert len(hass.config_entries.async_forward_entry_unload.mock_calls) == 5 diff --git a/tests/components/homematicip_cloud/test_init.py b/tests/components/homematicip_cloud/test_init.py index e6a8cb16777..2fe8c4a7ba1 100644 --- a/tests/components/homematicip_cloud/test_init.py +++ b/tests/components/homematicip_cloud/test_init.py @@ -74,7 +74,6 @@ async def test_setup_defined_hosts_no_known_auth(hass): } }) is True - # Flow started for discovered bridge assert len(mock_config_entries.flow.mock_calls) == 1 assert mock_config_entries.flow.mock_calls[0][2]['data'] == { hmipc.HMIPC_HAPID: 'ABC123',