2021-07-28 19:41:11 +00:00
|
|
|
"""Tests for Renault setup process."""
|
2021-08-16 10:52:58 +00:00
|
|
|
from unittest.mock import patch
|
2021-07-28 19:41:11 +00:00
|
|
|
|
|
|
|
import aiohttp
|
|
|
|
from renault_api.gigya.exceptions import InvalidCredentialsException
|
|
|
|
|
|
|
|
from homeassistant.components.renault.const import DOMAIN
|
2021-08-16 10:52:58 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntryState
|
2021-07-28 19:41:11 +00:00
|
|
|
|
2021-08-16 10:52:58 +00:00
|
|
|
from . import get_mock_config_entry, setup_renault_integration_simple
|
2021-07-28 19:41:11 +00:00
|
|
|
|
|
|
|
|
2021-08-16 10:52:58 +00:00
|
|
|
async def test_setup_unload_entry(hass):
|
2021-07-28 19:41:11 +00:00
|
|
|
"""Test entry setup and unload."""
|
2021-08-16 10:52:58 +00:00
|
|
|
with patch("homeassistant.components.renault.PLATFORMS", []):
|
|
|
|
config_entry = await setup_renault_integration_simple(hass)
|
2021-07-28 19:41:11 +00:00
|
|
|
|
2021-08-16 10:52:58 +00:00
|
|
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
|
|
|
assert config_entry.state is ConfigEntryState.LOADED
|
|
|
|
assert config_entry.unique_id in hass.data[DOMAIN]
|
2021-07-28 19:41:11 +00:00
|
|
|
|
2021-08-16 10:52:58 +00:00
|
|
|
# Unload the entry and verify that the data has been removed
|
|
|
|
await hass.config_entries.async_unload(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
|
|
|
assert config_entry.unique_id not in hass.data[DOMAIN]
|
2021-07-28 19:41:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_entry_bad_password(hass):
|
|
|
|
"""Test entry setup and unload."""
|
|
|
|
# Create a mock entry so we don't have to go through config flow
|
2021-08-16 10:52:58 +00:00
|
|
|
config_entry = get_mock_config_entry()
|
|
|
|
config_entry.add_to_hass(hass)
|
2021-07-28 19:41:11 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"renault_api.renault_session.RenaultSession.login",
|
|
|
|
side_effect=InvalidCredentialsException(403042, "invalid loginID or password"),
|
|
|
|
):
|
2021-08-16 10:52:58 +00:00
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
|
|
|
assert config_entry.state is ConfigEntryState.SETUP_ERROR
|
|
|
|
assert not hass.data.get(DOMAIN)
|
2021-07-28 19:41:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_entry_exception(hass):
|
|
|
|
"""Test ConfigEntryNotReady when API raises an exception during entry setup."""
|
2021-08-16 10:52:58 +00:00
|
|
|
config_entry = get_mock_config_entry()
|
|
|
|
config_entry.add_to_hass(hass)
|
2021-07-28 19:41:11 +00:00
|
|
|
|
|
|
|
# In this case we are testing the condition where async_setup_entry raises
|
|
|
|
# ConfigEntryNotReady.
|
|
|
|
with patch(
|
|
|
|
"renault_api.renault_session.RenaultSession.login",
|
|
|
|
side_effect=aiohttp.ClientConnectionError,
|
2021-08-16 10:52:58 +00:00
|
|
|
):
|
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
|
|
|
assert config_entry.state is ConfigEntryState.SETUP_RETRY
|
|
|
|
assert not hass.data.get(DOMAIN)
|