2021-05-24 11:08:24 +00:00
|
|
|
"""The Wallbox integration."""
|
2021-11-23 21:30:22 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-05-24 11:08:24 +00:00
|
|
|
from wallbox import Wallbox
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-04 13:10:01 +00:00
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
|
2021-05-24 11:08:24 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-10-07 09:56:59 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
2021-05-24 11:08:24 +00:00
|
|
|
|
2023-10-07 09:56:59 +00:00
|
|
|
from .const import CONF_STATION, DOMAIN, UPDATE_INTERVAL
|
|
|
|
from .coordinator import InvalidAuth, WallboxCoordinator
|
2021-05-24 11:08:24 +00:00
|
|
|
|
2024-01-16 08:47:53 +00:00
|
|
|
PLATFORMS = [Platform.LOCK, Platform.NUMBER, Platform.SENSOR, Platform.SWITCH]
|
2022-04-26 21:27:43 +00:00
|
|
|
|
2021-05-24 11:08:24 +00:00
|
|
|
|
2021-05-27 15:39:06 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-05-24 11:08:24 +00:00
|
|
|
"""Set up Wallbox from a config entry."""
|
2022-11-09 14:22:54 +00:00
|
|
|
wallbox = Wallbox(
|
|
|
|
entry.data[CONF_USERNAME],
|
|
|
|
entry.data[CONF_PASSWORD],
|
|
|
|
jwtTokenDrift=UPDATE_INTERVAL,
|
|
|
|
)
|
2021-10-27 17:53:14 +00:00
|
|
|
wallbox_coordinator = WallboxCoordinator(
|
2021-05-24 11:08:24 +00:00
|
|
|
entry.data[CONF_STATION],
|
2021-10-27 17:53:14 +00:00
|
|
|
wallbox,
|
2021-05-24 11:08:24 +00:00
|
|
|
hass,
|
|
|
|
)
|
|
|
|
|
2021-11-15 16:25:19 +00:00
|
|
|
try:
|
|
|
|
await wallbox_coordinator.async_validate_input()
|
2021-05-24 11:08:24 +00:00
|
|
|
|
2021-11-15 16:25:19 +00:00
|
|
|
except InvalidAuth as ex:
|
|
|
|
raise ConfigEntryAuthFailed from ex
|
2021-05-24 11:08:24 +00:00
|
|
|
|
2021-11-15 16:25:19 +00:00
|
|
|
await wallbox_coordinator.async_config_entry_first_refresh()
|
2021-05-24 11:08:24 +00:00
|
|
|
|
2021-11-15 16:25:19 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = wallbox_coordinator
|
2021-10-27 17:53:14 +00:00
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2021-05-24 11:08:24 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-10-06 08:48:11 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-05-24 11:08:24 +00:00
|
|
|
"""Unload a config entry."""
|
2021-06-10 07:56:35 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2021-05-24 11:08:24 +00:00
|
|
|
if unload_ok:
|
2021-11-15 16:25:19 +00:00
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
2021-05-24 11:08:24 +00:00
|
|
|
|
|
|
|
return unload_ok
|