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, MealieShoppingListCoordinator,
MealieStatisticsCoordinator, MealieStatisticsCoordinator,
) )
from .services import setup_services from .services import async_setup_services
from .utils import create_version from .utils import create_version
PLATFORMS: list[Platform] = [Platform.CALENDAR, Platform.SENSOR, Platform.TODO] 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: async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Mealie component.""" """Set up the Mealie component."""
setup_services(hass) async_setup_services(hass)
return True return True

View File

@ -19,6 +19,7 @@ from homeassistant.core import (
ServiceCall, ServiceCall,
ServiceResponse, ServiceResponse,
SupportsResponse, SupportsResponse,
callback,
) )
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers import config_validation as cv 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.""" """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( raise ServiceValidationError(
translation_domain=DOMAIN, translation_domain=DOMAIN,
translation_key="integration_not_found", translation_key="integration_not_found",
@ -115,143 +117,149 @@ def async_get_entry(hass: HomeAssistant, config_entry_id: str) -> MealieConfigEn
return cast(MealieConfigEntry, entry) return cast(MealieConfigEntry, entry)
def setup_services(hass: HomeAssistant) -> None: async def _async_get_mealplan(call: ServiceCall) -> ServiceResponse:
"""Set up the services for the Mealie integration.""" """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: async def _async_get_recipe(call: ServiceCall) -> ServiceResponse:
"""Get a recipe.""" """Get a recipe."""
entry = async_get_entry(hass, call.data[ATTR_CONFIG_ENTRY_ID]) entry = _async_get_entry(call)
recipe_id = call.data[ATTR_RECIPE_ID] recipe_id = call.data[ATTR_RECIPE_ID]
client = entry.runtime_data.client client = entry.runtime_data.client
try: try:
recipe = await client.get_recipe(recipe_id) recipe = await client.get_recipe(recipe_id)
except MealieConnectionError as err: except MealieConnectionError as err:
raise HomeAssistantError( raise HomeAssistantError(
translation_domain=DOMAIN, translation_domain=DOMAIN,
translation_key="connection_error", translation_key="connection_error",
) from err ) from err
except MealieNotFoundError as err: except MealieNotFoundError as err:
raise ServiceValidationError( raise ServiceValidationError(
translation_domain=DOMAIN, translation_domain=DOMAIN,
translation_key="recipe_not_found", translation_key="recipe_not_found",
translation_placeholders={"recipe_id": recipe_id}, translation_placeholders={"recipe_id": recipe_id},
) from err ) 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 {"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: async def _async_set_random_mealplan(call: ServiceCall) -> ServiceResponse:
"""Set a random mealplan.""" """Set a random mealplan."""
entry = async_get_entry(hass, call.data[ATTR_CONFIG_ENTRY_ID]) entry = _async_get_entry(call)
mealplan_date = call.data[ATTR_DATE] mealplan_date = call.data[ATTR_DATE]
entry_type = MealplanEntryType(call.data[ATTR_ENTRY_TYPE]) entry_type = MealplanEntryType(call.data[ATTR_ENTRY_TYPE])
client = entry.runtime_data.client client = entry.runtime_data.client
try: try:
mealplan = await client.random_mealplan(mealplan_date, entry_type) mealplan = await client.random_mealplan(mealplan_date, entry_type)
except MealieConnectionError as err: except MealieConnectionError as err:
raise HomeAssistantError( raise HomeAssistantError(
translation_domain=DOMAIN, translation_domain=DOMAIN,
translation_key="connection_error", translation_key="connection_error",
) from err ) from err
if call.return_response: if call.return_response:
return {"mealplan": asdict(mealplan)} return {"mealplan": asdict(mealplan)}
return None return None
async def async_set_mealplan(call: ServiceCall) -> ServiceResponse:
"""Set a mealplan.""" async def _async_set_mealplan(call: ServiceCall) -> ServiceResponse:
entry = async_get_entry(hass, call.data[ATTR_CONFIG_ENTRY_ID]) """Set a mealplan."""
mealplan_date = call.data[ATTR_DATE] entry = _async_get_entry(call)
entry_type = MealplanEntryType(call.data[ATTR_ENTRY_TYPE]) mealplan_date = call.data[ATTR_DATE]
client = entry.runtime_data.client entry_type = MealplanEntryType(call.data[ATTR_ENTRY_TYPE])
try: client = entry.runtime_data.client
mealplan = await client.set_mealplan( try:
mealplan_date, mealplan = await client.set_mealplan(
entry_type, mealplan_date,
recipe_id=call.data.get(ATTR_RECIPE_ID), entry_type,
note_title=call.data.get(ATTR_NOTE_TITLE), recipe_id=call.data.get(ATTR_RECIPE_ID),
note_text=call.data.get(ATTR_NOTE_TEXT), note_title=call.data.get(ATTR_NOTE_TITLE),
) note_text=call.data.get(ATTR_NOTE_TEXT),
except MealieConnectionError as err: )
raise HomeAssistantError( except MealieConnectionError as err:
translation_domain=DOMAIN, raise HomeAssistantError(
translation_key="connection_error", translation_domain=DOMAIN,
) from err translation_key="connection_error",
if call.return_response: ) from err
return {"mealplan": asdict(mealplan)} if call.return_response:
return None 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( hass.services.async_register(
DOMAIN, DOMAIN,
SERVICE_GET_MEALPLAN, SERVICE_GET_MEALPLAN,
async_get_mealplan, _async_get_mealplan,
schema=SERVICE_GET_MEALPLAN_SCHEMA, schema=SERVICE_GET_MEALPLAN_SCHEMA,
supports_response=SupportsResponse.ONLY, supports_response=SupportsResponse.ONLY,
) )
hass.services.async_register( hass.services.async_register(
DOMAIN, DOMAIN,
SERVICE_GET_RECIPE, SERVICE_GET_RECIPE,
async_get_recipe, _async_get_recipe,
schema=SERVICE_GET_RECIPE_SCHEMA, schema=SERVICE_GET_RECIPE_SCHEMA,
supports_response=SupportsResponse.ONLY, supports_response=SupportsResponse.ONLY,
) )
hass.services.async_register( hass.services.async_register(
DOMAIN, DOMAIN,
SERVICE_IMPORT_RECIPE, SERVICE_IMPORT_RECIPE,
async_import_recipe, _async_import_recipe,
schema=SERVICE_IMPORT_RECIPE_SCHEMA, schema=SERVICE_IMPORT_RECIPE_SCHEMA,
supports_response=SupportsResponse.OPTIONAL, supports_response=SupportsResponse.OPTIONAL,
) )
hass.services.async_register( hass.services.async_register(
DOMAIN, DOMAIN,
SERVICE_SET_RANDOM_MEALPLAN, SERVICE_SET_RANDOM_MEALPLAN,
async_set_random_mealplan, _async_set_random_mealplan,
schema=SERVICE_SET_RANDOM_MEALPLAN_SCHEMA, schema=SERVICE_SET_RANDOM_MEALPLAN_SCHEMA,
supports_response=SupportsResponse.OPTIONAL, supports_response=SupportsResponse.OPTIONAL,
) )
hass.services.async_register( hass.services.async_register(
DOMAIN, DOMAIN,
SERVICE_SET_MEALPLAN, SERVICE_SET_MEALPLAN,
async_set_mealplan, _async_set_mealplan,
schema=SERVICE_SET_MEALPLAN_SCHEMA, schema=SERVICE_SET_MEALPLAN_SCHEMA,
supports_response=SupportsResponse.OPTIONAL, supports_response=SupportsResponse.OPTIONAL,
) )