Skip to content

Commit fd409a0

Browse files
Merge branch 'master' into pygrocyupdate
2 parents 88075dd + 4916605 commit fd409a0

File tree

3 files changed

+180
-12
lines changed

3 files changed

+180
-12
lines changed

custom_components/grocy/manifest.json

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
{
2-
"domain": "grocy",
3-
"name": "Grocy",
4-
"documentation": "https://github.com/custom-components/grocy",
5-
"issue_tracker": "https://github.com/custom-components/grocy/issues",
6-
"dependencies": ["http"],
7-
"config_flow": true,
8-
"codeowners": ["@SebRut", "@isabellaalstrom"],
9-
"requirements": ["pygrocy==1.5.0"],
10-
"version": "0.0.0",
11-
"iot_class": "local_polling"
12-
}
2+
"domain": "grocy",
3+
"name": "Grocy",
4+
"codeowners": [
5+
"@SebRut",
6+
"@isabellaalstrom"
7+
],
8+
"config_flow": true,
9+
"dependencies": [
10+
"http"
11+
],
12+
"documentation": "https://github.com/custom-components/grocy",
13+
"iot_class": "local_polling",
14+
"issue_tracker": "https://github.com/custom-components/grocy/issues",
15+
"requirements": [
16+
"pygrocy==1.5.0"
17+
],
18+
"version": "0.0.0"
19+
}

custom_components/grocy/services.py

+64
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@
2323
SERVICE_DATA = "data"
2424
SERVICE_RECIPE_ID = "recipe_id"
2525
SERVICE_BATTERY_ID = "battery_id"
26+
SERVICE_OBJECT_ID = "object_id"
2627

2728
SERVICE_ADD_PRODUCT = "add_product_to_stock"
2829
SERVICE_OPEN_PRODUCT = "open_product"
2930
SERVICE_CONSUME_PRODUCT = "consume_product_from_stock"
3031
SERVICE_EXECUTE_CHORE = "execute_chore"
3132
SERVICE_COMPLETE_TASK = "complete_task"
3233
SERVICE_ADD_GENERIC = "add_generic"
34+
SERVICE_UPDATE_GENERIC = "update_generic"
35+
SERVICE_DELETE_GENERIC = "delete_generic"
3336
SERVICE_CONSUME_RECIPE = "consume_recipe"
3437
SERVICE_TRACK_BATTERY = "track_battery"
3538

@@ -92,6 +95,25 @@
9295
)
9396
)
9497

98+
SERVICE_UPDATE_GENERIC_SCHEMA = vol.All(
99+
vol.Schema(
100+
{
101+
vol.Required(SERVICE_ENTITY_TYPE): str,
102+
vol.Required(SERVICE_OBJECT_ID): vol.Coerce(int),
103+
vol.Required(SERVICE_DATA): object,
104+
}
105+
)
106+
)
107+
108+
SERVICE_DELETE_GENERIC_SCHEMA = vol.All(
109+
vol.Schema(
110+
{
111+
vol.Required(SERVICE_ENTITY_TYPE): str,
112+
vol.Required(SERVICE_OBJECT_ID): vol.Coerce(int),
113+
}
114+
)
115+
)
116+
95117
SERVICE_CONSUME_RECIPE_SCHEMA = vol.All(
96118
vol.Schema(
97119
{
@@ -115,6 +137,8 @@
115137
(SERVICE_EXECUTE_CHORE, SERVICE_EXECUTE_CHORE_SCHEMA),
116138
(SERVICE_COMPLETE_TASK, SERVICE_COMPLETE_TASK_SCHEMA),
117139
(SERVICE_ADD_GENERIC, SERVICE_ADD_GENERIC_SCHEMA),
140+
(SERVICE_UPDATE_GENERIC, SERVICE_UPDATE_GENERIC_SCHEMA),
141+
(SERVICE_DELETE_GENERIC, SERVICE_DELETE_GENERIC_SCHEMA),
118142
(SERVICE_CONSUME_RECIPE, SERVICE_CONSUME_RECIPE_SCHEMA),
119143
(SERVICE_TRACK_BATTERY, SERVICE_TRACK_BATTERY_SCHEMA),
120144
]
@@ -151,6 +175,12 @@ async def async_call_grocy_service(service_call: ServiceCall) -> None:
151175
elif service == SERVICE_ADD_GENERIC:
152176
await async_add_generic_service(hass, coordinator, service_data)
153177

178+
elif service == SERVICE_UPDATE_GENERIC:
179+
await async_update_generic_service(hass, coordinator, service_data)
180+
181+
elif service == SERVICE_DELETE_GENERIC:
182+
await async_delete_generic_service(hass, coordinator, service_data)
183+
154184
elif service == SERVICE_CONSUME_RECIPE:
155185
await async_consume_recipe_service(hass, coordinator, service_data)
156186

@@ -261,6 +291,40 @@ def wrapper():
261291
await hass.async_add_executor_job(wrapper)
262292

263293

294+
async def async_update_generic_service(hass, coordinator, data):
295+
"""Update a generic entity in Grocy."""
296+
entity_type_raw = data.get(SERVICE_ENTITY_TYPE, None)
297+
entity_type = EntityType.TASKS
298+
299+
if entity_type_raw is not None:
300+
entity_type = EntityType(entity_type_raw)
301+
302+
object_id = data[SERVICE_OBJECT_ID]
303+
304+
data = data[SERVICE_DATA]
305+
306+
def wrapper():
307+
coordinator.grocy_api.update_generic(entity_type, object_id, data)
308+
309+
await hass.async_add_executor_job(wrapper)
310+
311+
312+
async def async_delete_generic_service(hass, coordinator, data):
313+
"""Delete a generic entity in Grocy."""
314+
entity_type_raw = data.get(SERVICE_ENTITY_TYPE, None)
315+
entity_type = EntityType.TASKS
316+
317+
if entity_type_raw is not None:
318+
entity_type = EntityType(entity_type_raw)
319+
320+
object_id = data[SERVICE_OBJECT_ID]
321+
322+
def wrapper():
323+
coordinator.grocy_api.delete_generic(entity_type, object_id)
324+
325+
await hass.async_add_executor_job(wrapper)
326+
327+
264328
async def async_consume_recipe_service(hass, coordinator, data):
265329
"""Consume a recipe in Grocy."""
266330
recipe_id = data[SERVICE_RECIPE_ID]

custom_components/grocy/services.yaml

+98-1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,103 @@ add_generic:
184184
selector:
185185
object:
186186

187+
188+
update_generic:
189+
name: Update Generic
190+
description: Edits a single object of the given entity type
191+
fields:
192+
entity_type:
193+
name: Entity Type
194+
description: The type of entity you like to update.
195+
required: true
196+
example: 'tasks'
197+
default: 'tasks'
198+
selector:
199+
select:
200+
options:
201+
- "products"
202+
- "chores"
203+
- "product_barcodes"
204+
- "batteries"
205+
- "locations"
206+
- "quantity_units"
207+
- "quantity_unit_conversions"
208+
- "shopping_list"
209+
- "shopping_lists"
210+
- "shopping_locations"
211+
- "recipes"
212+
- "recipes_pos"
213+
- "recipes_nestings"
214+
- "tasks"
215+
- "task_categories"
216+
- "product_groups"
217+
- "equipment"
218+
- "userfields"
219+
- "userentities"
220+
- "userobjects"
221+
- "meal_plan"
222+
223+
object_id:
224+
name: Object ID
225+
description: The ID of the entity to update.
226+
required: true
227+
example: '1'
228+
selector:
229+
text:
230+
231+
data:
232+
name: Data
233+
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"
234+
required: true
235+
default: {"name": "Task name", "due_date": "2021-05-21"}
236+
selector:
237+
object:
238+
239+
240+
delete_generic:
241+
name: Delete Generic
242+
description: Deletes a single object of the given entity type
243+
fields:
244+
entity_type:
245+
name: Entity Type
246+
description: The type of entity to be deleted.
247+
required: true
248+
example: 'tasks'
249+
default: 'tasks'
250+
selector:
251+
select:
252+
options:
253+
- "products"
254+
- "chores"
255+
- "product_barcodes"
256+
- "batteries"
257+
- "locations"
258+
- "quantity_units"
259+
- "quantity_unit_conversions"
260+
- "shopping_list"
261+
- "shopping_lists"
262+
- "shopping_locations"
263+
- "recipes"
264+
- "recipes_pos"
265+
- "recipes_nestings"
266+
- "tasks"
267+
- "task_categories"
268+
- "product_groups"
269+
- "equipment"
270+
- "userfields"
271+
- "userentities"
272+
- "userobjects"
273+
- "meal_plan"
274+
275+
object_id:
276+
name: Object ID
277+
description: The ID of the entity to delete.
278+
required: true
279+
example: '1'
280+
selector:
281+
text:
282+
283+
187284
consume_recipe:
188285
name: Consume Recipe
189286
description: Consumes the given recipe
@@ -206,4 +303,4 @@ track_battery:
206303
required: true
207304
description: The id of the battery
208305
selector:
209-
text:
306+
text:

0 commit comments

Comments
 (0)