Extend REST API to return service response data #1074
-
Original contentHey y'all,When service responses were introduced, the To put some more fire under us, now that weather forecast attributes have been deprecated, service calls are the only way to retrieve weather forecast data, but this doesn't work through the API. This leaves users with no way to retrieve it. See home-assistant/core#99820 I originally opened this PR to address this, and was kindly redirected here to have a conversation about it. Current stateCurrently, calling My draft solutionIn order not to break backwards compatibility, my implementation added a If the service is Alternatives@bdraco suggested creating an entirely new API endpoint for this, since the response format would be drastically different based on the presence of the parameter. I'm not particularly committed to one choice or the other: either way the user will have to know in advance if the service supports returning data or not, because they will need to add the parameter or call another endpoint. Thoughts? First rewriteContextWhen service responses were introduced, the Now that weather forecast attributes have been deprecated, service calls are the only way to retrieve weather forecast data, but this doesn't work through the API. This leaves users with no way to retrieve it. See home-assistant/core#99820 as an example. Exposing this through the API is fairly uncontroversial, but deciding where and how to expose it has generated a bit of discussion. The original proposal was to add a The original behaviour of returning all state changes (even ones unrelated to the service call) was already weird at best when it was first added. However, I'm sure plenty of people rely on it, so I don't want to get rid of it. At the same time, creating an entirely new endpoint for this feels equally kludgy:
Lastly, people might want to have access to both the response data and the set of state changes. ProposalDeprecate the current response formatWe can follow whatever the current HASS deprecation timeline is. API calls in their current form will keep working exactly as they do now. Introduce a new response format"New" calls will return a response using the following format: {
"state_changes": [
{
"attributes": {},
"entity_id": "sun.sun",
"last_changed": "2016-05-30T21:43:32.418320+00:00",
"state": "below_horizon"
},
{
"attributes": {},
"entity_id": "process.Dropbox",
"last_changed": "22016-05-30T21:43:32.418320+00:00",
"state": "on"
}
],
"service_response": {
"forecast": [
{
"datetime": "2024-04-10T08:57:12.151445-04:00",
"condition": "rainy",
"precipitation_probability": 30,
"temperature": 18,
"templow": 8
},
{
"datetime": "2024-04-11T08:57:12.151493-04:00",
"condition": "rainy",
"precipitation_probability": 0,
"temperature": 20,
"templow": 8
}
]
}
} Behaviour during deprecation phase
It's not pretty, but it will maintain the status quo until we feel comfortable with removing the old behaviour altogether. Alternative: we introduce a new API version altogether, i.e. Behaviour after deprecationAll API calls will return the new format, and Consequences
ContextWhen service responses were introduced, the Now that weather forecast attributes have been deprecated, service calls are the only way to retrieve weather forecast data, but this doesn't work through the API. This leaves users with no way to retrieve it. See home-assistant/core#99820 as an example. ProposalWe will introduce a new endpoint: Request formatThe endpoint maintains the same request semantics as
Response formatIf the called service is either {
"state_changes": [
{
"attributes": {},
"entity_id": "sun.sun",
"last_changed": "2016-05-30T21:43:32.418320+00:00",
"state": "below_horizon"
},
{
"attributes": {},
"entity_id": "process.Dropbox",
"last_changed": "22016-05-30T21:43:32.418320+00:00",
"state": "on"
}
],
"service_response": {
"weather.new_york_forecast": {
"forecast": [
{
"condition": "clear-night",
"datetime": "2024-04-22T20:45:55.173725-04:00",
"precipitation_probability": 0,
"temperature": null,
"templow": 6.0
},
{
"condition": "rainy",
"datetime": "2024-04-23T20:45:55.173756-04:00",
"precipitation_probability": 60,
"temperature": 16.0,
"templow": 4.0
}
]
}
}
} If the service is {"message": "Service does not support response data."} Consequences
|
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 32 replies
-
I was just bitten by this today (see "user story" below) and I was considering implementing it myself before I found your PR with all the work done already, so thank you! As a REST API consumer, the separate endpoint vs. parameter issue doesn't matter that much, as long as this feature gets added. Though since that's what we're here to talk about, I do have some opinions to share:
User story: The removal of the weather forecast attribute in 2024.4 broke my wall-mounted weather forecast display. When I figured out why, I switched over to making a service call with the REST API, since my application is already using that. But then I found out that I can't get service response data with the REST API, and I really don't want to implement the WebSocket authentication handshake for such a simple application because (to my knowledge) HA doesn't support reading the API token from the pre-connection-upgrade |
Beta Was this translation helpful? Give feedback.
-
So far, this discussion is just restating what is in the PR, so I think making an actual proposal here would be useful. Getting into another level of detail here: The existing API returns a list of state changes and the service response output is a dictionary (output format described in option 3 here #948). I agree its odd to change the format so much based on the input parameters. Can you make a proposal for a new endpoint? |
Beta Was this translation helpful? Give feedback.
-
Well, the PR was my proposal, but an alternative was suggested and I'm not a project maintainer, so I came here to ask which option is preferable. Seems like the preference is for creating a new endpoint judging from all the responses, so I'll edit this into a proposal for that. |
Beta Was this translation helpful? Give feedback.
-
I was thinking about this some more. While I agree that changing the format of the response based on the query parameter is not great, the original behaviour of returning all state changes (even ones unrelated to the service call) was already weird at best when it was first added. However, I'm sure plenty of people rely on it, so I don't want to get rid of it. At the same time, creating an entirely new endpoint feels equally kludgy:
Lastly, people might want to have access to both the response data and the set of state changes. For these reasons, I'll propose a different change: deprecate the current format (with the usual HASS deprecation timeline), and introduce a new, more flexible response format that supports both state changes and service response data. I'll update the proposal accordingly. |
Beta Was this translation helpful? Give feedback.
-
Proposal updated. |
Beta Was this translation helpful? Give feedback.
-
The REST API has been superseded by the websocket API, and we don't want to add additional REST endpoints. If there's interest in an updated REST API, that should be offered as a custom integration. |
Beta Was this translation helpful? Give feedback.
-
Because you can't install home-assistant-cli on hosting server. Only run php and java scripts. |
Beta Was this translation helpful? Give feedback.
-
So, just chiming in with my 0.02e. There is a proposal by @akx to allow making WS calls over the REST API (#959), which unfortunately did not receive any interest when it was created. If I'm reading correctly, having something like that would allow simpler interfacing with the WS API (e.g., using curl or other http clients), and solve the original issue and similar use cases rather amicably without further changes or maintenance requirements to the REST API, also in the future. Any opinions on whether such approach would be acceptable? Discussing further on that should be done in the linked issue. |
Beta Was this translation helpful? Give feedback.
-
Long term, thats probably a better solution if we want to continue to support a REST API as we don't want to have to maintain two APIs any more than we are already doing now. Most of the REST APIs are not used internally anymore, and we have been moving as much as possible to the websocket api since its so much faster and flexible for our use cases. The flip side here is that we haven't documented many of the websocket apis because we want to have flexibility to change them without having to do long deprecations or relying on outside parties using them. We would have to move a bit slower and start documenting them if we wanted to do something like that which might be an unpopular position to take since it will generate significant more work. |
Beta Was this translation helpful? Give feedback.
-
We discussed this again in the core team and we've accepted the proposal. I've reopened + unlocked the implementation PR home-assistant/core#115046. Note: This doesn't mean that the exact details of the API changes as described in this proposal are necessarily OK, let's work those details out in the review of the implementation PR. |
Beta Was this translation helpful? Give feedback.
-
Thank you for fixing this! |
Beta Was this translation helpful? Give feedback.
We discussed this again in the core team and we've accepted the proposal. I've reopened + unlocked the implementation PR home-assistant/core#115046.
Note: This doesn't mean that the exact details of the API changes as described in this proposal are necessarily OK, let's work those details out in the review of the implementation PR.