Simplify habitica service actions (#146746)

This commit is contained in:
epenet 2025-06-16 14:37:38 +02:00 committed by GitHub
parent 25c408484c
commit d657964729
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

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