Fix habitica entry unload clean up (#46798)
* Fix habitica entry unload clean up * Fix service remove * Add entry setup and unload test * Fix config flow tests * Assert servicepull/46836/head
parent
fe4cf611f7
commit
43a5852561
|
@ -169,6 +169,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||
if unload_ok:
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
if len(hass.config_entries.async_entries) == 1:
|
||||
hass.components.webhook.async_unregister(SERVICE_API_CALL)
|
||||
if len(hass.config_entries.async_entries(DOMAIN)) == 1:
|
||||
hass.services.async_remove(DOMAIN, SERVICE_API_CALL)
|
||||
return unload_ok
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
"""Test the habitica config flow."""
|
||||
from asyncio import Future
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
from aiohttp import ClientResponseError
|
||||
|
||||
from homeassistant import config_entries, setup
|
||||
from homeassistant.components.habitica.config_flow import InvalidAuth
|
||||
from homeassistant.components.habitica.const import DEFAULT_URL, DOMAIN
|
||||
from homeassistant.const import HTTP_OK
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
@ -19,12 +18,8 @@ async def test_form(hass):
|
|||
assert result["type"] == "form"
|
||||
assert result["errors"] == {}
|
||||
|
||||
request_mock = MagicMock()
|
||||
type(request_mock).status_code = HTTP_OK
|
||||
|
||||
mock_obj = MagicMock()
|
||||
mock_obj.user.get.return_value = Future()
|
||||
mock_obj.user.get.return_value.set_result(None)
|
||||
mock_obj.user.get = AsyncMock()
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.habitica.config_flow.HabitipyAsync",
|
||||
|
@ -58,9 +53,12 @@ async def test_form_invalid_credentials(hass):
|
|||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
mock_obj = MagicMock()
|
||||
mock_obj.user.get = AsyncMock(side_effect=ClientResponseError(MagicMock(), ()))
|
||||
|
||||
with patch(
|
||||
"habitipy.aio.HabitipyAsync",
|
||||
side_effect=InvalidAuth,
|
||||
"homeassistant.components.habitica.config_flow.HabitipyAsync",
|
||||
return_value=mock_obj,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
|
@ -81,9 +79,12 @@ async def test_form_unexpected_exception(hass):
|
|||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
mock_obj = MagicMock()
|
||||
mock_obj.user.get = AsyncMock(side_effect=Exception)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.habitica.config_flow.HabitipyAsync",
|
||||
side_effect=Exception,
|
||||
return_value=mock_obj,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
|
@ -98,7 +99,7 @@ async def test_form_unexpected_exception(hass):
|
|||
assert result2["errors"] == {"base": "unknown"}
|
||||
|
||||
|
||||
async def test_manual_flow_config_exist(hass, aioclient_mock):
|
||||
async def test_manual_flow_config_exist(hass):
|
||||
"""Test config flow discovers only already configured config."""
|
||||
MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
|
@ -114,9 +115,7 @@ async def test_manual_flow_config_exist(hass, aioclient_mock):
|
|||
assert result["step_id"] == "user"
|
||||
|
||||
mock_obj = MagicMock()
|
||||
mock_obj.user.get.side_effect = AsyncMock(
|
||||
return_value={"api_user": "test-api-user"}
|
||||
)
|
||||
mock_obj.user.get = AsyncMock(return_value={"api_user": "test-api-user"})
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.habitica.config_flow.HabitipyAsync",
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
"""Test the habitica init module."""
|
||||
from homeassistant.components.habitica.const import (
|
||||
DEFAULT_URL,
|
||||
DOMAIN,
|
||||
SERVICE_API_CALL,
|
||||
)
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_entry_setup_unload(hass, aioclient_mock):
|
||||
"""Test integration setup and unload."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="test-api-user",
|
||||
data={
|
||||
"api_user": "test-api-user",
|
||||
"api_key": "test-api-key",
|
||||
"url": DEFAULT_URL,
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
aioclient_mock.get(
|
||||
"https://habitica.com/api/v3/user",
|
||||
json={"data": {"api_user": "test-api-user", "profile": {"name": "test_user"}}},
|
||||
)
|
||||
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.services.has_service(DOMAIN, SERVICE_API_CALL)
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
|
||||
assert not hass.services.has_service(DOMAIN, SERVICE_API_CALL)
|
Loading…
Reference in New Issue