Use read-only options in emoncms options flow (#129926)

* Use read-only options in emoncms options flow

* Don't store URL and API_KEY in entry options
This commit is contained in:
epenet 2024-11-06 13:09:05 +01:00 committed by GitHub
parent 25449b424f
commit a7ba4bd086
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 20 deletions

View File

@ -72,7 +72,7 @@ class EmoncmsConfigFlow(ConfigFlow, domain=DOMAIN):
config_entry: ConfigEntry,
) -> EmoncmsOptionsFlow:
"""Get the options flow for this handler."""
return EmoncmsOptionsFlow()
return EmoncmsOptionsFlow(config_entry)
async def async_step_user(
self, user_input: dict[str, Any] | None = None
@ -175,18 +175,23 @@ class EmoncmsConfigFlow(ConfigFlow, domain=DOMAIN):
class EmoncmsOptionsFlow(OptionsFlow):
"""Emoncms Options flow handler."""
def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize emoncms options flow."""
self._url = config_entry.data[CONF_URL]
self._api_key = config_entry.data[CONF_API_KEY]
async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Manage the options."""
errors: dict[str, str] = {}
description_placeholders = {}
data = self.options if self.options else self.config_entry.data
url = data[CONF_URL]
api_key = data[CONF_API_KEY]
include_only_feeds = data.get(CONF_ONLY_INCLUDE_FEEDID, [])
include_only_feeds = self.config_entry.options.get(
CONF_ONLY_INCLUDE_FEEDID,
self.config_entry.data.get(CONF_ONLY_INCLUDE_FEEDID, []),
)
options: list = include_only_feeds
result = await get_feed_list(self.hass, url, api_key)
result = await get_feed_list(self.hass, self._url, self._api_key)
if not result[CONF_SUCCESS]:
errors["base"] = "api_error"
description_placeholders = {"details": result[CONF_MESSAGE]}
@ -196,10 +201,7 @@ class EmoncmsOptionsFlow(OptionsFlow):
if user_input:
include_only_feeds = user_input[CONF_ONLY_INCLUDE_FEEDID]
return self.async_create_entry(
title=sensor_name(url),
data={
CONF_URL: url,
CONF_API_KEY: api_key,
CONF_ONLY_INCLUDE_FEEDID: include_only_feeds,
},
)

View File

@ -138,10 +138,11 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the emoncms sensors."""
config = entry.options if entry.options else entry.data
name = sensor_name(config[CONF_URL])
exclude_feeds = config.get(CONF_EXCLUDE_FEEDID)
include_only_feeds = config.get(CONF_ONLY_INCLUDE_FEEDID)
name = sensor_name(entry.data[CONF_URL])
exclude_feeds = entry.data.get(CONF_EXCLUDE_FEEDID)
include_only_feeds = entry.options.get(
CONF_ONLY_INCLUDE_FEEDID, entry.data.get(CONF_ONLY_INCLUDE_FEEDID)
)
if exclude_feeds is None and include_only_feeds is None:
return

View File

@ -97,10 +97,6 @@ async def test_user_flow(
assert len(mock_setup_entry.mock_calls) == 1
USER_OPTIONS = {
CONF_ONLY_INCLUDE_FEEDID: ["1"],
}
CONFIG_ENTRY = {
CONF_API_KEY: "my_api_key",
CONF_ONLY_INCLUDE_FEEDID: ["1"],
@ -116,15 +112,19 @@ async def test_options_flow(
) -> None:
"""Options flow - success test."""
await setup_integration(hass, config_entry)
assert config_entry.options == {}
result = await hass.config_entries.options.async_init(config_entry.entry_id)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input=USER_OPTIONS,
user_input={
CONF_ONLY_INCLUDE_FEEDID: ["1"],
},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"] == CONFIG_ENTRY
assert config_entry.options == CONFIG_ENTRY
assert config_entry.options == {
CONF_ONLY_INCLUDE_FEEDID: ["1"],
}
async def test_options_flow_failure(