Skip to content

Commit

Permalink
Move config option to OptionsFlow in iss (#65303)
Browse files Browse the repository at this point in the history
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
  • Loading branch information
DurgNomis-drol and frenck authored Feb 14, 2022
1 parent 5be5a01 commit 7cb0ce0
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 27 deletions.
7 changes: 7 additions & 0 deletions homeassistant/components/iss/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
4 changes: 2 additions & 2 deletions homeassistant/components/iss/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 41 additions & 9 deletions homeassistant/components/iss/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

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


Expand All @@ -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
Expand All @@ -26,17 +36,12 @@ async def async_step_user(self, user_input=None) -> FlowResult:

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."""
Expand All @@ -46,3 +51,30 @@ async def async_step_import(self, conf: dict) -> FlowResult:
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,
}
),
)
14 changes: 10 additions & 4 deletions homeassistant/components/iss/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}
}
12 changes: 9 additions & 3 deletions homeassistant/components/iss/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}
Expand Down
42 changes: 33 additions & 9 deletions tests/components/iss/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
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}
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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"

0 comments on commit 7cb0ce0

Please sign in to comment.