mirror of
https://github.com/home-assistant/core.git
synced 2026-07-03 12:46:09 +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>
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
"""The MyPermobil integration."""
|
|
|
|
import logging
|
|
|
|
from mypermobil import MyPermobil, MyPermobilClientException
|
|
|
|
from homeassistant.const import (
|
|
CONF_CODE,
|
|
CONF_EMAIL,
|
|
CONF_REGION,
|
|
CONF_TOKEN,
|
|
CONF_TTL,
|
|
Platform,
|
|
)
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from .const import APPLICATION
|
|
from .coordinator import MyPermobilCoordinator, PermobilConfigEntry
|
|
|
|
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: PermobilConfigEntry) -> bool:
|
|
"""Set up MyPermobil from a config entry."""
|
|
|
|
# create the API object from the config and save it in hass
|
|
session = async_get_clientsession(hass)
|
|
p_api = MyPermobil(
|
|
application=APPLICATION,
|
|
session=session,
|
|
email=entry.data[CONF_EMAIL],
|
|
region=entry.data[CONF_REGION],
|
|
code=entry.data[CONF_CODE],
|
|
token=entry.data[CONF_TOKEN],
|
|
expiration_date=entry.data[CONF_TTL],
|
|
)
|
|
try:
|
|
p_api.self_authenticate()
|
|
except MyPermobilClientException as err:
|
|
_LOGGER.error("Error authenticating %s", err)
|
|
raise ConfigEntryAuthFailed(f"Config error for {p_api.email}") from err
|
|
|
|
# create the coordinator with the API object
|
|
coordinator = MyPermobilCoordinator(hass, entry, p_api)
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
entry.runtime_data = coordinator
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: PermobilConfigEntry) -> bool:
|
|
"""Unload a config entry."""
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|