1 """The Met Office integration."""
3 from __future__
import annotations
27 DEFAULT_SCAN_INTERVAL,
29 METOFFICE_COORDINATES,
30 METOFFICE_DAILY_COORDINATOR,
31 METOFFICE_HOURLY_COORDINATOR,
36 from .data
import MetOfficeData
37 from .helpers
import fetch_data, fetch_site
39 _LOGGER = logging.getLogger(__name__)
41 PLATFORMS = [Platform.SENSOR, Platform.WEATHER]
45 """Set up a Met Office entry."""
47 latitude = entry.data[CONF_LATITUDE]
48 longitude = entry.data[CONF_LONGITUDE]
49 api_key = entry.data[CONF_API_KEY]
50 site_name = entry.data[CONF_NAME]
52 coordinates = f
"{latitude}_{longitude}"
56 entity_entry: er.RegistryEntry,
57 ) -> dict[str, Any] |
None:
58 """Update unique ID of entity entry."""
60 if entity_entry.domain != Platform.SENSOR:
64 "Station Name":
"name",
66 "Temperature":
"temperature",
67 "Feels Like Temperature":
"feels_like_temperature",
68 "Wind Speed":
"wind_speed",
69 "Wind Direction":
"wind_direction",
70 "Wind Gust":
"wind_gust",
71 "Visibility":
"visibility",
72 "Visibility Distance":
"visibility_distance",
74 "Probability of Precipitation":
"precipitation",
75 "Humidity":
"humidity",
78 match = re.search(f
"(?P<name>.*)_{coordinates}.*", entity_entry.unique_id)
83 if (name := match.group(
"name"))
in name_to_key:
85 "new_unique_id": entity_entry.unique_id.replace(name, name_to_key[name])
89 await er.async_migrate_entries(hass, entry.entry_id, update_unique_id)
91 connection = datapoint.connection(api_key=api_key)
93 site = await hass.async_add_executor_job(
94 fetch_site, connection, latitude, longitude
97 raise ConfigEntryNotReady
99 async
def async_update_3hourly() -> MetOfficeData:
100 return await hass.async_add_executor_job(
101 fetch_data, connection, site, MODE_3HOURLY
104 async
def async_update_daily() -> MetOfficeData:
105 return await hass.async_add_executor_job(
106 fetch_data, connection, site, MODE_DAILY
113 name=f
"MetOffice Hourly Coordinator for {site_name}",
114 update_method=async_update_3hourly,
115 update_interval=DEFAULT_SCAN_INTERVAL,
122 name=f
"MetOffice Daily Coordinator for {site_name}",
123 update_method=async_update_daily,
124 update_interval=DEFAULT_SCAN_INTERVAL,
127 metoffice_hass_data = hass.data.setdefault(DOMAIN, {})
128 metoffice_hass_data[entry.entry_id] = {
129 METOFFICE_HOURLY_COORDINATOR: metoffice_hourly_coordinator,
130 METOFFICE_DAILY_COORDINATOR: metoffice_daily_coordinator,
131 METOFFICE_NAME: site_name,
132 METOFFICE_COORDINATES: coordinates,
136 await asyncio.gather(
137 metoffice_hourly_coordinator.async_config_entry_first_refresh(),
138 metoffice_daily_coordinator.async_config_entry_first_refresh(),
141 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
147 """Unload a config entry."""
148 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
150 hass.data[DOMAIN].pop(entry.entry_id)
151 if not hass.data[DOMAIN]:
152 hass.data.pop(DOMAIN)
157 """Return device registry information."""
159 entry_type=dr.DeviceEntryType.SERVICE,
160 identifiers={(DOMAIN, coordinates)},
161 manufacturer=
"Met Office",
162 name=f
"Met Office {name}",
dict[str, str]|None update_unique_id(er.RegistryEntry entity_entry, str unique_id)
DeviceInfo get_device_info(str coordinates, str name)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)