Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions homeassistant/components/met/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(self, hass, config_entry):
self.weather = MetWeatherData(
hass, config_entry.data, hass.config.units.is_metric
)
self.weather.init_data()
self.weather.set_coordinates()

update_interval = timedelta(minutes=randrange(55, 65))

Expand All @@ -88,8 +88,8 @@ def track_home(self):

async def _async_update_weather_data(_event=None):
"""Update weather data."""
self.weather.init_data()
await self.async_refresh()
if self.weather.set_coordinates():
await self.async_refresh()

self._unsub_track_home = self.hass.bus.async_listen(
EVENT_CORE_CONFIG_UPDATE, _async_update_weather_data
Expand All @@ -114,9 +114,10 @@ def __init__(self, hass, config, is_metric):
self.current_weather_data = {}
self.daily_forecast = None
self.hourly_forecast = None
self._coordinates = None

def init_data(self):
"""Weather data inialization - get the coordinates."""
def set_coordinates(self):
"""Weather data inialization - set the coordinates."""
if self._config.get(CONF_TRACK_HOME, False):
latitude = self.hass.config.latitude
longitude = self.hass.config.longitude
Expand All @@ -136,10 +137,14 @@ def init_data(self):
"lon": str(longitude),
"msl": str(elevation),
}
if coordinates == self._coordinates:
return False
self._coordinates = coordinates

self._weather_data = metno.MetWeatherData(
coordinates, async_get_clientsession(self.hass), api_url=URL
)
return True

async def fetch_data(self):
"""Fetch data from API - (current weather and forecast)."""
Expand Down
5 changes: 5 additions & 0 deletions tests/components/met/test_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ async def test_tracking_home(hass, mock_weather):

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]
await hass.config_entries.async_remove(entry.entry_id)
await hass.async_block_till_done()
Expand Down