Cleanup of HomematicIP Cloud tests (#31181)

* CleanUp tests for HomematicIP_Cloud

* Remove not required CoroutineMock

* remove None return in mocks, add asserts

* rewrite test
This commit is contained in:
SukramJ 2020-01-27 21:34:15 +01:00 committed by Martin Hjelmare
parent 1d537ad416
commit 1dbfc66669
4 changed files with 145 additions and 200 deletions

View File

@ -1,5 +1,5 @@
"""Initializer helpers for HomematicIP fake server.""" """Initializer helpers for HomematicIP fake server."""
from asynctest import MagicMock, Mock, patch from asynctest import CoroutineMock, MagicMock, Mock, patch
from homematicip.aio.auth import AsyncAuth from homematicip.aio.auth import AsyncAuth
from homematicip.aio.connection import AsyncConnection from homematicip.aio.connection import AsyncConnection
from homematicip.aio.home import AsyncHome from homematicip.aio.home import AsyncHome
@ -9,14 +9,19 @@ from homeassistant import config_entries
from homeassistant.components.homematicip_cloud import ( from homeassistant.components.homematicip_cloud import (
DOMAIN as HMIPC_DOMAIN, DOMAIN as HMIPC_DOMAIN,
async_setup as hmip_async_setup, async_setup as hmip_async_setup,
const as hmipc,
hap as hmip_hap,
) )
from homeassistant.components.homematicip_cloud.const import (
HMIPC_AUTHTOKEN,
HMIPC_HAPID,
HMIPC_NAME,
HMIPC_PIN,
)
from homeassistant.components.homematicip_cloud.hap import HomematicipHAP
from homeassistant.helpers.typing import ConfigType, HomeAssistantType from homeassistant.helpers.typing import ConfigType, HomeAssistantType
from .helper import AUTH_TOKEN, HAPID, HAPPIN, HomeTemplate from .helper import AUTH_TOKEN, HAPID, HAPPIN, HomeTemplate
from tests.common import MockConfigEntry, mock_coro from tests.common import MockConfigEntry
@pytest.fixture(name="mock_connection") @pytest.fixture(name="mock_connection")
@ -30,8 +35,8 @@ def mock_connection_fixture() -> AsyncConnection:
connection._restCall.side_effect = ( # pylint: disable=protected-access connection._restCall.side_effect = ( # pylint: disable=protected-access
_rest_call_side_effect _rest_call_side_effect
) )
connection.api_call.return_value = mock_coro(True) connection.api_call = CoroutineMock(return_value=True)
connection.init.side_effect = mock_coro(True) connection.init = CoroutineMock(side_effect=True)
return connection return connection
@ -40,10 +45,10 @@ def mock_connection_fixture() -> AsyncConnection:
def hmip_config_entry_fixture() -> config_entries.ConfigEntry: def hmip_config_entry_fixture() -> config_entries.ConfigEntry:
"""Create a mock config entriy for homematic ip cloud.""" """Create a mock config entriy for homematic ip cloud."""
entry_data = { entry_data = {
hmipc.HMIPC_HAPID: HAPID, HMIPC_HAPID: HAPID,
hmipc.HMIPC_AUTHTOKEN: AUTH_TOKEN, HMIPC_AUTHTOKEN: AUTH_TOKEN,
hmipc.HMIPC_NAME: "", HMIPC_NAME: "",
hmipc.HMIPC_PIN: HAPPIN, HMIPC_PIN: HAPPIN,
} }
config_entry = MockConfigEntry( config_entry = MockConfigEntry(
version=1, version=1,
@ -68,7 +73,7 @@ def default_mock_home_fixture(mock_connection) -> AsyncHome:
@pytest.fixture(name="default_mock_hap") @pytest.fixture(name="default_mock_hap")
async def default_mock_hap_fixture( async def default_mock_hap_fixture(
hass: HomeAssistantType, mock_connection, hmip_config_entry hass: HomeAssistantType, mock_connection, hmip_config_entry
) -> hmip_hap.HomematicipHAP: ) -> HomematicipHAP:
"""Create a mocked homematic access point.""" """Create a mocked homematic access point."""
return await get_mock_hap(hass, mock_connection, hmip_config_entry) return await get_mock_hap(hass, mock_connection, hmip_config_entry)
@ -77,17 +82,17 @@ async def get_mock_hap(
hass: HomeAssistantType, hass: HomeAssistantType,
mock_connection, mock_connection,
hmip_config_entry: config_entries.ConfigEntry, hmip_config_entry: config_entries.ConfigEntry,
) -> hmip_hap.HomematicipHAP: ) -> HomematicipHAP:
"""Create a mocked homematic access point.""" """Create a mocked homematic access point."""
hass.config.components.add(HMIPC_DOMAIN) hass.config.components.add(HMIPC_DOMAIN)
hap = hmip_hap.HomematicipHAP(hass, hmip_config_entry) hap = HomematicipHAP(hass, hmip_config_entry)
home_name = hmip_config_entry.data["name"] home_name = hmip_config_entry.data["name"]
mock_home = ( mock_home = (
HomeTemplate(connection=mock_connection, home_name=home_name) HomeTemplate(connection=mock_connection, home_name=home_name)
.init_home() .init_home()
.get_async_home_mock() .get_async_home_mock()
) )
with patch.object(hap, "get_hap", return_value=mock_coro(mock_home)): with patch.object(hap, "get_hap", return_value=mock_home):
assert await hap.async_setup() assert await hap.async_setup()
mock_home.on_update(hap.async_update) mock_home.on_update(hap.async_update)
mock_home.on_create(hap.async_create_entity) mock_home.on_create(hap.async_create_entity)
@ -104,10 +109,10 @@ def hmip_config_fixture() -> ConfigType:
"""Create a config for homematic ip cloud.""" """Create a config for homematic ip cloud."""
entry_data = { entry_data = {
hmipc.HMIPC_HAPID: HAPID, HMIPC_HAPID: HAPID,
hmipc.HMIPC_AUTHTOKEN: AUTH_TOKEN, HMIPC_AUTHTOKEN: AUTH_TOKEN,
hmipc.HMIPC_NAME: "", HMIPC_NAME: "",
hmipc.HMIPC_PIN: HAPPIN, HMIPC_PIN: HAPPIN,
} }
return {HMIPC_DOMAIN: [entry_data]} return {HMIPC_DOMAIN: [entry_data]}
@ -122,7 +127,7 @@ def dummy_config_fixture() -> ConfigType:
@pytest.fixture(name="mock_hap_with_service") @pytest.fixture(name="mock_hap_with_service")
async def mock_hap_with_service_fixture( async def mock_hap_with_service_fixture(
hass: HomeAssistantType, default_mock_hap, dummy_config hass: HomeAssistantType, default_mock_hap, dummy_config
) -> hmip_hap.HomematicipHAP: ) -> HomematicipHAP:
"""Create a fake homematic access point with hass services.""" """Create a fake homematic access point with hass services."""
await hmip_async_setup(hass, dummy_config) await hmip_async_setup(hass, dummy_config)
await hass.async_block_till_done() await hass.async_block_till_done()

View File

@ -1,25 +1,30 @@
"""Tests for HomematicIP Cloud config flow.""" """Tests for HomematicIP Cloud config flow."""
from asynctest import patch from asynctest import patch
from homeassistant.components.homematicip_cloud import const from homeassistant.components.homematicip_cloud.const import (
DOMAIN as HMIPC_DOMAIN,
HMIPC_AUTHTOKEN,
HMIPC_HAPID,
HMIPC_NAME,
HMIPC_PIN,
)
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
DEFAULT_CONFIG = {HMIPC_HAPID: "ABC123", HMIPC_PIN: "123", HMIPC_NAME: "hmip"}
IMPORT_CONFIG = {HMIPC_HAPID: "ABC123", HMIPC_AUTHTOKEN: "123", HMIPC_NAME: "hmip"}
async def test_flow_works(hass): async def test_flow_works(hass):
"""Test config flow.""" """Test config flow."""
config = {
const.HMIPC_HAPID: "ABC123",
const.HMIPC_PIN: "123",
const.HMIPC_NAME: "hmip",
}
with patch( with patch(
"homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_checkbutton", "homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_checkbutton",
return_value=False, return_value=False,
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
const.DOMAIN, context={"source": "user"}, data=config HMIPC_DOMAIN, context={"source": "user"}, data=DEFAULT_CONFIG
) )
assert result["type"] == "form" assert result["type"] == "form"
@ -57,18 +62,12 @@ async def test_flow_works(hass):
async def test_flow_init_connection_error(hass): async def test_flow_init_connection_error(hass):
"""Test config flow with accesspoint connection error.""" """Test config flow with accesspoint connection error."""
config = {
const.HMIPC_HAPID: "ABC123",
const.HMIPC_PIN: "123",
const.HMIPC_NAME: "hmip",
}
with patch( with patch(
"homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_setup", "homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_setup",
return_value=False, return_value=False,
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
const.DOMAIN, context={"source": "user"}, data=config HMIPC_DOMAIN, context={"source": "user"}, data=DEFAULT_CONFIG
) )
assert result["type"] == "form" assert result["type"] == "form"
@ -77,12 +76,6 @@ async def test_flow_init_connection_error(hass):
async def test_flow_link_connection_error(hass): async def test_flow_link_connection_error(hass):
"""Test config flow client registration connection error.""" """Test config flow client registration connection error."""
config = {
const.HMIPC_HAPID: "ABC123",
const.HMIPC_PIN: "123",
const.HMIPC_NAME: "hmip",
}
with patch( with patch(
"homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_checkbutton", "homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_checkbutton",
return_value=True, return_value=True,
@ -94,7 +87,7 @@ async def test_flow_link_connection_error(hass):
return_value=False, return_value=False,
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
const.DOMAIN, context={"source": "user"}, data=config HMIPC_DOMAIN, context={"source": "user"}, data=DEFAULT_CONFIG
) )
assert result["type"] == "abort" assert result["type"] == "abort"
@ -103,12 +96,6 @@ async def test_flow_link_connection_error(hass):
async def test_flow_link_press_button(hass): async def test_flow_link_press_button(hass):
"""Test config flow ask for pressing the blue button.""" """Test config flow ask for pressing the blue button."""
config = {
const.HMIPC_HAPID: "ABC123",
const.HMIPC_PIN: "123",
const.HMIPC_NAME: "hmip",
}
with patch( with patch(
"homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_checkbutton", "homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_checkbutton",
return_value=False, return_value=False,
@ -117,7 +104,7 @@ async def test_flow_link_press_button(hass):
return_value=True, return_value=True,
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
const.DOMAIN, context={"source": "user"}, data=config HMIPC_DOMAIN, context={"source": "user"}, data=DEFAULT_CONFIG
) )
assert result["type"] == "form" assert result["type"] == "form"
@ -129,7 +116,7 @@ async def test_init_flow_show_form(hass):
"""Test config flow shows up with a form.""" """Test config flow shows up with a form."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
const.DOMAIN, context={"source": "user"} HMIPC_DOMAIN, context={"source": "user"}
) )
assert result["type"] == "form" assert result["type"] == "form"
assert result["step_id"] == "init" assert result["step_id"] == "init"
@ -137,19 +124,13 @@ async def test_init_flow_show_form(hass):
async def test_init_already_configured(hass): async def test_init_already_configured(hass):
"""Test accesspoint is already configured.""" """Test accesspoint is already configured."""
MockConfigEntry(domain=const.DOMAIN, unique_id="ABC123").add_to_hass(hass) MockConfigEntry(domain=HMIPC_DOMAIN, unique_id="ABC123").add_to_hass(hass)
config = {
const.HMIPC_HAPID: "ABC123",
const.HMIPC_PIN: "123",
const.HMIPC_NAME: "hmip",
}
with patch( with patch(
"homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_checkbutton", "homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_checkbutton",
return_value=True, return_value=True,
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
const.DOMAIN, context={"source": "user"}, data=config HMIPC_DOMAIN, context={"source": "user"}, data=DEFAULT_CONFIG
) )
assert result["type"] == "abort" assert result["type"] == "abort"
@ -158,12 +139,6 @@ async def test_init_already_configured(hass):
async def test_import_config(hass): async def test_import_config(hass):
"""Test importing a host with an existing config file.""" """Test importing a host with an existing config file."""
config = {
const.HMIPC_HAPID: "ABC123",
const.HMIPC_AUTHTOKEN: "123",
const.HMIPC_NAME: "hmip",
}
with patch( with patch(
"homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_checkbutton", "homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_checkbutton",
return_value=True, return_value=True,
@ -175,7 +150,7 @@ async def test_import_config(hass):
return_value=True, return_value=True,
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
const.DOMAIN, context={"source": "import"}, data=config HMIPC_DOMAIN, context={"source": "import"}, data=IMPORT_CONFIG
) )
assert result["type"] == "create_entry" assert result["type"] == "create_entry"
@ -186,13 +161,7 @@ async def test_import_config(hass):
async def test_import_existing_config(hass): async def test_import_existing_config(hass):
"""Test abort of an existing accesspoint from config.""" """Test abort of an existing accesspoint from config."""
MockConfigEntry(domain=const.DOMAIN, unique_id="ABC123").add_to_hass(hass) MockConfigEntry(domain=HMIPC_DOMAIN, unique_id="ABC123").add_to_hass(hass)
config = {
const.HMIPC_HAPID: "ABC123",
const.HMIPC_AUTHTOKEN: "123",
const.HMIPC_NAME: "hmip",
}
with patch( with patch(
"homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_checkbutton", "homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_checkbutton",
return_value=True, return_value=True,
@ -204,7 +173,7 @@ async def test_import_existing_config(hass):
return_value=True, return_value=True,
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
const.DOMAIN, context={"source": "import"}, data=config HMIPC_DOMAIN, context={"source": "import"}, data=IMPORT_CONFIG
) )
assert result["type"] == "abort" assert result["type"] == "abort"

View File

@ -5,81 +5,71 @@ from homematicip.aio.auth import AsyncAuth
from homematicip.base.base_connection import HmipConnectionError from homematicip.base.base_connection import HmipConnectionError
import pytest import pytest
from homeassistant.components.homematicip_cloud import ( from homeassistant.components.homematicip_cloud import DOMAIN as HMIPC_DOMAIN
DOMAIN as HMIPC_DOMAIN, from homeassistant.components.homematicip_cloud.const import (
const, HMIPC_AUTHTOKEN,
errors, HMIPC_HAPID,
hap as hmipc, HMIPC_NAME,
HMIPC_PIN,
) )
from homeassistant.components.homematicip_cloud.errors import HmipcConnectionError
from homeassistant.components.homematicip_cloud.hap import ( from homeassistant.components.homematicip_cloud.hap import (
HomematicipAuth, HomematicipAuth,
HomematicipHAP, HomematicipHAP,
) )
from homeassistant.config_entries import ENTRY_STATE_LOADED, ENTRY_STATE_NOT_LOADED
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
from .helper import HAPID, HAPPIN from .helper import HAPID, HAPPIN
from tests.common import mock_coro, mock_coro_func from tests.common import MockConfigEntry
async def test_auth_setup(hass): async def test_auth_setup(hass):
"""Test auth setup for client registration.""" """Test auth setup for client registration."""
config = { config = {HMIPC_HAPID: "ABC123", HMIPC_PIN: "123", HMIPC_NAME: "hmip"}
const.HMIPC_HAPID: "ABC123", hmip_auth = HomematicipAuth(hass, config)
const.HMIPC_PIN: "123", with patch.object(hmip_auth, "get_auth"):
const.HMIPC_NAME: "hmip", assert await hmip_auth.async_setup()
}
hap = hmipc.HomematicipAuth(hass, config)
with patch.object(hap, "get_auth", return_value=mock_coro()):
assert await hap.async_setup()
async def test_auth_setup_connection_error(hass): async def test_auth_setup_connection_error(hass):
"""Test auth setup connection error behaviour.""" """Test auth setup connection error behaviour."""
config = { config = {HMIPC_HAPID: "ABC123", HMIPC_PIN: "123", HMIPC_NAME: "hmip"}
const.HMIPC_HAPID: "ABC123", hmip_auth = HomematicipAuth(hass, config)
const.HMIPC_PIN: "123", with patch.object(hmip_auth, "get_auth", side_effect=HmipcConnectionError):
const.HMIPC_NAME: "hmip", assert not await hmip_auth.async_setup()
}
hap = hmipc.HomematicipAuth(hass, config)
with patch.object(hap, "get_auth", side_effect=errors.HmipcConnectionError):
assert not await hap.async_setup()
async def test_auth_auth_check_and_register(hass): async def test_auth_auth_check_and_register(hass):
"""Test auth client registration.""" """Test auth client registration."""
config = { config = {HMIPC_HAPID: "ABC123", HMIPC_PIN: "123", HMIPC_NAME: "hmip"}
const.HMIPC_HAPID: "ABC123",
const.HMIPC_PIN: "123", hmip_auth = HomematicipAuth(hass, config)
const.HMIPC_NAME: "hmip", hmip_auth.auth = Mock(spec=AsyncAuth)
}
hap = hmipc.HomematicipAuth(hass, config)
hap.auth = Mock()
with patch.object( with patch.object(
hap.auth, "isRequestAcknowledged", return_value=mock_coro(True) hmip_auth.auth, "isRequestAcknowledged", return_value=True
), patch.object( ), patch.object(
hap.auth, "requestAuthToken", return_value=mock_coro("ABC") hmip_auth.auth, "requestAuthToken", return_value="ABC"
), patch.object( ), patch.object(
hap.auth, "confirmAuthToken", return_value=mock_coro() hmip_auth.auth, "confirmAuthToken"
): ):
assert await hap.async_checkbutton() assert await hmip_auth.async_checkbutton()
assert await hap.async_register() == "ABC" assert await hmip_auth.async_register() == "ABC"
async def test_auth_auth_check_and_register_with_exception(hass): async def test_auth_auth_check_and_register_with_exception(hass):
"""Test auth client registration.""" """Test auth client registration."""
config = { config = {HMIPC_HAPID: "ABC123", HMIPC_PIN: "123", HMIPC_NAME: "hmip"}
const.HMIPC_HAPID: "ABC123", hmip_auth = HomematicipAuth(hass, config)
const.HMIPC_PIN: "123", hmip_auth.auth = Mock(spec=AsyncAuth)
const.HMIPC_NAME: "hmip",
}
hap = hmipc.HomematicipAuth(hass, config)
hap.auth = Mock(spec=AsyncAuth)
with patch.object( with patch.object(
hap.auth, "isRequestAcknowledged", side_effect=HmipConnectionError hmip_auth.auth, "isRequestAcknowledged", side_effect=HmipConnectionError
), patch.object(hap.auth, "requestAuthToken", side_effect=HmipConnectionError): ), patch.object(
assert not await hap.async_checkbutton() hmip_auth.auth, "requestAuthToken", side_effect=HmipConnectionError
assert await hap.async_register() is False ):
assert not await hmip_auth.async_checkbutton()
assert await hmip_auth.async_register() is False
async def test_hap_setup_works(aioclient_mock): async def test_hap_setup_works(aioclient_mock):
@ -87,13 +77,9 @@ async def test_hap_setup_works(aioclient_mock):
hass = Mock() hass = Mock()
entry = Mock() entry = Mock()
home = Mock() home = Mock()
entry.data = { entry.data = {HMIPC_HAPID: "ABC123", HMIPC_AUTHTOKEN: "123", HMIPC_NAME: "hmip"}
hmipc.HMIPC_HAPID: "ABC123", hap = HomematicipHAP(hass, entry)
hmipc.HMIPC_AUTHTOKEN: "123", with patch.object(hap, "get_hap", return_value=home):
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() assert await hap.async_setup()
assert hap.home is home assert hap.home is home
@ -112,44 +98,34 @@ async def test_hap_setup_connection_error():
"""Test a failed accesspoint setup.""" """Test a failed accesspoint setup."""
hass = Mock() hass = Mock()
entry = Mock() entry = Mock()
entry.data = { entry.data = {HMIPC_HAPID: "ABC123", HMIPC_AUTHTOKEN: "123", HMIPC_NAME: "hmip"}
hmipc.HMIPC_HAPID: "ABC123", hap = HomematicipHAP(hass, entry)
hmipc.HMIPC_AUTHTOKEN: "123", with patch.object(hap, "get_hap", side_effect=HmipcConnectionError), pytest.raises(
hmipc.HMIPC_NAME: "hmip", ConfigEntryNotReady
} ):
hap = hmipc.HomematicipHAP(hass, entry)
with patch.object(
hap, "get_hap", side_effect=errors.HmipcConnectionError
), pytest.raises(ConfigEntryNotReady):
await hap.async_setup() await hap.async_setup()
assert not hass.async_add_job.mock_calls assert not hass.async_add_job.mock_calls
assert not hass.config_entries.flow.async_init.mock_calls assert not hass.config_entries.flow.async_init.mock_calls
async def test_hap_reset_unloads_entry_if_setup(): async def test_hap_reset_unloads_entry_if_setup(hass, default_mock_hap, hmip_config):
"""Test calling reset while the entry has been setup.""" """Test calling reset while the entry has been setup."""
hass = Mock() MockConfigEntry(
entry = Mock() domain=HMIPC_DOMAIN,
home = Mock() unique_id=HAPID,
home.disable_events = mock_coro_func() data=hmip_config[HMIPC_DOMAIN][0],
entry.data = { state=ENTRY_STATE_LOADED,
hmipc.HMIPC_HAPID: "ABC123", ).add_to_hass(hass)
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()
assert hap.home is home assert hass.data[HMIPC_DOMAIN][HAPID] == default_mock_hap
assert not hass.services.async_register.mock_calls config_entries = hass.config_entries.async_entries(HMIPC_DOMAIN)
assert len(hass.config_entries.async_forward_entry_setup.mock_calls) == 8 assert len(config_entries) == 1
# hap_reset is called during unload
hass.config_entries.async_forward_entry_unload.return_value = mock_coro(True) await hass.config_entries.async_unload(config_entries[0].entry_id)
await hap.async_reset() # entry is unloaded
assert config_entries[0].state == ENTRY_STATE_NOT_LOADED
assert len(hass.config_entries.async_forward_entry_unload.mock_calls) == 8 assert hass.data[HMIPC_DOMAIN] == {}
async def test_hap_create(hass, hmip_config_entry, simple_mock_home): async def test_hap_create(hass, hmip_config_entry, simple_mock_home):
@ -160,7 +136,7 @@ async def test_hap_create(hass, hmip_config_entry, simple_mock_home):
with patch( with patch(
"homeassistant.components.homematicip_cloud.hap.AsyncHome", "homeassistant.components.homematicip_cloud.hap.AsyncHome",
return_value=simple_mock_home, return_value=simple_mock_home,
), patch.object(hap, "async_connect", return_value=mock_coro(None)): ), patch.object(hap, "async_connect"):
assert await hap.async_setup() assert await hap.async_setup()
@ -185,11 +161,7 @@ async def test_hap_create_exception(hass, hmip_config_entry, simple_mock_home):
async def test_auth_create(hass, simple_mock_auth): async def test_auth_create(hass, simple_mock_auth):
"""Mock AsyncAuth to execute get_auth.""" """Mock AsyncAuth to execute get_auth."""
config = { config = {HMIPC_HAPID: HAPID, HMIPC_PIN: HAPPIN, HMIPC_NAME: "hmip"}
const.HMIPC_HAPID: HAPID,
const.HMIPC_PIN: HAPPIN,
const.HMIPC_NAME: "hmip",
}
hmip_auth = HomematicipAuth(hass, config) hmip_auth = HomematicipAuth(hass, config)
assert hmip_auth assert hmip_auth
@ -204,11 +176,7 @@ async def test_auth_create(hass, simple_mock_auth):
async def test_auth_create_exception(hass, simple_mock_auth): async def test_auth_create_exception(hass, simple_mock_auth):
"""Mock AsyncAuth to execute get_auth.""" """Mock AsyncAuth to execute get_auth."""
config = { config = {HMIPC_HAPID: HAPID, HMIPC_PIN: HAPPIN, HMIPC_NAME: "hmip"}
const.HMIPC_HAPID: HAPID,
const.HMIPC_PIN: HAPPIN,
const.HMIPC_NAME: "hmip",
}
hmip_auth = HomematicipAuth(hass, config) hmip_auth = HomematicipAuth(hass, config)
simple_mock_auth.connectionRequest.side_effect = HmipConnectionError simple_mock_auth.connectionRequest.side_effect = HmipConnectionError
assert hmip_auth assert hmip_auth

View File

@ -1,33 +1,43 @@
"""Test HomematicIP Cloud setup process.""" """Test HomematicIP Cloud setup process."""
from asynctest import CoroutineMock, patch from asynctest import CoroutineMock, Mock, patch
from homeassistant.components import homematicip_cloud as hmipc from homeassistant.components.homematicip_cloud.const import (
CONF_ACCESSPOINT,
CONF_AUTHTOKEN,
DOMAIN as HMIPC_DOMAIN,
HMIPC_AUTHTOKEN,
HMIPC_HAPID,
HMIPC_NAME,
)
from homeassistant.components.homematicip_cloud.hap import HomematicipHAP
from homeassistant.config_entries import ENTRY_STATE_LOADED, ENTRY_STATE_NOT_LOADED
from homeassistant.const import CONF_NAME
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import Mock, MockConfigEntry from tests.common import MockConfigEntry
async def test_config_with_accesspoint_passed_to_config_entry(hass): async def test_config_with_accesspoint_passed_to_config_entry(hass):
"""Test that config for a accesspoint are loaded via config entry.""" """Test that config for a accesspoint are loaded via config entry."""
entry_config = { entry_config = {
hmipc.CONF_ACCESSPOINT: "ABC123", CONF_ACCESSPOINT: "ABC123",
hmipc.CONF_AUTHTOKEN: "123", CONF_AUTHTOKEN: "123",
hmipc.CONF_NAME: "name", CONF_NAME: "name",
} }
# no config_entry exists # no config_entry exists
assert len(hass.config_entries.async_entries(hmipc.DOMAIN)) == 0 assert len(hass.config_entries.async_entries(HMIPC_DOMAIN)) == 0
# no acccesspoint exists # no acccesspoint exists
assert not hass.data.get(hmipc.DOMAIN) assert not hass.data.get(HMIPC_DOMAIN)
assert ( assert (
await async_setup_component(hass, hmipc.DOMAIN, {hmipc.DOMAIN: entry_config}) await async_setup_component(hass, HMIPC_DOMAIN, {HMIPC_DOMAIN: entry_config})
is True is True
) )
# config_entry created for access point # config_entry created for access point
config_entries = hass.config_entries.async_entries(hmipc.DOMAIN) config_entries = hass.config_entries.async_entries(HMIPC_DOMAIN)
assert len(config_entries) == 1 assert len(config_entries) == 1
assert config_entries[0].data == { assert config_entries[0].data == {
"authtoken": "123", "authtoken": "123",
@ -35,21 +45,17 @@ async def test_config_with_accesspoint_passed_to_config_entry(hass):
"name": "name", "name": "name",
} }
# defined access_point created for config_entry # defined access_point created for config_entry
assert isinstance(hass.data[hmipc.DOMAIN]["ABC123"], hmipc.HomematicipHAP) assert isinstance(hass.data[HMIPC_DOMAIN]["ABC123"], HomematicipHAP)
async def test_config_already_registered_not_passed_to_config_entry(hass): async def test_config_already_registered_not_passed_to_config_entry(hass):
"""Test that an already registered accesspoint does not get imported.""" """Test that an already registered accesspoint does not get imported."""
mock_config = { mock_config = {HMIPC_AUTHTOKEN: "123", HMIPC_HAPID: "ABC123", HMIPC_NAME: "name"}
hmipc.HMIPC_AUTHTOKEN: "123", MockConfigEntry(domain=HMIPC_DOMAIN, data=mock_config).add_to_hass(hass)
hmipc.HMIPC_HAPID: "ABC123",
hmipc.HMIPC_NAME: "name",
}
MockConfigEntry(domain=hmipc.DOMAIN, data=mock_config).add_to_hass(hass)
# one config_entry exists # one config_entry exists
config_entries = hass.config_entries.async_entries(hmipc.DOMAIN) config_entries = hass.config_entries.async_entries(HMIPC_DOMAIN)
assert len(config_entries) == 1 assert len(config_entries) == 1
assert config_entries[0].data == { assert config_entries[0].data == {
"authtoken": "123", "authtoken": "123",
@ -60,17 +66,17 @@ async def test_config_already_registered_not_passed_to_config_entry(hass):
assert not config_entries[0].unique_id assert not config_entries[0].unique_id
entry_config = { entry_config = {
hmipc.CONF_ACCESSPOINT: "ABC123", CONF_ACCESSPOINT: "ABC123",
hmipc.CONF_AUTHTOKEN: "123", CONF_AUTHTOKEN: "123",
hmipc.CONF_NAME: "name", CONF_NAME: "name",
} }
assert ( assert (
await async_setup_component(hass, hmipc.DOMAIN, {hmipc.DOMAIN: entry_config}) await async_setup_component(hass, HMIPC_DOMAIN, {HMIPC_DOMAIN: entry_config})
is True is True
) )
# no new config_entry created / still one config_entry # no new config_entry created / still one config_entry
config_entries = hass.config_entries.async_entries(hmipc.DOMAIN) config_entries = hass.config_entries.async_entries(HMIPC_DOMAIN)
assert len(config_entries) == 1 assert len(config_entries) == 1
assert config_entries[0].data == { assert config_entries[0].data == {
"authtoken": "123", "authtoken": "123",
@ -83,14 +89,10 @@ async def test_config_already_registered_not_passed_to_config_entry(hass):
async def test_unload_entry(hass): async def test_unload_entry(hass):
"""Test being able to unload an entry.""" """Test being able to unload an entry."""
mock_config = { mock_config = {HMIPC_AUTHTOKEN: "123", HMIPC_HAPID: "ABC123", HMIPC_NAME: "name"}
hmipc.HMIPC_AUTHTOKEN: "123", MockConfigEntry(domain=HMIPC_DOMAIN, data=mock_config).add_to_hass(hass)
hmipc.HMIPC_HAPID: "ABC123",
hmipc.HMIPC_NAME: "name",
}
MockConfigEntry(domain=hmipc.DOMAIN, data=mock_config).add_to_hass(hass)
with patch.object(hmipc, "HomematicipHAP") as mock_hap: with patch("homeassistant.components.homematicip_cloud.HomematicipHAP") as mock_hap:
instance = mock_hap.return_value instance = mock_hap.return_value
instance.async_setup = CoroutineMock(return_value=True) instance.async_setup = CoroutineMock(return_value=True)
instance.home.id = "1" instance.home.id = "1"
@ -99,18 +101,19 @@ async def test_unload_entry(hass):
instance.home.currentAPVersion = "mock-ap-version" instance.home.currentAPVersion = "mock-ap-version"
instance.async_reset = CoroutineMock(return_value=True) instance.async_reset = CoroutineMock(return_value=True)
assert await async_setup_component(hass, hmipc.DOMAIN, {}) is True assert await async_setup_component(hass, HMIPC_DOMAIN, {}) is True
assert mock_hap.return_value.mock_calls[0][0] == "async_setup" assert mock_hap.return_value.mock_calls[0][0] == "async_setup"
assert hass.data[hmipc.DOMAIN]["ABC123"] assert hass.data[HMIPC_DOMAIN]["ABC123"]
config_entries = hass.config_entries.async_entries(hmipc.DOMAIN) config_entries = hass.config_entries.async_entries(HMIPC_DOMAIN)
assert len(config_entries) == 1 assert len(config_entries) == 1
assert config_entries[0].state == ENTRY_STATE_LOADED
await hass.config_entries.async_unload(config_entries[0].entry_id) await hass.config_entries.async_unload(config_entries[0].entry_id)
assert config_entries[0].state == ENTRY_STATE_NOT_LOADED
assert mock_hap.return_value.mock_calls[3][0] == "async_reset"
# entry is unloaded # entry is unloaded
assert hass.data[hmipc.DOMAIN] == {} assert hass.data[HMIPC_DOMAIN] == {}
async def test_hmip_dump_hap_config_services(hass, mock_hap_with_service): async def test_hmip_dump_hap_config_services(hass, mock_hap_with_service):