mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 07:37:34 +00:00
Emoncms add reconfigure flow (#145108)
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
parent
f780b9763d
commit
87b00fdc7b
@ -179,6 +179,47 @@ class EmoncmsConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def async_step_reconfigure(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> ConfigFlowResult:
|
||||||
|
"""Reconfigure the entry."""
|
||||||
|
errors: dict[str, str] = {}
|
||||||
|
description_placeholders = {}
|
||||||
|
reconfig_entry = self._get_reconfigure_entry()
|
||||||
|
if user_input is not None:
|
||||||
|
url = user_input[CONF_URL]
|
||||||
|
api_key = user_input[CONF_API_KEY]
|
||||||
|
emoncms_client = EmoncmsClient(
|
||||||
|
url, api_key, session=async_get_clientsession(self.hass)
|
||||||
|
)
|
||||||
|
result = await get_feed_list(emoncms_client)
|
||||||
|
if not result[CONF_SUCCESS]:
|
||||||
|
errors["base"] = "api_error"
|
||||||
|
description_placeholders = {"details": result[CONF_MESSAGE]}
|
||||||
|
else:
|
||||||
|
await self.async_set_unique_id(await emoncms_client.async_get_uuid())
|
||||||
|
self._abort_if_unique_id_mismatch()
|
||||||
|
return self.async_update_reload_and_abort(
|
||||||
|
reconfig_entry,
|
||||||
|
title=sensor_name(url),
|
||||||
|
data=user_input,
|
||||||
|
reload_even_if_entry_is_unchanged=False,
|
||||||
|
)
|
||||||
|
return self.async_show_form(
|
||||||
|
step_id="reconfigure",
|
||||||
|
data_schema=self.add_suggested_values_to_schema(
|
||||||
|
vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Required(CONF_URL): str,
|
||||||
|
vol.Required(CONF_API_KEY): str,
|
||||||
|
}
|
||||||
|
),
|
||||||
|
user_input or reconfig_entry.data,
|
||||||
|
),
|
||||||
|
errors=errors,
|
||||||
|
description_placeholders=description_placeholders,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class EmoncmsOptionsFlow(OptionsFlow):
|
class EmoncmsOptionsFlow(OptionsFlow):
|
||||||
"""Emoncms Options flow handler."""
|
"""Emoncms Options flow handler."""
|
||||||
|
@ -22,7 +22,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "This server is already configured"
|
"already_configured": "This server is already configured",
|
||||||
|
"unique_id_mismatch": "This emoncms serial number does not match the previous serial number",
|
||||||
|
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"selector": {
|
"selector": {
|
||||||
|
@ -43,6 +43,8 @@ FLOW_RESULT = {
|
|||||||
|
|
||||||
SENSOR_NAME = "emoncms@1.1.1.1"
|
SENSOR_NAME = "emoncms@1.1.1.1"
|
||||||
|
|
||||||
|
UNIQUE_ID = "123-53535292"
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def config_entry() -> MockConfigEntry:
|
def config_entry() -> MockConfigEntry:
|
||||||
@ -65,7 +67,7 @@ def config_entry_unique_id() -> MockConfigEntry:
|
|||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
title=SENSOR_NAME,
|
title=SENSOR_NAME,
|
||||||
data=FLOW_RESULT_SECOND_URL,
|
data=FLOW_RESULT_SECOND_URL,
|
||||||
unique_id="123-53535292",
|
unique_id=UNIQUE_ID,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -121,5 +123,5 @@ async def emoncms_client() -> AsyncGenerator[AsyncMock]:
|
|||||||
):
|
):
|
||||||
client = mock_client.return_value
|
client = mock_client.return_value
|
||||||
client.async_request.return_value = {"success": True, "message": FEEDS}
|
client.async_request.return_value = {"success": True, "message": FEEDS}
|
||||||
client.async_get_uuid.return_value = "123-53535292"
|
client.async_get_uuid.return_value = UNIQUE_ID
|
||||||
yield client
|
yield client
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
from unittest.mock import AsyncMock
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.emoncms.const import (
|
from homeassistant.components.emoncms.const import (
|
||||||
CONF_ONLY_INCLUDE_FEEDID,
|
CONF_ONLY_INCLUDE_FEEDID,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
@ -15,7 +17,7 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.data_entry_flow import FlowResultType
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
|
|
||||||
from . import setup_integration
|
from . import setup_integration
|
||||||
from .conftest import EMONCMS_FAILURE, FLOW_RESULT, SENSOR_NAME
|
from .conftest import EMONCMS_FAILURE, FLOW_RESULT, SENSOR_NAME, UNIQUE_ID
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
@ -25,6 +27,74 @@ USER_INPUT = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("url", "api_key"),
|
||||||
|
[
|
||||||
|
(USER_INPUT[CONF_URL], "regenerated_api_key"),
|
||||||
|
("http://1.1.1.2", USER_INPUT[CONF_API_KEY]),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_reconfigure(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
emoncms_client: AsyncMock,
|
||||||
|
url: str,
|
||||||
|
api_key: str,
|
||||||
|
) -> None:
|
||||||
|
"""Test reconfigure flow."""
|
||||||
|
new_input = {
|
||||||
|
CONF_URL: url,
|
||||||
|
CONF_API_KEY: api_key,
|
||||||
|
}
|
||||||
|
config_entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
title=SENSOR_NAME,
|
||||||
|
data=new_input,
|
||||||
|
unique_id=UNIQUE_ID,
|
||||||
|
)
|
||||||
|
await setup_integration(hass, config_entry)
|
||||||
|
result = await config_entry.start_reconfigure_flow(hass)
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["errors"] == {}
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
new_input,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "reconfigure_successful"
|
||||||
|
assert config_entry.data == new_input
|
||||||
|
|
||||||
|
|
||||||
|
async def test_reconfigure_api_error(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
emoncms_client: AsyncMock,
|
||||||
|
) -> None:
|
||||||
|
"""Test reconfigure flow with API error."""
|
||||||
|
config_entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
title=SENSOR_NAME,
|
||||||
|
data=USER_INPUT,
|
||||||
|
unique_id=UNIQUE_ID,
|
||||||
|
)
|
||||||
|
await setup_integration(hass, config_entry)
|
||||||
|
emoncms_client.async_request.return_value = EMONCMS_FAILURE
|
||||||
|
result = await config_entry.start_reconfigure_flow(hass)
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["errors"] == {}
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
USER_INPUT,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["errors"] == {"base": "api_error"}
|
||||||
|
assert result["description_placeholders"]["details"] == "failure"
|
||||||
|
assert result["step_id"] == "reconfigure"
|
||||||
|
|
||||||
|
|
||||||
async def test_user_flow_failure(
|
async def test_user_flow_failure(
|
||||||
hass: HomeAssistant, emoncms_client: AsyncMock
|
hass: HomeAssistant, emoncms_client: AsyncMock
|
||||||
) -> None:
|
) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user