Simplify mealie service actions (#146601)

This commit is contained in:
epenet 2025-06-12 12:17:27 +02:00 committed by GitHub
parent 4160521349
commit 9d1e60cf7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 115 additions and 107 deletions

View File

@ -24,7 +24,7 @@ from .coordinator import (
MealieShoppingListCoordinator,
MealieStatisticsCoordinator,
)
from .services import setup_services
from .services import async_setup_services
from .utils import create_version
PLATFORMS: list[Platform] = [Platform.CALENDAR, Platform.SENSOR, Platform.TODO]
@ -34,7 +34,7 @@ CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Mealie component."""
setup_services(hass)
async_setup_services(hass)
return True

View File

@ -19,6 +19,7 @@ from homeassistant.core import (
ServiceCall,
ServiceResponse,
SupportsResponse,
callback,
)
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers import config_validation as cv
@ -98,9 +99,10 @@ SERVICE_SET_MEALPLAN_SCHEMA = vol.Any(
)
def async_get_entry(hass: HomeAssistant, config_entry_id: str) -> MealieConfigEntry:
def _async_get_entry(call: ServiceCall) -> MealieConfigEntry:
"""Get the Mealie config entry."""
if not (entry := hass.config_entries.async_get_entry(config_entry_id)):
config_entry_id: str = call.data[ATTR_CONFIG_ENTRY_ID]
if not (entry := call.hass.config_entries.async_get_entry(config_entry_id)):
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="integration_not_found",
@ -115,143 +117,149 @@ def async_get_entry(hass: HomeAssistant, config_entry_id: str) -> MealieConfigEn
return cast(MealieConfigEntry, entry)
def setup_services(hass: HomeAssistant) -> None:
"""Set up the services for the Mealie integration."""
async def _async_get_mealplan(call: ServiceCall) -> ServiceResponse:
"""Get the mealplan for a specific range."""
entry = _async_get_entry(call)
start_date = call.data.get(ATTR_START_DATE, date.today())
end_date = call.data.get(ATTR_END_DATE, date.today())
if end_date < start_date:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="end_date_before_start_date",
)
client = entry.runtime_data.client
try:
mealplans = await client.get_mealplans(start_date, end_date)
except MealieConnectionError as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="connection_error",
) from err
return {"mealplan": [asdict(x) for x in mealplans.items]}
async def async_get_mealplan(call: ServiceCall) -> ServiceResponse:
"""Get the mealplan for a specific range."""
entry = async_get_entry(hass, call.data[ATTR_CONFIG_ENTRY_ID])
start_date = call.data.get(ATTR_START_DATE, date.today())
end_date = call.data.get(ATTR_END_DATE, date.today())
if end_date < start_date:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="end_date_before_start_date",
)
client = entry.runtime_data.client
try:
mealplans = await client.get_mealplans(start_date, end_date)
except MealieConnectionError as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="connection_error",
) from err
return {"mealplan": [asdict(x) for x in mealplans.items]}
async def async_get_recipe(call: ServiceCall) -> ServiceResponse:
"""Get a recipe."""
entry = async_get_entry(hass, call.data[ATTR_CONFIG_ENTRY_ID])
recipe_id = call.data[ATTR_RECIPE_ID]
client = entry.runtime_data.client
try:
recipe = await client.get_recipe(recipe_id)
except MealieConnectionError as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="connection_error",
) from err
except MealieNotFoundError as err:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="recipe_not_found",
translation_placeholders={"recipe_id": recipe_id},
) from err
async def _async_get_recipe(call: ServiceCall) -> ServiceResponse:
"""Get a recipe."""
entry = _async_get_entry(call)
recipe_id = call.data[ATTR_RECIPE_ID]
client = entry.runtime_data.client
try:
recipe = await client.get_recipe(recipe_id)
except MealieConnectionError as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="connection_error",
) from err
except MealieNotFoundError as err:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="recipe_not_found",
translation_placeholders={"recipe_id": recipe_id},
) from err
return {"recipe": asdict(recipe)}
async def _async_import_recipe(call: ServiceCall) -> ServiceResponse:
"""Import a recipe."""
entry = _async_get_entry(call)
url = call.data[ATTR_URL]
include_tags = call.data.get(ATTR_INCLUDE_TAGS, False)
client = entry.runtime_data.client
try:
recipe = await client.import_recipe(url, include_tags)
except MealieValidationError as err:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="could_not_import_recipe",
) from err
except MealieConnectionError as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="connection_error",
) from err
if call.return_response:
return {"recipe": asdict(recipe)}
return None
async def async_import_recipe(call: ServiceCall) -> ServiceResponse:
"""Import a recipe."""
entry = async_get_entry(hass, call.data[ATTR_CONFIG_ENTRY_ID])
url = call.data[ATTR_URL]
include_tags = call.data.get(ATTR_INCLUDE_TAGS, False)
client = entry.runtime_data.client
try:
recipe = await client.import_recipe(url, include_tags)
except MealieValidationError as err:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="could_not_import_recipe",
) from err
except MealieConnectionError as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="connection_error",
) from err
if call.return_response:
return {"recipe": asdict(recipe)}
return None
async def async_set_random_mealplan(call: ServiceCall) -> ServiceResponse:
"""Set a random mealplan."""
entry = async_get_entry(hass, call.data[ATTR_CONFIG_ENTRY_ID])
mealplan_date = call.data[ATTR_DATE]
entry_type = MealplanEntryType(call.data[ATTR_ENTRY_TYPE])
client = entry.runtime_data.client
try:
mealplan = await client.random_mealplan(mealplan_date, entry_type)
except MealieConnectionError as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="connection_error",
) from err
if call.return_response:
return {"mealplan": asdict(mealplan)}
return None
async def _async_set_random_mealplan(call: ServiceCall) -> ServiceResponse:
"""Set a random mealplan."""
entry = _async_get_entry(call)
mealplan_date = call.data[ATTR_DATE]
entry_type = MealplanEntryType(call.data[ATTR_ENTRY_TYPE])
client = entry.runtime_data.client
try:
mealplan = await client.random_mealplan(mealplan_date, entry_type)
except MealieConnectionError as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="connection_error",
) from err
if call.return_response:
return {"mealplan": asdict(mealplan)}
return None
async def async_set_mealplan(call: ServiceCall) -> ServiceResponse:
"""Set a mealplan."""
entry = async_get_entry(hass, call.data[ATTR_CONFIG_ENTRY_ID])
mealplan_date = call.data[ATTR_DATE]
entry_type = MealplanEntryType(call.data[ATTR_ENTRY_TYPE])
client = entry.runtime_data.client
try:
mealplan = await client.set_mealplan(
mealplan_date,
entry_type,
recipe_id=call.data.get(ATTR_RECIPE_ID),
note_title=call.data.get(ATTR_NOTE_TITLE),
note_text=call.data.get(ATTR_NOTE_TEXT),
)
except MealieConnectionError as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="connection_error",
) from err
if call.return_response:
return {"mealplan": asdict(mealplan)}
return None
async def _async_set_mealplan(call: ServiceCall) -> ServiceResponse:
"""Set a mealplan."""
entry = _async_get_entry(call)
mealplan_date = call.data[ATTR_DATE]
entry_type = MealplanEntryType(call.data[ATTR_ENTRY_TYPE])
client = entry.runtime_data.client
try:
mealplan = await client.set_mealplan(
mealplan_date,
entry_type,
recipe_id=call.data.get(ATTR_RECIPE_ID),
note_title=call.data.get(ATTR_NOTE_TITLE),
note_text=call.data.get(ATTR_NOTE_TEXT),
)
except MealieConnectionError as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="connection_error",
) from err
if call.return_response:
return {"mealplan": asdict(mealplan)}
return None
@callback
def async_setup_services(hass: HomeAssistant) -> None:
"""Set up the services for the Mealie integration."""
hass.services.async_register(
DOMAIN,
SERVICE_GET_MEALPLAN,
async_get_mealplan,
_async_get_mealplan,
schema=SERVICE_GET_MEALPLAN_SCHEMA,
supports_response=SupportsResponse.ONLY,
)
hass.services.async_register(
DOMAIN,
SERVICE_GET_RECIPE,
async_get_recipe,
_async_get_recipe,
schema=SERVICE_GET_RECIPE_SCHEMA,
supports_response=SupportsResponse.ONLY,
)
hass.services.async_register(
DOMAIN,
SERVICE_IMPORT_RECIPE,
async_import_recipe,
_async_import_recipe,
schema=SERVICE_IMPORT_RECIPE_SCHEMA,
supports_response=SupportsResponse.OPTIONAL,
)
hass.services.async_register(
DOMAIN,
SERVICE_SET_RANDOM_MEALPLAN,
async_set_random_mealplan,
_async_set_random_mealplan,
schema=SERVICE_SET_RANDOM_MEALPLAN_SCHEMA,
supports_response=SupportsResponse.OPTIONAL,
)
hass.services.async_register(
DOMAIN,
SERVICE_SET_MEALPLAN,
async_set_mealplan,
_async_set_mealplan,
schema=SERVICE_SET_MEALPLAN_SCHEMA,
supports_response=SupportsResponse.OPTIONAL,
)