2024-06-21 09:04:55 +00:00
|
|
|
"""The Mealie integration."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-07-07 15:13:15 +00:00
|
|
|
from aiomealie import MealieAuthenticationError, MealieClient, MealieConnectionError
|
|
|
|
|
|
|
|
from homeassistant.const import CONF_API_TOKEN, CONF_HOST, Platform
|
2024-06-21 09:04:55 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2024-07-07 15:13:15 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady
|
2024-07-07 19:19:20 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
2024-07-07 15:13:15 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from homeassistant.helpers.device_registry import DeviceEntryType
|
2024-07-07 19:19:20 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2024-06-21 09:04:55 +00:00
|
|
|
|
2024-07-07 15:13:15 +00:00
|
|
|
from .const import DOMAIN
|
2024-07-09 16:39:22 +00:00
|
|
|
from .coordinator import (
|
|
|
|
MealieConfigEntry,
|
|
|
|
MealieData,
|
|
|
|
MealieMealplanCoordinator,
|
|
|
|
MealieShoppingListCoordinator,
|
|
|
|
)
|
2024-07-07 19:19:20 +00:00
|
|
|
from .services import setup_services
|
2024-06-21 09:04:55 +00:00
|
|
|
|
2024-07-09 16:39:22 +00:00
|
|
|
PLATFORMS: list[Platform] = [Platform.CALENDAR, Platform.TODO]
|
2024-06-21 09:04:55 +00:00
|
|
|
|
2024-07-07 19:19:20 +00:00
|
|
|
CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|
|
|
"""Set up the Mealie component."""
|
|
|
|
setup_services(hass)
|
|
|
|
return True
|
|
|
|
|
2024-06-21 09:04:55 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: MealieConfigEntry) -> bool:
|
|
|
|
"""Set up Mealie from a config entry."""
|
2024-07-07 15:13:15 +00:00
|
|
|
client = MealieClient(
|
|
|
|
entry.data[CONF_HOST],
|
|
|
|
token=entry.data[CONF_API_TOKEN],
|
|
|
|
session=async_get_clientsession(hass),
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
about = await client.get_about()
|
|
|
|
except MealieAuthenticationError as error:
|
|
|
|
raise ConfigEntryError("Authentication failed") from error
|
|
|
|
except MealieConnectionError as error:
|
|
|
|
raise ConfigEntryNotReady(error) from error
|
|
|
|
|
|
|
|
assert entry.unique_id
|
|
|
|
device_registry = dr.async_get(hass)
|
|
|
|
device_registry.async_get_or_create(
|
|
|
|
config_entry_id=entry.entry_id,
|
|
|
|
identifiers={(DOMAIN, entry.unique_id)},
|
|
|
|
entry_type=DeviceEntryType.SERVICE,
|
|
|
|
sw_version=about.version,
|
|
|
|
)
|
|
|
|
|
2024-07-09 16:39:22 +00:00
|
|
|
mealplan_coordinator = MealieMealplanCoordinator(hass, client)
|
|
|
|
shoppinglist_coordinator = MealieShoppingListCoordinator(hass, client)
|
2024-06-21 09:04:55 +00:00
|
|
|
|
2024-07-09 16:39:22 +00:00
|
|
|
await mealplan_coordinator.async_config_entry_first_refresh()
|
2024-06-21 09:04:55 +00:00
|
|
|
|
2024-07-09 16:39:22 +00:00
|
|
|
await shoppinglist_coordinator.async_get_shopping_lists()
|
|
|
|
await shoppinglist_coordinator.async_config_entry_first_refresh()
|
|
|
|
|
|
|
|
entry.runtime_data = MealieData(
|
|
|
|
client, mealplan_coordinator, shoppinglist_coordinator
|
|
|
|
)
|
2024-06-21 09:04:55 +00:00
|
|
|
|
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: MealieConfigEntry) -> bool:
|
|
|
|
"""Unload a config entry."""
|
|
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|