mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 23:27:37 +00:00
Clean up Mealie coordinator (#122310)
* Clean up Mealie coordinator * Clean up Mealie coordinator * Clean up Mealie coordinator * Fix * Fix
This commit is contained in:
parent
a78d6b8c36
commit
8d01ad98eb
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from abc import abstractmethod
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
@ -44,56 +45,50 @@ class MealieDataUpdateCoordinator[_DataT](DataUpdateCoordinator[_DataT]):
|
|||||||
"""Base coordinator."""
|
"""Base coordinator."""
|
||||||
|
|
||||||
config_entry: MealieConfigEntry
|
config_entry: MealieConfigEntry
|
||||||
|
_name: str
|
||||||
|
_update_interval: timedelta
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, hass: HomeAssistant, client: MealieClient) -> None:
|
||||||
self,
|
"""Initialize the Mealie data coordinator."""
|
||||||
hass: HomeAssistant,
|
|
||||||
name: str,
|
|
||||||
client: MealieClient,
|
|
||||||
update_interval: timedelta,
|
|
||||||
) -> None:
|
|
||||||
"""Initialize the Withings data coordinator."""
|
|
||||||
super().__init__(
|
super().__init__(
|
||||||
hass,
|
hass,
|
||||||
LOGGER,
|
LOGGER,
|
||||||
name=name,
|
name=self._name,
|
||||||
update_interval=update_interval,
|
update_interval=self._update_interval,
|
||||||
)
|
)
|
||||||
self.client = client
|
self.client = client
|
||||||
|
|
||||||
|
async def _async_update_data(self) -> _DataT:
|
||||||
|
"""Fetch data from Mealie."""
|
||||||
|
try:
|
||||||
|
return await self._async_update_internal()
|
||||||
|
except MealieAuthenticationError as error:
|
||||||
|
raise ConfigEntryAuthFailed from error
|
||||||
|
except MealieConnectionError as error:
|
||||||
|
raise UpdateFailed(error) from error
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
async def _async_update_internal(self) -> _DataT:
|
||||||
|
"""Fetch data from Mealie."""
|
||||||
|
|
||||||
|
|
||||||
class MealieMealplanCoordinator(
|
class MealieMealplanCoordinator(
|
||||||
MealieDataUpdateCoordinator[dict[MealplanEntryType, list[Mealplan]]]
|
MealieDataUpdateCoordinator[dict[MealplanEntryType, list[Mealplan]]]
|
||||||
):
|
):
|
||||||
"""Class to manage fetching Mealie data."""
|
"""Class to manage fetching Mealie data."""
|
||||||
|
|
||||||
def __init__(self, hass: HomeAssistant, client: MealieClient) -> None:
|
_name = "MealieMealplan"
|
||||||
"""Initialize coordinator."""
|
_update_interval = timedelta(hours=1)
|
||||||
super().__init__(
|
|
||||||
hass,
|
|
||||||
name="MealieMealplan",
|
|
||||||
client=client,
|
|
||||||
update_interval=timedelta(hours=1),
|
|
||||||
)
|
|
||||||
self.client = client
|
|
||||||
|
|
||||||
async def _async_update_data(self) -> dict[MealplanEntryType, list[Mealplan]]:
|
async def _async_update_internal(self) -> dict[MealplanEntryType, list[Mealplan]]:
|
||||||
next_week = dt_util.now() + WEEK
|
next_week = dt_util.now() + WEEK
|
||||||
try:
|
current_date = dt_util.now().date()
|
||||||
data = (
|
next_week_date = next_week.date()
|
||||||
await self.client.get_mealplans(dt_util.now().date(), next_week.date())
|
response = await self.client.get_mealplans(current_date, next_week_date)
|
||||||
).items
|
|
||||||
except MealieAuthenticationError as error:
|
|
||||||
raise ConfigEntryAuthFailed from error
|
|
||||||
except MealieConnectionError as error:
|
|
||||||
raise UpdateFailed(error) from error
|
|
||||||
res: dict[MealplanEntryType, list[Mealplan]] = {
|
res: dict[MealplanEntryType, list[Mealplan]] = {
|
||||||
MealplanEntryType.BREAKFAST: [],
|
type_: [] for type_ in MealplanEntryType
|
||||||
MealplanEntryType.LUNCH: [],
|
|
||||||
MealplanEntryType.DINNER: [],
|
|
||||||
MealplanEntryType.SIDE: [],
|
|
||||||
}
|
}
|
||||||
for meal in data:
|
for meal in response.items:
|
||||||
res[meal.entry_type].append(meal)
|
res[meal.entry_type].append(meal)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
@ -111,20 +106,13 @@ class MealieShoppingListCoordinator(
|
|||||||
):
|
):
|
||||||
"""Class to manage fetching Mealie Shopping list data."""
|
"""Class to manage fetching Mealie Shopping list data."""
|
||||||
|
|
||||||
def __init__(self, hass: HomeAssistant, client: MealieClient) -> None:
|
_name = "MealieShoppingList"
|
||||||
"""Initialize coordinator."""
|
_update_interval = timedelta(minutes=5)
|
||||||
super().__init__(
|
|
||||||
hass,
|
|
||||||
name="MealieShoppingLists",
|
|
||||||
client=client,
|
|
||||||
update_interval=timedelta(minutes=5),
|
|
||||||
)
|
|
||||||
|
|
||||||
async def _async_update_data(
|
async def _async_update_internal(
|
||||||
self,
|
self,
|
||||||
) -> dict[str, ShoppingListData]:
|
) -> dict[str, ShoppingListData]:
|
||||||
shopping_list_items = {}
|
shopping_list_items = {}
|
||||||
try:
|
|
||||||
shopping_lists = (await self.client.get_shopping_lists()).items
|
shopping_lists = (await self.client.get_shopping_lists()).items
|
||||||
for shopping_list in shopping_lists:
|
for shopping_list in shopping_lists:
|
||||||
shopping_list_id = shopping_list.list_id
|
shopping_list_id = shopping_list.list_id
|
||||||
@ -136,31 +124,16 @@ class MealieShoppingListCoordinator(
|
|||||||
shopping_list_items[shopping_list_id] = ShoppingListData(
|
shopping_list_items[shopping_list_id] = ShoppingListData(
|
||||||
shopping_list=shopping_list, items=shopping_items
|
shopping_list=shopping_list, items=shopping_items
|
||||||
)
|
)
|
||||||
except MealieAuthenticationError as error:
|
|
||||||
raise ConfigEntryAuthFailed from error
|
|
||||||
except MealieConnectionError as error:
|
|
||||||
raise UpdateFailed(error) from error
|
|
||||||
return shopping_list_items
|
return shopping_list_items
|
||||||
|
|
||||||
|
|
||||||
class MealieStatisticsCoordinator(MealieDataUpdateCoordinator[Statistics]):
|
class MealieStatisticsCoordinator(MealieDataUpdateCoordinator[Statistics]):
|
||||||
"""Class to manage fetching Mealie Statistics data."""
|
"""Class to manage fetching Mealie Statistics data."""
|
||||||
|
|
||||||
def __init__(self, hass: HomeAssistant, client: MealieClient) -> None:
|
_name = "MealieStatistics"
|
||||||
"""Initialize coordinator."""
|
_update_interval = timedelta(minutes=15)
|
||||||
super().__init__(
|
|
||||||
hass,
|
|
||||||
name="MealieStatistics",
|
|
||||||
client=client,
|
|
||||||
update_interval=timedelta(minutes=15),
|
|
||||||
)
|
|
||||||
|
|
||||||
async def _async_update_data(
|
async def _async_update_internal(
|
||||||
self,
|
self,
|
||||||
) -> Statistics:
|
) -> Statistics:
|
||||||
try:
|
|
||||||
return await self.client.get_statistics()
|
return await self.client.get_statistics()
|
||||||
except MealieAuthenticationError as error:
|
|
||||||
raise ConfigEntryAuthFailed from error
|
|
||||||
except MealieConnectionError as error:
|
|
||||||
raise UpdateFailed(error) from error
|
|
||||||
|
@ -32,6 +32,15 @@ async def test_device_info(
|
|||||||
assert device_entry == snapshot
|
assert device_entry == snapshot
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"field",
|
||||||
|
[
|
||||||
|
"get_about",
|
||||||
|
"get_mealplans",
|
||||||
|
"get_shopping_lists",
|
||||||
|
"get_statistics",
|
||||||
|
],
|
||||||
|
)
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("exc", "state"),
|
("exc", "state"),
|
||||||
[
|
[
|
||||||
@ -43,11 +52,12 @@ async def test_setup_failure(
|
|||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_mealie_client: AsyncMock,
|
mock_mealie_client: AsyncMock,
|
||||||
mock_config_entry: MockConfigEntry,
|
mock_config_entry: MockConfigEntry,
|
||||||
|
field: str,
|
||||||
exc: Exception,
|
exc: Exception,
|
||||||
state: ConfigEntryState,
|
state: ConfigEntryState,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test setup failure."""
|
"""Test setup failure."""
|
||||||
mock_mealie_client.get_about.side_effect = exc
|
getattr(mock_mealie_client, field).side_effect = exc
|
||||||
|
|
||||||
await setup_integration(hass, mock_config_entry)
|
await setup_integration(hass, mock_config_entry)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user