From 8efad3fe43e26b8fcf9462cc9bd630a058df477a Mon Sep 17 00:00:00 2001 From: Guy Khmelnitsky Date: Wed, 11 Sep 2024 08:20:16 +0300 Subject: [PATCH] fix: Support Legacy Configuration --- custom_components/ims/__init__.py | 3 +- custom_components/ims/config_flow.py | 59 +++++++++++++++++++++++++--- custom_components/ims/weather.py | 5 +++ 3 files changed, 59 insertions(+), 8 deletions(-) diff --git a/custom_components/ims/__init__.py b/custom_components/ims/__init__.py index 3944f1c..4b743a5 100644 --- a/custom_components/ims/__init__.py +++ b/custom_components/ims/__init__.py @@ -53,7 +53,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: # Extract list of int from forecast days/ hours string if present # _LOGGER.warning('forecast_days_type: ' + str(type(forecast_days))) - is_legacy_city = False if isinstance(city, int | str): is_legacy_city = True @@ -136,7 +135,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: entry, [PLATFORMS[1]] ) - _LOGGER.info("Unloading IMS Weather") + _LOGGER.info(f"Unloading IMS Weather. Successful: {unload_ok}") if unload_ok: update_listener = hass.data[DOMAIN][entry.entry_id][UPDATE_LISTENER] diff --git a/custom_components/ims/config_flow.py b/custom_components/ims/config_flow.py index 1c740aa..a3bca93 100644 --- a/custom_components/ims/config_flow.py +++ b/custom_components/ims/config_flow.py @@ -251,13 +251,60 @@ async def async_step_init(self, user_input=None): """Manage the options.""" global cities_data if not cities_data: - cities_data = _get_localized_cities(self.hass) + cities_data = await _get_localized_cities(self.hass) + + city_options = {city_id: city["name"] for city_id, city in cities_data.items()} + + current_city = self.config_entry.data.get(CONF_CITY) + conf_city = self.config_entry.options.get(CONF_CITY) + + _LOGGER.info(f"current city: {current_city}") + _LOGGER.info(f"conf city: {conf_city}") + is_legacy_city = False + if isinstance(current_city, int | str): + is_legacy_city = True + + city_id = current_city if is_legacy_city else current_city["lid"] + _LOGGER.info(f"city_id: {city_id}") if user_input is not None: - # entry = self.config_entry + errors = [] + city_id = user_input[CONF_CITY] + city = cities_data[str(city_id)] + user_input[CONF_CITY] = city + language = user_input[CONF_LANGUAGE] + forecast_mode = user_input[CONF_MODE] + entity_name = user_input[CONF_NAME] + # image_path = user_input[CONF_IMAGES_PATH] + forecast_platform = user_input[IMS_PLATFORM] + + # Convert scan interval to timedelta + if isinstance(user_input[CONF_UPDATE_INTERVAL], str): + user_input[CONF_UPDATE_INTERVAL] = cv.time_period_str( + user_input[CONF_UPDATE_INTERVAL] + ) + + # Convert scan interval to number of minutes + if isinstance(user_input[CONF_UPDATE_INTERVAL], timedelta): + user_input[CONF_UPDATE_INTERVAL] = user_input[ + CONF_UPDATE_INTERVAL + ].total_minutes() + + api_status = "No API Call made" + try: + api_status = await _is_ims_api_online( + self.hass, user_input[CONF_LANGUAGE], user_input[CONF_CITY] + ) - # _LOGGER.warning('async_step_init_Options') - return self.async_create_entry(title=user_input[CONF_NAME], data=user_input) + except Exception: + _LOGGER.warning("IMS Weather Setup Error: HTTP Error: %s", api_status) + errors["base"] = "API Error: " + api_status + + if not errors: + return self.async_create_entry( + title=user_input[CONF_NAME], data=user_input + ) + _LOGGER.warning(errors) return self.async_show_form( step_id="init", @@ -274,9 +321,9 @@ async def async_step_init(self, user_input=None): CONF_CITY, default=self.config_entry.options.get( CONF_CITY, - self.config_entry.data.get(CONF_CITY, 1), + str(city_id), ), - ): int, + ): vol.In(city_options), vol.Optional( CONF_LANGUAGE, default=self.config_entry.options.get( diff --git a/custom_components/ims/weather.py b/custom_components/ims/weather.py index 18a8ef9..e00c06a 100644 --- a/custom_components/ims/weather.py +++ b/custom_components/ims/weather.py @@ -87,6 +87,11 @@ async def async_setup_entry( unique_id = f"{config_entry.unique_id}" + is_legacy_city = False + if isinstance(city, int | str): + is_legacy_city = True + + # Round Output output_round = "No"