diff --git a/homeassistant/components/habitica/services.py b/homeassistant/components/habitica/services.py index 8ca80ff63ad..440e2d4fb23 100644 --- a/homeassistant/components/habitica/services.py +++ b/homeassistant/components/habitica/services.py @@ -9,6 +9,7 @@ from typing import Any from aiohttp import ClientResponseError import voluptuous as vol +from homeassistant.config_entries import ConfigEntryState from homeassistant.const import ATTR_NAME, CONF_NAME from homeassistant.core import ( HomeAssistant, @@ -54,6 +55,21 @@ SERVICE_CAST_SKILL_SCHEMA = vol.Schema( ) +def get_config_entry(hass: HomeAssistant, entry_id: str) -> HabiticaConfigEntry: + """Return config entry or raise if not found or not loaded.""" + if not (entry := hass.config_entries.async_get_entry(entry_id)): + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="entry_not_found", + ) + if entry.state is not ConfigEntryState.LOADED: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="entry_not_loaded", + ) + return entry + + def async_setup_services(hass: HomeAssistant) -> None: """Set up services for Habitica integration.""" @@ -86,14 +102,7 @@ def async_setup_services(hass: HomeAssistant) -> None: async def cast_skill(call: ServiceCall) -> ServiceResponse: """Skill action.""" - entry: HabiticaConfigEntry | None - if not ( - entry := hass.config_entries.async_get_entry(call.data[ATTR_CONFIG_ENTRY]) - ): - raise ServiceValidationError( - translation_domain=DOMAIN, - translation_key="entry_not_found", - ) + entry = get_config_entry(hass, call.data[ATTR_CONFIG_ENTRY]) coordinator = entry.runtime_data skill = { "pickpocket": {"spellId": "pickPocket", "cost": "10 MP"}, diff --git a/homeassistant/components/habitica/strings.json b/homeassistant/components/habitica/strings.json index 62b01260010..390dc3ba9ae 100644 --- a/homeassistant/components/habitica/strings.json +++ b/homeassistant/components/habitica/strings.json @@ -219,7 +219,10 @@ "message": "Unable to cast skill, your character does not have the skill or spell {skill}." }, "entry_not_found": { - "message": "The selected character is currently not configured or loaded in Home Assistant." + "message": "The selected character is not configured in Home Assistant." + }, + "entry_not_loaded": { + "message": "The selected character is currently not loaded or disabled in Home Assistant." }, "task_not_found": { "message": "Unable to cast skill, could not find the task {task}"