Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Meteoclimatic weather data."""
2 
3 import logging
4 
5 from meteoclimatic import MeteoclimaticClient
6 from meteoclimatic.exceptions import MeteoclimaticError
7 
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
11 
12 from .const import CONF_STATION_CODE, DOMAIN, PLATFORMS, SCAN_INTERVAL
13 
14 _LOGGER = logging.getLogger(__name__)
15 
16 
17 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
18  """Set up a Meteoclimatic entry."""
19  station_code = entry.data[CONF_STATION_CODE]
20  meteoclimatic_client = MeteoclimaticClient()
21 
22  async def async_update_data():
23  """Obtain the latest data from Meteoclimatic."""
24  try:
25  data = await hass.async_add_executor_job(
26  meteoclimatic_client.weather_at_station, station_code
27  )
28  except MeteoclimaticError as err:
29  raise UpdateFailed(f"Error while retrieving data: {err}") from err
30  return data.__dict__
31 
32  coordinator = DataUpdateCoordinator(
33  hass,
34  _LOGGER,
35  config_entry=entry,
36  name=f"Meteoclimatic weather for {entry.title} ({station_code})",
37  update_method=async_update_data,
38  update_interval=SCAN_INTERVAL,
39  )
40 
41  await coordinator.async_config_entry_first_refresh()
42 
43  hass.data.setdefault(DOMAIN, {})
44  hass.data[DOMAIN][entry.entry_id] = coordinator
45 
46  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
47 
48  return True
49 
50 
51 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
52  """Unload a config entry."""
53  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:17
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:51