1
mirror of https://github.com/home-assistant/core synced 2024-10-01 05:30:36 +02:00

Allow flows to know if user is in advanced mode (#34629)

This commit is contained in:
Paulus Schoutsen 2020-04-24 09:31:56 -07:00 committed by GitHub
parent 4afa2a2651
commit 6404882ec4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 6 deletions

View File

@ -253,6 +253,16 @@ class FlowHandler:
# Set by developer
VERSION = 1
@property
def source(self) -> Optional[str]:
"""Source that initialized the flow."""
return self.context.get("source", None)
@property
def show_advanced_options(self) -> bool:
"""If we should show advanced options."""
return self.context.get("show_advanced_options", False)
@callback
def async_show_form(
self,

View File

@ -49,7 +49,13 @@ class FlowManagerIndexView(_BaseFlowManagerView):
"""View to create config flows."""
@RequestDataValidator(
vol.Schema({vol.Required("handler"): vol.Any(str, list)}, extra=vol.ALLOW_EXTRA)
vol.Schema(
{
vol.Required("handler"): vol.Any(str, list),
vol.Optional("show_advanced_options", default=False): cv.boolean,
},
extra=vol.ALLOW_EXTRA,
)
)
async def post(self, request, data):
"""Handle a POST request."""
@ -60,7 +66,11 @@ class FlowManagerIndexView(_BaseFlowManagerView):
try:
result = await self._flow_mgr.async_init(
handler, context={"source": config_entries.SOURCE_USER}
handler,
context={
"source": config_entries.SOURCE_USER,
"show_advanced_options": data["show_advanced_options"],
},
)
except data_entry_flow.UnknownHandler:
return self.json_message("Invalid handler specified", HTTP_NOT_FOUND)

View File

@ -141,13 +141,17 @@ async def test_initialize_flow(hass, client):
return self.async_show_form(
step_id="user",
data_schema=schema,
description_placeholders={"url": "https://example.com"},
description_placeholders={
"url": "https://example.com",
"show_advanced_options": self.show_advanced_options,
},
errors={"username": "Should be unique."},
)
with patch.dict(HANDLERS, {"test": TestFlow}):
resp = await client.post(
"/api/config/config_entries/flow", json={"handler": "test"}
"/api/config/config_entries/flow",
json={"handler": "test", "show_advanced_options": True},
)
assert resp.status == 200
@ -163,7 +167,10 @@ async def test_initialize_flow(hass, client):
{"name": "username", "required": True, "type": "string"},
{"name": "password", "required": True, "type": "string"},
],
"description_placeholders": {"url": "https://example.com"},
"description_placeholders": {
"url": "https://example.com",
"show_advanced_options": True,
},
"errors": {"username": "Should be unique."},
}

View File

@ -26,7 +26,6 @@ def manager():
flow = handler()
flow.init_step = context.get("init_step", "init")
flow.source = context.get("source")
return flow
async def async_finish_flow(self, flow, result):