mirror of
https://github.com/home-assistant/core.git
synced 2026-09-07 14:01:20 +01:00
108 lines
3.5 KiB
Python
108 lines
3.5 KiB
Python
"""Integration for Peblar EV chargers."""
|
|
|
|
import asyncio
|
|
|
|
from aiohttp import CookieJar
|
|
from peblar import (
|
|
AccessMode,
|
|
Peblar,
|
|
PeblarAuthenticationError,
|
|
PeblarConnectionError,
|
|
PeblarError,
|
|
)
|
|
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, Platform
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
|
from homeassistant.helpers import config_validation as cv
|
|
from homeassistant.helpers.aiohttp_client import async_create_clientsession
|
|
from homeassistant.helpers.typing import ConfigType
|
|
|
|
from .const import DOMAIN
|
|
from .coordinator import (
|
|
PeblarConfigEntry,
|
|
PeblarDataUpdateCoordinator,
|
|
PeblarRuntimeData,
|
|
PeblarUserConfigurationDataUpdateCoordinator,
|
|
PeblarVersionDataUpdateCoordinator,
|
|
)
|
|
from .services import async_setup_services
|
|
|
|
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
|
|
|
|
PLATFORMS = [
|
|
Platform.BINARY_SENSOR,
|
|
Platform.BUTTON,
|
|
Platform.NUMBER,
|
|
Platform.SELECT,
|
|
Platform.SENSOR,
|
|
Platform.SWITCH,
|
|
Platform.UPDATE,
|
|
]
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|
"""Set up the Peblar integration."""
|
|
async_setup_services(hass)
|
|
return True
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: PeblarConfigEntry) -> bool:
|
|
"""Set up Peblar from a config entry."""
|
|
# Set up connection to the Peblar charger
|
|
peblar = Peblar(
|
|
host=entry.data[CONF_HOST],
|
|
session=async_create_clientsession(hass, cookie_jar=CookieJar(unsafe=True)),
|
|
)
|
|
try:
|
|
await peblar.login(password=entry.data[CONF_PASSWORD])
|
|
system_information = await peblar.system_information()
|
|
api = await peblar.rest_api(enable=True, access_mode=AccessMode.READ_WRITE)
|
|
except PeblarConnectionError as err:
|
|
raise ConfigEntryNotReady(
|
|
translation_domain=DOMAIN,
|
|
translation_key="communication_error",
|
|
translation_placeholders={"error": str(err)},
|
|
) from err
|
|
except PeblarAuthenticationError as err:
|
|
raise ConfigEntryAuthFailed(
|
|
translation_domain=DOMAIN,
|
|
translation_key="authentication_error",
|
|
) from err
|
|
except PeblarError as err:
|
|
raise ConfigEntryNotReady(
|
|
translation_domain=DOMAIN,
|
|
translation_key="unknown_error",
|
|
translation_placeholders={"error": str(err)},
|
|
) from err
|
|
|
|
# Setup the data coordinators
|
|
meter_coordinator = PeblarDataUpdateCoordinator(hass, entry, api)
|
|
user_configuration_coordinator = PeblarUserConfigurationDataUpdateCoordinator(
|
|
hass, entry, peblar
|
|
)
|
|
version_coordinator = PeblarVersionDataUpdateCoordinator(hass, entry, peblar)
|
|
await asyncio.gather(
|
|
meter_coordinator.async_config_entry_first_refresh(),
|
|
user_configuration_coordinator.async_config_entry_first_refresh(),
|
|
version_coordinator.async_config_entry_first_refresh(),
|
|
)
|
|
|
|
# Store the runtime data
|
|
entry.runtime_data = PeblarRuntimeData(
|
|
data_coordinator=meter_coordinator,
|
|
system_information=system_information,
|
|
user_configuration_coordinator=user_configuration_coordinator,
|
|
version_coordinator=version_coordinator,
|
|
)
|
|
|
|
# Forward the setup to the platforms
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: PeblarConfigEntry) -> bool:
|
|
"""Unload Peblar config entry."""
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|