Skip to content

[rest] Support sending Item command/state as JSON #4760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2025

Conversation

florian-h05
Copy link
Contributor

@florian-h05 florian-h05 commented Apr 27, 2025

Adds support for sending command/state through media type application/json to the existing plain text endpoints.

@florian-h05 florian-h05 requested a review from a team as a code owner April 27, 2025 21:59
@openhab-bot
Copy link
Collaborator

This pull request has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/integrating-openhab-and-garmin/155748/22

@spacemanspiff2007
Copy link
Contributor

What about state updates? If commands are available these should work, too.

Are you aware of the webhook binding?
Imho it tries to solve the same issue. Maybe it makes sense to consolidate into an official binding/endpoint?

@TheNinth7
Copy link

Are you aware of the webhook binding?
Imho it tries to solve the same issue. Maybe it makes sense to consolidate into an official binding/endpoint?

@spacemanspiff2007,

I’m the original requester for this change.
I’m developing an openHAB app for Garmin wearables. The Garmin SDK doesn’t support sending web requests with raw content, as required by the current API for sending a command.

The Webhook binding also doesn’t natively support sending commands to items. You need to create a custom Webhook setup, which involves a relatively complex configuration using a JEXL script in the Thing and additional JavaScript processing.

To make the Garmin app accessible to a wider audience, it would be much better if the needed API support were provided out of the box.

@spacemanspiff2007
Copy link
Contributor

@TheNinth7
I'm not against using get requests to push data into openHAB, I explicitly support it.
This worked from OH1 - OH3 through Basic UI (unsupported) but then was unfortunately removed.

All I'm saying is that

  • state updates should be supported, too
  • maybe it makes sense to implement a subset of the webhook binding
  • maybe besides raw content json should also be supported for interacting with items, too

@florian-h05
Copy link
Contributor Author

What about state updates? If commands are available these should work, too.

I didn’t notice the method for this, I will add that.

maybe it makes sense to implement a subset of the webhook binding

I think there is some architectural difference here. The webhook binding won’t be allowed to directly control items as it is a binding, it provides a way to specify custom webhooks. I think its functionality being a binding only is fine, though it would be nice to be part of the distribution.
The changes I do are to allow usage of the REST API like webhooks, so you only need HTTP GET for accessing some REST API functionality that currently requires the use of different HTTP methods.

maybe besides raw content json should also be supported for interacting with items, too

That’s a good point, some clients only support posting JSON. It shouldn’t be a problem supporting multiple media types for the POST/PUT endpoints.

- Adds support for sending command/state as JSON to the existing plain text endpoints.
- Adds new or extends existing endpoints where command/state can be passed through query params, allowing to send them from webhooks.

Signed-off-by: Florian Hotze <[email protected]>
@florian-h05 florian-h05 changed the title [rest] Add webhook-like endpoint for sending command to Item [rest] Support sending Item command/state as JSON and through HTTP GET Apr 28, 2025
@florian-h05
Copy link
Contributor Author

So i am finished with adding the same functionality for states and JSON support for the existing send command/state endpoints.

@openhab-bot
Copy link
Collaborator

This pull request has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/openhab-for-garmin/163891/1

@splatch
Copy link
Contributor

splatch commented May 2, 2025

Just a generic comment around the merit - for rest interfaces GET requests should issue read requests (as verb suggests), rather than write.
It is not a big issue for me, but it makes OH interface more of RPC. We already have some crippled implementation of toggle functionality for Android app, its just another edge case for another app.

@florian-h05 florian-h05 marked this pull request as ready for review May 2, 2025 22:15
@kaikreuzer
Copy link
Member

Just a generic comment around the merit - for rest interfaces GET requests should issue read requests

This is the reason why I always argued against such a "feature" - it completely goes against the core principles of a REST API and GET requests being safe methods. I'm therefore still not a fan of adding that to our REST API. As @spacemanspiff2007 there was a "hidden" workaround in the past by having the Classic UI offering such an endpoint, which was not part of the REST API.

If the main use case is the support of webhooks, the approach with the webhook bindings looks much cleaner to me.

Just my two cents on the topic.

@florian-h05
Copy link
Contributor Author

What about splitting out the GET webhook functionality into a new core webhook API?
IMO it would be much more convenient to have support for sending commands and states directly without the need for a third party binding and linking channels.

@TheNinth7
Copy link

From the perspective of the openHAB app I’m developing for Garmin, I can support both PUT and POST requests, with content types of either application/x-www-form-urlencoded or application/json. However, the Garmin SDK doesn’t always behave exactly as documented, so I’m happy to test in advance whichever approach you think best fits the openHAB REST API model before committing to an implementation.

@spacemanspiff2007
Copy link
Contributor

@kaikreuzer
I fully agree with you that it's completely against the core principles of REST.
But I'm also torn since it should be trivial to get data into openHAB.

Back in the days I had an old webcam on the only way to get notifications from it into openHAB was through a GET request because that was all the webcam offered. So it was either making that GET request work or nothing.
As you can guess I was not hesitant to use the workaround even though it was a dirty hack.

So I suggest that there is at least something or some way to provide more flexibility to ingest data, even though it's not the "proper" API.

Accepting json for commands / updates is imho a good idea in any case.
Maybe it makes sense to split this into two parts?

@kaikreuzer
Copy link
Member

So I suggest that there is at least something or some way to provide more flexibility to ingest data, even though it's not the "proper" API.

Yes, I see the recurring request and see that there are many external services that don't follow REST principles, so I won't block any solution for this in core.

But I wouldn't advice to dilute the existing REST API and its endpoints by simply adding non-REST behavior to it like here.
It might be better to maybe introduce a new dedicated endpoint like /json-rpc/... next to the /rest/... ones to keep these things apart.

Signed-off-by: Florian Hotze <[email protected]>
@florian-h05 florian-h05 changed the title [rest] Support sending Item command/state as JSON and through HTTP GET [rest] Support sending Item command/state through JSON media type May 5, 2025
@florian-h05 florian-h05 changed the title [rest] Support sending Item command/state through JSON media type [rest] Support sending Item command/state as JSON May 5, 2025
@florian-h05
Copy link
Contributor Author

@kaikreuzer I have removed the new GET endpoints from this PR, it now only contains JSON support for the existing plain text command/state endpoints. Can you please review it?

@digitaldan
Copy link
Contributor

digitaldan commented May 5, 2025

I also like the idea of a JSON-RPC endpoint for this, its simple, follows a specification, and keeps the REST api clean. I also would definitely use this, i use Caddy to proxy and rewrite requests from a few devices who only support get requests for events. Would be nice to remove those rules.

Copy link
Member

@kaikreuzer kaikreuzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@kaikreuzer kaikreuzer merged commit 8ff69a3 into openhab:main May 9, 2025
4 checks passed
@kaikreuzer kaikreuzer added this to the 5.0 milestone May 9, 2025
@kaikreuzer kaikreuzer added the enhancement An enhancement or new feature of the Core label May 9, 2025
@florian-h05 florian-h05 deleted the command-webhook branch May 9, 2025 20:33
@Consumes(MediaType.APPLICATION_JSON)
public Response postItemCommandJson(@PathParam("itemname") String itemname, ValueContainer valueContainer) {
return sendItemCommandInternal(itemname, valueContainer.value());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lolodomo this made me think of you. I know we decided not to do it, but isn't this what you were trying to do but having trouble to do it? i.e. trying to accept either plain text and json.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Professing both MIME types is relatively simple, the most difficult part is getting Swagger work.

florian-h05 added a commit to florian-h05/openhab-core that referenced this pull request May 11, 2025
Signed-off-by: Florian Hotze <[email protected]>
(cherry picked from commit 8ff69a3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement or new feature of the Core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants