1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-15 07:36:16 +00:00

Use service helper to extract mealie config entry (#162800)

This commit is contained in:
epenet
2026-02-11 17:19:03 +01:00
committed by GitHub
parent bf79721e97
commit d69b816459
3 changed files with 21 additions and 37 deletions

View File

@@ -2,7 +2,6 @@
from dataclasses import asdict
from datetime import date
from typing import cast
from aiomealie import (
MealieConnectionError,
@@ -13,7 +12,6 @@ from aiomealie import (
from awesomeversion import AwesomeVersion
import voluptuous as vol
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import ATTR_CONFIG_ENTRY_ID, ATTR_DATE
from homeassistant.core import (
HomeAssistant,
@@ -23,7 +21,7 @@ from homeassistant.core import (
callback,
)
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import config_validation as cv, service
from .const import (
ATTR_END_DATE,
@@ -110,24 +108,6 @@ SERVICE_SET_MEALPLAN_SCHEMA = vol.Any(
)
def _async_get_entry(call: ServiceCall) -> MealieConfigEntry:
"""Get the Mealie config entry."""
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",
translation_placeholders={"target": DOMAIN},
)
if entry.state is not ConfigEntryState.LOADED:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="not_loaded",
translation_placeholders={"target": entry.title},
)
return cast(MealieConfigEntry, entry)
def _validate_mealplan_type(version: AwesomeVersion, entry_type: str) -> None:
"""Validate mealplan entry type, if prior to 3.7.0."""
@@ -151,7 +131,9 @@ def _validate_mealplan_type(version: AwesomeVersion, entry_type: str) -> None:
async def _async_get_mealplan(call: ServiceCall) -> ServiceResponse:
"""Get the mealplan for a specific range."""
entry = _async_get_entry(call)
entry: MealieConfigEntry = service.async_get_config_entry(
call.hass, DOMAIN, 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:
@@ -172,7 +154,9 @@ async def _async_get_mealplan(call: ServiceCall) -> ServiceResponse:
async def _async_get_recipe(call: ServiceCall) -> ServiceResponse:
"""Get a recipe."""
entry = _async_get_entry(call)
entry: MealieConfigEntry = service.async_get_config_entry(
call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY_ID]
)
recipe_id = call.data[ATTR_RECIPE_ID]
client = entry.runtime_data.client
try:
@@ -193,7 +177,9 @@ async def _async_get_recipe(call: ServiceCall) -> ServiceResponse:
async def _async_get_recipes(call: ServiceCall) -> ServiceResponse:
"""Get recipes."""
entry = _async_get_entry(call)
entry: MealieConfigEntry = service.async_get_config_entry(
call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY_ID]
)
search_terms = call.data.get(ATTR_SEARCH_TERMS)
result_limit = call.data.get(ATTR_RESULT_LIMIT, 10)
client = entry.runtime_data.client
@@ -214,7 +200,9 @@ async def _async_get_recipes(call: ServiceCall) -> ServiceResponse:
async def _async_import_recipe(call: ServiceCall) -> ServiceResponse:
"""Import a recipe."""
entry = _async_get_entry(call)
entry: MealieConfigEntry = service.async_get_config_entry(
call.hass, DOMAIN, 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
@@ -237,7 +225,9 @@ async def _async_import_recipe(call: ServiceCall) -> ServiceResponse:
async def _async_set_random_mealplan(call: ServiceCall) -> ServiceResponse:
"""Set a random mealplan."""
entry = _async_get_entry(call)
entry: MealieConfigEntry = service.async_get_config_entry(
call.hass, DOMAIN, 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
@@ -258,7 +248,9 @@ async def _async_set_random_mealplan(call: ServiceCall) -> ServiceResponse:
async def _async_set_mealplan(call: ServiceCall) -> ServiceResponse:
"""Set a mealplan."""
entry = _async_get_entry(call)
entry: MealieConfigEntry = service.async_get_config_entry(
call.hass, DOMAIN, 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

View File

@@ -132,9 +132,6 @@
"end_date_before_start_date": {
"message": "End date must be after start date."
},
"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."
},
@@ -144,9 +141,6 @@
"no_recipes_found": {
"message": "No recipes found matching your search."
},
"not_loaded": {
"message": "{target} is not loaded."
},
"recipe_not_found": {
"message": "Recipe with ID or slug `{recipe_id}` not found."
},

View File

@@ -542,7 +542,7 @@ async def test_service_entry_availability(
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
with pytest.raises(ServiceValidationError, match="Mock Title is not loaded"):
with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"):
await hass.services.async_call(
DOMAIN,
service,
@@ -551,9 +551,7 @@ async def test_service_entry_availability(
return_response=True,
)
with pytest.raises(
ServiceValidationError, match='Integration "mealie" not found in registry'
):
with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"):
await hass.services.async_call(
DOMAIN,
service,