mirror of
https://github.com/home-assistant/core.git
synced 2026-08-24 10:13:52 -05:00
87 lines
2.8 KiB
Python
87 lines
2.8 KiB
Python
"""The AirNow integration."""
|
|
|
|
import datetime
|
|
import logging
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import (
|
|
CONF_API_KEY,
|
|
CONF_LATITUDE,
|
|
CONF_LONGITUDE,
|
|
CONF_RADIUS,
|
|
Platform,
|
|
)
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from .coordinator import AirNowConfigEntry, AirNowDataUpdateCoordinator
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
PLATFORMS = [Platform.SENSOR]
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: AirNowConfigEntry) -> bool:
|
|
"""Set up AirNow from a config entry."""
|
|
api_key = entry.data[CONF_API_KEY]
|
|
latitude = entry.data[CONF_LATITUDE]
|
|
longitude = entry.data[CONF_LONGITUDE]
|
|
|
|
# Reports are published hourly but update twice per hour
|
|
update_interval = datetime.timedelta(minutes=30)
|
|
|
|
# Setup the Coordinator
|
|
session = async_get_clientsession(hass)
|
|
coordinator = AirNowDataUpdateCoordinator(
|
|
hass, entry, session, api_key, latitude, longitude, update_interval
|
|
)
|
|
|
|
# Sync with Coordinator
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
# Store Entity and Initialize Platforms
|
|
entry.runtime_data = coordinator
|
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
|
|
# Clean up unused device entries with no entities
|
|
device_registry = dr.async_get(hass)
|
|
entity_registry = er.async_get(hass)
|
|
|
|
device_entries = dr.async_entries_for_config_entry(
|
|
device_registry, config_entry_id=entry.entry_id
|
|
)
|
|
for dev in device_entries:
|
|
dev_entities = er.async_entries_for_device(
|
|
entity_registry, dev.id, include_disabled_entities=True
|
|
)
|
|
if not dev_entities:
|
|
device_registry.async_remove_device(dev.id)
|
|
|
|
return True
|
|
|
|
|
|
async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
"""Migrate old entry."""
|
|
_LOGGER.debug("Migrating from version %s", entry.version)
|
|
|
|
if entry.version < 3:
|
|
# The 2026 AirNow API dropped the distance parameter, so the radius
|
|
# option no longer affects lookups. Strip it from both older layouts:
|
|
# version 1 kept it in data, version 2 in options.
|
|
new_data = {k: v for k, v in entry.data.items() if k != CONF_RADIUS}
|
|
new_options = {k: v for k, v in entry.options.items() if k != CONF_RADIUS}
|
|
|
|
hass.config_entries.async_update_entry(
|
|
entry, data=new_data, options=new_options, version=3
|
|
)
|
|
|
|
_LOGGER.info("Migration to version %s successful", entry.version)
|
|
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: AirNowConfigEntry) -> bool:
|
|
"""Unload a config entry."""
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|