Skip to content

Commit

Permalink
Add tests for config flow
Browse files Browse the repository at this point in the history
  • Loading branch information
jdejaegh committed Jan 8, 2024
1 parent 90d4dd3 commit 2cca89f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions custom_components/irm_kmi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions custom_components/irm_kmi/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ async def async_step_user(self, user_input: dict | None = None) -> FlowResult:
"""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:
Expand All @@ -56,10 +56,10 @@ async def async_step_user(self, user_input: dict | None = None) -> FlowResult:
'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])
Expand All @@ -77,6 +77,7 @@ async def async_step_user(self, user_input: dict | None = None) -> FlowResult:
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)),
Expand Down
3 changes: 1 addition & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[tool:pytest]
testpaths = tests
norecursedirs = .git
addopts =
--cov=custom_components
addopts = -s -v
asyncio_mode = auto

0 comments on commit 2cca89f

Please sign in to comment.