mirror of
https://github.com/home-assistant/core.git
synced 2026-02-14 23:28:42 +00:00
Use service helper to extract habitica config entry (#162795)
Co-authored-by: Manu <4445816+tr4nt0r@users.noreply.github.com>
This commit is contained in:
@@ -28,7 +28,6 @@ from habiticalib import (
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.todo import ATTR_RENAME
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import ATTR_DATE, ATTR_NAME
|
||||
from homeassistant.core import (
|
||||
HomeAssistant,
|
||||
@@ -38,7 +37,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 homeassistant.helpers.selector import ConfigEntrySelector
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
@@ -243,24 +242,11 @@ SERVICE_TASK_TYPE_MAP = {
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
async def _cast_skill(call: ServiceCall) -> ServiceResponse:
|
||||
"""Skill action."""
|
||||
entry = get_config_entry(call.hass, call.data[ATTR_CONFIG_ENTRY])
|
||||
entry: HabiticaConfigEntry = service.async_get_config_entry(
|
||||
call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY]
|
||||
)
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
skill = SKILL_MAP[call.data[ATTR_SKILL]]
|
||||
@@ -324,7 +310,9 @@ async def _cast_skill(call: ServiceCall) -> ServiceResponse:
|
||||
|
||||
async def _manage_quests(call: ServiceCall) -> ServiceResponse:
|
||||
"""Accept, reject, start, leave or cancel quests."""
|
||||
entry = get_config_entry(call.hass, call.data[ATTR_CONFIG_ENTRY])
|
||||
entry: HabiticaConfigEntry = service.async_get_config_entry(
|
||||
call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY]
|
||||
)
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
FUNC_MAP = {
|
||||
@@ -372,7 +360,9 @@ async def _manage_quests(call: ServiceCall) -> ServiceResponse:
|
||||
|
||||
async def _score_task(call: ServiceCall) -> ServiceResponse:
|
||||
"""Score a task action."""
|
||||
entry = get_config_entry(call.hass, call.data[ATTR_CONFIG_ENTRY])
|
||||
entry: HabiticaConfigEntry = service.async_get_config_entry(
|
||||
call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY]
|
||||
)
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
direction = (
|
||||
@@ -436,7 +426,9 @@ async def _score_task(call: ServiceCall) -> ServiceResponse:
|
||||
async def _transformation(call: ServiceCall) -> ServiceResponse:
|
||||
"""User a transformation item on a player character."""
|
||||
|
||||
entry = get_config_entry(call.hass, call.data[ATTR_CONFIG_ENTRY])
|
||||
entry: HabiticaConfigEntry = service.async_get_config_entry(
|
||||
call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY]
|
||||
)
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
item = ITEMID_MAP[call.data[ATTR_ITEM]]
|
||||
@@ -519,7 +511,9 @@ async def _transformation(call: ServiceCall) -> ServiceResponse:
|
||||
async def _get_tasks(call: ServiceCall) -> ServiceResponse:
|
||||
"""Get tasks action."""
|
||||
|
||||
entry = get_config_entry(call.hass, call.data[ATTR_CONFIG_ENTRY])
|
||||
entry: HabiticaConfigEntry = service.async_get_config_entry(
|
||||
call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY]
|
||||
)
|
||||
coordinator = entry.runtime_data
|
||||
response: list[TaskData] = coordinator.data.tasks
|
||||
|
||||
@@ -568,7 +562,9 @@ async def _get_tasks(call: ServiceCall) -> ServiceResponse:
|
||||
|
||||
async def _create_or_update_task(call: ServiceCall) -> ServiceResponse: # noqa: C901
|
||||
"""Create or update task action."""
|
||||
entry = get_config_entry(call.hass, call.data[ATTR_CONFIG_ENTRY])
|
||||
entry: HabiticaConfigEntry = service.async_get_config_entry(
|
||||
call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY]
|
||||
)
|
||||
coordinator = entry.runtime_data
|
||||
await coordinator.async_refresh()
|
||||
is_update = call.service in (
|
||||
@@ -852,7 +848,7 @@ async def _create_or_update_task(call: ServiceCall) -> ServiceResponse: # noqa:
|
||||
def async_setup_services(hass: HomeAssistant) -> None:
|
||||
"""Set up services for Habitica integration."""
|
||||
|
||||
for service in (
|
||||
for service_name in (
|
||||
SERVICE_ABORT_QUEST,
|
||||
SERVICE_ACCEPT_QUEST,
|
||||
SERVICE_CANCEL_QUEST,
|
||||
@@ -862,13 +858,13 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
):
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
service,
|
||||
service_name,
|
||||
_manage_quests,
|
||||
schema=SERVICE_MANAGE_QUEST_SCHEMA,
|
||||
supports_response=SupportsResponse.ONLY,
|
||||
)
|
||||
|
||||
for service in (
|
||||
for service_name in (
|
||||
SERVICE_UPDATE_DAILY,
|
||||
SERVICE_UPDATE_HABIT,
|
||||
SERVICE_UPDATE_REWARD,
|
||||
@@ -876,12 +872,12 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
):
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
service,
|
||||
service_name,
|
||||
_create_or_update_task,
|
||||
schema=SERVICE_UPDATE_TASK_SCHEMA,
|
||||
supports_response=SupportsResponse.ONLY,
|
||||
)
|
||||
for service in (
|
||||
for service_name in (
|
||||
SERVICE_CREATE_DAILY,
|
||||
SERVICE_CREATE_HABIT,
|
||||
SERVICE_CREATE_REWARD,
|
||||
@@ -889,7 +885,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
):
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
service,
|
||||
service_name,
|
||||
_create_or_update_task,
|
||||
schema=SERVICE_CREATE_TASK_SCHEMA,
|
||||
supports_response=SupportsResponse.ONLY,
|
||||
|
||||
@@ -550,12 +550,6 @@
|
||||
"delete_todos_failed": {
|
||||
"message": "Unable to delete item from Habitica to-do list, please try again"
|
||||
},
|
||||
"entry_not_found": {
|
||||
"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."
|
||||
},
|
||||
"frequency_not_monthly": {
|
||||
"message": "Unable to update task, monthly repeat settings apply only to monthly recurring dailies."
|
||||
},
|
||||
|
||||
@@ -322,10 +322,7 @@ async def test_get_config_entry(
|
||||
) -> None:
|
||||
"""Test Habitica config entry exceptions."""
|
||||
|
||||
with pytest.raises(
|
||||
ServiceValidationError,
|
||||
match="The selected character is not configured in Home Assistant",
|
||||
):
|
||||
with pytest.raises(ServiceValidationError) as err:
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_CAST_SKILL,
|
||||
@@ -337,13 +334,11 @@ async def test_get_config_entry(
|
||||
return_response=True,
|
||||
blocking=True,
|
||||
)
|
||||
assert err.value.translation_key == "service_config_entry_not_found"
|
||||
|
||||
assert await hass.config_entries.async_unload(config_entry.entry_id)
|
||||
|
||||
with pytest.raises(
|
||||
ServiceValidationError,
|
||||
match="The selected character is currently not loaded or disabled in Home Assistant",
|
||||
):
|
||||
with pytest.raises(ServiceValidationError) as err:
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_CAST_SKILL,
|
||||
@@ -355,6 +350,7 @@ async def test_get_config_entry(
|
||||
return_response=True,
|
||||
blocking=True,
|
||||
)
|
||||
assert err.value.translation_key == "service_config_entry_not_loaded"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
||||
Reference in New Issue
Block a user