Skip to content

Commit

Permalink
Replace _host_in_configuration_exists with async_abort_entries_match …
Browse files Browse the repository at this point in the history
…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>
  • Loading branch information
dontinelli and joostlek authored Sep 2, 2024
1 parent 671aaa7 commit d68ee8d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 51 deletions.
24 changes: 5 additions & 19 deletions homeassistant/components/solarlog/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,13 @@

from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.core import HomeAssistant, callback
from homeassistant.util import slugify

from .const import DEFAULT_HOST, DEFAULT_NAME, DOMAIN

_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):
"""Handle a config flow for solarlog."""

Expand All @@ -36,12 +27,6 @@ def __init__(self) -> None:
"""Initialize the config flow."""
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:
"""Return parsed host url."""
url = urlparse(host, "http")
Expand Down Expand Up @@ -72,12 +57,13 @@ async def async_step_user(
"""Step when user initializes a integration."""
self._errors = {}
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])

if self._host_in_configuration_exists(user_input[CONF_HOST]):
self._errors[CONF_HOST] = "already_configured"
elif await self._test_connection(user_input[CONF_HOST]):
self._async_abort_entries_match({CONF_HOST: 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(
title=user_input[CONF_NAME], data=user_input
)
Expand Down
49 changes: 17 additions & 32 deletions tests/components/solarlog/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import pytest
from solarlog_cli.solarlog_exceptions import SolarLogConnectionError, SolarLogError

from homeassistant import config_entries
from homeassistant.components.solarlog import config_flow
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.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
Expand All @@ -21,7 +21,7 @@ async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
"""Test we get the form."""

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["errors"] == {}
Expand Down Expand Up @@ -60,7 +60,7 @@ async def test_user(
) -> None:
"""Test user config."""
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["errors"] == {}
Expand Down Expand Up @@ -125,40 +125,25 @@ async def test_form_exceptions(

async def test_abort_if_already_setup(hass: HomeAssistant, test_connect: None) -> None:
"""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)
result = await flow.async_step_user(
{CONF_HOST: HOST, CONF_NAME: "solarlog_test_7_8_9", "extended_data": False}
MockConfigEntry(domain=DOMAIN, data={CONF_NAME: NAME, CONF_HOST: HOST}).add_to_hass(
hass
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {CONF_HOST: "already_configured"}

# Should fail, same HOST and NAME
result = await flow.async_step_user({CONF_HOST: HOST, CONF_NAME: NAME})
assert result["type"] is FlowResultType.FORM
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}
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "solarlog_test_7_8_9"
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.FORM
assert result["step_id"] == "user"
assert result["errors"] == {}

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"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: HOST, CONF_NAME: "solarlog_test_7_8_9", "extended_data": False},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"


async def test_reconfigure_flow(
Expand All @@ -178,7 +163,7 @@ async def test_reconfigure_flow(
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": config_entries.SOURCE_RECONFIGURE,
"source": SOURCE_RECONFIGURE,
"entry_id": entry.entry_id,
},
)
Expand Down

0 comments on commit d68ee8d

Please sign in to comment.