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,12 +117,9 @@ 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."""
async def async_get_mealplan(call: ServiceCall) -> ServiceResponse:
"""Get the mealplan for a specific range.""" """Get the mealplan for a specific range."""
entry = async_get_entry(hass, call.data[ATTR_CONFIG_ENTRY_ID]) entry = _async_get_entry(call)
start_date = call.data.get(ATTR_START_DATE, date.today()) start_date = call.data.get(ATTR_START_DATE, date.today())
end_date = call.data.get(ATTR_END_DATE, date.today()) end_date = call.data.get(ATTR_END_DATE, date.today())
if end_date < start_date: if end_date < start_date:
@ -138,9 +137,10 @@ def setup_services(hass: HomeAssistant) -> None:
) from err ) from err
return {"mealplan": [asdict(x) for x in mealplans.items]} 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:
@ -158,9 +158,10 @@ def setup_services(hass: HomeAssistant) -> None:
) from err ) from err
return {"recipe": asdict(recipe)} return {"recipe": asdict(recipe)}
async def async_import_recipe(call: ServiceCall) -> ServiceResponse:
async def _async_import_recipe(call: ServiceCall) -> ServiceResponse:
"""Import a recipe.""" """Import a recipe."""
entry = async_get_entry(hass, call.data[ATTR_CONFIG_ENTRY_ID]) entry = _async_get_entry(call)
url = call.data[ATTR_URL] url = call.data[ATTR_URL]
include_tags = call.data.get(ATTR_INCLUDE_TAGS, False) include_tags = call.data.get(ATTR_INCLUDE_TAGS, False)
client = entry.runtime_data.client client = entry.runtime_data.client
@ -180,9 +181,10 @@ def setup_services(hass: HomeAssistant) -> None:
return {"recipe": asdict(recipe)} return {"recipe": asdict(recipe)}
return None 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
@ -197,9 +199,10 @@ def setup_services(hass: HomeAssistant) -> None:
return {"mealplan": asdict(mealplan)} return {"mealplan": asdict(mealplan)}
return None return None
async def async_set_mealplan(call: ServiceCall) -> ServiceResponse:
async def _async_set_mealplan(call: ServiceCall) -> ServiceResponse:
"""Set a mealplan.""" """Set a 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
@ -220,38 +223,43 @@ def setup_services(hass: HomeAssistant) -> None:
return {"mealplan": asdict(mealplan)} return {"mealplan": asdict(mealplan)}
return None 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,
) )