core/homeassistant/components/seventeentrack/__init__.py

49 lines
1.6 KiB
Python

"""The seventeentrack component."""
from pyseventeentrack import Client as SeventeenTrackClient
from pyseventeentrack.errors import SeventeenTrackError
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.typing import ConfigType
from .const import DOMAIN
from .coordinator import SeventeenTrackCoordinator
from .services import setup_services
PLATFORMS: list[Platform] = [Platform.SENSOR]
CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the 17Track component."""
setup_services(hass)
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up 17Track from a config entry."""
session = async_get_clientsession(hass)
client = SeventeenTrackClient(session=session)
try:
await client.profile.login(entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD])
except SeventeenTrackError as err:
raise ConfigEntryNotReady from err
seventeen_coordinator = SeventeenTrackCoordinator(hass, client)
await seventeen_coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = seventeen_coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True