Simplify habitica service actions

pull/146746/head
epenet 2025-06-13 14:42:42 +00:00
parent ff17d79e73
commit 41e135e699
1 changed files with 540 additions and 535 deletions

View File

@ -250,13 +250,9 @@ def get_config_entry(hass: HomeAssistant, entry_id: str) -> HabiticaConfigEntry:
return entry
@callback
def async_setup_services(hass: HomeAssistant) -> None: # noqa: C901
"""Set up services for Habitica integration."""
async def cast_skill(call: ServiceCall) -> ServiceResponse:
async def _cast_skill(call: ServiceCall) -> ServiceResponse:
"""Skill action."""
entry = get_config_entry(hass, call.data[ATTR_CONFIG_ENTRY])
entry = get_config_entry(call.hass, call.data[ATTR_CONFIG_ENTRY])
coordinator = entry.runtime_data
skill = SKILL_MAP[call.data[ATTR_SKILL]]
@ -317,9 +313,10 @@ def async_setup_services(hass: HomeAssistant) -> None: # noqa: C901
await coordinator.async_request_refresh()
return asdict(response.data)
async def manage_quests(call: ServiceCall) -> ServiceResponse:
async def _manage_quests(call: ServiceCall) -> ServiceResponse:
"""Accept, reject, start, leave or cancel quests."""
entry = get_config_entry(hass, call.data[ATTR_CONFIG_ENTRY])
entry = get_config_entry(call.hass, call.data[ATTR_CONFIG_ENTRY])
coordinator = entry.runtime_data
FUNC_MAP = {
@ -364,25 +361,10 @@ def async_setup_services(hass: HomeAssistant) -> None: # noqa: C901
else:
return asdict(response.data)
for service in (
SERVICE_ABORT_QUEST,
SERVICE_ACCEPT_QUEST,
SERVICE_CANCEL_QUEST,
SERVICE_LEAVE_QUEST,
SERVICE_REJECT_QUEST,
SERVICE_START_QUEST,
):
hass.services.async_register(
DOMAIN,
service,
manage_quests,
schema=SERVICE_MANAGE_QUEST_SCHEMA,
supports_response=SupportsResponse.ONLY,
)
async def score_task(call: ServiceCall) -> ServiceResponse:
async def _score_task(call: ServiceCall) -> ServiceResponse:
"""Score a task action."""
entry = get_config_entry(hass, call.data[ATTR_CONFIG_ENTRY])
entry = get_config_entry(call.hass, call.data[ATTR_CONFIG_ENTRY])
coordinator = entry.runtime_data
direction = (
@ -442,10 +424,11 @@ def async_setup_services(hass: HomeAssistant) -> None: # noqa: C901
await coordinator.async_request_refresh()
return asdict(response.data)
async def transformation(call: ServiceCall) -> ServiceResponse:
async def _transformation(call: ServiceCall) -> ServiceResponse:
"""User a transformation item on a player character."""
entry = get_config_entry(hass, call.data[ATTR_CONFIG_ENTRY])
entry = get_config_entry(call.hass, call.data[ATTR_CONFIG_ENTRY])
coordinator = entry.runtime_data
item = ITEMID_MAP[call.data[ATTR_ITEM]]
@ -524,10 +507,11 @@ def async_setup_services(hass: HomeAssistant) -> None: # noqa: C901
else:
return asdict(response.data)
async def get_tasks(call: ServiceCall) -> ServiceResponse:
async def _get_tasks(call: ServiceCall) -> ServiceResponse:
"""Get tasks action."""
entry = get_config_entry(hass, call.data[ATTR_CONFIG_ENTRY])
entry = get_config_entry(call.hass, call.data[ATTR_CONFIG_ENTRY])
coordinator = entry.runtime_data
response: list[TaskData] = coordinator.data.tasks
@ -573,9 +557,10 @@ def async_setup_services(hass: HomeAssistant) -> None: # noqa: C901
return result
async def create_or_update_task(call: ServiceCall) -> ServiceResponse: # noqa: C901
async def _create_or_update_task(call: ServiceCall) -> ServiceResponse: # noqa: C901
"""Create or update task action."""
entry = get_config_entry(hass, call.data[ATTR_CONFIG_ENTRY])
entry = get_config_entry(call.hass, call.data[ATTR_CONFIG_ENTRY])
coordinator = entry.runtime_data
await coordinator.async_refresh()
is_update = call.service in (
@ -635,8 +620,7 @@ def async_setup_services(hass: HomeAssistant) -> None: # noqa: C901
try:
update_tags.update(
{
user_tags.get(tag_name.lower())
or (await create_tag(tag_name))
user_tags.get(tag_name.lower()) or (await create_tag(tag_name))
for tag_name in tags
}
)
@ -850,6 +834,27 @@ def async_setup_services(hass: HomeAssistant) -> None: # noqa: C901
else:
return response.data.to_dict(omit_none=True)
@callback
def async_setup_services(hass: HomeAssistant) -> None:
"""Set up services for Habitica integration."""
for service in (
SERVICE_ABORT_QUEST,
SERVICE_ACCEPT_QUEST,
SERVICE_CANCEL_QUEST,
SERVICE_LEAVE_QUEST,
SERVICE_REJECT_QUEST,
SERVICE_START_QUEST,
):
hass.services.async_register(
DOMAIN,
service,
_manage_quests,
schema=SERVICE_MANAGE_QUEST_SCHEMA,
supports_response=SupportsResponse.ONLY,
)
for service in (
SERVICE_UPDATE_DAILY,
SERVICE_UPDATE_HABIT,
@ -859,7 +864,7 @@ def async_setup_services(hass: HomeAssistant) -> None: # noqa: C901
hass.services.async_register(
DOMAIN,
service,
create_or_update_task,
_create_or_update_task,
schema=SERVICE_UPDATE_TASK_SCHEMA,
supports_response=SupportsResponse.ONLY,
)
@ -872,7 +877,7 @@ def async_setup_services(hass: HomeAssistant) -> None: # noqa: C901
hass.services.async_register(
DOMAIN,
service,
create_or_update_task,
_create_or_update_task,
schema=SERVICE_CREATE_TASK_SCHEMA,
supports_response=SupportsResponse.ONLY,
)
@ -880,7 +885,7 @@ def async_setup_services(hass: HomeAssistant) -> None: # noqa: C901
hass.services.async_register(
DOMAIN,
SERVICE_CAST_SKILL,
cast_skill,
_cast_skill,
schema=SERVICE_CAST_SKILL_SCHEMA,
supports_response=SupportsResponse.ONLY,
)
@ -888,14 +893,14 @@ def async_setup_services(hass: HomeAssistant) -> None: # noqa: C901
hass.services.async_register(
DOMAIN,
SERVICE_SCORE_HABIT,
score_task,
_score_task,
schema=SERVICE_SCORE_TASK_SCHEMA,
supports_response=SupportsResponse.ONLY,
)
hass.services.async_register(
DOMAIN,
SERVICE_SCORE_REWARD,
score_task,
_score_task,
schema=SERVICE_SCORE_TASK_SCHEMA,
supports_response=SupportsResponse.ONLY,
)
@ -903,14 +908,14 @@ def async_setup_services(hass: HomeAssistant) -> None: # noqa: C901
hass.services.async_register(
DOMAIN,
SERVICE_TRANSFORMATION,
transformation,
_transformation,
schema=SERVICE_TRANSFORMATION_SCHEMA,
supports_response=SupportsResponse.ONLY,
)
hass.services.async_register(
DOMAIN,
SERVICE_GET_TASKS,
get_tasks,
_get_tasks,
schema=SERVICE_GET_TASKS_SCHEMA,
supports_response=SupportsResponse.ONLY,
)