Skip to content

Commit

Permalink
fix: config flow options and clearing location (#46)
Browse files Browse the repository at this point in the history
* fix: config flow options and clearing location

* clean up unused import
* clean up testing debug code

* adjust tests
  • Loading branch information
firstof9 authored Sep 18, 2021
1 parent 9307ecb commit 8efc280
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
3 changes: 0 additions & 3 deletions custom_components/openei/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
updated_config[CONF_PLAN] = updated_config[CONF_MANUAL_PLAN]
updated_config.pop(CONF_MANUAL_PLAN, None)

if CONF_LOCATION in updated_config.keys() and updated_config[CONF_LOCATION] == "":
updated_config.pop(CONF_LOCATION, None)

_LOGGER.debug("updated_config: %s", updated_config)
if updated_config != entry.data:
hass.config_entries.async_update_entry(entry, data=updated_config)
Expand Down
11 changes: 4 additions & 7 deletions custom_components/openei/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from homeassistant import config_entries
from homeassistant.components.sensor import DOMAIN as SENSORS_DOMAIN
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import config_validation as cv
import openeihttp
import voluptuous as vol

Expand Down Expand Up @@ -41,7 +40,6 @@ async def async_step_user(self, user_input=None):
self._errors = {}

if user_input is not None:
_LOGGER.debug("Step 1: %s", user_input)
self._data.update(user_input)
return await self.async_step_user_2()

Expand Down Expand Up @@ -122,8 +120,9 @@ async def async_step_init(self, user_input=None): # pylint: disable=unused-argu
async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
if user_input is not None:
if user_input[CONF_LOCATION] == '""':
user_input[CONF_LOCATION] = ""
self._data.update(user_input)
_LOGGER.debug("Step 1: %s", user_input)
return await self.async_step_user_2()

return await self._show_config_form(user_input)
Expand All @@ -133,7 +132,6 @@ async def async_step_user_2(self, user_input=None):
_LOGGER.debug("data: %s", self._data)
if user_input is not None:
self._data.update(user_input)
_LOGGER.debug("Step 2: %s", user_input)
return await self.async_step_user_3()

return await self._show_config_form_2(user_input)
Expand All @@ -143,7 +141,6 @@ async def async_step_user_3(self, user_input=None):
_LOGGER.debug("data: %s", self._data)
if user_input is not None:
self._data.update(user_input)
_LOGGER.debug("Step 3: %s", user_input)
return self.async_create_entry(title="", data=self._data)

return await self._show_config_form_3(user_input)
Expand Down Expand Up @@ -273,7 +270,7 @@ async def _get_utility_list(hass, user_input) -> list | None:
address = user_input[CONF_LOCATION]
radius = user_input[CONF_RADIUS]

if not bool(user_input[CONF_LOCATION]):
if not bool(address):
lat = hass.config.latitude
lon = hass.config.longitude
address = None
Expand All @@ -299,7 +296,7 @@ async def _get_plan_list(hass, user_input) -> list | None:
radius = user_input[CONF_RADIUS]
utility = user_input[CONF_UTILITY]

if not bool(user_input[CONF_LOCATION]):
if not bool(address):
lat = hass.config.latitude
lon = hass.config.longitude
address = None
Expand Down
12 changes: 6 additions & 6 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async def test_form(
)
assert result["type"] == "form"
assert result["errors"] == {}
# assert result["title"] == title_1
# assert result['title'] == title_1

with patch(
"custom_components.openei.async_setup", return_value=True
Expand Down Expand Up @@ -168,7 +168,7 @@ async def test_options_flow(

assert result["type"] == "form"
assert result["errors"] == {}
# assert result["title"] == title_1
# assert result['title'] == title_1

with patch("custom_components.openei.async_setup", return_value=True), patch(
"custom_components.openei.async_setup_entry",
Expand Down Expand Up @@ -277,7 +277,7 @@ async def test_options_flow_no_changes(

assert result["type"] == "form"
assert result["errors"] == {}
# assert result["title"] == title_1
# assert result['title'] == title_1

with patch("custom_components.openei.async_setup", return_value=True), patch(
"custom_components.openei.async_setup_entry",
Expand Down Expand Up @@ -324,7 +324,7 @@ async def test_options_flow_no_changes(
{
"api_key": "fakeAPIKey",
"radius": 0,
"location": "''",
"location": '""',
},
"user_2",
{
Expand All @@ -343,7 +343,7 @@ async def test_options_flow_no_changes(
"utility": "Fake Utility Co",
"rate_plan": "randomstring",
"sensor": "(none)",
"location": "''",
"location": "",
"manual_plan": "",
},
),
Expand Down Expand Up @@ -386,7 +386,7 @@ async def test_options_flow_some_changes(

assert result["type"] == "form"
assert result["errors"] == {}
# assert result["title"] == title_1
# assert result['title'] == title_1

with patch("custom_components.openei.async_setup", return_value=True), patch(
"custom_components.openei.async_setup_entry",
Expand Down

0 comments on commit 8efc280

Please sign in to comment.