mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 05:47:10 +00:00
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:
parent
25449b424f
commit
a7ba4bd086
@ -72,7 +72,7 @@ class EmoncmsConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
config_entry: ConfigEntry,
|
config_entry: ConfigEntry,
|
||||||
) -> EmoncmsOptionsFlow:
|
) -> EmoncmsOptionsFlow:
|
||||||
"""Get the options flow for this handler."""
|
"""Get the options flow for this handler."""
|
||||||
return EmoncmsOptionsFlow()
|
return EmoncmsOptionsFlow(config_entry)
|
||||||
|
|
||||||
async def async_step_user(
|
async def async_step_user(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
@ -175,18 +175,23 @@ class EmoncmsConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
class EmoncmsOptionsFlow(OptionsFlow):
|
class EmoncmsOptionsFlow(OptionsFlow):
|
||||||
"""Emoncms Options flow handler."""
|
"""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(
|
async def async_step_init(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Manage the options."""
|
"""Manage the options."""
|
||||||
errors: dict[str, str] = {}
|
errors: dict[str, str] = {}
|
||||||
description_placeholders = {}
|
description_placeholders = {}
|
||||||
data = self.options if self.options else self.config_entry.data
|
include_only_feeds = self.config_entry.options.get(
|
||||||
url = data[CONF_URL]
|
CONF_ONLY_INCLUDE_FEEDID,
|
||||||
api_key = data[CONF_API_KEY]
|
self.config_entry.data.get(CONF_ONLY_INCLUDE_FEEDID, []),
|
||||||
include_only_feeds = data.get(CONF_ONLY_INCLUDE_FEEDID, [])
|
)
|
||||||
options: list = include_only_feeds
|
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]:
|
if not result[CONF_SUCCESS]:
|
||||||
errors["base"] = "api_error"
|
errors["base"] = "api_error"
|
||||||
description_placeholders = {"details": result[CONF_MESSAGE]}
|
description_placeholders = {"details": result[CONF_MESSAGE]}
|
||||||
@ -196,10 +201,7 @@ class EmoncmsOptionsFlow(OptionsFlow):
|
|||||||
if user_input:
|
if user_input:
|
||||||
include_only_feeds = user_input[CONF_ONLY_INCLUDE_FEEDID]
|
include_only_feeds = user_input[CONF_ONLY_INCLUDE_FEEDID]
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=sensor_name(url),
|
|
||||||
data={
|
data={
|
||||||
CONF_URL: url,
|
|
||||||
CONF_API_KEY: api_key,
|
|
||||||
CONF_ONLY_INCLUDE_FEEDID: include_only_feeds,
|
CONF_ONLY_INCLUDE_FEEDID: include_only_feeds,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -138,10 +138,11 @@ async def async_setup_entry(
|
|||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the emoncms sensors."""
|
"""Set up the emoncms sensors."""
|
||||||
config = entry.options if entry.options else entry.data
|
name = sensor_name(entry.data[CONF_URL])
|
||||||
name = sensor_name(config[CONF_URL])
|
exclude_feeds = entry.data.get(CONF_EXCLUDE_FEEDID)
|
||||||
exclude_feeds = config.get(CONF_EXCLUDE_FEEDID)
|
include_only_feeds = entry.options.get(
|
||||||
include_only_feeds = config.get(CONF_ONLY_INCLUDE_FEEDID)
|
CONF_ONLY_INCLUDE_FEEDID, entry.data.get(CONF_ONLY_INCLUDE_FEEDID)
|
||||||
|
)
|
||||||
|
|
||||||
if exclude_feeds is None and include_only_feeds is None:
|
if exclude_feeds is None and include_only_feeds is None:
|
||||||
return
|
return
|
||||||
|
@ -97,10 +97,6 @@ async def test_user_flow(
|
|||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
USER_OPTIONS = {
|
|
||||||
CONF_ONLY_INCLUDE_FEEDID: ["1"],
|
|
||||||
}
|
|
||||||
|
|
||||||
CONFIG_ENTRY = {
|
CONFIG_ENTRY = {
|
||||||
CONF_API_KEY: "my_api_key",
|
CONF_API_KEY: "my_api_key",
|
||||||
CONF_ONLY_INCLUDE_FEEDID: ["1"],
|
CONF_ONLY_INCLUDE_FEEDID: ["1"],
|
||||||
@ -116,15 +112,19 @@ async def test_options_flow(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Options flow - success test."""
|
"""Options flow - success test."""
|
||||||
await setup_integration(hass, config_entry)
|
await setup_integration(hass, config_entry)
|
||||||
|
assert config_entry.options == {}
|
||||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input=USER_OPTIONS,
|
user_input={
|
||||||
|
CONF_ONLY_INCLUDE_FEEDID: ["1"],
|
||||||
|
},
|
||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result["data"] == CONFIG_ENTRY
|
assert config_entry.options == {
|
||||||
assert config_entry.options == CONFIG_ENTRY
|
CONF_ONLY_INCLUDE_FEEDID: ["1"],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async def test_options_flow_failure(
|
async def test_options_flow_failure(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user