Move config option to OptionsFlow in iss (#65303)
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
parent
5be5a014f3
commit
7cb0ce0eec
6 changed files with 102 additions and 27 deletions
|
@ -15,6 +15,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
|
||||
entry.async_on_unload(entry.add_update_listener(update_listener))
|
||||
|
||||
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
||||
|
||||
return True
|
||||
|
@ -25,3 +27,8 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||
del hass.data[DOMAIN]
|
||||
return unload_ok
|
||||
|
||||
|
||||
async def update_listener(hass: HomeAssistant, entry: ConfigEntry):
|
||||
"""Handle options update."""
|
||||
await hass.config_entries.async_reload(entry.entry_id)
|
||||
|
|
|
@ -72,8 +72,8 @@ async def async_setup_entry(
|
|||
) -> None:
|
||||
"""Set up the sensor platform."""
|
||||
|
||||
name = entry.data.get(CONF_NAME, DEFAULT_NAME)
|
||||
show_on_map = entry.data.get(CONF_SHOW_ON_MAP, False)
|
||||
name = entry.title
|
||||
show_on_map = entry.options.get(CONF_SHOW_ON_MAP, False)
|
||||
|
||||
try:
|
||||
iss_data = IssData(hass.config.latitude, hass.config.longitude)
|
||||
|
|
|
@ -4,8 +4,10 @@ import voluptuous as vol
|
|||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.const import CONF_NAME, CONF_SHOW_ON_MAP
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.data_entry_flow import FlowResult
|
||||
|
||||
from .binary_sensor import DEFAULT_NAME
|
||||
from .const import DOMAIN
|
||||
|
||||
|
||||
|
@ -14,6 +16,14 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
|
||||
VERSION = 1
|
||||
|
||||
@staticmethod
|
||||
@callback
|
||||
def async_get_options_flow(
|
||||
config_entry: config_entries.ConfigEntry,
|
||||
) -> config_entries.OptionsFlow:
|
||||
"""Get the options flow for this handler."""
|
||||
return OptionsFlowHandler(config_entry)
|
||||
|
||||
async def async_step_user(self, user_input=None) -> FlowResult:
|
||||
"""Handle a flow initialized by the user."""
|
||||
# Check if already configured
|
||||
|
@ -26,17 +36,12 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
|
||||
if user_input is not None:
|
||||
return self.async_create_entry(
|
||||
title="International Space Station", data=user_input
|
||||
title=user_input.get(CONF_NAME, DEFAULT_NAME),
|
||||
data={},
|
||||
options={CONF_SHOW_ON_MAP: user_input.get(CONF_SHOW_ON_MAP, False)},
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_SHOW_ON_MAP, default=False): bool,
|
||||
}
|
||||
),
|
||||
)
|
||||
return self.async_show_form(step_id="user")
|
||||
|
||||
async def async_step_import(self, conf: dict) -> FlowResult:
|
||||
"""Import a configuration from configuration.yaml."""
|
||||
|
@ -46,3 +51,30 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
CONF_SHOW_ON_MAP: conf[CONF_SHOW_ON_MAP],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class OptionsFlowHandler(config_entries.OptionsFlow):
|
||||
"""Config flow options handler for iss."""
|
||||
|
||||
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
|
||||
"""Initialize options flow."""
|
||||
self.config_entry = config_entry
|
||||
self.options = dict(config_entry.options)
|
||||
|
||||
async def async_step_init(self, user_input=None) -> FlowResult:
|
||||
"""Manage the options."""
|
||||
if user_input is not None:
|
||||
self.options.update(user_input)
|
||||
return self.async_create_entry(title="", data=self.options)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="init",
|
||||
data_schema=vol.Schema(
|
||||
{
|
||||
vol.Optional(
|
||||
CONF_SHOW_ON_MAP,
|
||||
default=self.config_entry.options.get(CONF_SHOW_ON_MAP, False),
|
||||
): bool,
|
||||
}
|
||||
),
|
||||
)
|
||||
|
|
|
@ -2,15 +2,21 @@
|
|||
"config": {
|
||||
"step": {
|
||||
"user": {
|
||||
"description": "Do you want to configure the International Space Station?",
|
||||
"data": {
|
||||
"show_on_map": "Show on map?"
|
||||
}
|
||||
"description": "Do you want to configure International Space Station (ISS)?"
|
||||
}
|
||||
},
|
||||
"abort": {
|
||||
"single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]",
|
||||
"latitude_longitude_not_defined": "Latitude and longitude are not defined in Home Assistant."
|
||||
}
|
||||
},
|
||||
"options": {
|
||||
"step": {
|
||||
"init": {
|
||||
"data": {
|
||||
"show_on_map": "Show on map"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,10 +6,16 @@
|
|||
},
|
||||
"step": {
|
||||
"user": {
|
||||
"description": "Do you want to configure International Space Station (ISS)?"
|
||||
}
|
||||
}
|
||||
},
|
||||
"options": {
|
||||
"step": {
|
||||
"init": {
|
||||
"data": {
|
||||
"show_on_map": "Show on map?"
|
||||
},
|
||||
"description": "Do you want to configure the International Space Station?"
|
||||
"show_on_map": "Show on map"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,11 +7,12 @@ from homeassistant.components.iss.const import DOMAIN
|
|||
from homeassistant.config import async_process_ha_core_config
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
|
||||
from homeassistant.const import CONF_NAME, CONF_SHOW_ON_MAP
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_import(hass):
|
||||
async def test_import(hass: HomeAssistant):
|
||||
"""Test entry will be imported."""
|
||||
|
||||
imported_config = {CONF_NAME: DEFAULT_NAME, CONF_SHOW_ON_MAP: False}
|
||||
|
@ -22,10 +23,11 @@ async def test_import(hass):
|
|||
DOMAIN, context={"source": SOURCE_IMPORT}, data=imported_config
|
||||
)
|
||||
assert result.get("type") == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result.get("result").data == imported_config
|
||||
assert result.get("result").title == DEFAULT_NAME
|
||||
assert result.get("result").options == {CONF_SHOW_ON_MAP: False}
|
||||
|
||||
|
||||
async def test_create_entry(hass):
|
||||
async def test_create_entry(hass: HomeAssistant):
|
||||
"""Test we can finish a config flow."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
|
@ -39,14 +41,14 @@ async def test_create_entry(hass):
|
|||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_SHOW_ON_MAP: True},
|
||||
{},
|
||||
)
|
||||
|
||||
assert result.get("type") == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result.get("result").data[CONF_SHOW_ON_MAP] is True
|
||||
assert result.get("result").data == {}
|
||||
|
||||
|
||||
async def test_integration_already_exists(hass):
|
||||
async def test_integration_already_exists(hass: HomeAssistant):
|
||||
"""Test we only allow a single config flow."""
|
||||
|
||||
MockConfigEntry(
|
||||
|
@ -55,14 +57,14 @@ async def test_integration_already_exists(hass):
|
|||
).add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data={CONF_SHOW_ON_MAP: False}
|
||||
DOMAIN, context={"source": SOURCE_USER}, data={}
|
||||
)
|
||||
|
||||
assert result.get("type") == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result.get("reason") == "single_instance_allowed"
|
||||
|
||||
|
||||
async def test_abort_no_home(hass):
|
||||
async def test_abort_no_home(hass: HomeAssistant):
|
||||
"""Test we don't create an entry if no coordinates are set."""
|
||||
|
||||
await async_process_ha_core_config(
|
||||
|
@ -71,8 +73,30 @@ async def test_abort_no_home(hass):
|
|||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data={CONF_SHOW_ON_MAP: False}
|
||||
DOMAIN, context={"source": SOURCE_USER}, data={}
|
||||
)
|
||||
|
||||
assert result.get("type") == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result.get("reason") == "latitude_longitude_not_defined"
|
||||
|
||||
|
||||
async def test_options(hass: HomeAssistant):
|
||||
"""Test options flow."""
|
||||
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={},
|
||||
)
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
optionflow = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||
|
||||
configured = await hass.config_entries.options.async_configure(
|
||||
optionflow["flow_id"],
|
||||
user_input={
|
||||
CONF_SHOW_ON_MAP: True,
|
||||
},
|
||||
)
|
||||
|
||||
assert configured.get("type") == "create_entry"
|
||||
|
|
Loading…
Add table
Reference in a new issue