From 364a8ed28135a82b06ac2b8c9198e93a07a992f8 Mon Sep 17 00:00:00 2001 From: karwosts Date: Thu, 13 Apr 2023 21:30:48 -0700 Subject: [PATCH 1/2] Add Update/Delete generic services --- custom_components/grocy/services.py | 64 +++++++++++++++++ custom_components/grocy/services.yaml | 99 ++++++++++++++++++++++++++- 2 files changed, 162 insertions(+), 1 deletion(-) diff --git a/custom_components/grocy/services.py b/custom_components/grocy/services.py index 5069ac6..97c3293 100644 --- a/custom_components/grocy/services.py +++ b/custom_components/grocy/services.py @@ -23,6 +23,7 @@ SERVICE_DATA = "data" SERVICE_RECIPE_ID = "recipe_id" SERVICE_BATTERY_ID = "battery_id" +SERVICE_OBJECT_ID = "object_id" SERVICE_ADD_PRODUCT = "add_product_to_stock" SERVICE_OPEN_PRODUCT = "open_product" @@ -30,6 +31,8 @@ SERVICE_EXECUTE_CHORE = "execute_chore" SERVICE_COMPLETE_TASK = "complete_task" SERVICE_ADD_GENERIC = "add_generic" +SERVICE_UPDATE_GENERIC = "update_generic" +SERVICE_DELETE_GENERIC = "delete_generic" SERVICE_CONSUME_RECIPE = "consume_recipe" SERVICE_TRACK_BATTERY = "track_battery" @@ -92,6 +95,25 @@ ) ) +SERVICE_UPDATE_GENERIC_SCHEMA = vol.All( + vol.Schema( + { + vol.Required(SERVICE_ENTITY_TYPE): str, + vol.Required(SERVICE_OBJECT_ID): vol.Coerce(int), + vol.Required(SERVICE_DATA): object, + } + ) +) + +SERVICE_DELETE_GENERIC_SCHEMA = vol.All( + vol.Schema( + { + vol.Required(SERVICE_ENTITY_TYPE): str, + vol.Required(SERVICE_OBJECT_ID): vol.Coerce(int), + } + ) +) + SERVICE_CONSUME_RECIPE_SCHEMA = vol.All( vol.Schema( { @@ -115,6 +137,8 @@ (SERVICE_EXECUTE_CHORE, SERVICE_EXECUTE_CHORE_SCHEMA), (SERVICE_COMPLETE_TASK, SERVICE_COMPLETE_TASK_SCHEMA), (SERVICE_ADD_GENERIC, SERVICE_ADD_GENERIC_SCHEMA), + (SERVICE_UPDATE_GENERIC, SERVICE_UPDATE_GENERIC_SCHEMA), + (SERVICE_DELETE_GENERIC, SERVICE_DELETE_GENERIC_SCHEMA), (SERVICE_CONSUME_RECIPE, SERVICE_CONSUME_RECIPE_SCHEMA), (SERVICE_TRACK_BATTERY, SERVICE_TRACK_BATTERY_SCHEMA), ] @@ -151,6 +175,12 @@ async def async_call_grocy_service(service_call: ServiceCall) -> None: elif service == SERVICE_ADD_GENERIC: await async_add_generic_service(hass, coordinator, service_data) + elif service == SERVICE_UPDATE_GENERIC: + await async_update_generic_service(hass, coordinator, service_data) + + elif service == SERVICE_DELETE_GENERIC: + await async_delete_generic_service(hass, coordinator, service_data) + elif service == SERVICE_CONSUME_RECIPE: await async_consume_recipe_service(hass, coordinator, service_data) @@ -261,6 +291,40 @@ def wrapper(): await hass.async_add_executor_job(wrapper) +async def async_update_generic_service(hass, coordinator, data): + """Update a generic entity in Grocy.""" + entity_type_raw = data.get(SERVICE_ENTITY_TYPE, None) + entity_type = EntityType.TASKS + + if entity_type_raw is not None: + entity_type = EntityType(entity_type_raw) + + object_id = data[SERVICE_OBJECT_ID] + + data = data[SERVICE_DATA] + + def wrapper(): + coordinator.grocy_api.update_generic(entity_type, object_id, data) + + await hass.async_add_executor_job(wrapper) + + +async def async_delete_generic_service(hass, coordinator, data): + """Delete a generic entity in Grocy.""" + entity_type_raw = data.get(SERVICE_ENTITY_TYPE, None) + entity_type = EntityType.TASKS + + if entity_type_raw is not None: + entity_type = EntityType(entity_type_raw) + + object_id = data[SERVICE_OBJECT_ID] + + def wrapper(): + coordinator.grocy_api.delete_generic(entity_type, object_id) + + await hass.async_add_executor_job(wrapper) + + async def async_consume_recipe_service(hass, coordinator, data): """Consume a recipe in Grocy.""" recipe_id = data[SERVICE_RECIPE_ID] diff --git a/custom_components/grocy/services.yaml b/custom_components/grocy/services.yaml index 2ffe40e..0e4c489 100644 --- a/custom_components/grocy/services.yaml +++ b/custom_components/grocy/services.yaml @@ -184,6 +184,103 @@ add_generic: selector: object: + +update_generic: + name: Update Generic + description: Edits a single object of the given entity type + fields: + entity_type: + name: Entity Type + description: The type of entity you like to update. + required: true + example: 'tasks' + default: 'tasks' + selector: + select: + options: + - "products" + - "chores" + - "product_barcodes" + - "batteries" + - "locations" + - "quantity_units" + - "quantity_unit_conversions" + - "shopping_list" + - "shopping_lists" + - "shopping_locations" + - "recipes" + - "recipes_pos" + - "recipes_nestings" + - "tasks" + - "task_categories" + - "product_groups" + - "equipment" + - "userfields" + - "userentities" + - "userobjects" + - "meal_plan" + + object_id: + name: Object ID + descriuption: The ID of the entity to update. + required: true + example: '1' + selector: + text: + + data: + name: Data + description: "JSON object with what data you want to update (yaml format also works). See Grocy api documentation on Generic entity interactions: https://demo.grocy.info/api" + required: true + default: {"name": "Task name", "due_date": "2021-05-21"} + selector: + object: + + +delete_generic: + name: Delete Generic + description: Deletes a single object of the given entity type + fields: + entity_type: + name: Entity Type + description: The type of entity you like to update. + required: true + example: 'tasks' + default: 'tasks' + selector: + select: + options: + - "products" + - "chores" + - "product_barcodes" + - "batteries" + - "locations" + - "quantity_units" + - "quantity_unit_conversions" + - "shopping_list" + - "shopping_lists" + - "shopping_locations" + - "recipes" + - "recipes_pos" + - "recipes_nestings" + - "tasks" + - "task_categories" + - "product_groups" + - "equipment" + - "userfields" + - "userentities" + - "userobjects" + - "meal_plan" + + object_id: + name: Object ID + descriuption: The ID of the entity to delete. + required: true + example: '1' + selector: + text: + + consume_recipe: name: Consume Recipe description: Consumes the given recipe @@ -206,4 +303,4 @@ track_battery: required: true description: The id of the battery selector: - text: \ No newline at end of file + text: From fdc3f593d9d16f228994819640a26a01c930eff1 Mon Sep 17 00:00:00 2001 From: karwosts Date: Fri, 14 Apr 2023 05:05:22 -0700 Subject: [PATCH 2/2] fix typo --- custom_components/grocy/services.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/custom_components/grocy/services.yaml b/custom_components/grocy/services.yaml index 0e4c489..b77cb8e 100644 --- a/custom_components/grocy/services.yaml +++ b/custom_components/grocy/services.yaml @@ -222,7 +222,7 @@ update_generic: object_id: name: Object ID - descriuption: The ID of the entity to update. + description: The ID of the entity to update. required: true example: '1' selector: @@ -243,7 +243,7 @@ delete_generic: fields: entity_type: name: Entity Type - description: The type of entity you like to update. + description: The type of entity to be deleted. required: true example: 'tasks' default: 'tasks' @@ -274,7 +274,7 @@ delete_generic: object_id: name: Object ID - descriuption: The ID of the entity to delete. + description: The ID of the entity to delete. required: true example: '1' selector: