Skip to content

Commit

Permalink
Merge pull request #246 from marcelvriend/feature/open-product-service
Browse files Browse the repository at this point in the history
Add open product service
  • Loading branch information
isabellaalstrom authored Sep 8, 2022
2 parents 7c92d0b + f2a550d commit 054b713
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions custom_components/grocy/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(
{
Expand Down Expand Up @@ -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),
Expand All @@ -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)

Expand Down Expand Up @@ -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]
Expand Down
30 changes: 30 additions & 0 deletions custom_components/grocy/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 054b713

Please sign in to comment.