Skip to content

Commit

Permalink
Cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
bieniu committed Oct 1, 2024
1 parent a708879 commit c729ab3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 52 deletions.
38 changes: 25 additions & 13 deletions custom_components/blitzortung/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,10 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: BlitzortungConfig

latitude = config_entry.data[CONF_LATITUDE]
longitude = config_entry.data[CONF_LONGITUDE]
radius = config_entry.options.get(CONF_RADIUS, DEFAULT_RADIUS)
max_tracked_lightnings = config_entry.options.get(
CONF_MAX_TRACKED_LIGHTNINGS, DEFAULT_MAX_TRACKED_LIGHTNINGS
)
time_window_seconds = (
config_entry.options.get(CONF_TIME_WINDOW, DEFAULT_TIME_WINDOW) * 60
)
radius = config_entry.options[CONF_RADIUS]
max_tracked_lightnings = config_entry.options[CONF_MAX_TRACKED_LIGHTNINGS]
time_window_seconds = config_entry.options[CONF_TIME_WINDOW] * 60

if max_tracked_lightnings >= 500:
_LOGGER.warning(
"Large number of tracked lightnings: %s, it may lead to"
Expand Down Expand Up @@ -142,12 +139,27 @@ async def async_migrate_entry(hass, entry: BlitzortungConfigEntry):
)
entry.version = 4
if entry.version == 4:
entry_data = entry.data.copy()
latitude = entry.options.get(CONF_LATITUDE) or hass.config.latitude
longitude = entry.options.get(CONF_LONGITUDE) or hass.config.longitude
entry_data[CONF_LATITUDE] = latitude
entry_data[CONF_LONGITUDE] = longitude
hass.config_entries.async_update_entry(entry, data=entry_data, version=5)
new_data = entry.data.copy()

latitude = entry.options.get(CONF_LATITUDE, hass.config.latitude)
longitude = entry.options.get(CONF_LONGITUDE, hass.config.longitude)
radius = entry.options.get(CONF_RADIUS, DEFAULT_RADIUS)
time_window = entry.options.get(CONF_TIME_WINDOW, DEFAULT_TIME_WINDOW)
max_tracked_lightnings = entry.options.get(
CONF_MAX_TRACKED_LIGHTNINGS, DEFAULT_MAX_TRACKED_LIGHTNINGS
)

new_data[CONF_LATITUDE] = latitude
new_data[CONF_LONGITUDE] = longitude
new_options = {
CONF_RADIUS: radius,
CONF_TIME_WINDOW: time_window,
CONF_MAX_TRACKED_LIGHTNINGS: max_tracked_lightnings,
}

hass.config_entries.async_update_entry(
entry, data=new_data, options=new_options, version=5
)

return True

Expand Down
93 changes: 54 additions & 39 deletions custom_components/blitzortung/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
"""Config flow for blitzortung integration."""

import voluptuous as vol

from typing import Any
import homeassistant.helpers.config_validation as cv
from homeassistant import config_entries
from homeassistant.config_entries import (
ConfigEntry,
ConfigFlow,
ConfigFlowResult,
OptionsFlowWithConfigEntry,
)
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME

from .const import (
Expand All @@ -16,23 +21,30 @@
DOMAIN,
)

DEFAULT_CONF_NAME = "Blitzortung"


class DomainConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
class BlitortungConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for blitzortung."""

VERSION = 5
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_PUSH

async def async_step_user(self, user_input=None):
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle the initial step."""
if user_input is not None:
await self.async_set_unique_id(
f"{user_input[CONF_LATITUDE]}-{user_input[CONF_LONGITUDE]}"
)
self._abort_if_unique_id_configured()
return self.async_create_entry(title=user_input[CONF_NAME], data=user_input)
return self.async_create_entry(
title=user_input[CONF_NAME],
data=user_input,
options={
CONF_RADIUS: DEFAULT_RADIUS,
CONF_MAX_TRACKED_LIGHTNINGS: DEFAULT_MAX_TRACKED_LIGHTNINGS,
CONF_TIME_WINDOW: DEFAULT_TIME_WINDOW,
},
)

return self.async_show_form(
step_id="user",
Expand All @@ -54,43 +66,46 @@ async def async_step_user(self, user_input=None):
)

@staticmethod
def async_get_options_flow(config_entry):
return OptionsFlowHandler(config_entry)
def async_get_options_flow(config_entry: ConfigEntry):
return BlitzortungOptionsFlowHandler(config_entry)


class OptionsFlowHandler(config_entries.OptionsFlow):
def __init__(self, config_entry: config_entries.ConfigEntry):
"""Initialize options flow."""
self.config_entry = config_entry
class BlitzortungOptionsFlowHandler(OptionsFlowWithConfigEntry):
"""Handle an options flow for Blitzortung."""

async def async_step_init(self, user_input=None):
async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Manage the options."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)
return self.async_create_entry(data=user_input)

return self.async_show_form(
step_id="init",
data_schema=vol.Schema(
{
vol.Required(
CONF_RADIUS,
default=self.config_entry.options.get(
CONF_RADIUS, DEFAULT_RADIUS
),
): int,
vol.Optional(
options_schema = vol.Schema(
{
vol.Required(
CONF_RADIUS,
default=self.config_entry.options.get(CONF_RADIUS, DEFAULT_RADIUS),
): int,
vol.Optional(
CONF_TIME_WINDOW,
default=self.config_entry.options.get(
CONF_TIME_WINDOW,
default=self.config_entry.options.get(
CONF_TIME_WINDOW,
DEFAULT_TIME_WINDOW,
),
): int,
vol.Optional(
DEFAULT_TIME_WINDOW,
),
): int,
vol.Optional(
CONF_MAX_TRACKED_LIGHTNINGS,
default=self.config_entry.options.get(
CONF_MAX_TRACKED_LIGHTNINGS,
default=self.config_entry.options.get(
CONF_MAX_TRACKED_LIGHTNINGS,
DEFAULT_MAX_TRACKED_LIGHTNINGS,
),
): int,
}
DEFAULT_MAX_TRACKED_LIGHTNINGS,
),
): int,
}
)

return self.async_show_form(
step_id="init",
data_schema=self.add_suggested_values_to_schema(
options_schema, self.config_entry.options
),
)

0 comments on commit c729ab3

Please sign in to comment.