2021-07-20 04:30:00 +00:00
|
|
|
"""The CO2 Signal integration."""
|
2024-03-08 13:51:32 +00:00
|
|
|
|
2021-07-20 04:30:00 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-11-13 19:48:33 +00:00
|
|
|
from aioelectricitymaps import ElectricityMaps
|
|
|
|
|
2021-07-20 04:30:00 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-11-13 19:48:33 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY, Platform
|
2021-07-20 04:30:00 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-11-13 19:48:33 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2021-07-20 04:30:00 +00:00
|
|
|
|
2024-05-02 12:04:21 +00:00
|
|
|
from .const import DOMAIN # noqa: F401
|
2023-09-18 09:56:28 +00:00
|
|
|
from .coordinator import CO2SignalCoordinator
|
2021-07-20 04:30:00 +00:00
|
|
|
|
2021-12-03 16:51:30 +00:00
|
|
|
PLATFORMS = [Platform.SENSOR]
|
2021-07-23 21:35:11 +00:00
|
|
|
|
2024-05-17 13:42:58 +00:00
|
|
|
type CO2SignalConfigEntry = ConfigEntry[CO2SignalCoordinator]
|
2021-07-23 21:35:11 +00:00
|
|
|
|
2024-05-02 12:04:21 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: CO2SignalConfigEntry) -> bool:
|
2021-07-20 04:30:00 +00:00
|
|
|
"""Set up CO2 Signal from a config entry."""
|
2023-11-13 19:48:33 +00:00
|
|
|
session = async_get_clientsession(hass)
|
|
|
|
coordinator = CO2SignalCoordinator(
|
|
|
|
hass, ElectricityMaps(token=entry.data[CONF_API_KEY], session=session)
|
|
|
|
)
|
2021-07-23 21:35:11 +00:00
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
|
2024-05-02 12:04:21 +00:00
|
|
|
entry.runtime_data = coordinator
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2021-07-20 04:30:00 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2024-05-02 12:04:21 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: CO2SignalConfigEntry) -> bool:
|
2021-07-20 04:30:00 +00:00
|
|
|
"""Unload a config entry."""
|
|
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|