1111from homeassistant .util .dt import parse_datetime
1212from 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
4445class 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