Skip to content

Commit 0bcee4f

Browse files
authored
Merge pull request #7 from andrejs2/config_flow
fix: Resolve configuration and unload errors
2 parents ece35e7 + d22fa05 commit 0bcee4f

File tree

5 files changed

+24
-25
lines changed

5 files changed

+24
-25
lines changed

custom_components/arso_potresi/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55

66
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
77
"""Nastavi integracijo preko config entryja."""
8+
config_entry.async_on_unload(config_entry.add_update_listener(async_reload_entry))
89
await hass.config_entries.async_forward_entry_setups(config_entry, ["sensor"])
910
return True
1011

1112
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
1213
"""Odstrani integracijo."""
13-
return await hass.config_entries.async_forward_entry_unloads(config_entry, ["sensor"])
14+
return await hass.config_entries.async_forward_entry_unload(config_entry, "sensor")
15+
16+
async def async_reload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
17+
"""Ponovno naloži integracijo, ko se spremenijo nastavitve."""
18+
await hass.config_entries.async_reload(config_entry.entry_id)

custom_components/arso_potresi/config_flow.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import voluptuous as vol
22
from homeassistant import config_entries
33
from homeassistant.core import callback
4+
from homeassistant.config_entries import OptionsFlowWithConfigEntry
45

5-
from .const import DOMAIN, DEFAULT_SCAN_INTERVAL
6+
from .const import DOMAIN, DEFAULT_SCAN_INTERVAL, DEFAULT_HISTORY_DAYS
67

78
class ArsoPotresiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
89
"""Config flow za ARSO Potresi integracijo."""
@@ -22,32 +23,30 @@ async def async_step_user(self, user_input=None):
2223

2324
data_schema = vol.Schema({
2425
vol.Optional("scan_interval", default=DEFAULT_SCAN_INTERVAL): int,
25-
vol.Optional("history_days", default=7): int,
26+
vol.Optional("history_days", default=DEFAULT_HISTORY_DAYS): int,
2627
})
2728
return self.async_show_form(
2829
step_id="user", data_schema=data_schema, errors=errors
2930
)
3031

31-
class OptionsFlowHandler(config_entries.OptionsFlow):
32+
class OptionsFlowHandler(OptionsFlowWithConfigEntry):
3233
"""Tok možnosti za ARSO Potresi integracijo."""
33-
def __init__(self, config_entry):
34-
self.config_entry = config_entry
3534

3635
async def async_step_init(self, user_input=None):
3736
"""Upravljanje možnosti."""
3837
if user_input is not None:
39-
return self.async_create_entry(title="", data=user_input)
38+
return self.async_create_entry(data=user_input)
39+
40+
options = self.config_entry.options or self.config_entry.data
4041

41-
42-
options = self.config_entry.data
4342
options_schema = vol.Schema({
4443
vol.Optional(
4544
"scan_interval",
4645
default=options.get("scan_interval", DEFAULT_SCAN_INTERVAL)
4746
): int,
4847
vol.Optional(
4948
"history_days",
50-
default=options.get("history_days", 7)
49+
default=options.get("history_days", DEFAULT_HISTORY_DAYS)
5150
): int,
5251
})
5352

@@ -56,6 +55,7 @@ async def async_step_init(self, user_input=None):
5655
data_schema=options_schema,
5756
description_placeholders={
5857
"scan_interval": options.get("scan_interval", DEFAULT_SCAN_INTERVAL),
59-
"history_days": options.get("history_days", 7),
58+
"history_days": options.get("history_days", DEFAULT_HISTORY_DAYS),
6059
},
6160
)
61+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
DOMAIN = "arso_potresi"
22
DEFAULT_API_URL = "https://potresi.arso.gov.si/sc/potresi/public"
33
DEFAULT_SCAN_INTERVAL = 5 # privzeto osveževanje vsakih 5 minut
4+
DEFAULT_HISTORY_DAYS = 7

custom_components/arso_potresi/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"iot_class": "cloud_polling",
99
"issue_tracker": "https://github.com/andrejs2/arso_potresi/issues",
1010
"requirements": [],
11-
"version": "1.2.0"
11+
"version": "1.2.1"
1212
}

custom_components/arso_potresi/sensor.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from homeassistant.util.dt import parse_datetime
1212
from homeassistant.helpers.entity_platform import AddEntitiesCallback
1313

14-
from .const import DOMAIN, DEFAULT_API_URL
14+
from .const import DOMAIN, DEFAULT_API_URL, DEFAULT_SCAN_INTERVAL, DEFAULT_HISTORY_DAYS
1515

1616
_LOGGER = logging.getLogger(__name__)
1717

@@ -37,8 +37,9 @@ async def async_setup_entry(
3737
async_add_entities: AddEntitiesCallback
3838
) -> None:
3939
"""Nastavi ARSO Potresi senzor iz config entryja."""
40-
scan_interval = config_entry.data.get("scan_interval", 5)
41-
history_days = config_entry.data.get("history_days", 7) # <--- PREBEREMO NOVO POLJE
40+
options = config_entry.options or config_entry.data
41+
scan_interval = options.get("scan_interval", DEFAULT_SCAN_INTERVAL)
42+
history_days = options.get("history_days", DEFAULT_HISTORY_DAYS)
4243
async_add_entities([ArsoPotresiSensor(scan_interval, history_days)], True)
4344

4445
class ArsoPotresiSensor(Entity):
@@ -47,7 +48,7 @@ class ArsoPotresiSensor(Entity):
4748
def __init__(self, scan_interval, history_days):
4849
self._api_url = DEFAULT_API_URL
4950
self._scan_interval = timedelta(minutes=scan_interval)
50-
self._history_days = history_days
51+
self._history_days = history_days
5152
self._state = None
5253
self._attributes = {}
5354
self._name = "ARSO Potresi"
@@ -98,22 +99,19 @@ async def async_update(self):
9899
_LOGGER.warning("Prejeto ni bilo podatkov")
99100
return
100101

101-
102102
now = datetime.now(pytz.utc)
103103
time_limit = now - timedelta(days=self._history_days)
104104

105-
106105
filtered_earthquakes = [e for e in data if parse_datetime(e.get("TIME")).astimezone(pytz.utc) >= time_limit]
107106

108107
if not filtered_earthquakes:
109108
_LOGGER.warning("Ni potresov v izbranem časovnem obdobju.")
110109
self._state = "Ni potresov"
111110
self._attributes = {}
112111
return
113-
112+
114113
latest = filtered_earthquakes[0]
115114

116-
# Parse lokalnega časa iz TIME
117115
dt_local = parse_datetime(latest.get("TIME"))
118116
try:
119117
dt_utc = pytz.UTC.localize(datetime.strptime(latest.get("TIME_ORIG"), "%Y-%m-%d %H:%M:%S"))
@@ -129,7 +127,6 @@ async def async_update(self):
129127
intensity = latest.get("INTENZITETA") if latest.get("INTENZITETA") is not None else "-"
130128
verified = "DA" if latest.get("REVISION") == 1 else "NE"
131129

132-
133130
self._state = latest.get("GEOLOC", "Neznano")
134131
self._attributes = {
135132
"Lokalni čas potresa": format_datetime(dt_local),
@@ -143,11 +140,8 @@ async def async_update(self):
143140
ATTR_ATTRIBUTION: ATTRIBUTION,
144141
}
145142

146-
147143
history = []
148-
149144
for earthquake in filtered_earthquakes:
150-
# Parse lokalnega časa iz TIME
151145
dt_local_hist = parse_datetime(earthquake.get("TIME"))
152146
try:
153147
dt_utc_hist = pytz.UTC.localize(datetime.strptime(earthquake.get("TIME_ORIG"), "%Y-%m-%d %H:%M:%S"))
@@ -176,7 +170,6 @@ async def async_update(self):
176170
history.append(earthquake_data)
177171

178172
self._attributes["Zgodovina potresov"] = history
179-
180173

181174
except Exception as e:
182175
_LOGGER.error("Prišlo je do izjeme pri async_update: %s", e)

0 commit comments

Comments
 (0)