custom-components / grocy

Custom Grocy integration for Home Assistant
Apache License 2.0
153 stars 45 forks source link

Unable to manually schedule a chore #261

Closed dgrnbrg closed 1 year ago

dgrnbrg commented 1 year ago

Describe the bug It is not possible to manually schedule a chore execution through the integration.

Expected behavior I thought that if I did grocy.add_generic with a body like:

service: grocy.add_generic
data:
  entity_type: tasks
  data:
    id: "51"
    rescheduled_date: "{{states('sensor.date')}}"
    rescheduled_next_execution_assigned_to_user_id: "1"

this would schedule chore 51 for today. However, this didn't work. It seems like perhaps an "Edit Generic" to the URI /objects/{entity}/{objectId} is missing.

What is your installed versions of Home Assistant, Grocy and this integration?

Home assistant 2023.2.5

Grocy 3.3.2

Grocy integration - I'm not sure how to find the version.

How do you have Grocy installed? Add-on or external?

I'm using the grocy add-on.

Have you added debugging to the log, and what does the log say?

Logger: homeassistant.components.websocket_api.http.connection
Source: custom_components/grocy/services.py:230 
Integration: Home Assistant WebSocket API ([documentation](https://www.home-assistant.io/integrations/websocket_api), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+websocket_api%22)) 
First occurred: 11:07:46 AM (3 occurrences) 
Last logged: 11:14:52 AM

[1519711136] Error handling message: Unknown error (unknown_error) from 192.168.0.106 (Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6.1 Safari/605.1.15)
[1846709624] Error handling message: Unknown error (unknown_error) from 192.168.0.106 (Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6.1 Safari/605.1.15)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/decorators.py", line 27, in _handle_async_response
    await func(hass, connection, msg)
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 646, in handle_execute_script
    await script_obj.async_run(msg.get("variables"), context=context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1524, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 409, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 453, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 476, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 451, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 684, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1787, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1824, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/config/custom_components/grocy/services.py", line 137, in async_call_grocy_service
    await async_add_generic_service(hass, coordinator, service_data)
  File "/config/custom_components/grocy/services.py", line 232, in async_add_generic_service
    await hass.async_add_executor_job(wrapper)
  File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/grocy/services.py", line 230, in wrapper
    coordinator.grocy_api.add_generic(entity_type, data)
  File "/usr/local/lib/python3.10/site-packages/pygrocy/grocy.py", line 368, in add_generic
    return self._api_client.add_generic(entity_type.value, data)
  File "/usr/local/lib/python3.10/site-packages/pygrocy/grocy_api_client.py", line 775, in add_generic
    return self._do_post_request(f"objects/{entity_type}", data)
  File "/usr/local/lib/python3.10/site-packages/pygrocy/grocy_api_client.py", line 376, in _do_post_request
    raise GrocyError(resp)
pygrocy.errors.grocy_error.GrocyError: <Response [400]>
Aasikki commented 1 year ago

Looks like I have the same issue, I want to create a task with an automation, but I also want to be able to track it via automation, and fire automation based on tracking it via grocy, but it's impossible.

This works only as long as the task id has never been used before or the task with the id is deleted from grocy ui. Even if the task is marked executed, it still doesn't allow creating a new task with the same id.

data:
  entity_type: tasks
  data:
    name: "House Chore: Empty the dishwasher"
    id: "2"
    due_date: "{{ now().year }}-{{ now().month }}-{{ now().day }}"

Quite unfortunate, as automatically executing tasks where would be very useful.

karwosts commented 1 year ago

I filed a PR adding the update_generic service, which I think handles these cases. You can manually schedule chores, and edit task details, etc.

Aasikki commented 1 year ago

#

I filed a PR adding the update_generic service, which I think handles these cases. You can manually schedule chores, and edit task details, etc.

That's amazing, thank you very much!

Aasikki commented 1 year ago

After the update I can now modify and delete tasks, but if I reschedule a task that's marked done, it still remains marked done. Maybe there should be something like undo: true for the update_generic service? I could also delete the task when done but then I'd need to run another automation just to do that.

karwosts commented 1 year ago

I believe update_generic with done: false in the data section will change it from done back to incomplete.

Aasikki commented 1 year ago

I believe update_generic with done: false in the data section will change it from done back to incomplete.

Can confirm that it does, thanks! From my end it looks like the issue is now fully solved.