Skip to content

Commit

Permalink
fix: allow omitted entry for location on setup (#44)
Browse files Browse the repository at this point in the history
Updated config flow instructions
  • Loading branch information
firstof9 authored Sep 17, 2021
1 parent 9414d40 commit 9307ecb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
21 changes: 12 additions & 9 deletions custom_components/openei/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ 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 @@ -188,16 +189,20 @@ def _get_schema_step_1(
if user_input is None:
user_input = {}

if CONF_LOCATION in user_input.keys() and user_input[CONF_LOCATION] == '""':
user_input[CONF_LOCATION] = ""

if CONF_LOCATION in default_dict.keys() and default_dict[CONF_LOCATION] == '""':
default_dict[CONF_LOCATION] = ""

def _get_default(key: str, fallback_default: Any = None) -> None:
"""Gets default value for key."""
return user_input.get(key, default_dict.get(key, fallback_default))

return vol.Schema(
{
vol.Required(CONF_API_KEY, default=_get_default(CONF_API_KEY)): cv.string,
vol.Optional(
CONF_LOCATION, default=_get_default(CONF_LOCATION, "")
): cv.string,
vol.Required(CONF_API_KEY, default=_get_default(CONF_API_KEY)): str,
vol.Optional(CONF_LOCATION, default=_get_default(CONF_LOCATION, "")): str,
vol.Required(CONF_RADIUS, default=_get_default(CONF_RADIUS, 0)): vol.All(
vol.Coerce(int), vol.Range(min=0, max=200)
),
Expand Down Expand Up @@ -252,9 +257,7 @@ def _get_default(key: str, fallback_default: Any = None) -> Any | None:
vol.Required(CONF_PLAN, default=_get_default(CONF_PLAN, "")): vol.In(
plan_list
),
vol.Optional(
CONF_MANUAL_PLAN, default=_get_default(CONF_PLAN, "")
): cv.string,
vol.Optional(CONF_MANUAL_PLAN, default=_get_default(CONF_PLAN, "")): str,
vol.Required(
CONF_SENSOR, default=_get_default(CONF_SENSOR, "(none)")
): vol.In(_get_entities(hass, SENSORS_DOMAIN, "energy", "(none)")),
Expand All @@ -270,7 +273,7 @@ async def _get_utility_list(hass, user_input) -> list | None:
address = user_input[CONF_LOCATION]
radius = user_input[CONF_RADIUS]

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

if user_input[CONF_LOCATION] in [None, '""', "''"]:
if not bool(user_input[CONF_LOCATION]):
lat = hass.config.latitude
lon = hass.config.longitude
address = None
Expand Down
8 changes: 4 additions & 4 deletions custom_components/openei/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"step": {
"user": {
"title": "OpenEI (Step 1)",
"description": "If you do not have an API Key yet you can get one here: https://openei.org/services/api/signup/\n\nIf location information is omitted your latitude and longitude will be used.\n\nEnter \"\" in address to use latitude and longitude from Home Assistant.",
"description": "If you do not have an API Key yet you can get one here: https://openei.org/services/api/signup/\n\nOmit address to use latitude and longitude from Home Assistant.",
"data": {
"api_key": "API Key",
"radius": "Radius in miles (optional)",
Expand All @@ -19,7 +19,7 @@
},
"user_3": {
"title": "OpenEI (Step 3)",
"description": "Select your plan from the list. If you are unsure, check your utility bill.",
"description": "Select your plan from the list. If you are unsure, check your utility bill.\n\nManual plan will override the selected rate plan.",
"data": {
"rate_plan": "Rate Plan",
"sensor": "Energy sensor for Tier plans (optional)",
Expand All @@ -38,7 +38,7 @@
"step": {
"user": {
"title": "OpenEI (Step 1)",
"description": "If you do not have an API Key yet you can get one here: https://openei.org/services/api/signup/\n\nIf location information is omitted your latitude and longitude will be used.\n\nEnter \"\" in address to use latitude and longitude from Home Assistant.",
"description": "If you do not have an API Key yet you can get one here: https://openei.org/services/api/signup/ \n\nEnter \"\" in address to clear it and use latitude and longitude from Home Assistant.",
"data": {
"api_key": "API Key",
"radius": "Radius in miles (optional)",
Expand All @@ -54,7 +54,7 @@
},
"user_3": {
"title": "OpenEI (Step 3)",
"description": "Select your plan from the list. If you are unsure, check your utility bill.",
"description": "Select your plan from the list. If you are unsure, check your utility bill.\n\nManual plan will override the selected rate plan.",
"data": {
"rate_plan": "Rate Plan",
"sensor": "Energy sensor for Tier plans (optional)",
Expand Down

0 comments on commit 9307ecb

Please sign in to comment.