mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Allow multiple entries in Tautulli (#74406)
This commit is contained in:
parent
f463b22053
commit
a8b8b245d1
@ -30,7 +30,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
)
|
)
|
||||||
coordinator = TautulliDataUpdateCoordinator(hass, host_configuration, api_client)
|
coordinator = TautulliDataUpdateCoordinator(hass, host_configuration, api_client)
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
hass.data[DOMAIN] = coordinator
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
@ -39,7 +39,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||||
hass.data.pop(DOMAIN)
|
hass.data[DOMAIN].pop(entry.entry_id)
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,10 +24,9 @@ class TautulliConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> FlowResult:
|
) -> FlowResult:
|
||||||
"""Handle a flow initiated by the user."""
|
"""Handle a flow initiated by the user."""
|
||||||
if self._async_current_entries():
|
|
||||||
return self.async_abort(reason="single_instance_allowed")
|
|
||||||
errors = {}
|
errors = {}
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
|
self._async_abort_entries_match({CONF_URL: user_input[CONF_URL]})
|
||||||
if (error := await self.validate_input(user_input)) is None:
|
if (error := await self.validate_input(user_input)) is None:
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=DEFAULT_NAME,
|
title=DEFAULT_NAME,
|
||||||
|
@ -232,7 +232,7 @@ async def async_setup_entry(
|
|||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Tautulli sensor."""
|
"""Set up Tautulli sensor."""
|
||||||
coordinator: TautulliDataUpdateCoordinator = hass.data[DOMAIN]
|
coordinator: TautulliDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
entities: list[TautulliSensor | TautulliSessionSensor] = [
|
entities: list[TautulliSensor | TautulliSessionSensor] = [
|
||||||
TautulliSensor(
|
TautulliSensor(
|
||||||
coordinator,
|
coordinator,
|
||||||
|
@ -23,7 +23,7 @@
|
|||||||
"unknown": "[%key:common::config_flow::error::unknown%]"
|
"unknown": "[%key:common::config_flow::error::unknown%]"
|
||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]",
|
"already_configured": "[%key:common::config_flow::abort::already_configured_service%]",
|
||||||
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
|
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
"config": {
|
"config": {
|
||||||
"abort": {
|
"abort": {
|
||||||
"reauth_successful": "Re-authentication was successful",
|
"reauth_successful": "Re-authentication was successful",
|
||||||
"single_instance_allowed": "Already configured. Only a single configuration possible."
|
"already_configured": "Service is already configured"
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
"cannot_connect": "Failed to connect",
|
"cannot_connect": "Failed to connect",
|
||||||
|
@ -2,14 +2,9 @@
|
|||||||
|
|
||||||
from unittest.mock import AsyncMock, patch
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
from homeassistant.components.tautulli.const import CONF_MONITORED_USERS, DOMAIN
|
from homeassistant.components.tautulli.const import DOMAIN
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_API_KEY,
|
CONF_API_KEY,
|
||||||
CONF_HOST,
|
|
||||||
CONF_MONITORED_CONDITIONS,
|
|
||||||
CONF_PATH,
|
|
||||||
CONF_PORT,
|
|
||||||
CONF_SSL,
|
|
||||||
CONF_URL,
|
CONF_URL,
|
||||||
CONF_VERIFY_SSL,
|
CONF_VERIFY_SSL,
|
||||||
CONTENT_TYPE_JSON,
|
CONTENT_TYPE_JSON,
|
||||||
@ -22,7 +17,6 @@ from tests.test_util.aiohttp import AiohttpClientMocker
|
|||||||
API_KEY = "abcd"
|
API_KEY = "abcd"
|
||||||
URL = "http://1.2.3.4:8181/test"
|
URL = "http://1.2.3.4:8181/test"
|
||||||
NAME = "Tautulli"
|
NAME = "Tautulli"
|
||||||
SSL = False
|
|
||||||
VERIFY_SSL = True
|
VERIFY_SSL = True
|
||||||
|
|
||||||
CONF_DATA = {
|
CONF_DATA = {
|
||||||
@ -30,19 +24,6 @@ CONF_DATA = {
|
|||||||
CONF_URL: URL,
|
CONF_URL: URL,
|
||||||
CONF_VERIFY_SSL: VERIFY_SSL,
|
CONF_VERIFY_SSL: VERIFY_SSL,
|
||||||
}
|
}
|
||||||
CONF_IMPORT_DATA = {
|
|
||||||
CONF_API_KEY: API_KEY,
|
|
||||||
CONF_HOST: "1.2.3.4",
|
|
||||||
CONF_MONITORED_CONDITIONS: ["Stream count"],
|
|
||||||
CONF_MONITORED_USERS: ["test"],
|
|
||||||
CONF_PORT: "8181",
|
|
||||||
CONF_PATH: "/test",
|
|
||||||
CONF_SSL: SSL,
|
|
||||||
CONF_VERIFY_SSL: VERIFY_SSL,
|
|
||||||
}
|
|
||||||
|
|
||||||
DEFAULT_USERS = [{11111111: {"enabled": False}, 22222222: {"enabled": False}}]
|
|
||||||
SELECTED_USERNAMES = ["user1"]
|
|
||||||
|
|
||||||
|
|
||||||
def patch_config_flow_tautulli(mocked_tautulli) -> AsyncMock:
|
def patch_config_flow_tautulli(mocked_tautulli) -> AsyncMock:
|
||||||
@ -104,9 +85,6 @@ async def setup_integration(
|
|||||||
CONF_VERIFY_SSL: VERIFY_SSL,
|
CONF_VERIFY_SSL: VERIFY_SSL,
|
||||||
CONF_API_KEY: api_key,
|
CONF_API_KEY: api_key,
|
||||||
},
|
},
|
||||||
options={
|
|
||||||
CONF_MONITORED_USERS: DEFAULT_USERS,
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
@ -6,36 +6,15 @@ from pytautulli import exceptions
|
|||||||
from homeassistant import data_entry_flow
|
from homeassistant import data_entry_flow
|
||||||
from homeassistant.components.tautulli.const import DOMAIN
|
from homeassistant.components.tautulli.const import DOMAIN
|
||||||
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
||||||
from homeassistant.const import CONF_API_KEY, CONF_SOURCE
|
from homeassistant.const import CONF_API_KEY, CONF_SOURCE, CONF_URL, CONF_VERIFY_SSL
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from . import (
|
from . import CONF_DATA, NAME, patch_config_flow_tautulli, setup_integration
|
||||||
CONF_DATA,
|
|
||||||
CONF_IMPORT_DATA,
|
|
||||||
NAME,
|
|
||||||
patch_config_flow_tautulli,
|
|
||||||
setup_integration,
|
|
||||||
)
|
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||||
|
|
||||||
|
|
||||||
async def test_flow_user_single_instance_allowed(hass: HomeAssistant) -> None:
|
|
||||||
"""Test user step single instance allowed."""
|
|
||||||
entry = MockConfigEntry(domain=DOMAIN, data=CONF_DATA)
|
|
||||||
entry.add_to_hass(hass)
|
|
||||||
|
|
||||||
with patch_config_flow_tautulli(AsyncMock()):
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN,
|
|
||||||
context={"source": SOURCE_USER},
|
|
||||||
data=CONF_IMPORT_DATA,
|
|
||||||
)
|
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
|
||||||
assert result["reason"] == "single_instance_allowed"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_flow_user(hass: HomeAssistant) -> None:
|
async def test_flow_user(hass: HomeAssistant) -> None:
|
||||||
"""Test user initiated flow."""
|
"""Test user initiated flow."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
@ -126,6 +105,50 @@ async def test_flow_user_unknown_error(hass: HomeAssistant) -> None:
|
|||||||
assert result2["data"] == CONF_DATA
|
assert result2["data"] == CONF_DATA
|
||||||
|
|
||||||
|
|
||||||
|
async def test_flow_user_already_configured(hass: HomeAssistant) -> None:
|
||||||
|
"""Test user step already configured."""
|
||||||
|
entry = MockConfigEntry(domain=DOMAIN, data=CONF_DATA)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
with patch_config_flow_tautulli(AsyncMock()):
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN,
|
||||||
|
context={"source": SOURCE_USER},
|
||||||
|
data=CONF_DATA,
|
||||||
|
)
|
||||||
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_flow_user_multiple_entries_allowed(hass: HomeAssistant) -> None:
|
||||||
|
"""Test user step can configure multiple entries."""
|
||||||
|
entry = MockConfigEntry(domain=DOMAIN, data=CONF_DATA)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN, context={CONF_SOURCE: SOURCE_USER}
|
||||||
|
)
|
||||||
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
|
assert result["step_id"] == "user"
|
||||||
|
assert result["errors"] == {}
|
||||||
|
|
||||||
|
input = {
|
||||||
|
CONF_URL: "http://1.2.3.5:8181/test",
|
||||||
|
CONF_API_KEY: "efgh",
|
||||||
|
CONF_VERIFY_SSL: True,
|
||||||
|
}
|
||||||
|
with patch_config_flow_tautulli(AsyncMock()):
|
||||||
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
user_input=input,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||||
|
assert result2["title"] == NAME
|
||||||
|
assert result2["data"] == input
|
||||||
|
|
||||||
|
|
||||||
async def test_flow_reauth(
|
async def test_flow_reauth(
|
||||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||||
) -> None:
|
) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user