mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Replace _host_in_configuration_exists with async_abort_entries_match in solarlog (#125099)
* Add diagnostics to solarlog * Fix wrong comment * Move to async_abort_entries_match * Remove obsolete method solarlog_entries * Update tests/components/solarlog/test_config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update tests/components/solarlog/test_config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update tests/components/solarlog/test_config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update tests/components/solarlog/test_config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Amend import of config_entries.SOURCE_USER * Update tests/components/solarlog/test_config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Ruff --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
parent
671aaa7e95
commit
d68ee8dcea
@ -10,7 +10,6 @@ import voluptuous as vol
|
|||||||
|
|
||||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||||
from homeassistant.const import CONF_HOST, CONF_NAME
|
from homeassistant.const import CONF_HOST, CONF_NAME
|
||||||
from homeassistant.core import HomeAssistant, callback
|
|
||||||
from homeassistant.util import slugify
|
from homeassistant.util import slugify
|
||||||
|
|
||||||
from .const import DEFAULT_HOST, DEFAULT_NAME, DOMAIN
|
from .const import DEFAULT_HOST, DEFAULT_NAME, DOMAIN
|
||||||
@ -18,14 +17,6 @@ from .const import DEFAULT_HOST, DEFAULT_NAME, DOMAIN
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@callback
|
|
||||||
def solarlog_entries(hass: HomeAssistant) -> set[str]:
|
|
||||||
"""Return the hosts already configured."""
|
|
||||||
return {
|
|
||||||
entry.data[CONF_HOST] for entry in hass.config_entries.async_entries(DOMAIN)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class SolarLogConfigFlow(ConfigFlow, domain=DOMAIN):
|
class SolarLogConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a config flow for solarlog."""
|
"""Handle a config flow for solarlog."""
|
||||||
|
|
||||||
@ -36,12 +27,6 @@ class SolarLogConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
"""Initialize the config flow."""
|
"""Initialize the config flow."""
|
||||||
self._errors: dict = {}
|
self._errors: dict = {}
|
||||||
|
|
||||||
def _host_in_configuration_exists(self, host: str) -> bool:
|
|
||||||
"""Return True if host exists in configuration."""
|
|
||||||
if host in solarlog_entries(self.hass):
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def _parse_url(self, host: str) -> str:
|
def _parse_url(self, host: str) -> str:
|
||||||
"""Return parsed host url."""
|
"""Return parsed host url."""
|
||||||
url = urlparse(host, "http")
|
url = urlparse(host, "http")
|
||||||
@ -72,12 +57,13 @@ class SolarLogConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
"""Step when user initializes a integration."""
|
"""Step when user initializes a integration."""
|
||||||
self._errors = {}
|
self._errors = {}
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
user_input[CONF_NAME] = slugify(user_input[CONF_NAME])
|
|
||||||
user_input[CONF_HOST] = self._parse_url(user_input[CONF_HOST])
|
user_input[CONF_HOST] = self._parse_url(user_input[CONF_HOST])
|
||||||
|
|
||||||
if self._host_in_configuration_exists(user_input[CONF_HOST]):
|
self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
|
||||||
self._errors[CONF_HOST] = "already_configured"
|
|
||||||
elif await self._test_connection(user_input[CONF_HOST]):
|
user_input[CONF_NAME] = slugify(user_input[CONF_NAME])
|
||||||
|
|
||||||
|
if await self._test_connection(user_input[CONF_HOST]):
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=user_input[CONF_NAME], data=user_input
|
title=user_input[CONF_NAME], data=user_input
|
||||||
)
|
)
|
||||||
|
@ -5,9 +5,9 @@ from unittest.mock import AsyncMock, patch
|
|||||||
import pytest
|
import pytest
|
||||||
from solarlog_cli.solarlog_exceptions import SolarLogConnectionError, SolarLogError
|
from solarlog_cli.solarlog_exceptions import SolarLogConnectionError, SolarLogError
|
||||||
|
|
||||||
from homeassistant import config_entries
|
|
||||||
from homeassistant.components.solarlog import config_flow
|
from homeassistant.components.solarlog import config_flow
|
||||||
from homeassistant.components.solarlog.const import DOMAIN
|
from homeassistant.components.solarlog.const import DOMAIN
|
||||||
|
from homeassistant.config_entries import SOURCE_RECONFIGURE, SOURCE_USER
|
||||||
from homeassistant.const import CONF_HOST, CONF_NAME
|
from homeassistant.const import CONF_HOST, CONF_NAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResultType
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
@ -21,7 +21,7 @@ async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
|
|||||||
"""Test we get the form."""
|
"""Test we get the form."""
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": SOURCE_USER}
|
||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
@ -60,7 +60,7 @@ async def test_user(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Test user config."""
|
"""Test user config."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": SOURCE_USER}
|
||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
@ -125,40 +125,25 @@ async def test_form_exceptions(
|
|||||||
|
|
||||||
async def test_abort_if_already_setup(hass: HomeAssistant, test_connect: None) -> None:
|
async def test_abort_if_already_setup(hass: HomeAssistant, test_connect: None) -> None:
|
||||||
"""Test we abort if the device is already setup."""
|
"""Test we abort if the device is already setup."""
|
||||||
flow = init_config_flow(hass)
|
|
||||||
MockConfigEntry(
|
|
||||||
domain="solarlog", data={CONF_NAME: NAME, CONF_HOST: HOST}
|
|
||||||
).add_to_hass(hass)
|
|
||||||
|
|
||||||
# Should fail, same HOST different NAME (default)
|
MockConfigEntry(domain=DOMAIN, data={CONF_NAME: NAME, CONF_HOST: HOST}).add_to_hass(
|
||||||
result = await flow.async_step_user(
|
hass
|
||||||
{CONF_HOST: HOST, CONF_NAME: "solarlog_test_7_8_9", "extended_data": False}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN, context={"source": SOURCE_USER}
|
||||||
|
)
|
||||||
|
|
||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["errors"] == {CONF_HOST: "already_configured"}
|
assert result["step_id"] == "user"
|
||||||
|
assert result["errors"] == {}
|
||||||
|
|
||||||
# Should fail, same HOST and NAME
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result = await flow.async_step_user({CONF_HOST: HOST, CONF_NAME: NAME})
|
result["flow_id"],
|
||||||
assert result["type"] is FlowResultType.FORM
|
{CONF_HOST: HOST, CONF_NAME: "solarlog_test_7_8_9", "extended_data": False},
|
||||||
assert result["errors"] == {CONF_HOST: "already_configured"}
|
|
||||||
|
|
||||||
# SHOULD pass, diff HOST (without http://), different NAME
|
|
||||||
result = await flow.async_step_user(
|
|
||||||
{CONF_HOST: "2.2.2.2", CONF_NAME: "solarlog_test_7_8_9", "extended_data": False}
|
|
||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
assert result["type"] is FlowResultType.ABORT
|
||||||
assert result["title"] == "solarlog_test_7_8_9"
|
assert result["reason"] == "already_configured"
|
||||||
assert result["data"][CONF_HOST] == "http://2.2.2.2"
|
|
||||||
|
|
||||||
# SHOULD pass, diff HOST, same NAME
|
|
||||||
result = await flow.async_step_user(
|
|
||||||
{CONF_HOST: "http://2.2.2.2", CONF_NAME: NAME, "extended_data": False}
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
||||||
assert result["title"] == "solarlog_test_1_2_3"
|
|
||||||
assert result["data"][CONF_HOST] == "http://2.2.2.2"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_reconfigure_flow(
|
async def test_reconfigure_flow(
|
||||||
@ -178,7 +163,7 @@ async def test_reconfigure_flow(
|
|||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={
|
context={
|
||||||
"source": config_entries.SOURCE_RECONFIGURE,
|
"source": SOURCE_RECONFIGURE,
|
||||||
"entry_id": entry.entry_id,
|
"entry_id": entry.entry_id,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user