Improve exceptions in habitica cast skill action (#129603)

* Raise a different exception when entry not loaded

* adjust type hints

* move `get_config_entry` to services module
This commit is contained in:
Manu 2024-11-04 16:46:38 +01:00 committed by GitHub
parent 365f8046ac
commit a5f3c434e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 9 deletions

View File

@ -9,6 +9,7 @@ from typing import Any
from aiohttp import ClientResponseError from aiohttp import ClientResponseError
import voluptuous as vol import voluptuous as vol
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import ATTR_NAME, CONF_NAME from homeassistant.const import ATTR_NAME, CONF_NAME
from homeassistant.core import ( from homeassistant.core import (
HomeAssistant, 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: def async_setup_services(hass: HomeAssistant) -> None:
"""Set up services for Habitica integration.""" """Set up services for Habitica integration."""
@ -86,14 +102,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
async def cast_skill(call: ServiceCall) -> ServiceResponse: async def cast_skill(call: ServiceCall) -> ServiceResponse:
"""Skill action.""" """Skill action."""
entry: HabiticaConfigEntry | None entry = get_config_entry(hass, call.data[ATTR_CONFIG_ENTRY])
if not (
entry := hass.config_entries.async_get_entry(call.data[ATTR_CONFIG_ENTRY])
):
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="entry_not_found",
)
coordinator = entry.runtime_data coordinator = entry.runtime_data
skill = { skill = {
"pickpocket": {"spellId": "pickPocket", "cost": "10 MP"}, "pickpocket": {"spellId": "pickPocket", "cost": "10 MP"},

View File

@ -219,7 +219,10 @@
"message": "Unable to cast skill, your character does not have the skill or spell {skill}." "message": "Unable to cast skill, your character does not have the skill or spell {skill}."
}, },
"entry_not_found": { "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": { "task_not_found": {
"message": "Unable to cast skill, could not find the task {task}" "message": "Unable to cast skill, could not find the task {task}"