Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions homeassistant/components/kostal_plenticore/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession

from .const import DOMAIN
from .const import CONF_SERVICE_CODE, DOMAIN
from .helper import get_hostname_id

_LOGGER = logging.getLogger(__name__)
Expand All @@ -21,6 +21,7 @@
{
vol.Required(CONF_HOST): str,
vol.Required(CONF_PASSWORD): str,
vol.Optional(CONF_SERVICE_CODE): str,
}
)

Expand All @@ -32,8 +33,10 @@ async def test_connection(hass: HomeAssistant, data) -> str:
"""

session = async_get_clientsession(hass)
async with ApiClient(session, data["host"]) as client:
await client.login(data["password"])
async with ApiClient(session, data[CONF_HOST]) as client:
await client.login(
data[CONF_PASSWORD], service_code=data.get(CONF_SERVICE_CODE)
)
hostname_id = await get_hostname_id(client)
values = await client.get_setting_values("scb:network", hostname_id)

Expand Down Expand Up @@ -70,3 +73,29 @@ async def async_step_user(
return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors=errors
)

async def async_step_reconfigure(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Add reconfigure step to allow to reconfigure a config entry."""
errors = {}

if user_input is not None:
try:
hostname = await test_connection(self.hass, user_input)
except AuthenticationException as ex:
errors[CONF_PASSWORD] = "invalid_auth"
_LOGGER.error("Error response: %s", ex)
except (ClientError, TimeoutError):
errors[CONF_HOST] = "cannot_connect"
except Exception:
_LOGGER.exception("Unexpected exception")
errors[CONF_BASE] = "unknown"
else:
return self.async_update_reload_and_abort(
entry=self._get_reconfigure_entry(), title=hostname, data=user_input
)

return self.async_show_form(
step_id="reconfigure", data_schema=DATA_SCHEMA, errors=errors
)
1 change: 1 addition & 0 deletions homeassistant/components/kostal_plenticore/const.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Constants for the Kostal Plenticore Solar Inverter integration."""

DOMAIN = "kostal_plenticore"
CONF_SERVICE_CODE = "service_code"
7 changes: 5 additions & 2 deletions homeassistant/components/kostal_plenticore/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

from .const import DOMAIN
from .const import CONF_SERVICE_CODE, DOMAIN
from .helper import get_hostname_id

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -59,7 +59,10 @@ async def async_setup(self) -> bool:
async_get_clientsession(self.hass), host=self.host
)
try:
await self._client.login(self.config_entry.data[CONF_PASSWORD])
await self._client.login(
self.config_entry.data[CONF_PASSWORD],
service_code=self.config_entry.data.get(CONF_SERVICE_CODE),
)
except AuthenticationException as err:
_LOGGER.error(
"Authentication exception connecting to %s: %s", self.host, err
Expand Down
13 changes: 11 additions & 2 deletions homeassistant/components/kostal_plenticore/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
"user": {
"data": {
"host": "[%key:common::config_flow::data::host%]",
"password": "[%key:common::config_flow::data::password%]"
"password": "[%key:common::config_flow::data::password%]",
"service_code": "Service code"
}
},
"reconfigure": {
"data": {
"host": "[%key:common::config_flow::data::host%]",
"password": "[%key:common::config_flow::data::password%]",
"service_code": "[%key:component::kostal_plenticore::config::step::user::data::service_code%]"
}
}
},
Expand All @@ -14,7 +22,8 @@
"unknown": "[%key:common::config_flow::error::unknown%]"
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]"
}
}
}
175 changes: 153 additions & 22 deletions tests/components/kostal_plenticore/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from homeassistant import config_entries
from homeassistant.components.kostal_plenticore.const import DOMAIN
from homeassistant.const import CONF_HOST, CONF_PASSWORD
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType

Expand Down Expand Up @@ -74,7 +75,7 @@ async def test_form_g1(
return_value={"scb:network": {"Hostname": "scb"}}
)

result2 = await hass.config_entries.flow.async_configure(
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
Expand All @@ -86,15 +87,15 @@ async def test_form_g1(
mock_apiclient_class.assert_called_once_with(ANY, "1.1.1.1")
mock_apiclient.__aenter__.assert_called_once()
mock_apiclient.__aexit__.assert_called_once()
mock_apiclient.login.assert_called_once_with("test-password")
mock_apiclient.login.assert_called_once_with("test-password", service_code=None)
mock_apiclient.get_settings.assert_called_once()
mock_apiclient.get_setting_values.assert_called_once_with(
"scb:network", "Hostname"
)

assert result2["type"] is FlowResultType.CREATE_ENTRY
assert result2["title"] == "scb"
assert result2["data"] == {
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "scb"
assert result["data"] == {
"host": "1.1.1.1",
"password": "test-password",
}
Expand Down Expand Up @@ -140,7 +141,7 @@ async def test_form_g2(
return_value={"scb:network": {"Network:Hostname": "scb"}}
)

result2 = await hass.config_entries.flow.async_configure(
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
Expand All @@ -152,21 +153,91 @@ async def test_form_g2(
mock_apiclient_class.assert_called_once_with(ANY, "1.1.1.1")
mock_apiclient.__aenter__.assert_called_once()
mock_apiclient.__aexit__.assert_called_once()
mock_apiclient.login.assert_called_once_with("test-password")
mock_apiclient.login.assert_called_once_with("test-password", service_code=None)
mock_apiclient.get_settings.assert_called_once()
mock_apiclient.get_setting_values.assert_called_once_with(
"scb:network", "Network:Hostname"
)

assert result2["type"] is FlowResultType.CREATE_ENTRY
assert result2["title"] == "scb"
assert result2["data"] == {
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "scb"
assert result["data"] == {
"host": "1.1.1.1",
"password": "test-password",
}
assert len(mock_setup_entry.mock_calls) == 1


async def test_form_g2_with_service_code(
hass: HomeAssistant,
mock_apiclient_class: type[ApiClient],
mock_apiclient: ApiClient,
) -> None:
"""Test the config flow for G2 models with a Service Code."""

result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}

with patch(
"homeassistant.components.kostal_plenticore.async_setup_entry",
return_value=True,
) as mock_setup_entry:
# mock of the context manager instance
mock_apiclient.login = AsyncMock()
mock_apiclient.get_settings = AsyncMock(
return_value={
"scb:network": [
SettingsData(
min="1",
max="63",
default=None,
access="readwrite",
unit=None,
id="Network:Hostname",
type="string",
),
]
}
)
mock_apiclient.get_setting_values = AsyncMock(
# G1 model has the entry id "Hostname"
return_value={"scb:network": {"Network:Hostname": "scb"}}
)

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
"password": "test-password",
"service_code": "test-service-code",
},
)
await hass.async_block_till_done()

mock_apiclient_class.assert_called_once_with(ANY, "1.1.1.1")
mock_apiclient.__aenter__.assert_called_once()
mock_apiclient.__aexit__.assert_called_once()
mock_apiclient.login.assert_called_once_with(
"test-password", service_code="test-service-code"
)
mock_apiclient.get_settings.assert_called_once()
mock_apiclient.get_setting_values.assert_called_once_with(
"scb:network", "Network:Hostname"
)

assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "scb"
assert result["data"] == {
"host": "1.1.1.1",
"password": "test-password",
"service_code": "test-service-code",
}
assert len(mock_setup_entry.mock_calls) == 1


async def test_form_invalid_auth(hass: HomeAssistant) -> None:
"""Test we handle invalid auth."""
result = await hass.config_entries.flow.async_init(
Expand All @@ -189,16 +260,16 @@ async def test_form_invalid_auth(hass: HomeAssistant) -> None:

mock_api_class.return_value = mock_api

result2 = await hass.config_entries.flow.async_configure(
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
"password": "test-password",
},
)

assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"password": "invalid_auth"}
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"password": "invalid_auth"}


async def test_form_cannot_connect(hass: HomeAssistant) -> None:
Expand All @@ -223,16 +294,16 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None:

mock_api_class.return_value = mock_api

result2 = await hass.config_entries.flow.async_configure(
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
"password": "test-password",
},
)

assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"host": "cannot_connect"}
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"host": "cannot_connect"}


async def test_form_unexpected_error(hass: HomeAssistant) -> None:
Expand All @@ -257,16 +328,16 @@ async def test_form_unexpected_error(hass: HomeAssistant) -> None:

mock_api_class.return_value = mock_api

result2 = await hass.config_entries.flow.async_configure(
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
"password": "test-password",
},
)

assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "unknown"}
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "unknown"}


async def test_already_configured(hass: HomeAssistant) -> None:
Expand All @@ -281,13 +352,73 @@ async def test_already_configured(hass: HomeAssistant) -> None:
DOMAIN, context={"source": config_entries.SOURCE_USER}
)

result2 = await hass.config_entries.flow.async_configure(
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
"password": "test-password",
},
)

assert result2["type"] is FlowResultType.ABORT
assert result2["reason"] == "already_configured"
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"


async def test_reconfigure(
hass: HomeAssistant,
mock_apiclient_class: type[ApiClient],
mock_apiclient: ApiClient,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test the config flow for G1 models."""

mock_config_entry.add_to_hass(hass)
result = await mock_config_entry.start_reconfigure_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure"
assert result["errors"] == {}

# mock of the context manager instance
mock_apiclient.login = AsyncMock()
mock_apiclient.get_settings = AsyncMock(
return_value={
"scb:network": [
SettingsData(
min="1",
max="63",
default=None,
access="readwrite",
unit=None,
id="Hostname",
type="string",
),
]
}
)
mock_apiclient.get_setting_values = AsyncMock(
# G1 model has the entry id "Hostname"
return_value={"scb:network": {"Hostname": "scb"}}
)

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
"password": "test-password",
},
)
await hass.async_block_till_done()

mock_apiclient_class.assert_called_once_with(ANY, "1.1.1.1")
mock_apiclient.__aenter__.assert_called_once()
mock_apiclient.__aexit__.assert_called_once()
mock_apiclient.login.assert_called_once_with("test-password", service_code=None)
mock_apiclient.get_settings.assert_called_once()
mock_apiclient.get_setting_values.assert_called_once_with("scb:network", "Hostname")

assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"

# changed entry
assert mock_config_entry.data[CONF_HOST] == "1.1.1.1"
assert mock_config_entry.data[CONF_PASSWORD] == "test-password"