From d3bafce1570b6efc3fcbf5db8ffe7c318f217566 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 28 Oct 2021 18:09:28 +0200 Subject: [PATCH] Use constants in acmeda config flow (#58590) Co-authored-by: epenet --- homeassistant/components/acmeda/config_flow.py | 13 +++++++------ homeassistant/data_entry_flow.py | 4 +++- tests/components/acmeda/test_config_flow.py | 6 +++--- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/acmeda/config_flow.py b/homeassistant/components/acmeda/config_flow.py index 1f288e84bc7..460357baa78 100644 --- a/homeassistant/components/acmeda/config_flow.py +++ b/homeassistant/components/acmeda/config_flow.py @@ -8,7 +8,8 @@ import aiopulse import async_timeout import voluptuous as vol -from homeassistant import config_entries +from homeassistant import config_entries, data_entry_flow +from homeassistant.const import CONF_HOST, CONF_ID from .const import DOMAIN @@ -27,9 +28,9 @@ class AcmedaFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): if ( user_input is not None and self.discovered_hubs is not None - and user_input["id"] in self.discovered_hubs + and user_input[CONF_ID] in self.discovered_hubs ): - return await self.async_create(self.discovered_hubs[user_input["id"]]) + return await self.async_create(self.discovered_hubs[user_input[CONF_ID]]) # Already configured hosts already_configured = { @@ -52,10 +53,10 @@ class AcmedaFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): self.discovered_hubs = {hub.id: hub for hub in hubs} return self.async_show_form( - step_id="user", + step_id=data_entry_flow.STEP_ID_USER, data_schema=vol.Schema( { - vol.Required("id"): vol.In( + vol.Required(CONF_ID): vol.In( {hub.id: f"{hub.id} {hub.host}" for hub in hubs} ) } @@ -65,4 +66,4 @@ class AcmedaFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): async def async_create(self, hub): """Create the Acmeda Hub entry.""" await self.async_set_unique_id(hub.id, raise_on_progress=False) - return self.async_create_entry(title=hub.id, data={"host": hub.host}) + return self.async_create_entry(title=hub.id, data={CONF_HOST: hub.host}) diff --git a/homeassistant/data_entry_flow.py b/homeassistant/data_entry_flow.py index c1f798fcc32..b5a35929459 100644 --- a/homeassistant/data_entry_flow.py +++ b/homeassistant/data_entry_flow.py @@ -5,7 +5,7 @@ import abc import asyncio from collections.abc import Iterable, Mapping from types import MappingProxyType -from typing import Any, TypedDict +from typing import Any, Final, TypedDict import uuid import voluptuous as vol @@ -21,6 +21,8 @@ RESULT_TYPE_EXTERNAL_STEP_DONE = "external_done" RESULT_TYPE_SHOW_PROGRESS = "progress" RESULT_TYPE_SHOW_PROGRESS_DONE = "progress_done" +STEP_ID_USER: Final = "user" + # Event that is fired when a flow is progressed via external or progress source. EVENT_DATA_ENTRY_FLOW_PROGRESSED = "data_entry_flow_progressed" diff --git a/tests/components/acmeda/test_config_flow.py b/tests/components/acmeda/test_config_flow.py index 269a72cd839..6355d1b1adb 100644 --- a/tests/components/acmeda/test_config_flow.py +++ b/tests/components/acmeda/test_config_flow.py @@ -69,7 +69,7 @@ async def test_show_form_one_hub(hass, mock_hub_discover, mock_hub_run): assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["title"] == dummy_hub_1.id assert result["result"].data == { - "host": DUMMY_HOST1, + CONF_HOST: DUMMY_HOST1, } # Check we performed the discovery @@ -92,7 +92,7 @@ async def test_show_form_two_hubs(hass, mock_hub_discover): ) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM - assert result["step_id"] == "user" + assert result["step_id"] == data_entry_flow.STEP_ID_USER # Check we performed the discovery assert len(mock_hub_discover.mock_calls) == 1 @@ -120,7 +120,7 @@ async def test_create_second_entry(hass, mock_hub_run, mock_hub_discover): assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["title"] == dummy_hub_2.id assert result["result"].data == { - "host": DUMMY_HOST2, + CONF_HOST: DUMMY_HOST2, }