From f2a550d2a1678e8d3e53d3ae5b24e5314de35143 Mon Sep 17 00:00:00 2001 From: Marcel Vriend <92307684+marcelvriend@users.noreply.github.com> Date: Wed, 7 Sep 2022 20:08:00 +0000 Subject: [PATCH] Add open product service --- README.md | 4 ++++ custom_components/grocy/services.py | 29 ++++++++++++++++++++++++++ custom_components/grocy/services.yaml | 30 +++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/README.md b/README.md index 675df1f..91ea1f7 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,10 @@ Adds a single object of the given entity type. Adds a given amount of a product to the stock. +- **Grocy: Open Product** (_grocy.open_product_) + +Opens a given amount of a product in stock. + - **Grocy: Track Battery** (_grocy.track_battery_) Tracks the given battery. diff --git a/custom_components/grocy/services.py b/custom_components/grocy/services.py index 0b3d443..5069ac6 100644 --- a/custom_components/grocy/services.py +++ b/custom_components/grocy/services.py @@ -25,6 +25,7 @@ SERVICE_BATTERY_ID = "battery_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" @@ -42,6 +43,16 @@ ) ) +SERVICE_OPEN_PRODUCT_SCHEMA = vol.All( + vol.Schema( + { + vol.Required(SERVICE_PRODUCT_ID): vol.Coerce(int), + vol.Required(SERVICE_AMOUNT): vol.Coerce(float), + vol.Optional(SERVICE_SUBPRODUCT_SUBSTITUTION): bool, + } + ) +) + SERVICE_CONSUME_PRODUCT_SCHEMA = vol.All( vol.Schema( { @@ -99,6 +110,7 @@ SERVICES_WITH_ACCOMPANYING_SCHEMA: list[tuple[str, vol.Schema]] = [ (SERVICE_ADD_PRODUCT, SERVICE_ADD_PRODUCT_SCHEMA), + (SERVICE_OPEN_PRODUCT, SERVICE_OPEN_PRODUCT_SCHEMA), (SERVICE_CONSUME_PRODUCT, SERVICE_CONSUME_PRODUCT_SCHEMA), (SERVICE_EXECUTE_CHORE, SERVICE_EXECUTE_CHORE_SCHEMA), (SERVICE_COMPLETE_TASK, SERVICE_COMPLETE_TASK_SCHEMA), @@ -124,6 +136,9 @@ async def async_call_grocy_service(service_call: ServiceCall) -> None: if service == SERVICE_ADD_PRODUCT: await async_add_product_service(hass, coordinator, service_data) + elif service == SERVICE_OPEN_PRODUCT: + await async_open_product_service(hass, coordinator, service_data) + elif service == SERVICE_CONSUME_PRODUCT: await async_consume_product_service(hass, coordinator, service_data) @@ -167,6 +182,20 @@ def wrapper(): await hass.async_add_executor_job(wrapper) +async def async_open_product_service(hass, coordinator, data): + """Open a product in Grocy.""" + product_id = data[SERVICE_PRODUCT_ID] + amount = data[SERVICE_AMOUNT] + allow_subproduct_substitution = data.get(SERVICE_SUBPRODUCT_SUBSTITUTION, False) + + def wrapper(): + coordinator.grocy_api.open_product( + product_id, amount, allow_subproduct_substitution + ) + + await hass.async_add_executor_job(wrapper) + + async def async_consume_product_service(hass, coordinator, data): """Consume a product in Grocy.""" product_id = data[SERVICE_PRODUCT_ID] diff --git a/custom_components/grocy/services.yaml b/custom_components/grocy/services.yaml index 690b5d8..2ffe40e 100644 --- a/custom_components/grocy/services.yaml +++ b/custom_components/grocy/services.yaml @@ -26,6 +26,36 @@ add_product_to_stock: description: The purchase price per purchase quantity unit of the added product selector: text: + +open_product: + name: Open Product + description: Opens a given amount of a product in stock + fields: + product_id: + name: Product Id + example: '3' + required: true + description: The id of the product to open + selector: + text: + amount: + name: Amount + example: 1 + required: true + description: The amount to open + selector: + number: + min: 1 + max: 1000 + mode: box + allow_subproduct_substitution: + name: Subproduct substitution + description: If subproduct substitution is allowed + example: false + default: false + selector: + boolean: + consume_product_from_stock: name: Consume Product From Stock description: Consumes a given amount of a product to the stock