Skip to content

Commit 3a55630

Browse files
committed
Remaster nearest city detection method
1 parent 113cc30 commit 3a55630

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

custom_components/gismeteo/__init__.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ def __init__(
107107
self._mode = mode
108108
self._cache = Cache(params) if params.get("cache_dir") is not None else None
109109

110-
self._city_id = self._get_city_id(latitude, longitude)
110+
self._latitude = latitude
111+
self._longitude = longitude
112+
self._city_id = None
113+
self._detect_city_id()
111114
_LOGGER.debug("Nearest city ID: %s", self._city_id)
112115

113116
self._current = {}
@@ -155,19 +158,26 @@ def _get(var: dict, ind: str, func: Optional[Callable] = None) -> Any:
155158
return None
156159
return res
157160

158-
def _get_city_id(self, lat, lng):
159-
"""Return the nearest city ID."""
160-
url = (BASE_URL + "/cities/?lat={}&lng={}&count=1&lang=en").format(lat, lng)
161-
cache_fname = f"city_{lat}_{lng}"
161+
def _detect_city_id(self):
162+
"""Detect the nearest city ID."""
163+
url = (BASE_URL + "/cities/?lat={}&lng={}&count=1&lang=en").format(
164+
self._latitude, self._longitude
165+
)
166+
cache_fname = f"city_{self._latitude}_{self._longitude}"
162167

163168
response = self._http_request(url, cache_fname)
164169
if not response:
165-
_LOGGER.error("Can't detect nearest city!")
166-
return None
170+
_LOGGER.error("Can't detect nearest city! Invalid server response.")
171+
return False
167172

168-
xml = etree.fromstring(response)
169-
item = xml.find("item")
170-
return self._get(item, "id", int)
173+
try:
174+
xml = etree.fromstring(response)
175+
item = xml.find("item")
176+
self._city_id = self._get(item, "id", int)
177+
return True
178+
except etree.ParseError:
179+
_LOGGER.warning("Can't detect nearest city! Invalid server response.")
180+
return False
171181

172182
@staticmethod
173183
def _is_day(testing_time, sunrise_time, sunset_time):
@@ -323,7 +333,9 @@ def _get_utime(source, tzone):
323333
def update(self):
324334
"""Get the latest data from Gismeteo."""
325335
if self._city_id is None:
326-
return
336+
if not self._detect_city_id():
337+
_LOGGER.warning("Can't update weather data!")
338+
return
327339

328340
url = (BASE_URL + "/forecast/?city={}&lang=en").format(self._city_id)
329341
cache_fname = f"forecast_{self._city_id}"

custom_components/gismeteo/sensor.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
https://github.com/Limych/ha-gismeteo/
1111
"""
1212
import logging
13+
from random import randint
14+
from time import sleep
1315

1416
import voluptuous as vol
1517
from homeassistant.components.weather import ATTR_FORECAST_CONDITION, PLATFORM_SCHEMA
@@ -70,6 +72,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
7072
forecast = config.get(CONF_FORECAST)
7173
cache_dir = config.get(CONF_CACHE_DIR, hass.config.path(STORAGE_DIR))
7274

75+
sleep(randint(0, 5))
7376
gism = Gismeteo(
7477
latitude,
7578
longitude,

custom_components/gismeteo/weather.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
https://github.com/Limych/ha-gismeteo/
1111
"""
1212
import logging
13+
from random import randint
14+
from time import sleep
1315

1416
import voluptuous as vol
1517
from homeassistant.components.weather import PLATFORM_SCHEMA, WeatherEntity
@@ -58,6 +60,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
5860
cache_dir = config.get(CONF_CACHE_DIR, hass.config.path(STORAGE_DIR))
5961
mode = config.get(CONF_MODE)
6062

63+
sleep(randint(0, 5))
6164
gism = Gismeteo(
6265
latitude,
6366
longitude,

0 commit comments

Comments
 (0)