55from collections .abc import Mapping
66from contextlib import suppress
77from typing import Any , cast
8+ import zoneinfo
89
910import voluptuous as vol
1011
3435)
3536from homeassistant .core import HomeAssistant , callback
3637from homeassistant .helpers import entity_registry as er
37- import homeassistant .helpers .config_validation as cv
3838from homeassistant .helpers .selector import (
3939 BooleanSelector ,
4040 EntitySelector ,
5656 CONF_ABOVE_GROUND ,
5757 CONF_DIRECTION ,
5858 CONF_ELEVATION_AT_TIME ,
59+ CONF_LOCATION_TEXT ,
5960 CONF_OBS_ELV ,
6061 CONF_SUNRISE_OBSTRUCTION ,
6162 CONF_SUNSET_OBSTRUCTION ,
@@ -191,20 +192,20 @@ async def async_step_location(
191192 errors : dict [str , str ] = {}
192193
193194 if user_input is not None :
194- user_input [CONF_TIME_ZONE ] = cv .time_zone (user_input [CONF_TIME_ZONE ])
195- location : dict [str , Any ] | str = user_input .pop (CONF_LOCATION )
196- if isinstance (location , dict ):
195+ if self ._use_map :
196+ location : dict [str , Any ] = user_input .pop (CONF_LOCATION )
197197 user_input [CONF_LATITUDE ] = location [CONF_LATITUDE ]
198198 user_input [CONF_LONGITUDE ] = location [CONF_LONGITUDE ]
199199 else :
200+ location_text : str = user_input .pop (CONF_LOCATION_TEXT )
200201 try :
201202 lat = lon = ""
202203 with suppress (ValueError ):
203- lat , lon = location .split ("," )
204+ lat , lon = location_text .split ("," )
204205 lat = lat .strip ()
205206 lon = lon .strip ()
206207 if not lat or not lon :
207- lat , lon = location .split ()
208+ lat , lon = location_text .split ()
208209 lat = lat .strip ()
209210 lon = lon .strip ()
210211 user_input [CONF_LATITUDE ] = float (lat )
@@ -215,23 +216,32 @@ async def async_step_location(
215216 self .options .update (user_input )
216217 return await self .async_step_observer_elevation ()
217218
218- location_selector = LocationSelector if self ._use_map else TextSelector
219- data_schema = vol .Schema (
219+ if self ._use_map :
220+ data_schema = vol .Schema ({vol .Required (CONF_LOCATION ): LocationSelector ()})
221+ else :
222+ data_schema = vol .Schema ({vol .Required (CONF_LOCATION_TEXT ): TextSelector ()})
223+ time_zones = list (
224+ await self .hass .async_add_executor_job (zoneinfo .available_timezones )
225+ )
226+ data_schema = data_schema .extend (
220227 {
221- vol .Required (CONF_LOCATION ): location_selector (),
222- vol .Required (CONF_TIME_ZONE ): TextSelector (),
228+ vol .Required (CONF_TIME_ZONE ): SelectSelector (
229+ SelectSelectorConfig (options = time_zones , sort = True )
230+ ),
223231 }
224232 )
225233
226234 latitude , longitude , time_zone = loc_from_options (self .hass , self .options )
227- suggested_values : dict [str , Any ] = {CONF_TIME_ZONE : time_zone }
235+ suggested_values : dict [str , Any ] = {}
228236 if self ._use_map :
229237 suggested_values [CONF_LOCATION ] = {
230238 CONF_LATITUDE : latitude ,
231239 CONF_LONGITUDE : longitude ,
232240 }
233241 else :
234- suggested_values [CONF_LOCATION ] = f"{ latitude } , { longitude } "
242+ suggested_values [CONF_LOCATION_TEXT ] = f"{ latitude } , { longitude } "
243+ if time_zone in time_zones :
244+ suggested_values [CONF_TIME_ZONE ] = time_zone
235245 data_schema = self .add_suggested_values_to_schema (data_schema , suggested_values )
236246
237247 return self .async_show_form (
0 commit comments