Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion homeassistant/components/mealie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: MealieConfigEntry) -> bo
await statistics_coordinator.async_config_entry_first_refresh()

entry.runtime_data = MealieData(
client, mealplan_coordinator, shoppinglist_coordinator, statistics_coordinator
client,
version,
mealplan_coordinator,
shoppinglist_coordinator,
statistics_coordinator,
)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
Expand Down
24 changes: 16 additions & 8 deletions homeassistant/components/mealie/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import datetime

from aiomealie import Mealplan, MealplanEntryType
from awesomeversion import AwesomeVersion

from homeassistant.components.calendar import CalendarEntity, CalendarEvent
from homeassistant.core import HomeAssistant
Expand All @@ -15,13 +16,6 @@

PARALLEL_UPDATES = 0

SUPPORTED_MEALPLAN_ENTRY_TYPES = [
MealplanEntryType.BREAKFAST,
MealplanEntryType.DINNER,
MealplanEntryType.LUNCH,
MealplanEntryType.SIDE,
]


async def async_setup_entry(
hass: HomeAssistant,
Expand All @@ -30,10 +24,24 @@ async def async_setup_entry(
) -> None:
"""Set up the calendar platform for entity."""
coordinator = entry.runtime_data.mealplan_coordinator
version = entry.runtime_data.version

supported_mealplan_entry_types: list[MealplanEntryType]
if version.valid and version < AwesomeVersion("v3.7.0"):
# Prior to Mealie 3.7.0, only these mealplan entry types were supported
supported_mealplan_entry_types = [
MealplanEntryType.BREAKFAST,
MealplanEntryType.DINNER,
MealplanEntryType.LUNCH,
MealplanEntryType.SIDE,
]
else:
# For Mealie 3.7.0 and newer and nightlies, add all current mealplan entry types
supported_mealplan_entry_types = list(MealplanEntryType)

async_add_entities(
MealieMealplanCalendarEntity(coordinator, entry_type)
for entry_type in SUPPORTED_MEALPLAN_ENTRY_TYPES
for entry_type in supported_mealplan_entry_types
)


Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/mealie/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
ShoppingList,
Statistics,
)
from awesomeversion import AwesomeVersion

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
Expand All @@ -33,6 +34,7 @@ class MealieData:
"""Mealie data type."""

client: MealieClient
version: AwesomeVersion
mealplan_coordinator: MealieMealplanCoordinator
shoppinglist_coordinator: MealieShoppingListCoordinator
statistics_coordinator: MealieStatisticsCoordinator
Expand Down
28 changes: 28 additions & 0 deletions homeassistant/components/mealie/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
MealieValidationError,
MealplanEntryType,
)
from awesomeversion import AwesomeVersion
import voluptuous as vol

from homeassistant.config_entries import ConfigEntryState
Expand Down Expand Up @@ -127,6 +128,27 @@ def _async_get_entry(call: ServiceCall) -> MealieConfigEntry:
return cast(MealieConfigEntry, entry)


def _validate_mealplan_type(version: AwesomeVersion, entry_type: str) -> None:
"""Validate mealplan entry type, if prior to 3.7.0."""

if (
version.valid
and version < AwesomeVersion("v3.7.0")
and entry_type
not in {
MealplanEntryType.BREAKFAST.value,
MealplanEntryType.DINNER.value,
MealplanEntryType.LUNCH.value,
MealplanEntryType.SIDE.value,
}
):
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="invalid_mealplan_entry_type",
translation_placeholders={"mealplan_type": entry_type},
)


async def _async_get_mealplan(call: ServiceCall) -> ServiceResponse:
"""Get the mealplan for a specific range."""
entry = _async_get_entry(call)
Expand Down Expand Up @@ -219,6 +241,9 @@ async def _async_set_random_mealplan(call: ServiceCall) -> ServiceResponse:
mealplan_date = call.data[ATTR_DATE]
entry_type = MealplanEntryType(call.data[ATTR_ENTRY_TYPE])
client = entry.runtime_data.client

_validate_mealplan_type(entry.runtime_data.version, entry_type.value)

try:
mealplan = await client.random_mealplan(mealplan_date, entry_type)
except MealieConnectionError as err:
Expand All @@ -237,6 +262,9 @@ async def _async_set_mealplan(call: ServiceCall) -> ServiceResponse:
mealplan_date = call.data[ATTR_DATE]
entry_type = MealplanEntryType(call.data[ATTR_ENTRY_TYPE])
client = entry.runtime_data.client

_validate_mealplan_type(entry.runtime_data.version, entry_type.value)

try:
mealplan = await client.set_mealplan(
mealplan_date,
Expand Down
6 changes: 6 additions & 0 deletions homeassistant/components/mealie/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ set_random_mealplan:
- lunch
- dinner
- side
- dessert
- snack
- drink
translation_key: mealplan_entry_type

set_mealplan:
Expand All @@ -98,6 +101,9 @@ set_mealplan:
- lunch
- dinner
- side
- dessert
- snack
- drink
translation_key: mealplan_entry_type
recipe_id:
selector:
Expand Down
17 changes: 16 additions & 1 deletion homeassistant/components/mealie/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,23 @@
"breakfast": {
"name": "Breakfast"
},
"dessert": {
"name": "Dessert"
},
"dinner": {
"name": "Dinner"
},
"drink": {
"name": "Drink"
},
"lunch": {
"name": "Lunch"
},
"side": {
"name": "Side"
},
"snack": {
"name": "Snack"
}
},
"sensor": {
Expand Down Expand Up @@ -126,6 +135,9 @@
"integration_not_found": {
"message": "Integration \"{target}\" not found in registry."
},
"invalid_mealplan_entry_type": {
"message": "Entry type {mealplan_type} is not valid for this Mealie version."
},
"item_not_found_error": {
"message": "Item {shopping_list_item} not found."
},
Expand Down Expand Up @@ -161,9 +173,12 @@
"mealplan_entry_type": {
"options": {
"breakfast": "[%key:component::mealie::entity::calendar::breakfast::name%]",
"dessert": "[%key:component::mealie::entity::calendar::dessert::name%]",
"dinner": "[%key:component::mealie::entity::calendar::dinner::name%]",
"drink": "[%key:component::mealie::entity::calendar::drink::name%]",
"lunch": "[%key:component::mealie::entity::calendar::lunch::name%]",
"side": "[%key:component::mealie::entity::calendar::side::name%]"
"side": "[%key:component::mealie::entity::calendar::side::name%]",
"snack": "[%key:component::mealie::entity::calendar::snack::name%]"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion tests/components/mealie/fixtures/about.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "v2.0.0"
"version": "v3.7.0"
}
Loading
Loading