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 # Set by developer
VERSION = 1 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 @callback
def async_show_form( def async_show_form(
self, self,

View file

@ -49,7 +49,13 @@ class FlowManagerIndexView(_BaseFlowManagerView):
"""View to create config flows.""" """View to create config flows."""
@RequestDataValidator( @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): async def post(self, request, data):
"""Handle a POST request.""" """Handle a POST request."""
@ -60,7 +66,11 @@ class FlowManagerIndexView(_BaseFlowManagerView):
try: try:
result = await self._flow_mgr.async_init( 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: except data_entry_flow.UnknownHandler:
return self.json_message("Invalid handler specified", HTTP_NOT_FOUND) 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( return self.async_show_form(
step_id="user", step_id="user",
data_schema=schema, 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."}, errors={"username": "Should be unique."},
) )
with patch.dict(HANDLERS, {"test": TestFlow}): with patch.dict(HANDLERS, {"test": TestFlow}):
resp = await client.post( 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 assert resp.status == 200
@ -163,7 +167,10 @@ async def test_initialize_flow(hass, client):
{"name": "username", "required": True, "type": "string"}, {"name": "username", "required": True, "type": "string"},
{"name": "password", "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."}, "errors": {"username": "Should be unique."},
} }

View file

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