From 51debea30c467c8245ef7bbab60407790c832db5 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 5 Nov 2026 10:19:17 +0100 Subject: [PATCH] Add action enums to habitica (#190350) --- homeassistant/components/habitica/const.py | 57 -- homeassistant/components/habitica/services.py | 448 ++++++----- tests/components/habitica/test_services.py | 694 ++++++++---------- 3 files changed, 577 insertions(+), 622 deletions(-) diff --git a/homeassistant/components/habitica/const.py b/homeassistant/components/habitica/const.py index dfcbecb5f05a..484ae292c6a6 100644 --- a/homeassistant/components/habitica/const.py +++ b/homeassistant/components/habitica/const.py @@ -17,63 +17,6 @@ DOMAIN = "habitica" MANUFACTURER = "HabitRPG, Inc." NAME = "Habitica" -ATTR_CONFIG_ENTRY = "config_entry" -ATTR_SKILL = "skill" -ATTR_TASK = "task" -ATTR_DIRECTION = "direction" -ATTR_TARGET = "target" -ATTR_ITEM = "item" -ATTR_TYPE = "type" -ATTR_PRIORITY = "priority" -ATTR_TAG = "tag" -ATTR_KEYWORD = "keyword" -ATTR_REMOVE_TAG = "remove_tag" -ATTR_ALIAS = "alias" -ATTR_PRIORITY = "priority" -ATTR_COST = "cost" -ATTR_NOTES = "notes" -ATTR_UP_DOWN = "up_down" -ATTR_FREQUENCY = "frequency" -ATTR_COUNTER_UP = "counter_up" -ATTR_COUNTER_DOWN = "counter_down" -ATTR_ADD_CHECKLIST_ITEM = "add_checklist_item" -ATTR_REMOVE_CHECKLIST_ITEM = "remove_checklist_item" -ATTR_SCORE_CHECKLIST_ITEM = "score_checklist_item" -ATTR_UNSCORE_CHECKLIST_ITEM = "unscore_checklist_item" -ATTR_COLLAPSE_CHECKLIST = "collapse_checklist" -ATTR_REMINDER = "reminder" -ATTR_REMOVE_REMINDER = "remove_reminder" -ATTR_CLEAR_REMINDER = "clear_reminder" -ATTR_CLEAR_DATE = "clear_date" -ATTR_REPEAT = "repeat" -ATTR_INTERVAL = "every_x" -ATTR_START_DATE = "start_date" -ATTR_REPEAT_MONTHLY = "repeat_monthly" -ATTR_STREAK = "streak" - -SERVICE_CAST_SKILL = "cast_skill" -SERVICE_START_QUEST = "start_quest" -SERVICE_ACCEPT_QUEST = "accept_quest" -SERVICE_CANCEL_QUEST = "cancel_quest" -SERVICE_ABORT_QUEST = "abort_quest" -SERVICE_REJECT_QUEST = "reject_quest" -SERVICE_LEAVE_QUEST = "leave_quest" -SERVICE_GET_TASKS = "get_tasks" - -SERVICE_SCORE_HABIT = "score_habit" -SERVICE_SCORE_REWARD = "score_reward" - -SERVICE_TRANSFORMATION = "transformation" - -SERVICE_UPDATE_REWARD = "update_reward" -SERVICE_CREATE_REWARD = "create_reward" -SERVICE_UPDATE_HABIT = "update_habit" -SERVICE_CREATE_HABIT = "create_habit" -SERVICE_UPDATE_TODO = "update_todo" -SERVICE_CREATE_TODO = "create_todo" -SERVICE_UPDATE_DAILY = "update_daily" -SERVICE_CREATE_DAILY = "create_daily" - DEVELOPER_ID = "4c4ca53f-c059-4ffa-966e-9d29dd405daf" X_CLIENT = f"{DEVELOPER_ID} - {APPLICATION_NAME} {__version__}" diff --git a/homeassistant/components/habitica/services.py b/homeassistant/components/habitica/services.py index 8cf445c0cd71..92ea41071e67 100644 --- a/homeassistant/components/habitica/services.py +++ b/homeassistant/components/habitica/services.py @@ -2,6 +2,7 @@ from dataclasses import asdict from datetime import UTC, datetime, time +from enum import StrEnum import logging from typing import TYPE_CHECKING, Any, cast from uuid import UUID, uuid4 @@ -25,8 +26,6 @@ from habiticalib import ( ) import probatio -from homeassistant.components.todo import ATTR_RENAME -from homeassistant.const import ATTR_DATE, ATTR_NAME from homeassistant.core import ( HomeAssistant, ServiceCall, @@ -39,100 +38,110 @@ from homeassistant.helpers import config_validation as cv, service from homeassistant.helpers.selector import ConfigEntrySelector from homeassistant.util import dt as dt_util -from .const import ( - ATTR_ADD_CHECKLIST_ITEM, - ATTR_ALIAS, - ATTR_CLEAR_DATE, - ATTR_CLEAR_REMINDER, - ATTR_COLLAPSE_CHECKLIST, - ATTR_CONFIG_ENTRY, - ATTR_COST, - ATTR_COUNTER_DOWN, - ATTR_COUNTER_UP, - ATTR_DIRECTION, - ATTR_FREQUENCY, - ATTR_INTERVAL, - ATTR_ITEM, - ATTR_KEYWORD, - ATTR_NOTES, - ATTR_PRIORITY, - ATTR_REMINDER, - ATTR_REMOVE_CHECKLIST_ITEM, - ATTR_REMOVE_REMINDER, - ATTR_REMOVE_TAG, - ATTR_REPEAT, - ATTR_REPEAT_MONTHLY, - ATTR_SCORE_CHECKLIST_ITEM, - ATTR_SKILL, - ATTR_START_DATE, - ATTR_STREAK, - ATTR_TAG, - ATTR_TARGET, - ATTR_TASK, - ATTR_TYPE, - ATTR_UNSCORE_CHECKLIST_ITEM, - ATTR_UP_DOWN, - DOMAIN, - SERVICE_ABORT_QUEST, - SERVICE_ACCEPT_QUEST, - SERVICE_CANCEL_QUEST, - SERVICE_CAST_SKILL, - SERVICE_CREATE_DAILY, - SERVICE_CREATE_HABIT, - SERVICE_CREATE_REWARD, - SERVICE_CREATE_TODO, - SERVICE_GET_TASKS, - SERVICE_LEAVE_QUEST, - SERVICE_REJECT_QUEST, - SERVICE_SCORE_HABIT, - SERVICE_SCORE_REWARD, - SERVICE_START_QUEST, - SERVICE_TRANSFORMATION, - SERVICE_UPDATE_DAILY, - SERVICE_UPDATE_HABIT, - SERVICE_UPDATE_REWARD, - SERVICE_UPDATE_TODO, - WEEK_DAYS, -) +from .const import DOMAIN, WEEK_DAYS from .coordinator import HabiticaConfigEntry _LOGGER = logging.getLogger(__name__) +class HabiticaService(StrEnum): + """Store keys for Habitica services.""" + + CAST_SKILL = "cast_skill" + ACCEPT_QUEST = "accept_quest" + REJECT_QUEST = "reject_quest" + START_QUEST = "start_quest" + CANCEL_QUEST = "cancel_quest" + ABORT_QUEST = "abort_quest" + LEAVE_QUEST = "leave_quest" + SCORE_HABIT = "score_habit" + SCORE_REWARD = "score_reward" + TRANSFORMATION = "transformation" + GET_TASKS = "get_tasks" + UPDATE_REWARD = "update_reward" + CREATE_REWARD = "create_reward" + UPDATE_HABIT = "update_habit" + CREATE_HABIT = "create_habit" + UPDATE_TODO = "update_todo" + CREATE_TODO = "create_todo" + UPDATE_DAILY = "update_daily" + CREATE_DAILY = "create_daily" + + +class HabiticaServiceArgument(StrEnum): + """Store keys for Habitica service arguments.""" + + CONFIG_ENTRY = "config_entry" + SKILL = "skill" + TASK = "task" + DIRECTION = "direction" + ITEM = "item" + TARGET = "target" + TYPE = "type" + PRIORITY = "priority" + TAG = "tag" + KEYWORD = "keyword" + RENAME = "rename" + NOTES = "notes" + COST = "cost" + REMOVE_TAG = "remove_tag" + ALIAS = "alias" + NAME = "name" + UP_DOWN = "up_down" + FREQUENCY = "frequency" + COUNTER_UP = "counter_up" + COUNTER_DOWN = "counter_down" + ADD_CHECKLIST_ITEM = "add_checklist_item" + REMOVE_CHECKLIST_ITEM = "remove_checklist_item" + SCORE_CHECKLIST_ITEM = "score_checklist_item" + UNSCORE_CHECKLIST_ITEM = "unscore_checklist_item" + COLLAPSE_CHECKLIST = "collapse_checklist" + DATE = "date" + CLEAR_DATE = "clear_date" + REMINDER = "reminder" + REMOVE_REMINDER = "remove_reminder" + CLEAR_REMINDER = "clear_reminder" + START_DATE = "start_date" + EVERY_X = "every_x" + REPEAT = "repeat" + REPEAT_MONTHLY = "repeat_monthly" + STREAK = "streak" + + SERVICE_CAST_SKILL_SCHEMA = probatio.Schema( { - probatio.Required(ATTR_CONFIG_ENTRY): ConfigEntrySelector( + probatio.Required(HabiticaServiceArgument.CONFIG_ENTRY): ConfigEntrySelector( {"integration": DOMAIN} ), - probatio.Required(ATTR_SKILL): cv.string, - probatio.Optional(ATTR_TASK): cv.string, + probatio.Required(HabiticaServiceArgument.SKILL): cv.string, + probatio.Optional(HabiticaServiceArgument.TASK): cv.string, } ) SERVICE_MANAGE_QUEST_SCHEMA = probatio.Schema( { - probatio.Required(ATTR_CONFIG_ENTRY): ConfigEntrySelector( + probatio.Required(HabiticaServiceArgument.CONFIG_ENTRY): ConfigEntrySelector( {"integration": DOMAIN} ), } ) SERVICE_SCORE_TASK_SCHEMA = probatio.Schema( { - probatio.Required(ATTR_CONFIG_ENTRY): ConfigEntrySelector( + probatio.Required(HabiticaServiceArgument.CONFIG_ENTRY): ConfigEntrySelector( {"integration": DOMAIN} ), - probatio.Required(ATTR_TASK): cv.string, - probatio.Optional(ATTR_DIRECTION): cv.string, + probatio.Required(HabiticaServiceArgument.TASK): cv.string, + probatio.Optional(HabiticaServiceArgument.DIRECTION): cv.string, } ) SERVICE_TRANSFORMATION_SCHEMA = probatio.Schema( { - probatio.Required(ATTR_CONFIG_ENTRY): ConfigEntrySelector( + probatio.Required(HabiticaServiceArgument.CONFIG_ENTRY): ConfigEntrySelector( {"integration": DOMAIN} ), - probatio.Required(ATTR_ITEM): cv.string, - probatio.Required(ATTR_TARGET): cv.string, + probatio.Required(HabiticaServiceArgument.ITEM): cv.string, + probatio.Required(HabiticaServiceArgument.TARGET): cv.string, } ) @@ -143,73 +152,93 @@ COLLAPSE_CHECKLIST_MAP = { BASE_TASK_SCHEMA = probatio.Schema( { - probatio.Required(ATTR_CONFIG_ENTRY): ConfigEntrySelector(), - probatio.Optional(ATTR_RENAME): cv.string, - probatio.Optional(ATTR_NOTES): cv.string, - probatio.Optional(ATTR_TAG): probatio.All(probatio.EnsureList(), [str]), - probatio.Optional(ATTR_ALIAS): probatio.All( + probatio.Required(HabiticaServiceArgument.CONFIG_ENTRY): ConfigEntrySelector(), + probatio.Optional(HabiticaServiceArgument.RENAME): cv.string, + probatio.Optional(HabiticaServiceArgument.NOTES): cv.string, + probatio.Optional(HabiticaServiceArgument.TAG): probatio.All( + probatio.EnsureList(), [str] + ), + probatio.Optional(HabiticaServiceArgument.ALIAS): probatio.All( cv.string, cv.matches_regex("^[a-zA-Z0-9-_]*$") ), - probatio.Optional(ATTR_COST): probatio.All( + probatio.Optional(HabiticaServiceArgument.COST): probatio.All( probatio.Coerce(float), probatio.Range(0) ), - probatio.Optional(ATTR_PRIORITY): probatio.All( + probatio.Optional(HabiticaServiceArgument.PRIORITY): probatio.All( probatio.Upper, probatio.In(TaskPriority._member_names_) ), - probatio.Optional(ATTR_UP_DOWN): probatio.All(probatio.EnsureList(), [str]), - probatio.Optional(ATTR_COUNTER_UP): probatio.All(int, probatio.Range(0)), - probatio.Optional(ATTR_COUNTER_DOWN): probatio.All(int, probatio.Range(0)), - probatio.Optional(ATTR_FREQUENCY): probatio.Coerce(Frequency), - probatio.Optional(ATTR_DATE): cv.date, - probatio.Optional(ATTR_CLEAR_DATE): cv.boolean, - probatio.Optional(ATTR_REMINDER): probatio.All( + probatio.Optional(HabiticaServiceArgument.UP_DOWN): probatio.All( + probatio.EnsureList(), [str] + ), + probatio.Optional(HabiticaServiceArgument.COUNTER_UP): probatio.All( + int, probatio.Range(0) + ), + probatio.Optional(HabiticaServiceArgument.COUNTER_DOWN): probatio.All( + int, probatio.Range(0) + ), + probatio.Optional(HabiticaServiceArgument.FREQUENCY): probatio.Coerce( + Frequency + ), + probatio.Optional(HabiticaServiceArgument.DATE): cv.date, + probatio.Optional(HabiticaServiceArgument.CLEAR_DATE): cv.boolean, + probatio.Optional(HabiticaServiceArgument.REMINDER): probatio.All( probatio.EnsureList(), [probatio.Any(cv.datetime, cv.time)] ), - probatio.Optional(ATTR_REMOVE_REMINDER): probatio.All( + probatio.Optional(HabiticaServiceArgument.REMOVE_REMINDER): probatio.All( probatio.EnsureList(), [probatio.Any(cv.datetime, cv.time)] ), - probatio.Optional(ATTR_CLEAR_REMINDER): cv.boolean, - probatio.Optional(ATTR_ADD_CHECKLIST_ITEM): probatio.All( + probatio.Optional(HabiticaServiceArgument.CLEAR_REMINDER): cv.boolean, + probatio.Optional(HabiticaServiceArgument.ADD_CHECKLIST_ITEM): probatio.All( probatio.EnsureList(), [str] ), - probatio.Optional(ATTR_REMOVE_CHECKLIST_ITEM): probatio.All( + probatio.Optional(HabiticaServiceArgument.REMOVE_CHECKLIST_ITEM): probatio.All( probatio.EnsureList(), [str] ), - probatio.Optional(ATTR_SCORE_CHECKLIST_ITEM): probatio.All( + probatio.Optional(HabiticaServiceArgument.SCORE_CHECKLIST_ITEM): probatio.All( probatio.EnsureList(), [str] ), - probatio.Optional(ATTR_UNSCORE_CHECKLIST_ITEM): probatio.All( + probatio.Optional(HabiticaServiceArgument.UNSCORE_CHECKLIST_ITEM): probatio.All( probatio.EnsureList(), [str] ), - probatio.Optional(ATTR_COLLAPSE_CHECKLIST): probatio.In(COLLAPSE_CHECKLIST_MAP), - probatio.Optional(ATTR_START_DATE): cv.date, - probatio.Optional(ATTR_INTERVAL): probatio.All(int, probatio.Range(0)), - probatio.Optional(ATTR_REPEAT): probatio.All( + probatio.Optional(HabiticaServiceArgument.COLLAPSE_CHECKLIST): probatio.In( + COLLAPSE_CHECKLIST_MAP + ), + probatio.Optional(HabiticaServiceArgument.START_DATE): cv.date, + probatio.Optional(HabiticaServiceArgument.EVERY_X): probatio.All( + int, probatio.Range(0) + ), + probatio.Optional(HabiticaServiceArgument.REPEAT): probatio.All( probatio.EnsureList(), [probatio.In(WEEK_DAYS)] ), - probatio.Optional(ATTR_REPEAT_MONTHLY): probatio.All( + probatio.Optional(HabiticaServiceArgument.REPEAT_MONTHLY): probatio.All( cv.string, probatio.In({"day_of_month", "day_of_week"}) ), - probatio.Optional(ATTR_STREAK): probatio.All(int, probatio.Range(0)), + probatio.Optional(HabiticaServiceArgument.STREAK): probatio.All( + int, probatio.Range(0) + ), } ) SERVICE_UPDATE_TASK_SCHEMA = BASE_TASK_SCHEMA.extend( { - probatio.Required(ATTR_TASK): cv.string, - probatio.Optional(ATTR_REMOVE_TAG): probatio.All(probatio.EnsureList(), [str]), + probatio.Required(HabiticaServiceArgument.TASK): cv.string, + probatio.Optional(HabiticaServiceArgument.REMOVE_TAG): probatio.All( + probatio.EnsureList(), [str] + ), } ) SERVICE_CREATE_TASK_SCHEMA = BASE_TASK_SCHEMA.extend( { - probatio.Required(ATTR_NAME): cv.string, + probatio.Required(HabiticaServiceArgument.NAME): cv.string, } ) SERVICE_DAILY_SCHEMA = { - probatio.Optional(ATTR_REMINDER): probatio.All(probatio.EnsureList(), [cv.time]), - probatio.Optional(ATTR_REMOVE_REMINDER): probatio.All( + probatio.Optional(HabiticaServiceArgument.REMINDER): probatio.All( + probatio.EnsureList(), [cv.time] + ), + probatio.Optional(HabiticaServiceArgument.REMOVE_REMINDER): probatio.All( probatio.EnsureList(), [cv.time] ), } @@ -217,20 +246,24 @@ SERVICE_DAILY_SCHEMA = { SERVICE_GET_TASKS_SCHEMA = probatio.Schema( { - probatio.Required(ATTR_CONFIG_ENTRY): ConfigEntrySelector( + probatio.Required(HabiticaServiceArgument.CONFIG_ENTRY): ConfigEntrySelector( {"integration": DOMAIN} ), - probatio.Optional(ATTR_TYPE): probatio.All( + probatio.Optional(HabiticaServiceArgument.TYPE): probatio.All( probatio.EnsureList(), [probatio.All(probatio.Upper, probatio.In({x.name for x in TaskType}))], ), - probatio.Optional(ATTR_PRIORITY): probatio.All( + probatio.Optional(HabiticaServiceArgument.PRIORITY): probatio.All( probatio.EnsureList(), [probatio.All(probatio.Upper, probatio.In({x.name for x in TaskPriority}))], ), - probatio.Optional(ATTR_TASK): probatio.All(probatio.EnsureList(), [str]), - probatio.Optional(ATTR_TAG): probatio.All(probatio.EnsureList(), [str]), - probatio.Optional(ATTR_KEYWORD): cv.string, + probatio.Optional(HabiticaServiceArgument.TASK): probatio.All( + probatio.EnsureList(), [str] + ), + probatio.Optional(HabiticaServiceArgument.TAG): probatio.All( + probatio.EnsureList(), [str] + ), + probatio.Optional(HabiticaServiceArgument.KEYWORD): cv.string, } ) @@ -255,38 +288,41 @@ ITEMID_MAP = { SERVICE_TASK_TYPE_MAP = { - SERVICE_UPDATE_REWARD: TaskType.REWARD, - SERVICE_CREATE_REWARD: TaskType.REWARD, - SERVICE_UPDATE_HABIT: TaskType.HABIT, - SERVICE_CREATE_HABIT: TaskType.HABIT, - SERVICE_UPDATE_TODO: TaskType.TODO, - SERVICE_CREATE_TODO: TaskType.TODO, - SERVICE_UPDATE_DAILY: TaskType.DAILY, - SERVICE_CREATE_DAILY: TaskType.DAILY, + HabiticaService.UPDATE_REWARD: TaskType.REWARD, + HabiticaService.CREATE_REWARD: TaskType.REWARD, + HabiticaService.UPDATE_HABIT: TaskType.HABIT, + HabiticaService.CREATE_HABIT: TaskType.HABIT, + HabiticaService.UPDATE_TODO: TaskType.TODO, + HabiticaService.CREATE_TODO: TaskType.TODO, + HabiticaService.UPDATE_DAILY: TaskType.DAILY, + HabiticaService.CREATE_DAILY: TaskType.DAILY, } async def _cast_skill(call: ServiceCall) -> ServiceResponse: """Skill action.""" entry: HabiticaConfigEntry = service.async_get_config_entry( - call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY] + call.hass, DOMAIN, call.data[HabiticaServiceArgument.CONFIG_ENTRY] ) coordinator = entry.runtime_data - skill = SKILL_MAP[call.data[ATTR_SKILL]] - cost = COST_MAP[call.data[ATTR_SKILL]] + skill = SKILL_MAP[call.data[HabiticaServiceArgument.SKILL]] + cost = COST_MAP[call.data[HabiticaServiceArgument.SKILL]] try: task_id = next( task.id for task in coordinator.data.tasks - if call.data[ATTR_TASK] in (str(task.id), task.alias, task.text) + if call.data[HabiticaServiceArgument.TASK] + in (str(task.id), task.alias, task.text) ) except StopIteration as e: raise ServiceValidationError( translation_domain=DOMAIN, translation_key="task_not_found", - translation_placeholders={"task": f"'{call.data[ATTR_TASK]}'"}, + translation_placeholders={ + "task": f"'{call.data[HabiticaServiceArgument.TASK]}'" + }, ) from e try: @@ -313,7 +349,9 @@ async def _cast_skill(call: ServiceCall) -> ServiceResponse: raise ServiceValidationError( translation_domain=DOMAIN, translation_key="skill_not_found", - translation_placeholders={"skill": call.data[ATTR_SKILL]}, + translation_placeholders={ + "skill": call.data[HabiticaServiceArgument.SKILL] + }, ) from e except HabiticaException as e: raise HomeAssistantError( @@ -335,20 +373,20 @@ async def _cast_skill(call: ServiceCall) -> ServiceResponse: async def _manage_quests(call: ServiceCall) -> ServiceResponse: """Accept, reject, start, leave or cancel quests.""" entry: HabiticaConfigEntry = service.async_get_config_entry( - call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY] + call.hass, DOMAIN, call.data[HabiticaServiceArgument.CONFIG_ENTRY] ) coordinator = entry.runtime_data func_map = { - SERVICE_ABORT_QUEST: coordinator.habitica.abort_quest, - SERVICE_ACCEPT_QUEST: coordinator.habitica.accept_quest, - SERVICE_CANCEL_QUEST: coordinator.habitica.cancel_quest, - SERVICE_LEAVE_QUEST: coordinator.habitica.leave_quest, - SERVICE_REJECT_QUEST: coordinator.habitica.reject_quest, - SERVICE_START_QUEST: coordinator.habitica.start_quest, + HabiticaService.ABORT_QUEST: coordinator.habitica.abort_quest, + HabiticaService.ACCEPT_QUEST: coordinator.habitica.accept_quest, + HabiticaService.CANCEL_QUEST: coordinator.habitica.cancel_quest, + HabiticaService.LEAVE_QUEST: coordinator.habitica.leave_quest, + HabiticaService.REJECT_QUEST: coordinator.habitica.reject_quest, + HabiticaService.START_QUEST: coordinator.habitica.start_quest, } - func = func_map[call.service] + func = func_map[HabiticaService(call.service)] try: response = await func() @@ -385,24 +423,29 @@ async def _manage_quests(call: ServiceCall) -> ServiceResponse: async def _score_task(call: ServiceCall) -> ServiceResponse: """Score a task action.""" entry: HabiticaConfigEntry = service.async_get_config_entry( - call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY] + call.hass, DOMAIN, call.data[HabiticaServiceArgument.CONFIG_ENTRY] ) coordinator = entry.runtime_data direction = ( - Direction.DOWN if call.data.get(ATTR_DIRECTION) == "down" else Direction.UP + Direction.DOWN + if call.data.get(HabiticaServiceArgument.DIRECTION) == "down" + else Direction.UP ) try: task_id, task_value = next( (task.id, task.value) for task in coordinator.data.tasks - if call.data[ATTR_TASK] in (str(task.id), task.alias, task.text) + if call.data[HabiticaServiceArgument.TASK] + in (str(task.id), task.alias, task.text) ) except StopIteration as e: raise ServiceValidationError( translation_domain=DOMAIN, translation_key="task_not_found", - translation_placeholders={"task": f"'{call.data[ATTR_TASK]}'"}, + translation_placeholders={ + "task": f"'{call.data[HabiticaServiceArgument.TASK]}'" + }, ) from e if TYPE_CHECKING: @@ -451,13 +494,13 @@ async def _transformation(call: ServiceCall) -> ServiceResponse: """User a transformation item on a player character.""" entry: HabiticaConfigEntry = service.async_get_config_entry( - call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY] + call.hass, DOMAIN, call.data[HabiticaServiceArgument.CONFIG_ENTRY] ) coordinator = entry.runtime_data - item = ITEMID_MAP[call.data[ATTR_ITEM]] + item = ITEMID_MAP[call.data[HabiticaServiceArgument.ITEM]] # check if target is self - if call.data[ATTR_TARGET] in ( + if call.data[HabiticaServiceArgument.TARGET] in ( str(coordinator.data.user.id), coordinator.data.user.profile.name, coordinator.data.user.auth.local.username, @@ -489,7 +532,7 @@ async def _transformation(call: ServiceCall) -> ServiceResponse: member.id for member in party.data if member.id - and call.data[ATTR_TARGET].lower() + and call.data[HabiticaServiceArgument.TARGET].lower() in ( str(member.id), str(member.auth.local.username).lower(), @@ -500,7 +543,9 @@ async def _transformation(call: ServiceCall) -> ServiceResponse: raise ServiceValidationError( translation_domain=DOMAIN, translation_key="target_not_found", - translation_placeholders={"target": f"'{call.data[ATTR_TARGET]}'"}, + translation_placeholders={ + "target": f"'{call.data[HabiticaServiceArgument.TARGET]}'" + }, ) from e try: response = await coordinator.habitica.cast_skill(item, target_id) @@ -514,7 +559,7 @@ async def _transformation(call: ServiceCall) -> ServiceResponse: raise ServiceValidationError( translation_domain=DOMAIN, translation_key="item_not_found", - translation_placeholders={"item": call.data[ATTR_ITEM]}, + translation_placeholders={"item": call.data[HabiticaServiceArgument.ITEM]}, ) from e except HabiticaException as e: raise HomeAssistantError( @@ -536,25 +581,27 @@ async def _get_tasks(call: ServiceCall) -> ServiceResponse: """Get tasks action.""" entry: HabiticaConfigEntry = service.async_get_config_entry( - call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY] + call.hass, DOMAIN, call.data[HabiticaServiceArgument.CONFIG_ENTRY] ) coordinator = entry.runtime_data response: list[TaskData] = coordinator.data.tasks - if types := {TaskType[x] for x in call.data.get(ATTR_TYPE, [])}: + if types := {TaskType[x] for x in call.data.get(HabiticaServiceArgument.TYPE, [])}: response = [task for task in response if task.Type in types] - if priority := {TaskPriority[x] for x in call.data.get(ATTR_PRIORITY, [])}: + if priority := { + TaskPriority[x] for x in call.data.get(HabiticaServiceArgument.PRIORITY, []) + }: response = [task for task in response if task.priority in priority] - if tasks := call.data.get(ATTR_TASK): + if tasks := call.data.get(HabiticaServiceArgument.TASK): response = [ task for task in response if str(task.id) in tasks or task.alias in tasks or task.text in tasks ] - if tags := call.data.get(ATTR_TAG): + if tags := call.data.get(HabiticaServiceArgument.TAG): tag_ids = { tag.id for tag in coordinator.data.user.tags @@ -568,7 +615,7 @@ async def _get_tasks(call: ServiceCall) -> ServiceResponse: for task in response if any(tag_id in task.tags for tag_id in tag_ids if task.tags) ] - if keyword := call.data.get(ATTR_KEYWORD): + if keyword := call.data.get(HabiticaServiceArgument.KEYWORD): keyword = keyword.lower() response = [ task @@ -587,17 +634,17 @@ async def _get_tasks(call: ServiceCall) -> ServiceResponse: async def _create_or_update_task(call: ServiceCall) -> ServiceResponse: # noqa: C901 """Create or update task action.""" entry: HabiticaConfigEntry = service.async_get_config_entry( - call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY] + call.hass, DOMAIN, call.data[HabiticaServiceArgument.CONFIG_ENTRY] ) coordinator = entry.runtime_data await coordinator.async_refresh() is_update = call.service in ( - SERVICE_UPDATE_HABIT, - SERVICE_UPDATE_REWARD, - SERVICE_UPDATE_TODO, - SERVICE_UPDATE_DAILY, + HabiticaService.UPDATE_HABIT, + HabiticaService.UPDATE_REWARD, + HabiticaService.UPDATE_TODO, + HabiticaService.UPDATE_DAILY, ) - task_type = SERVICE_TASK_TYPE_MAP[call.service] + task_type = SERVICE_TASK_TYPE_MAP[HabiticaService(call.service)] current_task = None if is_update: @@ -605,14 +652,17 @@ async def _create_or_update_task(call: ServiceCall) -> ServiceResponse: # noqa: current_task = next( task for task in coordinator.data.tasks - if call.data[ATTR_TASK] in (str(task.id), task.alias, task.text) + if call.data[HabiticaServiceArgument.TASK] + in (str(task.id), task.alias, task.text) and task.Type is task_type ) except StopIteration as e: raise ServiceValidationError( translation_domain=DOMAIN, translation_key="task_not_found", - translation_placeholders={"task": f"'{call.data[ATTR_TASK]}'"}, + translation_placeholders={ + "task": f"'{call.data[HabiticaServiceArgument.TASK]}'" + }, ) from e data = Task() @@ -620,14 +670,16 @@ async def _create_or_update_task(call: ServiceCall) -> ServiceResponse: # noqa: if not is_update: data["type"] = task_type - if (text := call.data.get(ATTR_RENAME)) or (text := call.data.get(ATTR_NAME)): + if (text := call.data.get(HabiticaServiceArgument.RENAME)) or ( + text := call.data.get(HabiticaServiceArgument.NAME) + ): data["text"] = text - if (notes := call.data.get(ATTR_NOTES)) is not None: + if (notes := call.data.get(HabiticaServiceArgument.NOTES)) is not None: data["notes"] = notes - tags = cast(list[str], call.data.get(ATTR_TAG)) - remove_tags = cast(list[str], call.data.get(ATTR_REMOVE_TAG)) + tags = cast(list[str], call.data.get(HabiticaServiceArgument.TAG)) + remove_tags = cast(list[str], call.data.get(HabiticaServiceArgument.REMOVE_TAG)) if tags or remove_tags: update_tags = set(current_task.tags) if current_task else set() @@ -682,55 +734,61 @@ async def _create_or_update_task(call: ServiceCall) -> ServiceResponse: # noqa: data["tags"] = list(update_tags) - if (alias := call.data.get(ATTR_ALIAS)) is not None: + if (alias := call.data.get(HabiticaServiceArgument.ALIAS)) is not None: data["alias"] = alias - if (cost := call.data.get(ATTR_COST)) is not None: + if (cost := call.data.get(HabiticaServiceArgument.COST)) is not None: data["value"] = cost - if priority := call.data.get(ATTR_PRIORITY): + if priority := call.data.get(HabiticaServiceArgument.PRIORITY): data["priority"] = TaskPriority[priority] - if frequency := call.data.get(ATTR_FREQUENCY): + if frequency := call.data.get(HabiticaServiceArgument.FREQUENCY): data["frequency"] = frequency else: frequency = current_task.frequency if current_task else Frequency.WEEKLY - if up_down := call.data.get(ATTR_UP_DOWN): + if up_down := call.data.get(HabiticaServiceArgument.UP_DOWN): data["up"] = "up" in up_down data["down"] = "down" in up_down - if counter_up := call.data.get(ATTR_COUNTER_UP): + if counter_up := call.data.get(HabiticaServiceArgument.COUNTER_UP): data["counterUp"] = counter_up - if counter_down := call.data.get(ATTR_COUNTER_DOWN): + if counter_down := call.data.get(HabiticaServiceArgument.COUNTER_DOWN): data["counterDown"] = counter_down - if due_date := call.data.get(ATTR_DATE): + if due_date := call.data.get(HabiticaServiceArgument.DATE): data["date"] = datetime.combine(due_date, time()) - if call.data.get(ATTR_CLEAR_DATE): + if call.data.get(HabiticaServiceArgument.CLEAR_DATE): data["date"] = None checklist = current_task.checklist if current_task else [] - if add_checklist_item := call.data.get(ATTR_ADD_CHECKLIST_ITEM): + if add_checklist_item := call.data.get(HabiticaServiceArgument.ADD_CHECKLIST_ITEM): checklist.extend( Checklist(completed=False, id=uuid4(), text=item) for item in add_checklist_item if not any(i.text == item for i in checklist) ) - if remove_checklist_item := call.data.get(ATTR_REMOVE_CHECKLIST_ITEM): + if remove_checklist_item := call.data.get( + HabiticaServiceArgument.REMOVE_CHECKLIST_ITEM + ): checklist = [ item for item in checklist if item.text not in remove_checklist_item ] - if score_checklist_item := call.data.get(ATTR_SCORE_CHECKLIST_ITEM): + if score_checklist_item := call.data.get( + HabiticaServiceArgument.SCORE_CHECKLIST_ITEM + ): for item in checklist: if item.text in score_checklist_item: item.completed = True - if unscore_checklist_item := call.data.get(ATTR_UNSCORE_CHECKLIST_ITEM): + if unscore_checklist_item := call.data.get( + HabiticaServiceArgument.UNSCORE_CHECKLIST_ITEM + ): for item in checklist: if item.text in unscore_checklist_item: item.completed = False @@ -742,12 +800,12 @@ async def _create_or_update_task(call: ServiceCall) -> ServiceResponse: # noqa: ): data["checklist"] = checklist - if collapse_checklist := call.data.get(ATTR_COLLAPSE_CHECKLIST): + if collapse_checklist := call.data.get(HabiticaServiceArgument.COLLAPSE_CHECKLIST): data["collapseChecklist"] = COLLAPSE_CHECKLIST_MAP[collapse_checklist] reminders = current_task.reminders if current_task else [] - if add_reminders := call.data.get(ATTR_REMINDER): + if add_reminders := call.data.get(HabiticaServiceArgument.REMINDER): if task_type is TaskType.TODO: existing_reminder_datetimes = { r.time.replace(tzinfo=None) for r in reminders @@ -772,7 +830,7 @@ async def _create_or_update_task(call: ServiceCall) -> ServiceResponse: # noqa: if r not in existing_reminder_times ) - if remove_reminder := call.data.get(ATTR_REMOVE_REMINDER): + if remove_reminder := call.data.get(HabiticaServiceArgument.REMOVE_REMINDER): if task_type is TaskType.TODO: reminders = list( filter( @@ -791,13 +849,13 @@ async def _create_or_update_task(call: ServiceCall) -> ServiceResponse: # noqa: ) ) - if clear_reminders := call.data.get(ATTR_CLEAR_REMINDER): + if clear_reminders := call.data.get(HabiticaServiceArgument.CLEAR_REMINDER): reminders = [] if add_reminders or remove_reminder or clear_reminders: data["reminders"] = reminders - if start_date := call.data.get(ATTR_START_DATE): + if start_date := call.data.get(HabiticaServiceArgument.START_DATE): data["startDate"] = datetime.combine(start_date, time()) else: start_date = ( @@ -805,7 +863,7 @@ async def _create_or_update_task(call: ServiceCall) -> ServiceResponse: # noqa: if current_task and current_task.startDate else dt_util.start_of_local_day() ) - if repeat := call.data.get(ATTR_REPEAT): + if repeat := call.data.get(HabiticaServiceArgument.REPEAT): if frequency is Frequency.WEEKLY: data["repeat"] = Repeat(**{d: d in repeat for d in WEEK_DAYS}) else: @@ -813,7 +871,7 @@ async def _create_or_update_task(call: ServiceCall) -> ServiceResponse: # noqa: translation_domain=DOMAIN, translation_key="frequency_not_weekly", ) - if repeat_monthly := call.data.get(ATTR_REPEAT_MONTHLY): + if repeat_monthly := call.data.get(HabiticaServiceArgument.REPEAT_MONTHLY): if frequency is not Frequency.MONTHLY: raise ServiceValidationError( translation_domain=DOMAIN, @@ -832,10 +890,10 @@ async def _create_or_update_task(call: ServiceCall) -> ServiceResponse: # noqa: data["daysOfMonth"] = [start_date.day] data["weeksOfMonth"] = [] - if (interval := call.data.get(ATTR_INTERVAL)) is not None: + if (interval := call.data.get(HabiticaServiceArgument.EVERY_X)) is not None: data["everyX"] = interval - if (streak := call.data.get(ATTR_STREAK)) is not None: + if (streak := call.data.get(HabiticaServiceArgument.STREAK)) is not None: data["streak"] = streak try: @@ -877,12 +935,12 @@ def async_setup_services(hass: HomeAssistant) -> None: """Set up services for Habitica integration.""" for service_name in ( - SERVICE_ABORT_QUEST, - SERVICE_ACCEPT_QUEST, - SERVICE_CANCEL_QUEST, - SERVICE_LEAVE_QUEST, - SERVICE_REJECT_QUEST, - SERVICE_START_QUEST, + HabiticaService.ABORT_QUEST, + HabiticaService.ACCEPT_QUEST, + HabiticaService.CANCEL_QUEST, + HabiticaService.LEAVE_QUEST, + HabiticaService.REJECT_QUEST, + HabiticaService.START_QUEST, ): hass.services.async_register( DOMAIN, @@ -893,10 +951,10 @@ def async_setup_services(hass: HomeAssistant) -> None: ) for service_name in ( - SERVICE_UPDATE_DAILY, - SERVICE_UPDATE_HABIT, - SERVICE_UPDATE_REWARD, - SERVICE_UPDATE_TODO, + HabiticaService.UPDATE_DAILY, + HabiticaService.UPDATE_HABIT, + HabiticaService.UPDATE_REWARD, + HabiticaService.UPDATE_TODO, ): hass.services.async_register( DOMAIN, @@ -906,10 +964,10 @@ def async_setup_services(hass: HomeAssistant) -> None: supports_response=SupportsResponse.OPTIONAL, ) for service_name in ( - SERVICE_CREATE_DAILY, - SERVICE_CREATE_HABIT, - SERVICE_CREATE_REWARD, - SERVICE_CREATE_TODO, + HabiticaService.CREATE_DAILY, + HabiticaService.CREATE_HABIT, + HabiticaService.CREATE_REWARD, + HabiticaService.CREATE_TODO, ): hass.services.async_register( DOMAIN, @@ -921,7 +979,7 @@ def async_setup_services(hass: HomeAssistant) -> None: hass.services.async_register( DOMAIN, - SERVICE_CAST_SKILL, + HabiticaService.CAST_SKILL, _cast_skill, schema=SERVICE_CAST_SKILL_SCHEMA, supports_response=SupportsResponse.OPTIONAL, @@ -929,14 +987,14 @@ def async_setup_services(hass: HomeAssistant) -> None: hass.services.async_register( DOMAIN, - SERVICE_SCORE_HABIT, + HabiticaService.SCORE_HABIT, _score_task, schema=SERVICE_SCORE_TASK_SCHEMA, supports_response=SupportsResponse.OPTIONAL, ) hass.services.async_register( DOMAIN, - SERVICE_SCORE_REWARD, + HabiticaService.SCORE_REWARD, _score_task, schema=SERVICE_SCORE_TASK_SCHEMA, supports_response=SupportsResponse.OPTIONAL, @@ -944,14 +1002,14 @@ def async_setup_services(hass: HomeAssistant) -> None: hass.services.async_register( DOMAIN, - SERVICE_TRANSFORMATION, + HabiticaService.TRANSFORMATION, _transformation, schema=SERVICE_TRANSFORMATION_SCHEMA, supports_response=SupportsResponse.OPTIONAL, ) hass.services.async_register( DOMAIN, - SERVICE_GET_TASKS, + HabiticaService.GET_TASKS, _get_tasks, schema=SERVICE_GET_TASKS_SCHEMA, supports_response=SupportsResponse.ONLY, diff --git a/tests/components/habitica/test_services.py b/tests/components/habitica/test_services.py index 831aec02b5d3..6c632ccb1b26 100644 --- a/tests/components/habitica/test_services.py +++ b/tests/components/habitica/test_services.py @@ -23,63 +23,12 @@ import pytest from pytest_homeassistant.common import MockConfigEntry, async_load_fixture from syrupy.assertion import SnapshotAssertion -from homeassistant.components.habitica.const import ( - ATTR_ADD_CHECKLIST_ITEM, - ATTR_ALIAS, - ATTR_CLEAR_DATE, - ATTR_CLEAR_REMINDER, - ATTR_COLLAPSE_CHECKLIST, - ATTR_CONFIG_ENTRY, - ATTR_COST, - ATTR_COUNTER_DOWN, - ATTR_COUNTER_UP, - ATTR_DIRECTION, - ATTR_FREQUENCY, - ATTR_INTERVAL, - ATTR_ITEM, - ATTR_KEYWORD, - ATTR_NOTES, - ATTR_PRIORITY, - ATTR_REMINDER, - ATTR_REMOVE_CHECKLIST_ITEM, - ATTR_REMOVE_REMINDER, - ATTR_REMOVE_TAG, - ATTR_REPEAT, - ATTR_REPEAT_MONTHLY, - ATTR_SCORE_CHECKLIST_ITEM, - ATTR_SKILL, - ATTR_START_DATE, - ATTR_STREAK, - ATTR_TAG, - ATTR_TARGET, - ATTR_TASK, - ATTR_TYPE, - ATTR_UNSCORE_CHECKLIST_ITEM, - ATTR_UP_DOWN, - DOMAIN, - SERVICE_ABORT_QUEST, - SERVICE_ACCEPT_QUEST, - SERVICE_CANCEL_QUEST, - SERVICE_CAST_SKILL, - SERVICE_CREATE_DAILY, - SERVICE_CREATE_HABIT, - SERVICE_CREATE_REWARD, - SERVICE_CREATE_TODO, - SERVICE_GET_TASKS, - SERVICE_LEAVE_QUEST, - SERVICE_REJECT_QUEST, - SERVICE_SCORE_HABIT, - SERVICE_SCORE_REWARD, - SERVICE_START_QUEST, - SERVICE_TRANSFORMATION, - SERVICE_UPDATE_DAILY, - SERVICE_UPDATE_HABIT, - SERVICE_UPDATE_REWARD, - SERVICE_UPDATE_TODO, +from homeassistant.components.habitica.const import DOMAIN +from homeassistant.components.habitica.services import ( + HabiticaService, + HabiticaServiceArgument, ) -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 from homeassistant.exceptions import HomeAssistantError, ServiceValidationError @@ -136,8 +85,8 @@ def uuid_mock() -> Generator[None]: [ ( { - ATTR_TASK: "2f6fcabc-f670-4ec3-ba65-817e8deea490", - ATTR_SKILL: "pickpocket", + HabiticaServiceArgument.TASK: "2f6fcabc-f670-4ec3-ba65-817e8deea490", + HabiticaServiceArgument.SKILL: "pickpocket", }, { "skill": Skill.PICKPOCKET, @@ -146,8 +95,8 @@ def uuid_mock() -> Generator[None]: ), ( { - ATTR_TASK: "2f6fcabc-f670-4ec3-ba65-817e8deea490", - ATTR_SKILL: "backstab", + HabiticaServiceArgument.TASK: "2f6fcabc-f670-4ec3-ba65-817e8deea490", + HabiticaServiceArgument.SKILL: "backstab", }, { "skill": Skill.BACKSTAB, @@ -156,8 +105,8 @@ def uuid_mock() -> Generator[None]: ), ( { - ATTR_TASK: "2f6fcabc-f670-4ec3-ba65-817e8deea490", - ATTR_SKILL: "fireball", + HabiticaServiceArgument.TASK: "2f6fcabc-f670-4ec3-ba65-817e8deea490", + HabiticaServiceArgument.SKILL: "fireball", }, { "skill": Skill.BURST_OF_FLAMES, @@ -166,8 +115,8 @@ def uuid_mock() -> Generator[None]: ), ( { - ATTR_TASK: "2f6fcabc-f670-4ec3-ba65-817e8deea490", - ATTR_SKILL: "smash", + HabiticaServiceArgument.TASK: "2f6fcabc-f670-4ec3-ba65-817e8deea490", + HabiticaServiceArgument.SKILL: "smash", }, { "skill": Skill.BRUTAL_SMASH, @@ -176,8 +125,8 @@ def uuid_mock() -> Generator[None]: ), ( { - ATTR_TASK: "Rechnungen bezahlen", - ATTR_SKILL: "smash", + HabiticaServiceArgument.TASK: "Rechnungen bezahlen", + HabiticaServiceArgument.SKILL: "smash", }, { "skill": Skill.BRUTAL_SMASH, @@ -186,8 +135,8 @@ def uuid_mock() -> Generator[None]: ), ( { - ATTR_TASK: "pay_bills", - ATTR_SKILL: "smash", + HabiticaServiceArgument.TASK: "pay_bills", + HabiticaServiceArgument.SKILL: "smash", }, { "skill": Skill.BRUTAL_SMASH, @@ -215,9 +164,9 @@ async def test_cast_skill( await hass.services.async_call( DOMAIN, - SERVICE_CAST_SKILL, + HabiticaService.CAST_SKILL, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, **service_data, }, return_response=True, @@ -236,8 +185,8 @@ async def test_cast_skill( [ ( { - ATTR_TASK: "task-not-found", - ATTR_SKILL: "smash", + HabiticaServiceArgument.TASK: "task-not-found", + HabiticaServiceArgument.SKILL: "smash", }, None, ServiceValidationError, @@ -245,8 +194,8 @@ async def test_cast_skill( ), ( { - ATTR_TASK: "Rechnungen bezahlen", - ATTR_SKILL: "smash", + HabiticaServiceArgument.TASK: "Rechnungen bezahlen", + HabiticaServiceArgument.SKILL: "smash", }, ERROR_TOO_MANY_REQUESTS, HomeAssistantError, @@ -254,8 +203,8 @@ async def test_cast_skill( ), ( { - ATTR_TASK: "Rechnungen bezahlen", - ATTR_SKILL: "smash", + HabiticaServiceArgument.TASK: "Rechnungen bezahlen", + HabiticaServiceArgument.SKILL: "smash", }, ERROR_NOT_FOUND, ServiceValidationError, @@ -264,8 +213,8 @@ async def test_cast_skill( ), ( { - ATTR_TASK: "Rechnungen bezahlen", - ATTR_SKILL: "smash", + HabiticaServiceArgument.TASK: "Rechnungen bezahlen", + HabiticaServiceArgument.SKILL: "smash", }, ERROR_NOT_AUTHORIZED, ServiceValidationError, @@ -274,8 +223,8 @@ async def test_cast_skill( ), ( { - ATTR_TASK: "Rechnungen bezahlen", - ATTR_SKILL: "smash", + HabiticaServiceArgument.TASK: "Rechnungen bezahlen", + HabiticaServiceArgument.SKILL: "smash", }, ERROR_BAD_REQUEST, HomeAssistantError, @@ -283,8 +232,8 @@ async def test_cast_skill( ), ( { - ATTR_TASK: "Rechnungen bezahlen", - ATTR_SKILL: "smash", + HabiticaServiceArgument.TASK: "Rechnungen bezahlen", + HabiticaServiceArgument.SKILL: "smash", }, ClientError, HomeAssistantError, @@ -307,9 +256,9 @@ async def test_cast_skill_exceptions( with pytest.raises(expected_exception, match=expected_exception_msg): await hass.services.async_call( DOMAIN, - SERVICE_CAST_SKILL, + HabiticaService.CAST_SKILL, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, **service_data, }, return_response=True, @@ -326,11 +275,11 @@ async def test_get_config_entry( with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, - SERVICE_CAST_SKILL, + HabiticaService.CAST_SKILL, service_data={ - ATTR_CONFIG_ENTRY: "0000000000000000", - ATTR_TASK: "2f6fcabc-f670-4ec3-ba65-817e8deea490", - ATTR_SKILL: "smash", + HabiticaServiceArgument.CONFIG_ENTRY: "0000000000000000", + HabiticaServiceArgument.TASK: "2f6fcabc-f670-4ec3-ba65-817e8deea490", + HabiticaServiceArgument.SKILL: "smash", }, return_response=True, blocking=True, @@ -342,11 +291,11 @@ async def test_get_config_entry( with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, - SERVICE_CAST_SKILL, + HabiticaService.CAST_SKILL, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, - ATTR_TASK: "2f6fcabc-f670-4ec3-ba65-817e8deea490", - ATTR_SKILL: "smash", + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.TASK: "2f6fcabc-f670-4ec3-ba65-817e8deea490", + HabiticaServiceArgument.SKILL: "smash", }, return_response=True, blocking=True, @@ -357,12 +306,12 @@ async def test_get_config_entry( @pytest.mark.parametrize( "service", [ - SERVICE_ABORT_QUEST, - SERVICE_ACCEPT_QUEST, - SERVICE_CANCEL_QUEST, - SERVICE_LEAVE_QUEST, - SERVICE_REJECT_QUEST, - SERVICE_START_QUEST, + HabiticaService.ABORT_QUEST, + HabiticaService.ACCEPT_QUEST, + HabiticaService.CANCEL_QUEST, + HabiticaService.LEAVE_QUEST, + HabiticaService.REJECT_QUEST, + HabiticaService.START_QUEST, ], ) async def test_handle_quests( @@ -376,7 +325,7 @@ async def test_handle_quests( await hass.services.async_call( DOMAIN, service, - service_data={ATTR_CONFIG_ENTRY: config_entry.entry_id}, + service_data={HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id}, return_response=True, blocking=True, ) @@ -422,12 +371,12 @@ async def test_handle_quests( @pytest.mark.parametrize( "service", [ - SERVICE_ACCEPT_QUEST, - SERVICE_ABORT_QUEST, - SERVICE_CANCEL_QUEST, - SERVICE_LEAVE_QUEST, - SERVICE_REJECT_QUEST, - SERVICE_START_QUEST, + HabiticaService.ACCEPT_QUEST, + HabiticaService.ABORT_QUEST, + HabiticaService.CANCEL_QUEST, + HabiticaService.LEAVE_QUEST, + HabiticaService.REJECT_QUEST, + HabiticaService.START_QUEST, ], ) async def test_handle_quests_exceptions( @@ -446,7 +395,7 @@ async def test_handle_quests_exceptions( await hass.services.async_call( DOMAIN, service, - service_data={ATTR_CONFIG_ENTRY: config_entry.entry_id}, + service_data={HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id}, return_response=True, blocking=True, ) @@ -456,10 +405,10 @@ async def test_handle_quests_exceptions( ("service", "service_data", "call_args"), [ ( - SERVICE_SCORE_HABIT, + HabiticaService.SCORE_HABIT, { - ATTR_TASK: "e97659e0-2c42-4599-a7bb-00282adc410d", - ATTR_DIRECTION: "up", + HabiticaServiceArgument.TASK: "e97659e0-2c42-4599-a7bb-00282adc410d", + HabiticaServiceArgument.DIRECTION: "up", }, { "task_id": UUID("e97659e0-2c42-4599-a7bb-00282adc410d"), @@ -467,10 +416,10 @@ async def test_handle_quests_exceptions( }, ), ( - SERVICE_SCORE_HABIT, + HabiticaService.SCORE_HABIT, { - ATTR_TASK: "e97659e0-2c42-4599-a7bb-00282adc410d", - ATTR_DIRECTION: "down", + HabiticaServiceArgument.TASK: "e97659e0-2c42-4599-a7bb-00282adc410d", + HabiticaServiceArgument.DIRECTION: "down", }, { "task_id": UUID("e97659e0-2c42-4599-a7bb-00282adc410d"), @@ -478,9 +427,9 @@ async def test_handle_quests_exceptions( }, ), ( - SERVICE_SCORE_REWARD, + HabiticaService.SCORE_REWARD, { - ATTR_TASK: "5e2ea1df-f6e6-4ba3-bccb-97c5ec63e99b", + HabiticaServiceArgument.TASK: "5e2ea1df-f6e6-4ba3-bccb-97c5ec63e99b", }, { "task_id": UUID("5e2ea1df-f6e6-4ba3-bccb-97c5ec63e99b"), @@ -488,10 +437,10 @@ async def test_handle_quests_exceptions( }, ), ( - SERVICE_SCORE_HABIT, + HabiticaService.SCORE_HABIT, { - ATTR_TASK: "Füge eine Aufgabe zu Habitica hinzu", - ATTR_DIRECTION: "up", + HabiticaServiceArgument.TASK: "Füge eine Aufgabe zu Habitica hinzu", + HabiticaServiceArgument.DIRECTION: "up", }, { "task_id": UUID("e97659e0-2c42-4599-a7bb-00282adc410d"), @@ -499,10 +448,10 @@ async def test_handle_quests_exceptions( }, ), ( - SERVICE_SCORE_HABIT, + HabiticaService.SCORE_HABIT, { - ATTR_TASK: "create_a_task", - ATTR_DIRECTION: "up", + HabiticaServiceArgument.TASK: "create_a_task", + HabiticaServiceArgument.DIRECTION: "up", }, { "task_id": UUID("e97659e0-2c42-4599-a7bb-00282adc410d"), @@ -532,7 +481,7 @@ async def test_score_task( DOMAIN, service, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, **service_data, }, return_response=True, @@ -552,8 +501,8 @@ async def test_score_task( [ ( { - ATTR_TASK: "task does not exist", - ATTR_DIRECTION: "up", + HabiticaServiceArgument.TASK: "task does not exist", + HabiticaServiceArgument.DIRECTION: "up", }, None, ServiceValidationError, @@ -561,8 +510,8 @@ async def test_score_task( ), ( { - ATTR_TASK: "e97659e0-2c42-4599-a7bb-00282adc410d", - ATTR_DIRECTION: "up", + HabiticaServiceArgument.TASK: "e97659e0-2c42-4599-a7bb-00282adc410d", + HabiticaServiceArgument.DIRECTION: "up", }, ERROR_TOO_MANY_REQUESTS, HomeAssistantError, @@ -570,8 +519,8 @@ async def test_score_task( ), ( { - ATTR_TASK: "e97659e0-2c42-4599-a7bb-00282adc410d", - ATTR_DIRECTION: "up", + HabiticaServiceArgument.TASK: "e97659e0-2c42-4599-a7bb-00282adc410d", + HabiticaServiceArgument.DIRECTION: "up", }, ERROR_BAD_REQUEST, HomeAssistantError, @@ -579,8 +528,8 @@ async def test_score_task( ), ( { - ATTR_TASK: "e97659e0-2c42-4599-a7bb-00282adc410d", - ATTR_DIRECTION: "up", + HabiticaServiceArgument.TASK: "e97659e0-2c42-4599-a7bb-00282adc410d", + HabiticaServiceArgument.DIRECTION: "up", }, ClientError, HomeAssistantError, @@ -588,8 +537,8 @@ async def test_score_task( ), ( { - ATTR_TASK: "5e2ea1df-f6e6-4ba3-bccb-97c5ec63e99b", - ATTR_DIRECTION: "up", + HabiticaServiceArgument.TASK: "5e2ea1df-f6e6-4ba3-bccb-97c5ec63e99b", + HabiticaServiceArgument.DIRECTION: "up", }, ERROR_NOT_AUTHORIZED, HomeAssistantError, @@ -613,9 +562,9 @@ async def test_score_task_exceptions( with pytest.raises(expected_exception, match=expected_exception_msg): await hass.services.async_call( DOMAIN, - SERVICE_SCORE_HABIT, + HabiticaService.SCORE_HABIT, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, **service_data, }, return_response=True, @@ -628,8 +577,8 @@ async def test_score_task_exceptions( [ ( { - ATTR_TARGET: "a380546a-94be-4b8e-8a0b-23e0d5c03303", - ATTR_ITEM: "spooky_sparkles", + HabiticaServiceArgument.TARGET: "a380546a-94be-4b8e-8a0b-23e0d5c03303", + HabiticaServiceArgument.ITEM: "spooky_sparkles", }, { "skill": Skill.SPOOKY_SPARKLES, @@ -638,8 +587,8 @@ async def test_score_task_exceptions( ), ( { - ATTR_TARGET: "a380546a-94be-4b8e-8a0b-23e0d5c03303", - ATTR_ITEM: "shiny_seed", + HabiticaServiceArgument.TARGET: "a380546a-94be-4b8e-8a0b-23e0d5c03303", + HabiticaServiceArgument.ITEM: "shiny_seed", }, { "skill": Skill.SHINY_SEED, @@ -648,8 +597,8 @@ async def test_score_task_exceptions( ), ( { - ATTR_TARGET: "a380546a-94be-4b8e-8a0b-23e0d5c03303", - ATTR_ITEM: "seafoam", + HabiticaServiceArgument.TARGET: "a380546a-94be-4b8e-8a0b-23e0d5c03303", + HabiticaServiceArgument.ITEM: "seafoam", }, { "skill": Skill.SEAFOAM, @@ -658,8 +607,8 @@ async def test_score_task_exceptions( ), ( { - ATTR_TARGET: "a380546a-94be-4b8e-8a0b-23e0d5c03303", - ATTR_ITEM: "snowball", + HabiticaServiceArgument.TARGET: "a380546a-94be-4b8e-8a0b-23e0d5c03303", + HabiticaServiceArgument.ITEM: "snowball", }, { "skill": Skill.SNOWBALL, @@ -668,8 +617,8 @@ async def test_score_task_exceptions( ), ( { - ATTR_TARGET: "test-user", - ATTR_ITEM: "spooky_sparkles", + HabiticaServiceArgument.TARGET: "test-user", + HabiticaServiceArgument.ITEM: "spooky_sparkles", }, { "skill": Skill.SPOOKY_SPARKLES, @@ -678,8 +627,8 @@ async def test_score_task_exceptions( ), ( { - ATTR_TARGET: "test-username", - ATTR_ITEM: "spooky_sparkles", + HabiticaServiceArgument.TARGET: "test-username", + HabiticaServiceArgument.ITEM: "spooky_sparkles", }, { "skill": Skill.SPOOKY_SPARKLES, @@ -688,8 +637,8 @@ async def test_score_task_exceptions( ), ( { - ATTR_TARGET: "ffce870c-3ff3-4fa4-bad1-87612e52b8e7", - ATTR_ITEM: "spooky_sparkles", + HabiticaServiceArgument.TARGET: "ffce870c-3ff3-4fa4-bad1-87612e52b8e7", + HabiticaServiceArgument.ITEM: "spooky_sparkles", }, { "skill": Skill.SPOOKY_SPARKLES, @@ -698,8 +647,8 @@ async def test_score_task_exceptions( ), ( { - ATTR_TARGET: "test-partymember-username", - ATTR_ITEM: "spooky_sparkles", + HabiticaServiceArgument.TARGET: "test-partymember-username", + HabiticaServiceArgument.ITEM: "spooky_sparkles", }, { "skill": Skill.SPOOKY_SPARKLES, @@ -708,8 +657,8 @@ async def test_score_task_exceptions( ), ( { - ATTR_TARGET: "test-partymember-displayname", - ATTR_ITEM: "spooky_sparkles", + HabiticaServiceArgument.TARGET: "test-partymember-displayname", + HabiticaServiceArgument.ITEM: "spooky_sparkles", }, { "skill": Skill.SPOOKY_SPARKLES, @@ -740,9 +689,9 @@ async def test_transformation( await hass.services.async_call( DOMAIN, - SERVICE_TRANSFORMATION, + HabiticaService.TRANSFORMATION, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, **service_data, }, return_response=True, @@ -763,8 +712,8 @@ async def test_transformation( [ ( { - ATTR_TARGET: "user-not-found", - ATTR_ITEM: "spooky_sparkles", + HabiticaServiceArgument.TARGET: "user-not-found", + HabiticaServiceArgument.ITEM: "spooky_sparkles", }, None, None, @@ -773,8 +722,8 @@ async def test_transformation( ), ( { - ATTR_TARGET: "test-partymember-username", - ATTR_ITEM: "spooky_sparkles", + HabiticaServiceArgument.TARGET: "test-partymember-username", + HabiticaServiceArgument.ITEM: "spooky_sparkles", }, ERROR_NOT_FOUND, None, @@ -784,8 +733,8 @@ async def test_transformation( ), ( { - ATTR_TARGET: "test-partymember-username", - ATTR_ITEM: "spooky_sparkles", + HabiticaServiceArgument.TARGET: "test-partymember-username", + HabiticaServiceArgument.ITEM: "spooky_sparkles", }, ERROR_BAD_REQUEST, None, @@ -794,8 +743,8 @@ async def test_transformation( ), ( { - ATTR_TARGET: "test-partymember-username", - ATTR_ITEM: "spooky_sparkles", + HabiticaServiceArgument.TARGET: "test-partymember-username", + HabiticaServiceArgument.ITEM: "spooky_sparkles", }, None, ERROR_TOO_MANY_REQUESTS, @@ -804,8 +753,8 @@ async def test_transformation( ), ( { - ATTR_TARGET: "test-partymember-username", - ATTR_ITEM: "spooky_sparkles", + HabiticaServiceArgument.TARGET: "test-partymember-username", + HabiticaServiceArgument.ITEM: "spooky_sparkles", }, None, ERROR_NOT_AUTHORIZED, @@ -814,8 +763,8 @@ async def test_transformation( ), ( { - ATTR_TARGET: "test-partymember-username", - ATTR_ITEM: "spooky_sparkles", + HabiticaServiceArgument.TARGET: "test-partymember-username", + HabiticaServiceArgument.ITEM: "spooky_sparkles", }, None, ERROR_BAD_REQUEST, @@ -824,8 +773,8 @@ async def test_transformation( ), ( { - ATTR_TARGET: "test-partymember-username", - ATTR_ITEM: "spooky_sparkles", + HabiticaServiceArgument.TARGET: "test-partymember-username", + HabiticaServiceArgument.ITEM: "spooky_sparkles", }, None, ClientError, @@ -834,8 +783,8 @@ async def test_transformation( ), ( { - ATTR_TARGET: "test-partymember-username", - ATTR_ITEM: "spooky_sparkles", + HabiticaServiceArgument.TARGET: "test-partymember-username", + HabiticaServiceArgument.ITEM: "spooky_sparkles", }, ClientError, None, @@ -861,9 +810,9 @@ async def test_transformation_exceptions( with pytest.raises(expected_exception, match=expected_exception_msg): await hass.services.async_call( DOMAIN, - SERVICE_TRANSFORMATION, + HabiticaService.TRANSFORMATION, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, **service_data, }, return_response=True, @@ -875,22 +824,27 @@ async def test_transformation_exceptions( ("service_data"), [ {}, - {ATTR_TYPE: ["daily"]}, - {ATTR_TYPE: ["habit"]}, - {ATTR_TYPE: ["todo"]}, - {ATTR_TYPE: ["reward"]}, - {ATTR_TYPE: ["daily", "habit"]}, - {ATTR_TYPE: ["todo", "reward"]}, - {ATTR_PRIORITY: "trivial"}, - {ATTR_PRIORITY: "easy"}, - {ATTR_PRIORITY: "medium"}, - {ATTR_PRIORITY: "hard"}, - {ATTR_TASK: ["Zahnseide benutzen", "Eine kurze Pause machen"]}, - {ATTR_TASK: ["f2c85972-1a19-4426-bc6d-ce3337b9d99f"]}, - {ATTR_TASK: ["alias_zahnseide_benutzen"]}, - {ATTR_TAG: ["Training", "Gesundheit + Wohlbefinden"]}, - {ATTR_KEYWORD: "gewohnheit"}, - {ATTR_TAG: ["Home Assistant"]}, + {HabiticaServiceArgument.TYPE: ["daily"]}, + {HabiticaServiceArgument.TYPE: ["habit"]}, + {HabiticaServiceArgument.TYPE: ["todo"]}, + {HabiticaServiceArgument.TYPE: ["reward"]}, + {HabiticaServiceArgument.TYPE: ["daily", "habit"]}, + {HabiticaServiceArgument.TYPE: ["todo", "reward"]}, + {HabiticaServiceArgument.PRIORITY: "trivial"}, + {HabiticaServiceArgument.PRIORITY: "easy"}, + {HabiticaServiceArgument.PRIORITY: "medium"}, + {HabiticaServiceArgument.PRIORITY: "hard"}, + { + HabiticaServiceArgument.TASK: [ + "Zahnseide benutzen", + "Eine kurze Pause machen", + ] + }, + {HabiticaServiceArgument.TASK: ["f2c85972-1a19-4426-bc6d-ce3337b9d99f"]}, + {HabiticaServiceArgument.TASK: ["alias_zahnseide_benutzen"]}, + {HabiticaServiceArgument.TAG: ["Training", "Gesundheit + Wohlbefinden"]}, + {HabiticaServiceArgument.KEYWORD: "gewohnheit"}, + {HabiticaServiceArgument.TAG: ["Home Assistant"]}, ], ids=[ "all_tasks", @@ -923,9 +877,9 @@ async def test_get_tasks( response = await hass.services.async_call( DOMAIN, - SERVICE_GET_TASKS, + HabiticaService.GET_TASKS, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, **service_data, }, return_response=True, @@ -958,10 +912,10 @@ async def test_get_tasks( @pytest.mark.parametrize( ("service", "task_id"), [ - (SERVICE_UPDATE_REWARD, "5e2ea1df-f6e6-4ba3-bccb-97c5ec63e99b"), - (SERVICE_UPDATE_HABIT, "f21fa608-cfc6-4413-9fc7-0eb1b48ca43a"), - (SERVICE_UPDATE_TODO, "88de7cd9-af2b-49ce-9afd-bf941d87336b"), - (SERVICE_UPDATE_DAILY, "6e53f1f5-a315-4edd-984d-8d762e4a08ef"), + (HabiticaService.UPDATE_REWARD, "5e2ea1df-f6e6-4ba3-bccb-97c5ec63e99b"), + (HabiticaService.UPDATE_HABIT, "f21fa608-cfc6-4413-9fc7-0eb1b48ca43a"), + (HabiticaService.UPDATE_TODO, "88de7cd9-af2b-49ce-9afd-bf941d87336b"), + (HabiticaService.UPDATE_DAILY, "6e53f1f5-a315-4edd-984d-8d762e4a08ef"), ], ) @pytest.mark.usefixtures("habitica") @@ -983,8 +937,8 @@ async def test_update_task_exceptions( DOMAIN, service, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, - ATTR_TASK: task_id, + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.TASK: task_id, }, return_response=True, blocking=True, @@ -1014,10 +968,10 @@ async def test_update_task_exceptions( @pytest.mark.parametrize( "service", [ - SERVICE_CREATE_DAILY, - SERVICE_CREATE_HABIT, - SERVICE_CREATE_REWARD, - SERVICE_CREATE_TODO, + HabiticaService.CREATE_DAILY, + HabiticaService.CREATE_HABIT, + HabiticaService.CREATE_REWARD, + HabiticaService.CREATE_TODO, ], ) @pytest.mark.usefixtures("habitica") @@ -1038,8 +992,8 @@ async def test_create_task_exceptions( DOMAIN, service, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, - ATTR_NAME: "TITLE", + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.NAME: "TITLE", }, return_response=True, blocking=True, @@ -1064,10 +1018,10 @@ async def test_task_not_found( ): await hass.services.async_call( DOMAIN, - SERVICE_UPDATE_REWARD, + HabiticaService.UPDATE_REWARD, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, - ATTR_TASK: task_id, + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.TASK: task_id, }, return_response=True, blocking=True, @@ -1079,25 +1033,25 @@ async def test_task_not_found( [ ( { - ATTR_COST: 100, + HabiticaServiceArgument.COST: 100, }, Task(value=100), ), ( { - ATTR_RENAME: "RENAME", + HabiticaServiceArgument.RENAME: "RENAME", }, Task(text="RENAME"), ), ( { - ATTR_NOTES: "NOTES", + HabiticaServiceArgument.NOTES: "NOTES", }, Task(notes="NOTES"), ), ( { - ATTR_ALIAS: "ALIAS", + HabiticaServiceArgument.ALIAS: "ALIAS", }, Task(alias="ALIAS"), ), @@ -1118,10 +1072,10 @@ async def test_update_reward( ) await hass.services.async_call( DOMAIN, - SERVICE_UPDATE_REWARD, + HabiticaService.UPDATE_REWARD, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, - ATTR_TASK: task_id, + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.TASK: task_id, **service_data, }, return_response=True, @@ -1135,28 +1089,28 @@ async def test_update_reward( [ ( { - ATTR_NAME: "TITLE", - ATTR_COST: 100, + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.COST: 100, }, Task(type=TaskType.REWARD, text="TITLE", value=100), ), ( { - ATTR_NAME: "TITLE", + HabiticaServiceArgument.NAME: "TITLE", }, Task(type=TaskType.REWARD, text="TITLE"), ), ( { - ATTR_NAME: "TITLE", - ATTR_NOTES: "NOTES", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.NOTES: "NOTES", }, Task(type=TaskType.REWARD, text="TITLE", notes="NOTES"), ), ( { - ATTR_NAME: "TITLE", - ATTR_ALIAS: "ALIAS", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.ALIAS: "ALIAS", }, Task(type=TaskType.REWARD, text="TITLE", alias="ALIAS"), ), @@ -1173,9 +1127,9 @@ async def test_create_reward( await hass.services.async_call( DOMAIN, - SERVICE_CREATE_REWARD, + HabiticaService.CREATE_REWARD, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, **service_data, }, return_response=True, @@ -1189,56 +1143,56 @@ async def test_create_reward( [ ( { - ATTR_RENAME: "RENAME", + HabiticaServiceArgument.RENAME: "RENAME", }, Task(text="RENAME"), ), ( { - ATTR_NOTES: "NOTES", + HabiticaServiceArgument.NOTES: "NOTES", }, Task(notes="NOTES"), ), ( { - ATTR_UP_DOWN: [""], + HabiticaServiceArgument.UP_DOWN: [""], }, Task(up=False, down=False), ), ( { - ATTR_UP_DOWN: ["up"], + HabiticaServiceArgument.UP_DOWN: ["up"], }, Task(up=True, down=False), ), ( { - ATTR_UP_DOWN: ["down"], + HabiticaServiceArgument.UP_DOWN: ["down"], }, Task(up=False, down=True), ), ( { - ATTR_PRIORITY: "trivial", + HabiticaServiceArgument.PRIORITY: "trivial", }, Task(priority=TaskPriority.TRIVIAL), ), ( { - ATTR_FREQUENCY: "daily", + HabiticaServiceArgument.FREQUENCY: "daily", }, Task(frequency=Frequency.DAILY), ), ( { - ATTR_COUNTER_UP: 1, - ATTR_COUNTER_DOWN: 2, + HabiticaServiceArgument.COUNTER_UP: 1, + HabiticaServiceArgument.COUNTER_DOWN: 2, }, Task(counterUp=1, counterDown=2), ), ( { - ATTR_ALIAS: "ALIAS", + HabiticaServiceArgument.ALIAS: "ALIAS", }, Task(alias="ALIAS"), ), @@ -1256,10 +1210,10 @@ async def test_update_habit( await hass.services.async_call( DOMAIN, - SERVICE_UPDATE_HABIT, + HabiticaService.UPDATE_HABIT, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, - ATTR_TASK: task_id, + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.TASK: task_id, **service_data, }, return_response=True, @@ -1273,56 +1227,56 @@ async def test_update_habit( [ ( { - ATTR_NAME: "TITLE", + HabiticaServiceArgument.NAME: "TITLE", }, Task(type=TaskType.HABIT, text="TITLE"), ), ( { - ATTR_NAME: "TITLE", - ATTR_NOTES: "NOTES", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.NOTES: "NOTES", }, Task(type=TaskType.HABIT, text="TITLE", notes="NOTES"), ), ( { - ATTR_NAME: "TITLE", - ATTR_UP_DOWN: [""], + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.UP_DOWN: [""], }, Task(type=TaskType.HABIT, text="TITLE", up=False, down=False), ), ( { - ATTR_NAME: "TITLE", - ATTR_UP_DOWN: ["up"], + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.UP_DOWN: ["up"], }, Task(type=TaskType.HABIT, text="TITLE", up=True, down=False), ), ( { - ATTR_NAME: "TITLE", - ATTR_UP_DOWN: ["down"], + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.UP_DOWN: ["down"], }, Task(type=TaskType.HABIT, text="TITLE", up=False, down=True), ), ( { - ATTR_NAME: "TITLE", - ATTR_PRIORITY: "trivial", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.PRIORITY: "trivial", }, Task(type=TaskType.HABIT, text="TITLE", priority=TaskPriority.TRIVIAL), ), ( { - ATTR_NAME: "TITLE", - ATTR_FREQUENCY: "daily", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.FREQUENCY: "daily", }, Task(type=TaskType.HABIT, text="TITLE", frequency=Frequency.DAILY), ), ( { - ATTR_NAME: "TITLE", - ATTR_ALIAS: "ALIAS", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.ALIAS: "ALIAS", }, Task(type=TaskType.HABIT, text="TITLE", alias="ALIAS"), ), @@ -1339,9 +1293,9 @@ async def test_create_habit( await hass.services.async_call( DOMAIN, - SERVICE_CREATE_HABIT, + HabiticaService.CREATE_HABIT, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, **service_data, }, return_response=True, @@ -1355,19 +1309,19 @@ async def test_create_habit( [ ( { - ATTR_RENAME: "RENAME", + HabiticaServiceArgument.RENAME: "RENAME", }, Task(text="RENAME"), ), ( { - ATTR_NOTES: "NOTES", + HabiticaServiceArgument.NOTES: "NOTES", }, Task(notes="NOTES"), ), ( { - ATTR_ADD_CHECKLIST_ITEM: "Checklist-item", + HabiticaServiceArgument.ADD_CHECKLIST_ITEM: "Checklist-item", }, Task( { @@ -1393,7 +1347,7 @@ async def test_create_habit( ), ( { - ATTR_REMOVE_CHECKLIST_ITEM: "Checklist-item1", + HabiticaServiceArgument.REMOVE_CHECKLIST_ITEM: "Checklist-item1", }, Task( { @@ -1409,7 +1363,7 @@ async def test_create_habit( ), ( { - ATTR_SCORE_CHECKLIST_ITEM: "Checklist-item1", + HabiticaServiceArgument.SCORE_CHECKLIST_ITEM: "Checklist-item1", }, Task( { @@ -1430,7 +1384,7 @@ async def test_create_habit( ), ( { - ATTR_UNSCORE_CHECKLIST_ITEM: "Checklist-item2", + HabiticaServiceArgument.UNSCORE_CHECKLIST_ITEM: "Checklist-item2", }, Task( { @@ -1451,25 +1405,25 @@ async def test_create_habit( ), ( { - ATTR_PRIORITY: "trivial", + HabiticaServiceArgument.PRIORITY: "trivial", }, Task(priority=TaskPriority.TRIVIAL), ), ( { - ATTR_DATE: "2025-03-05", + HabiticaServiceArgument.DATE: "2025-03-05", }, Task(date=datetime(2025, 3, 5)), ), ( { - ATTR_CLEAR_DATE: True, + HabiticaServiceArgument.CLEAR_DATE: True, }, Task(date=None), ), ( { - ATTR_REMINDER: ["2025-02-25T00:00"], + HabiticaServiceArgument.REMINDER: ["2025-02-25T00:00"], }, Task( { @@ -1485,31 +1439,31 @@ async def test_create_habit( ), ( { - ATTR_REMOVE_REMINDER: ["2025-02-25T00:00"], + HabiticaServiceArgument.REMOVE_REMINDER: ["2025-02-25T00:00"], }, Task({"reminders": []}), ), ( { - ATTR_CLEAR_REMINDER: True, + HabiticaServiceArgument.CLEAR_REMINDER: True, }, Task({"reminders": []}), ), ( { - ATTR_ALIAS: "ALIAS", + HabiticaServiceArgument.ALIAS: "ALIAS", }, Task(alias="ALIAS"), ), ( { - ATTR_COLLAPSE_CHECKLIST: "collapsed", + HabiticaServiceArgument.COLLAPSE_CHECKLIST: "collapsed", }, Task(collapseChecklist=True), ), ( { - ATTR_COLLAPSE_CHECKLIST: "expanded", + HabiticaServiceArgument.COLLAPSE_CHECKLIST: "expanded", }, Task(collapseChecklist=False), ), @@ -1528,10 +1482,10 @@ async def test_update_todo( await hass.services.async_call( DOMAIN, - SERVICE_UPDATE_TODO, + HabiticaService.UPDATE_TODO, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, - ATTR_TASK: task_id, + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.TASK: task_id, **service_data, }, return_response=True, @@ -1545,21 +1499,21 @@ async def test_update_todo( [ ( { - ATTR_NAME: "TITLE", + HabiticaServiceArgument.NAME: "TITLE", }, Task(type=TaskType.TODO, text="TITLE"), ), ( { - ATTR_NAME: "TITLE", - ATTR_NOTES: "NOTES", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.NOTES: "NOTES", }, Task(type=TaskType.TODO, text="TITLE", notes="NOTES"), ), ( { - ATTR_NAME: "TITLE", - ATTR_ADD_CHECKLIST_ITEM: "Checklist-item", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.ADD_CHECKLIST_ITEM: "Checklist-item", }, Task( type=TaskType.TODO, @@ -1575,22 +1529,22 @@ async def test_update_todo( ), ( { - ATTR_NAME: "TITLE", - ATTR_PRIORITY: "trivial", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.PRIORITY: "trivial", }, Task(type=TaskType.TODO, text="TITLE", priority=TaskPriority.TRIVIAL), ), ( { - ATTR_NAME: "TITLE", - ATTR_DATE: "2025-03-05", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.DATE: "2025-03-05", }, Task(type=TaskType.TODO, text="TITLE", date=datetime(2025, 3, 5)), ), ( { - ATTR_NAME: "TITLE", - ATTR_REMINDER: ["2025-02-25T00:00"], + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.REMINDER: ["2025-02-25T00:00"], }, Task( type=TaskType.TODO, @@ -1606,22 +1560,22 @@ async def test_update_todo( ), ( { - ATTR_NAME: "TITLE", - ATTR_ALIAS: "ALIAS", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.ALIAS: "ALIAS", }, Task(type=TaskType.TODO, text="TITLE", alias="ALIAS"), ), ( { - ATTR_NAME: "TITLE", - ATTR_COLLAPSE_CHECKLIST: "collapsed", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.COLLAPSE_CHECKLIST: "collapsed", }, Task(type=TaskType.TODO, text="TITLE", collapseChecklist=True), ), ( { - ATTR_NAME: "TITLE", - ATTR_COLLAPSE_CHECKLIST: "expanded", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.COLLAPSE_CHECKLIST: "expanded", }, Task(type=TaskType.TODO, text="TITLE", collapseChecklist=False), ), @@ -1639,9 +1593,9 @@ async def test_create_todo( await hass.services.async_call( DOMAIN, - SERVICE_CREATE_TODO, + HabiticaService.CREATE_TODO, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, **service_data, }, return_response=True, @@ -1655,19 +1609,19 @@ async def test_create_todo( [ ( { - ATTR_RENAME: "RENAME", + HabiticaServiceArgument.RENAME: "RENAME", }, Task(text="RENAME"), ), ( { - ATTR_NOTES: "NOTES", + HabiticaServiceArgument.NOTES: "NOTES", }, Task(notes="NOTES"), ), ( { - ATTR_ADD_CHECKLIST_ITEM: "Checklist-item", + HabiticaServiceArgument.ADD_CHECKLIST_ITEM: "Checklist-item", }, Task( { @@ -1693,7 +1647,7 @@ async def test_create_todo( ), ( { - ATTR_REMOVE_CHECKLIST_ITEM: "Checklist-item1", + HabiticaServiceArgument.REMOVE_CHECKLIST_ITEM: "Checklist-item1", }, Task( { @@ -1709,7 +1663,7 @@ async def test_create_todo( ), ( { - ATTR_SCORE_CHECKLIST_ITEM: "Checklist-item1", + HabiticaServiceArgument.SCORE_CHECKLIST_ITEM: "Checklist-item1", }, Task( { @@ -1730,7 +1684,7 @@ async def test_create_todo( ), ( { - ATTR_UNSCORE_CHECKLIST_ITEM: "Checklist-item2", + HabiticaServiceArgument.UNSCORE_CHECKLIST_ITEM: "Checklist-item2", }, Task( { @@ -1751,38 +1705,38 @@ async def test_create_todo( ), ( { - ATTR_PRIORITY: "trivial", + HabiticaServiceArgument.PRIORITY: "trivial", }, Task(priority=TaskPriority.TRIVIAL), ), ( { - ATTR_START_DATE: "2025-03-05", + HabiticaServiceArgument.START_DATE: "2025-03-05", }, Task(startDate=datetime(2025, 3, 5)), ), ( { - ATTR_FREQUENCY: "weekly", + HabiticaServiceArgument.FREQUENCY: "weekly", }, Task(frequency=Frequency.WEEKLY), ), ( { - ATTR_INTERVAL: 5, + HabiticaServiceArgument.EVERY_X: 5, }, Task(everyX=5), ), ( { - ATTR_INTERVAL: 0, + HabiticaServiceArgument.EVERY_X: 0, }, Task(everyX=0), ), ( { - ATTR_FREQUENCY: "weekly", - ATTR_REPEAT: ["m", "t", "w", "th"], + HabiticaServiceArgument.FREQUENCY: "weekly", + HabiticaServiceArgument.REPEAT: ["m", "t", "w", "th"], }, Task( frequency=Frequency.WEEKLY, @@ -1791,15 +1745,15 @@ async def test_create_todo( ), ( { - ATTR_FREQUENCY: "monthly", - ATTR_REPEAT_MONTHLY: "day_of_month", + HabiticaServiceArgument.FREQUENCY: "monthly", + HabiticaServiceArgument.REPEAT_MONTHLY: "day_of_month", }, Task(frequency=Frequency.MONTHLY, daysOfMonth=[20], weeksOfMonth=[]), ), ( { - ATTR_FREQUENCY: "monthly", - ATTR_REPEAT_MONTHLY: "day_of_week", + HabiticaServiceArgument.FREQUENCY: "monthly", + HabiticaServiceArgument.REPEAT_MONTHLY: "day_of_week", }, Task( frequency=Frequency.MONTHLY, @@ -1812,7 +1766,7 @@ async def test_create_todo( ), ( { - ATTR_REMINDER: ["10:00"], + HabiticaServiceArgument.REMINDER: ["10:00"], }, Task( { @@ -1828,25 +1782,25 @@ async def test_create_todo( ), ( { - ATTR_REMOVE_REMINDER: ["10:00"], + HabiticaServiceArgument.REMOVE_REMINDER: ["10:00"], }, Task({"reminders": []}), ), ( { - ATTR_CLEAR_REMINDER: True, + HabiticaServiceArgument.CLEAR_REMINDER: True, }, Task({"reminders": []}), ), ( { - ATTR_STREAK: 10, + HabiticaServiceArgument.STREAK: 10, }, Task(streak=10), ), ( { - ATTR_ALIAS: "ALIAS", + HabiticaServiceArgument.ALIAS: "ALIAS", }, Task(alias="ALIAS"), ), @@ -1866,10 +1820,10 @@ async def test_update_daily( await hass.services.async_call( DOMAIN, - SERVICE_UPDATE_DAILY, + HabiticaService.UPDATE_DAILY, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, - ATTR_TASK: task_id, + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.TASK: task_id, **service_data, }, return_response=True, @@ -1883,21 +1837,21 @@ async def test_update_daily( [ ( { - ATTR_NAME: "TITLE", + HabiticaServiceArgument.NAME: "TITLE", }, Task(type=TaskType.DAILY, text="TITLE"), ), ( { - ATTR_NAME: "TITLE", - ATTR_NOTES: "NOTES", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.NOTES: "NOTES", }, Task(type=TaskType.DAILY, text="TITLE", notes="NOTES"), ), ( { - ATTR_NAME: "TITLE", - ATTR_ADD_CHECKLIST_ITEM: "Checklist-item", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.ADD_CHECKLIST_ITEM: "Checklist-item", }, Task( type=TaskType.DAILY, @@ -1913,37 +1867,37 @@ async def test_update_daily( ), ( { - ATTR_NAME: "TITLE", - ATTR_PRIORITY: "trivial", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.PRIORITY: "trivial", }, Task(type=TaskType.DAILY, text="TITLE", priority=TaskPriority.TRIVIAL), ), ( { - ATTR_NAME: "TITLE", - ATTR_START_DATE: "2025-03-05", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.START_DATE: "2025-03-05", }, Task(type=TaskType.DAILY, text="TITLE", startDate=datetime(2025, 3, 5)), ), ( { - ATTR_NAME: "TITLE", - ATTR_FREQUENCY: "weekly", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.FREQUENCY: "weekly", }, Task(type=TaskType.DAILY, text="TITLE", frequency=Frequency.WEEKLY), ), ( { - ATTR_NAME: "TITLE", - ATTR_INTERVAL: 5, + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.EVERY_X: 5, }, Task(type=TaskType.DAILY, text="TITLE", everyX=5), ), ( { - ATTR_NAME: "TITLE", - ATTR_FREQUENCY: "weekly", - ATTR_REPEAT: ["m", "t", "w", "th"], + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.FREQUENCY: "weekly", + HabiticaServiceArgument.REPEAT: ["m", "t", "w", "th"], }, Task( type=TaskType.DAILY, @@ -1954,9 +1908,9 @@ async def test_update_daily( ), ( { - ATTR_NAME: "TITLE", - ATTR_FREQUENCY: "monthly", - ATTR_REPEAT_MONTHLY: "day_of_month", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.FREQUENCY: "monthly", + HabiticaServiceArgument.REPEAT_MONTHLY: "day_of_month", }, Task( type=TaskType.DAILY, @@ -1968,9 +1922,9 @@ async def test_update_daily( ), ( { - ATTR_NAME: "TITLE", - ATTR_FREQUENCY: "monthly", - ATTR_REPEAT_MONTHLY: "day_of_week", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.FREQUENCY: "monthly", + HabiticaServiceArgument.REPEAT_MONTHLY: "day_of_week", }, Task( type=TaskType.DAILY, @@ -1985,8 +1939,8 @@ async def test_update_daily( ), ( { - ATTR_NAME: "TITLE", - ATTR_REMINDER: ["10:00"], + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.REMINDER: ["10:00"], }, Task( type=TaskType.DAILY, @@ -2002,29 +1956,29 @@ async def test_update_daily( ), ( { - ATTR_NAME: "TITLE", - ATTR_REMOVE_REMINDER: ["10:00"], + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.REMOVE_REMINDER: ["10:00"], }, Task(type=TaskType.DAILY, text="TITLE", reminders=[]), ), ( { - ATTR_NAME: "TITLE", - ATTR_CLEAR_REMINDER: True, + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.CLEAR_REMINDER: True, }, Task(type=TaskType.DAILY, text="TITLE", reminders=[]), ), ( { - ATTR_NAME: "TITLE", - ATTR_STREAK: 10, + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.STREAK: 10, }, Task(type=TaskType.DAILY, text="TITLE", streak=10), ), ( { - ATTR_NAME: "TITLE", - ATTR_ALIAS: "ALIAS", + HabiticaServiceArgument.NAME: "TITLE", + HabiticaServiceArgument.ALIAS: "ALIAS", }, Task(type=TaskType.DAILY, text="TITLE", alias="ALIAS"), ), @@ -2043,9 +1997,9 @@ async def test_create_daily( await hass.services.async_call( DOMAIN, - SERVICE_CREATE_DAILY, + HabiticaService.CREATE_DAILY, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, **service_data, }, return_response=True, @@ -2058,16 +2012,16 @@ async def test_create_daily( "service_data", [ { - ATTR_FREQUENCY: "daily", - ATTR_REPEAT: ["m", "t", "w", "th"], + HabiticaServiceArgument.FREQUENCY: "daily", + HabiticaServiceArgument.REPEAT: ["m", "t", "w", "th"], }, { - ATTR_FREQUENCY: "weekly", - ATTR_REPEAT_MONTHLY: "day_of_month", + HabiticaServiceArgument.FREQUENCY: "weekly", + HabiticaServiceArgument.REPEAT_MONTHLY: "day_of_month", }, { - ATTR_FREQUENCY: "weekly", - ATTR_REPEAT_MONTHLY: "day_of_week", + HabiticaServiceArgument.FREQUENCY: "weekly", + HabiticaServiceArgument.REPEAT_MONTHLY: "day_of_week", }, ], ) @@ -2085,10 +2039,10 @@ async def test_update_daily_service_validation_errors( with pytest.raises(ServiceValidationError): await hass.services.async_call( DOMAIN, - SERVICE_UPDATE_DAILY, + HabiticaService.UPDATE_DAILY, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, - ATTR_TASK: task_id, + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.TASK: task_id, **service_data, }, return_response=True, @@ -2106,11 +2060,11 @@ async def test_tags( await hass.services.async_call( DOMAIN, - SERVICE_UPDATE_REWARD, + HabiticaService.UPDATE_REWARD, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, - ATTR_TASK: task_id, - ATTR_TAG: ["Schule"], + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.TASK: task_id, + HabiticaServiceArgument.TAG: ["Schule"], }, return_response=True, blocking=True, @@ -2135,11 +2089,11 @@ async def test_create_new_tag( await hass.services.async_call( DOMAIN, - SERVICE_UPDATE_REWARD, + HabiticaService.UPDATE_REWARD, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, - ATTR_TASK: task_id, - ATTR_TAG: ["Home Assistant"], + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.TASK: task_id, + HabiticaServiceArgument.TAG: ["Home Assistant"], }, return_response=True, blocking=True, @@ -2191,11 +2145,11 @@ async def test_create_new_tag_exception( with pytest.raises(expected_exception, match=exception_msg): await hass.services.async_call( DOMAIN, - SERVICE_UPDATE_REWARD, + HabiticaService.UPDATE_REWARD, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, - ATTR_TASK: task_id, - ATTR_TAG: ["Home Assistant"], + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.TASK: task_id, + HabiticaServiceArgument.TAG: ["Home Assistant"], }, return_response=True, blocking=True, @@ -2212,11 +2166,11 @@ async def test_remove_tags( await hass.services.async_call( DOMAIN, - SERVICE_UPDATE_REWARD, + HabiticaService.UPDATE_REWARD, service_data={ - ATTR_CONFIG_ENTRY: config_entry.entry_id, - ATTR_TASK: task_id, - ATTR_REMOVE_TAG: ["Kreativität"], + HabiticaServiceArgument.CONFIG_ENTRY: config_entry.entry_id, + HabiticaServiceArgument.TASK: task_id, + HabiticaServiceArgument.REMOVE_TAG: ["Kreativität"], }, return_response=True, blocking=True,