|
1 | 1 | import logging |
2 | | -from homeassistant.helpers import config_validation as cv |
3 | 2 | from homeassistant.config_entries import ConfigEntry |
4 | 3 | from homeassistant.core import HomeAssistant |
| 4 | +from homeassistant.exceptions import ConfigEntryNotReady |
| 5 | +from homeassistant.helpers import config_validation as cv |
| 6 | +from homeassistant.helpers.aiohttp_client import async_get_clientsession |
5 | 7 |
|
| 8 | +from .api import TunnelflightApi |
6 | 9 | from .const import DOMAIN |
7 | 10 | from .logbook_service import async_setup_services, async_unload_services |
8 | 11 |
|
@@ -36,7 +39,21 @@ async def async_setup(hass, config): |
36 | 39 | async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
37 | 40 | """Set up IBA Tunnelflight from a config entry.""" |
38 | 41 | hass.data.setdefault(DOMAIN, {}) |
39 | | - hass.data[DOMAIN][entry.entry_id] = entry.data |
| 42 | + |
| 43 | + session = async_get_clientsession(hass) |
| 44 | + api = TunnelflightApi( |
| 45 | + entry.data.get("username"), entry.data.get("password"), session |
| 46 | + ) |
| 47 | + |
| 48 | + try: |
| 49 | + login_success = await api.login() |
| 50 | + except Exception as err: |
| 51 | + raise ConfigEntryNotReady("Unable to connect to Tunnelflight API") from err |
| 52 | + |
| 53 | + if not login_success: |
| 54 | + raise ConfigEntryNotReady("Unable to authenticate with Tunnelflight API") |
| 55 | + |
| 56 | + hass.data[DOMAIN][entry.entry_id] = {"api": api, **entry.data} |
40 | 57 |
|
41 | 58 | # Set up platforms |
42 | 59 | await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) |
|
0 commit comments