mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 07:37:34 +00:00
Met.no - only update data if coordinates changed (#48756)
This commit is contained in:
parent
589f2240b1
commit
b558f20ad2
@ -68,7 +68,7 @@ class MetDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
self.weather = MetWeatherData(
|
self.weather = MetWeatherData(
|
||||||
hass, config_entry.data, hass.config.units.is_metric
|
hass, config_entry.data, hass.config.units.is_metric
|
||||||
)
|
)
|
||||||
self.weather.init_data()
|
self.weather.set_coordinates()
|
||||||
|
|
||||||
update_interval = timedelta(minutes=randrange(55, 65))
|
update_interval = timedelta(minutes=randrange(55, 65))
|
||||||
|
|
||||||
@ -88,8 +88,8 @@ class MetDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
|
|
||||||
async def _async_update_weather_data(_event=None):
|
async def _async_update_weather_data(_event=None):
|
||||||
"""Update weather data."""
|
"""Update weather data."""
|
||||||
self.weather.init_data()
|
if self.weather.set_coordinates():
|
||||||
await self.async_refresh()
|
await self.async_refresh()
|
||||||
|
|
||||||
self._unsub_track_home = self.hass.bus.async_listen(
|
self._unsub_track_home = self.hass.bus.async_listen(
|
||||||
EVENT_CORE_CONFIG_UPDATE, _async_update_weather_data
|
EVENT_CORE_CONFIG_UPDATE, _async_update_weather_data
|
||||||
@ -114,9 +114,10 @@ class MetWeatherData:
|
|||||||
self.current_weather_data = {}
|
self.current_weather_data = {}
|
||||||
self.daily_forecast = None
|
self.daily_forecast = None
|
||||||
self.hourly_forecast = None
|
self.hourly_forecast = None
|
||||||
|
self._coordinates = None
|
||||||
|
|
||||||
def init_data(self):
|
def set_coordinates(self):
|
||||||
"""Weather data inialization - get the coordinates."""
|
"""Weather data inialization - set the coordinates."""
|
||||||
if self._config.get(CONF_TRACK_HOME, False):
|
if self._config.get(CONF_TRACK_HOME, False):
|
||||||
latitude = self.hass.config.latitude
|
latitude = self.hass.config.latitude
|
||||||
longitude = self.hass.config.longitude
|
longitude = self.hass.config.longitude
|
||||||
@ -136,10 +137,14 @@ class MetWeatherData:
|
|||||||
"lon": str(longitude),
|
"lon": str(longitude),
|
||||||
"msl": str(elevation),
|
"msl": str(elevation),
|
||||||
}
|
}
|
||||||
|
if coordinates == self._coordinates:
|
||||||
|
return False
|
||||||
|
self._coordinates = coordinates
|
||||||
|
|
||||||
self._weather_data = metno.MetWeatherData(
|
self._weather_data = metno.MetWeatherData(
|
||||||
coordinates, async_get_clientsession(self.hass), api_url=URL
|
coordinates, async_get_clientsession(self.hass), api_url=URL
|
||||||
)
|
)
|
||||||
|
return True
|
||||||
|
|
||||||
async def fetch_data(self):
|
async def fetch_data(self):
|
||||||
"""Fetch data from API - (current weather and forecast)."""
|
"""Fetch data from API - (current weather and forecast)."""
|
||||||
|
@ -29,6 +29,11 @@ async def test_tracking_home(hass, mock_weather):
|
|||||||
|
|
||||||
assert len(mock_weather.mock_calls) == 8
|
assert len(mock_weather.mock_calls) == 8
|
||||||
|
|
||||||
|
# Same coordinates again should not trigger any new requests to met.no
|
||||||
|
await hass.config.async_update(latitude=10, longitude=20)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(mock_weather.mock_calls) == 8
|
||||||
|
|
||||||
entry = hass.config_entries.async_entries()[0]
|
entry = hass.config_entries.async_entries()[0]
|
||||||
await hass.config_entries.async_remove(entry.entry_id)
|
await hass.config_entries.async_remove(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user