2024-01-29 14:08:11 +00:00
|
|
|
"""The Bring! integration."""
|
2024-03-08 13:51:32 +00:00
|
|
|
|
2024-01-29 14:08:11 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2024-07-26 14:59:28 +00:00
|
|
|
from bring_api import (
|
|
|
|
Bring,
|
2024-01-29 14:08:11 +00:00
|
|
|
BringAuthException,
|
|
|
|
BringParseException,
|
|
|
|
BringRequestException,
|
|
|
|
)
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, Platform
|
|
|
|
from homeassistant.core import HomeAssistant
|
2024-06-22 13:14:53 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
2024-02-05 17:51:01 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2024-01-29 14:08:11 +00:00
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
from .coordinator import BringDataUpdateCoordinator
|
|
|
|
|
|
|
|
PLATFORMS: list[Platform] = [Platform.TODO]
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2024-05-17 13:42:58 +00:00
|
|
|
type BringConfigEntry = ConfigEntry[BringDataUpdateCoordinator]
|
2024-01-29 14:08:11 +00:00
|
|
|
|
2024-05-04 23:57:00 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: BringConfigEntry) -> bool:
|
2024-01-29 14:08:11 +00:00
|
|
|
"""Set up Bring! from a config entry."""
|
|
|
|
|
|
|
|
email = entry.data[CONF_EMAIL]
|
|
|
|
password = entry.data[CONF_PASSWORD]
|
|
|
|
|
2024-02-05 17:51:01 +00:00
|
|
|
session = async_get_clientsession(hass)
|
2024-02-13 12:25:32 +00:00
|
|
|
bring = Bring(session, email, password)
|
2024-01-29 14:08:11 +00:00
|
|
|
|
|
|
|
try:
|
2024-02-13 12:25:32 +00:00
|
|
|
await bring.login()
|
2024-01-29 14:08:11 +00:00
|
|
|
except BringRequestException as e:
|
|
|
|
raise ConfigEntryNotReady(
|
2024-04-13 22:26:37 +00:00
|
|
|
translation_domain=DOMAIN,
|
|
|
|
translation_key="setup_request_exception",
|
2024-01-29 14:08:11 +00:00
|
|
|
) from e
|
|
|
|
except BringParseException as e:
|
|
|
|
raise ConfigEntryNotReady(
|
2024-04-13 22:26:37 +00:00
|
|
|
translation_domain=DOMAIN,
|
2024-06-22 13:14:53 +00:00
|
|
|
translation_key="setup_parse_exception",
|
2024-04-13 22:26:37 +00:00
|
|
|
) from e
|
|
|
|
except BringAuthException as e:
|
2024-06-22 13:14:53 +00:00
|
|
|
raise ConfigEntryAuthFailed(
|
2024-04-13 22:26:37 +00:00
|
|
|
translation_domain=DOMAIN,
|
|
|
|
translation_key="setup_authentication_exception",
|
|
|
|
translation_placeholders={CONF_EMAIL: email},
|
2024-01-29 14:08:11 +00:00
|
|
|
) from e
|
|
|
|
|
|
|
|
coordinator = BringDataUpdateCoordinator(hass, bring)
|
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
|
2024-05-04 23:57:00 +00:00
|
|
|
entry.runtime_data = coordinator
|
2024-01-29 14:08:11 +00:00
|
|
|
|
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Unload a config entry."""
|
2024-05-04 23:57:00 +00:00
|
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|