From 2cca89f5baf93f25bab95d28a2a5e9bc75e05f9b Mon Sep 17 00:00:00 2001 From: Jules Dejaeghere Date: Mon, 8 Jan 2024 20:20:31 +0100 Subject: [PATCH] Add tests for config flow --- custom_components/irm_kmi/__init__.py | 1 + custom_components/irm_kmi/config_flow.py | 9 +++++---- setup.cfg | 3 +-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/custom_components/irm_kmi/__init__.py b/custom_components/irm_kmi/__init__.py index 8644ac3..39625a7 100644 --- a/custom_components/irm_kmi/__init__.py +++ b/custom_components/irm_kmi/__init__.py @@ -27,6 +27,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await coordinator.async_config_entry_first_refresh() except ConfigEntryError: # This happens when the zone is out of Benelux (no forecast available there) + # This should be caught by the config flow anyway return False await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) diff --git a/custom_components/irm_kmi/config_flow.py b/custom_components/irm_kmi/config_flow.py index 1a7dbe7..e81a9dc 100644 --- a/custom_components/irm_kmi/config_flow.py +++ b/custom_components/irm_kmi/config_flow.py @@ -39,11 +39,11 @@ class IrmKmiConfigFlow(ConfigFlow, domain=DOMAIN): """Define the user step of the configuration flow.""" errors = {} - if user_input is not None: + if user_input: _LOGGER.debug(f"Provided config user is: {user_input}") if (zone := self.hass.states.get(user_input[CONF_ZONE])) is None: - errors[CONF_ZONE] = 'Zone does not exist' + errors[CONF_ZONE] = 'zone_not_exist' # Check if zone is in Benelux if not errors: @@ -56,10 +56,10 @@ class IrmKmiConfigFlow(ConfigFlow, domain=DOMAIN): 'long': zone.attributes[ATTR_LONGITUDE]} ) except Exception: - errors['base'] = "Could not get data from the API" + errors['base'] = "api_error" if api_data.get('cityName', None) in OUT_OF_BENELUX: - errors[CONF_ZONE] = 'Zone is outside of Benelux' + errors[CONF_ZONE] = 'out_of_benelux' if not errors: await self.async_set_unique_id(user_input[CONF_ZONE]) @@ -77,6 +77,7 @@ class IrmKmiConfigFlow(ConfigFlow, domain=DOMAIN): return self.async_show_form( step_id="user", errors=errors, + description_placeholders={'zone': user_input.get('zone') if user_input is not None else None}, data_schema=vol.Schema({ vol.Required(CONF_ZONE): EntitySelector(EntitySelectorConfig(domain=ZONE_DOMAIN)), diff --git a/setup.cfg b/setup.cfg index 5f47d7a..8bd0e5f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,5 @@ [tool:pytest] testpaths = tests norecursedirs = .git -addopts = - --cov=custom_components +addopts = -s -v asyncio_mode = auto \ No newline at end of file