Skip to content

Commit

Permalink
fix: config flow empty strings convert to None (#42)
Browse files Browse the repository at this point in the history
* fix: config flow empty strings convert to None
  • Loading branch information
firstof9 authored Sep 15, 2021
1 parent 96100d8 commit f3c4612
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 12 additions & 4 deletions custom_components/openei/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ async def async_step_user(self, user_input=None):
self._errors = {}

if user_input is not None:
for key, value in user_input.items():
if not bool(value):
user_input[key] = None
self._data.update(user_input)
return await self.async_step_user_2()

Expand All @@ -51,6 +54,9 @@ async def async_step_user_2(self, user_input=None):
self._errors = {}

if user_input is not None:
for key, value in user_input.items():
if not bool(value):
user_input[key] = None
self._data.update(user_input)
return await self.async_step_user_3()

Expand All @@ -61,6 +67,9 @@ async def async_step_user_3(self, user_input=None):
self._errors = {}

if user_input is not None:
for key, value in user_input.items():
if not bool(value):
user_input[key] = None
self._data.update(user_input)
return self.async_create_entry(
title=self._data[CONF_UTILITY], data=self._data
Expand Down Expand Up @@ -270,17 +279,16 @@ def _get_default(key: str, fallback_default: Any = None) -> Any | None:

async def _get_utility_list(hass, user_input) -> list | None:
"""Return list of utilities by lat/lon."""

lat = None
lon = None
address = user_input[CONF_LOCATION] if bool(user_input[CONF_LOCATION]) else None
radius = user_input[CONF_RADIUS] if bool(user_input[CONF_RADIUS]) else None

if user_input[CONF_LOCATION] in [None, ""]:
if user_input.get(CONF_LOCATION) is None:
lat = hass.config.latitude
lon = hass.config.longitude

api = user_input[CONF_API_KEY]
radius = user_input[CONF_RADIUS]
address = user_input[CONF_LOCATION]

plans = openeihttp.Rates(api=api, lat=lat, lon=lon, radius=radius, address=address)
plans = await hass.async_add_executor_job(_lookup_plans, plans)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
"Fake Utility Co",
{
"api_key": "fakeAPIKey",
"radius": "",
"radius": None,
"utility": "Fake Utility Co",
"rate_plan": "randomstring",
"sensor": "(none)",
"location": "",
"manual_plan": "",
"location": None,
"manual_plan": None,
},
),
],
Expand Down

0 comments on commit f3c4612

Please sign in to comment.