Skip to content

Commit

Permalink
Merge pull request #267 from karwosts/master
Browse files Browse the repository at this point in the history
Add Update/Delete generic services
  • Loading branch information
isabellaalstrom authored Apr 17, 2023
2 parents 0165a78 + fdc3f59 commit 4916605
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 1 deletion.
64 changes: 64 additions & 0 deletions custom_components/grocy/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
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"
SERVICE_CONSUME_PRODUCT = "consume_product_from_stock"
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"

Expand Down Expand Up @@ -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(
{
Expand All @@ -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),
]
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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]
Expand Down
99 changes: 98 additions & 1 deletion custom_components/grocy/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
description: 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 to be deleted.
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
description: The ID of the entity to delete.
required: true
example: '1'
selector:
text:


consume_recipe:
name: Consume Recipe
description: Consumes the given recipe
Expand All @@ -206,4 +303,4 @@ track_battery:
required: true
description: The id of the battery
selector:
text:
text:

0 comments on commit 4916605

Please sign in to comment.