core/homeassistant/components/met/__init__.py

156 lines
4.9 KiB
Python
Raw Normal View History

"""The met component."""
Add DataUpdateCoordinator to met integration (#38405) * Add DataUpdateCoordinator to met integration * isort * redundant fetch_data * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * fix black, isort, flake8, hassfest, mypy * remove unused async_setup method * replace fetch_data by coordinator request_refresh * remove redundant async_update * track_home * Apply suggestions from code review * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update test_config_flow.py * Apply suggestions from code review * Apply suggestions from code review * Update __init__.py * Create test_init.py * Update homeassistant/components/met/__init__.py * Update __init__.py * Update __init__.py * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com>
2020-08-09 18:18:02 +00:00
from datetime import timedelta
import logging
from random import randrange
import metno
from homeassistant.const import (
CONF_ELEVATION,
CONF_LATITUDE,
CONF_LONGITUDE,
EVENT_CORE_CONFIG_UPDATE,
LENGTH_FEET,
LENGTH_METERS,
)
from homeassistant.core import Config, HomeAssistant
Add DataUpdateCoordinator to met integration (#38405) * Add DataUpdateCoordinator to met integration * isort * redundant fetch_data * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * fix black, isort, flake8, hassfest, mypy * remove unused async_setup method * replace fetch_data by coordinator request_refresh * remove redundant async_update * track_home * Apply suggestions from code review * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update test_config_flow.py * Apply suggestions from code review * Apply suggestions from code review * Update __init__.py * Create test_init.py * Update homeassistant/components/met/__init__.py * Update __init__.py * Update __init__.py * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com>
2020-08-09 18:18:02 +00:00
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.util.distance import convert as convert_distance
import homeassistant.util.dt as dt_util
from .const import CONF_TRACK_HOME, DOMAIN
2020-09-02 18:56:23 +00:00
URL = "https://aa015h6buqvih86i1.api.met.no/weatherapi/locationforecast/2.0/complete"
Add DataUpdateCoordinator to met integration (#38405) * Add DataUpdateCoordinator to met integration * isort * redundant fetch_data * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * fix black, isort, flake8, hassfest, mypy * remove unused async_setup method * replace fetch_data by coordinator request_refresh * remove redundant async_update * track_home * Apply suggestions from code review * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update test_config_flow.py * Apply suggestions from code review * Apply suggestions from code review * Update __init__.py * Create test_init.py * Update homeassistant/components/met/__init__.py * Update __init__.py * Update __init__.py * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com>
2020-08-09 18:18:02 +00:00
_LOGGER = logging.getLogger(__name__)
async def async_setup(hass: HomeAssistant, config: Config) -> bool:
"""Set up configured Met."""
Add DataUpdateCoordinator to met integration (#38405) * Add DataUpdateCoordinator to met integration * isort * redundant fetch_data * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * fix black, isort, flake8, hassfest, mypy * remove unused async_setup method * replace fetch_data by coordinator request_refresh * remove redundant async_update * track_home * Apply suggestions from code review * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update test_config_flow.py * Apply suggestions from code review * Apply suggestions from code review * Update __init__.py * Create test_init.py * Update homeassistant/components/met/__init__.py * Update __init__.py * Update __init__.py * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com>
2020-08-09 18:18:02 +00:00
hass.data.setdefault(DOMAIN, {})
return True
async def async_setup_entry(hass, config_entry):
"""Set up Met as config entry."""
Add DataUpdateCoordinator to met integration (#38405) * Add DataUpdateCoordinator to met integration * isort * redundant fetch_data * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * fix black, isort, flake8, hassfest, mypy * remove unused async_setup method * replace fetch_data by coordinator request_refresh * remove redundant async_update * track_home * Apply suggestions from code review * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update test_config_flow.py * Apply suggestions from code review * Apply suggestions from code review * Update __init__.py * Create test_init.py * Update homeassistant/components/met/__init__.py * Update __init__.py * Update __init__.py * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com>
2020-08-09 18:18:02 +00:00
coordinator = MetDataUpdateCoordinator(hass, config_entry)
await coordinator.async_refresh()
if not coordinator.last_update_success:
raise ConfigEntryNotReady
if config_entry.data.get(CONF_TRACK_HOME, False):
coordinator.track_home()
hass.data[DOMAIN][config_entry.entry_id] = coordinator
2019-07-31 19:25:30 +00:00
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, "weather")
)
Add DataUpdateCoordinator to met integration (#38405) * Add DataUpdateCoordinator to met integration * isort * redundant fetch_data * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * fix black, isort, flake8, hassfest, mypy * remove unused async_setup method * replace fetch_data by coordinator request_refresh * remove redundant async_update * track_home * Apply suggestions from code review * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update test_config_flow.py * Apply suggestions from code review * Apply suggestions from code review * Update __init__.py * Create test_init.py * Update homeassistant/components/met/__init__.py * Update __init__.py * Update __init__.py * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com>
2020-08-09 18:18:02 +00:00
return True
async def async_unload_entry(hass, config_entry):
"""Unload a config entry."""
2019-07-31 19:25:30 +00:00
await hass.config_entries.async_forward_entry_unload(config_entry, "weather")
Add DataUpdateCoordinator to met integration (#38405) * Add DataUpdateCoordinator to met integration * isort * redundant fetch_data * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * fix black, isort, flake8, hassfest, mypy * remove unused async_setup method * replace fetch_data by coordinator request_refresh * remove redundant async_update * track_home * Apply suggestions from code review * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update test_config_flow.py * Apply suggestions from code review * Apply suggestions from code review * Update __init__.py * Create test_init.py * Update homeassistant/components/met/__init__.py * Update __init__.py * Update __init__.py * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com>
2020-08-09 18:18:02 +00:00
hass.data[DOMAIN][config_entry.entry_id].untrack_home()
hass.data[DOMAIN].pop(config_entry.entry_id)
return True
Add DataUpdateCoordinator to met integration (#38405) * Add DataUpdateCoordinator to met integration * isort * redundant fetch_data * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * fix black, isort, flake8, hassfest, mypy * remove unused async_setup method * replace fetch_data by coordinator request_refresh * remove redundant async_update * track_home * Apply suggestions from code review * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update test_config_flow.py * Apply suggestions from code review * Apply suggestions from code review * Update __init__.py * Create test_init.py * Update homeassistant/components/met/__init__.py * Update __init__.py * Update __init__.py * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com>
2020-08-09 18:18:02 +00:00
class MetDataUpdateCoordinator(DataUpdateCoordinator):
"""Class to manage fetching Met data."""
def __init__(self, hass, config_entry):
"""Initialize global Met data updater."""
self._unsub_track_home = None
self.weather = MetWeatherData(
hass, config_entry.data, hass.config.units.is_metric
)
self.weather.init_data()
update_interval = timedelta(minutes=randrange(55, 65))
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=update_interval)
async def _async_update_data(self):
"""Fetch data from Met."""
try:
return await self.weather.fetch_data()
except Exception as err:
raise UpdateFailed(f"Update failed: {err}") from err
Add DataUpdateCoordinator to met integration (#38405) * Add DataUpdateCoordinator to met integration * isort * redundant fetch_data * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * fix black, isort, flake8, hassfest, mypy * remove unused async_setup method * replace fetch_data by coordinator request_refresh * remove redundant async_update * track_home * Apply suggestions from code review * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update test_config_flow.py * Apply suggestions from code review * Apply suggestions from code review * Update __init__.py * Create test_init.py * Update homeassistant/components/met/__init__.py * Update __init__.py * Update __init__.py * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com>
2020-08-09 18:18:02 +00:00
def track_home(self):
"""Start tracking changes to HA home setting."""
if self._unsub_track_home:
return
async def _async_update_weather_data(_event=None):
"""Update weather data."""
self.weather.init_data()
await self.async_refresh()
self._unsub_track_home = self.hass.bus.async_listen(
EVENT_CORE_CONFIG_UPDATE, _async_update_weather_data
)
def untrack_home(self):
"""Stop tracking changes to HA home setting."""
if self._unsub_track_home:
self._unsub_track_home()
self._unsub_track_home = None
class MetWeatherData:
"""Keep data for Met.no weather entities."""
def __init__(self, hass, config, is_metric):
"""Initialise the weather entity data."""
self.hass = hass
self._config = config
self._is_metric = is_metric
self._weather_data = None
self.current_weather_data = {}
self.daily_forecast = None
self.hourly_forecast = None
Add DataUpdateCoordinator to met integration (#38405) * Add DataUpdateCoordinator to met integration * isort * redundant fetch_data * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * fix black, isort, flake8, hassfest, mypy * remove unused async_setup method * replace fetch_data by coordinator request_refresh * remove redundant async_update * track_home * Apply suggestions from code review * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update test_config_flow.py * Apply suggestions from code review * Apply suggestions from code review * Update __init__.py * Create test_init.py * Update homeassistant/components/met/__init__.py * Update __init__.py * Update __init__.py * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com>
2020-08-09 18:18:02 +00:00
def init_data(self):
"""Weather data inialization - get the coordinates."""
if self._config.get(CONF_TRACK_HOME, False):
latitude = self.hass.config.latitude
longitude = self.hass.config.longitude
elevation = self.hass.config.elevation
else:
latitude = self._config[CONF_LATITUDE]
longitude = self._config[CONF_LONGITUDE]
elevation = self._config[CONF_ELEVATION]
if not self._is_metric:
elevation = int(
round(convert_distance(elevation, LENGTH_FEET, LENGTH_METERS))
)
coordinates = {
"lat": str(latitude),
"lon": str(longitude),
"msl": str(elevation),
}
self._weather_data = metno.MetWeatherData(
2020-09-02 18:56:23 +00:00
coordinates, async_get_clientsession(self.hass), api_url=URL
Add DataUpdateCoordinator to met integration (#38405) * Add DataUpdateCoordinator to met integration * isort * redundant fetch_data * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * fix black, isort, flake8, hassfest, mypy * remove unused async_setup method * replace fetch_data by coordinator request_refresh * remove redundant async_update * track_home * Apply suggestions from code review * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update test_config_flow.py * Apply suggestions from code review * Apply suggestions from code review * Update __init__.py * Create test_init.py * Update homeassistant/components/met/__init__.py * Update __init__.py * Update __init__.py * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com>
2020-08-09 18:18:02 +00:00
)
async def fetch_data(self):
"""Fetch data from API - (current weather and forecast)."""
await self._weather_data.fetching_data()
self.current_weather_data = self._weather_data.get_current_weather()
time_zone = dt_util.DEFAULT_TIME_ZONE
self.daily_forecast = self._weather_data.get_forecast(time_zone, False)
self.hourly_forecast = self._weather_data.get_forecast(time_zone, True)
Add DataUpdateCoordinator to met integration (#38405) * Add DataUpdateCoordinator to met integration * isort * redundant fetch_data * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * fix black, isort, flake8, hassfest, mypy * remove unused async_setup method * replace fetch_data by coordinator request_refresh * remove redundant async_update * track_home * Apply suggestions from code review * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update test_config_flow.py * Apply suggestions from code review * Apply suggestions from code review * Update __init__.py * Create test_init.py * Update homeassistant/components/met/__init__.py * Update __init__.py * Update __init__.py * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com>
2020-08-09 18:18:02 +00:00
return self