Skip to content

Commit 677017f

Browse files
committed
Provide list of available time zones in config flow
When config changes, only update entities that are enabled.
1 parent 3eafd70 commit 677017f

File tree

7 files changed

+41
-25
lines changed

7 files changed

+41
-25
lines changed

custom_components/sun2/config_flow.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from collections.abc import Mapping
66
from contextlib import suppress
77
from typing import Any, cast
8+
import zoneinfo
89

910
import voluptuous as vol
1011

@@ -34,7 +35,6 @@
3435
)
3536
from homeassistant.core import HomeAssistant, callback
3637
from homeassistant.helpers import entity_registry as er
37-
import homeassistant.helpers.config_validation as cv
3838
from homeassistant.helpers.selector import (
3939
BooleanSelector,
4040
EntitySelector,
@@ -56,6 +56,7 @@
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(

custom_components/sun2/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
CONF_DIRECTION = "direction"
1818
CONF_DISTANCE = "distance"
1919
CONF_ELEVATION_AT_TIME = "elevation_at_time"
20+
CONF_LOCATION_TEXT = "location_text"
2021
CONF_OBS_ELV = "observer_elevation"
2122
CONF_RELATIVE_HEIGHT = "relative_height"
2223
CONF_SUNRISE_OBSTRUCTION = "sunrise_obstruction"

custom_components/sun2/helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,8 @@ def _update_entities(
817817
self._obs_elvs = obs_elvs
818818
astral_data = AstralData(loc_data, obs_elvs)
819819
for entity in self._entities:
820-
self._update_entity(entity, astral_data)
820+
if entity.enabled:
821+
self._update_entity(entity, astral_data)
821822

822823
def _update_entity(self, entity: Sun2Entity, astral_data: AstralData) -> None:
823824
"""Update entity with new astral data."""

custom_components/sun2/translations/en.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@
5757
"title": "Location Options",
5858
"data": {
5959
"location": "Location",
60+
"location_text": "Location",
6061
"time_zone": "Time zone"
6162
},
6263
"data_description": {
63-
"time_zone": "See the \"TZ identifier\" column at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List."
64+
"location_text": "latitude, longitude"
6465
}
6566
},
6667
"location_menu": {
@@ -177,10 +178,11 @@
177178
"title": "Location Options",
178179
"data": {
179180
"location": "Location",
181+
"location_text": "Location",
180182
"time_zone": "Time zone"
181183
},
182184
"data_description": {
183-
"time_zone": "See the \"TZ identifier\" column at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List."
185+
"location_text": "latitude, longitude"
184186
}
185187
},
186188
"location_menu": {

custom_components/sun2/translations/nl.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@
5656
"location": {
5757
"title": "Opties voor locatie",
5858
"data": {
59-
"elevation": "Verhoging boven het maaiveld",
6059
"location": "Locatie",
60+
"location_text": "Locatie",
6161
"time_zone": "Tijdzone"
6262
},
6363
"data_description": {
64-
"time_zone": "Zie de kolom \"TZ identifier\" bij https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List."
64+
"location_text": "breedtegraad, lengtegraad"
6565
}
6666
},
6767
"location_name": {
@@ -146,12 +146,12 @@
146146
"location": {
147147
"title": "Opties voor locatie",
148148
"data": {
149-
"elevation": "Verhoging boven het maaiveld",
150149
"location": "Locatie",
150+
"location_text": "Locatie",
151151
"time_zone": "Tijdzone"
152152
},
153153
"data_description": {
154-
"time_zone": "Zie de kolom \"TZ identifier\" bij https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List."
154+
"location_text": "breedtegraad, lengtegraad"
155155
}
156156
},
157157
"remove_entities": {

custom_components/sun2/translations/ru.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@
5656
"location": {
5757
"title": "Настройки Местоположения",
5858
"data": {
59-
"elevation": "Высота над уровнем земли",
6059
"location": "Местоположение",
60+
"location_text": "Местоположение",
6161
"time_zone": "Часовой пояс"
6262
},
6363
"data_description": {
64-
"time_zone": "См. столбец \"Идентификатор TZ\" на https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List."
64+
"location_text": "широта, долгота"
6565
}
6666
},
6767
"location_name": {
@@ -146,12 +146,12 @@
146146
"location": {
147147
"title": "Настройки Местоположения",
148148
"data": {
149-
"elevation": "Высота над уровнем земли",
150149
"location": "Местоположение",
150+
"location_text": "Местоположение",
151151
"time_zone": "Часовой пояс"
152152
},
153153
"data_description": {
154-
"time_zone": "См. столбец \"Идентификатор TZ\" на https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List."
154+
"location_text": "широта, долгота"
155155
}
156156
},
157157
"remove_entities": {

custom_components/sun2/translations/sv.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@
5757
"title": "Platsalternativ",
5858
"data": {
5959
"location": "Plats",
60+
"location_text": "Plats",
6061
"time_zone": "Tidszon"
6162
},
6263
"data_description": {
63-
"time_zone": "Kolla i \"TZ identifier\"-kolumnen på https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List."
64+
"location_text": "latitud, longitud"
6465
}
6566
},
6667
"location_menu": {
@@ -177,10 +178,11 @@
177178
"title": "Platsalternativ",
178179
"data": {
179180
"location": "Plats",
181+
"location_text": "Plats",
180182
"time_zone": "Tidszon"
181183
},
182184
"data_description": {
183-
"time_zone": "Kolla i \"TZ identifier\"-kolumnen på https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List."
185+
"location_text": "latitud, longitud"
184186
}
185187
},
186188
"location_menu": {

0 commit comments

Comments
 (0)