From 9307ecb78af8ff4daae6d5fe561057fcbbece91d Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 17 Sep 2021 08:21:48 -0700 Subject: [PATCH] fix: allow omitted entry for location on setup (#44) Updated config flow instructions --- custom_components/openei/config_flow.py | 21 +++++++++++-------- custom_components/openei/translations/en.json | 8 +++---- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/custom_components/openei/config_flow.py b/custom_components/openei/config_flow.py index eae85af..fef6884 100644 --- a/custom_components/openei/config_flow.py +++ b/custom_components/openei/config_flow.py @@ -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() @@ -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) ), @@ -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)")), @@ -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 @@ -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 diff --git a/custom_components/openei/translations/en.json b/custom_components/openei/translations/en.json index ce1ed58..6082b81 100644 --- a/custom_components/openei/translations/en.json +++ b/custom_components/openei/translations/en.json @@ -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)", @@ -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)", @@ -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)", @@ -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)",