Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix reconfigure flow #104

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions custom_components/ims/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
59 changes: 53 additions & 6 deletions custom_components/ims/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions custom_components/ims/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Loading