Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions homeassistant/components/met/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][config_entry.entry_id] = coordinator

config_entry.async_on_unload(config_entry.add_update_listener(async_update_entry))

await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)

return True
Expand All @@ -85,6 +87,11 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
return unload_ok


async def async_update_entry(hass: HomeAssistant, config_entry: ConfigEntry):
"""Reload Met component when options changed."""
await hass.config_entries.async_reload(config_entry.entry_id)


class CannotConnect(HomeAssistantError):
"""Unable to connect to the web site."""

Expand Down
92 changes: 68 additions & 24 deletions homeassistant/components/met/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,30 @@ def configured_instances(hass: HomeAssistant) -> set[str]:
return set(entries)


class MetFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
def _get_data_schema(
name: str | None,
latitude: float | None,
longitude: float | None,
elevation: int | None,
) -> vol.Schema:
"""Get a schema with default values."""
return vol.Schema(
{
vol.Required(CONF_NAME, default=name): str,
vol.Required(CONF_LATITUDE, default=latitude): cv.latitude,
vol.Required(CONF_LONGITUDE, default=longitude): cv.longitude,
vol.Required(CONF_ELEVATION, default=elevation): int,
}
)


class MetConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for Met component."""

VERSION = 1

def __init__(self) -> None:
"""Init MetFlowHandler."""
"""Init MetConfigFlowHandler."""
self._errors: dict[str, Any] = {}

async def async_step_user(
Expand All @@ -59,30 +76,13 @@ async def async_step_user(
)
self._errors[CONF_NAME] = "already_configured"

return await self._show_config_form(
name=HOME_LOCATION_NAME,
latitude=self.hass.config.latitude,
longitude=self.hass.config.longitude,
elevation=self.hass.config.elevation,
)

async def _show_config_form(
self,
name: str | None = None,
latitude: float | None = None,
longitude: float | None = None,
elevation: int | None = None,
) -> FlowResult:
"""Show the configuration form to edit location data."""
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(CONF_NAME, default=name): str,
vol.Required(CONF_LATITUDE, default=latitude): cv.latitude,
vol.Required(CONF_LONGITUDE, default=longitude): cv.longitude,
vol.Required(CONF_ELEVATION, default=elevation): int,
}
data_schema=_get_data_schema(
name=HOME_LOCATION_NAME,
latitude=self.hass.config.latitude,
longitude=self.hass.config.longitude,
elevation=self.hass.config.elevation,
),
errors=self._errors,
)
Expand All @@ -102,3 +102,47 @@ async def async_step_onboarding(
return self.async_create_entry(
title=HOME_LOCATION_NAME, data={CONF_TRACK_HOME: True}
)

@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> config_entries.OptionsFlow:
"""Get the options flow for Met."""
return MetOptionsFlowHandler(config_entry)


class MetOptionsFlowHandler(config_entries.OptionsFlow):

@epenet epenet Feb 21, 2023

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this use a SchemaOptionsFlowHandler instead (see accuweather)?
Since this is just a single schema without any validation it seems a better fit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if SchemaOptionsFlowHandler works in this case. The OptionsFlow is calling hass.config_entries.async_update_entry to update the config entry, which SchemaOptionsFlowHandler doesn't seem to do?

"""Options flow for Met component."""

def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize the Met OptionsFlow."""
self._config_entry = config_entry
self._errors: dict[str, Any] = {}

async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Configure options for Met."""

if user_input is not None:
# Update config entry with data from user input
self.hass.config_entries.async_update_entry(
self._config_entry, data=user_input
)
return self.async_create_entry(
title=self._config_entry.title, data=user_input
)

config_data = self._config_entry.data

return self.async_show_form(
step_id="init",
data_schema=_get_data_schema(
name=config_data.get(CONF_NAME),
latitude=config_data.get(CONF_LATITUDE),
longitude=config_data.get(CONF_LONGITUDE),
elevation=config_data.get(CONF_ELEVATION),
),
errors=self._errors,
)
13 changes: 13 additions & 0 deletions homeassistant/components/met/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,18 @@
"abort": {
"no_home": "No home coordinates are set in the Home Assistant configuration"
}
},
"options": {
"step": {
"init": {
"title": "[%key:common::config_flow::data::location%]",
"data": {
"name": "[%key:common::config_flow::data::name%]",
"latitude": "[%key:common::config_flow::data::latitude%]",
"longitude": "[%key:common::config_flow::data::longitude%]",
"elevation": "[%key:common::config_flow::data::elevation%]"
}
}
}
}
}
27 changes: 27 additions & 0 deletions tests/components/met/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from homeassistant.const import CONF_ELEVATION, CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.core import HomeAssistant

from . import init_integration

from tests.common import MockConfigEntry


Expand Down Expand Up @@ -130,3 +132,28 @@ async def test_onboarding_step_abort_no_home(

assert result["type"] == "abort"
assert result["reason"] == "no_home"


async def test_show_options_form(hass: HomeAssistant) -> None:
"""Test show options form."""
entry = await init_integration(hass, track_home=True)

result = await hass.config_entries.options.async_init(entry.entry_id)

assert result["type"] == "form"
assert result["step_id"] == "init"


async def test_options_update_config_entry(hass: HomeAssistant) -> None:
"""Test options flow updating config entry."""
entry = await init_integration(hass, track_home=True)

update_data = {"name": "test", "latitude": 12, "longitude": 23, "elevation": 456}

result = await hass.config_entries.options.async_init(
entry.entry_id, data=update_data
)

assert result["type"] == "create_entry"
assert result["title"] == "Mock Title"
assert result["data"] == update_data
Comment thread
chrisx8 marked this conversation as resolved.
Outdated