Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Mealie integration."""
2 
3 from __future__ import annotations
4 
5 from aiomealie import MealieAuthenticationError, MealieClient, MealieError
6 
7 from homeassistant.const import CONF_API_TOKEN, CONF_HOST, CONF_VERIFY_SSL, Platform
8 from homeassistant.core import HomeAssistant
9 from homeassistant.exceptions import (
10  ConfigEntryAuthFailed,
11  ConfigEntryError,
12  ConfigEntryNotReady,
13 )
14 from homeassistant.helpers import config_validation as cv, device_registry as dr
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
16 from homeassistant.helpers.device_registry import DeviceEntryType
17 from homeassistant.helpers.typing import ConfigType
18 
19 from .const import DOMAIN, LOGGER, MIN_REQUIRED_MEALIE_VERSION
20 from .coordinator import (
21  MealieConfigEntry,
22  MealieData,
23  MealieMealplanCoordinator,
24  MealieShoppingListCoordinator,
25  MealieStatisticsCoordinator,
26 )
27 from .services import setup_services
28 from .utils import create_version
29 
30 PLATFORMS: list[Platform] = [Platform.CALENDAR, Platform.SENSOR, Platform.TODO]
31 
32 CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
33 
34 
35 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
36  """Set up the Mealie component."""
37  setup_services(hass)
38  return True
39 
40 
41 async def async_setup_entry(hass: HomeAssistant, entry: MealieConfigEntry) -> bool:
42  """Set up Mealie from a config entry."""
43  client = MealieClient(
44  entry.data[CONF_HOST],
45  token=entry.data[CONF_API_TOKEN],
47  hass, verify_ssl=entry.data.get(CONF_VERIFY_SSL, True)
48  ),
49  )
50  try:
51  await client.define_household_support()
52  about = await client.get_about()
53  version = create_version(about.version)
54  except MealieAuthenticationError as error:
55  raise ConfigEntryAuthFailed from error
56  except MealieError as error:
57  raise ConfigEntryNotReady(error) from error
58 
59  if not version.valid:
60  LOGGER.warning(
61  "It seems like you are using the nightly version of Mealie, nightly"
62  " versions could have changes that stop this integration working"
63  )
64  if version.valid and version < MIN_REQUIRED_MEALIE_VERSION:
65  raise ConfigEntryError(
66  translation_domain=DOMAIN,
67  translation_key="version_error",
68  translation_placeholders={
69  "mealie_version": about.version,
70  "min_version": MIN_REQUIRED_MEALIE_VERSION,
71  },
72  )
73 
74  assert entry.unique_id
75  device_registry = dr.async_get(hass)
76  device_registry.async_get_or_create(
77  config_entry_id=entry.entry_id,
78  identifiers={(DOMAIN, entry.unique_id)},
79  entry_type=DeviceEntryType.SERVICE,
80  sw_version=about.version,
81  )
82 
83  mealplan_coordinator = MealieMealplanCoordinator(hass, client)
84  shoppinglist_coordinator = MealieShoppingListCoordinator(hass, client)
85  statistics_coordinator = MealieStatisticsCoordinator(hass, client)
86 
87  await mealplan_coordinator.async_config_entry_first_refresh()
88  await shoppinglist_coordinator.async_config_entry_first_refresh()
89  await statistics_coordinator.async_config_entry_first_refresh()
90 
91  entry.runtime_data = MealieData(
92  client, mealplan_coordinator, shoppinglist_coordinator, statistics_coordinator
93  )
94 
95  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
96 
97  return True
98 
99 
100 async def async_unload_entry(hass: HomeAssistant, entry: MealieConfigEntry) -> bool:
101  """Unload a config entry."""
102  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
AwesomeVersion create_version(str version)
Definition: utils.py:8
bool async_unload_entry(HomeAssistant hass, MealieConfigEntry entry)
Definition: __init__.py:100
bool async_setup_entry(HomeAssistant hass, MealieConfigEntry entry)
Definition: __init__.py:41
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:35
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)