mirror of
https://github.com/home-assistant/core.git
synced 2026-05-31 20:54:23 +01:00
d766aae436
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: frenck <195327+frenck@users.noreply.github.com>
53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
"""The WattTime integration."""
|
|
|
|
from aiowatttime import Client
|
|
from aiowatttime.errors import InvalidCredentialsError, WattTimeError
|
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
|
from homeassistant.helpers import aiohttp_client
|
|
|
|
from .const import LOGGER
|
|
from .coordinator import WattTimeConfigEntry, WattTimeCoordinator
|
|
|
|
PLATFORMS: list[Platform] = [Platform.SENSOR]
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: WattTimeConfigEntry) -> bool:
|
|
"""Set up WattTime from a config entry."""
|
|
session = aiohttp_client.async_get_clientsession(hass)
|
|
|
|
try:
|
|
client = await Client.async_login(
|
|
entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD], session=session
|
|
)
|
|
except InvalidCredentialsError as err:
|
|
raise ConfigEntryAuthFailed("Invalid username/password") from err
|
|
except WattTimeError as err:
|
|
LOGGER.error("Error while authenticating with WattTime: %s", err)
|
|
return False
|
|
|
|
coordinator = WattTimeCoordinator(hass, entry, client)
|
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
entry.runtime_data = coordinator
|
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
|
|
entry.async_on_unload(entry.add_update_listener(async_reload_entry))
|
|
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: WattTimeConfigEntry) -> bool:
|
|
"""Unload a config entry."""
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
|
|
|
|
async def async_reload_entry(
|
|
hass: HomeAssistant, config_entry: WattTimeConfigEntry
|
|
) -> None:
|
|
"""Handle an options update."""
|
|
await hass.config_entries.async_reload(config_entry.entry_id)
|