RyeGuy
(Ryan)
December 4, 2023, 7:08pm
1
According to the documentation , I should be able to use one API URL to handle multiple switches (and sensors). I am able to get multiple sensors to work, but when I add the switches and check my configuration I get the following error. Invalid config for [rest]: [switch] is an invalid option for [rest] . Below is a snippet of the code: (Note: I am using an include file to handle all my rest api)
(configuration.yaml)
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: !include sensors.yaml
switch: !include switch.yaml
template: !include templates.yaml
mqtt: !include mqtt.yaml
#command_line: !include command_line.yaml
#modbus: !include modbus.yaml
rest: !include rest.yaml
rest_command: !include rest_command.yaml
(rest.yaml)
- resource: http://10.100.100.225/state.xml
scan_interval: 10
method: GET
sensor:
- name: barn_furnace_heater_x310_input1
unique_id: 9ef03a74-d2e6-11ed-afa1-120420230001
value_template: '{{ value_json["datavalues"].input1state }}'
- name: barn_furnace_room_x310_sensor1
unique_id: 9ef03a74-d2e6-11ed-afa1-120320230101
unit_of_measurement: "°F"
value_template: '{{ value_json["datavalues"].sensor1 }}'
switch:
- name: barn_furnace_heater_x310_relay1
unique_id: 9ef03a74-d2e6-11ed-afa1-112420230001
value_template: '{{ value_json["datavalues"].relay1state }}'
turn_on:
- service: rest_command.x310_post_api
data:
item: "relay1state=1"
turn_off:
- service: rest_command.x310_post_api
data:
item: "relay1state=0"
tom_l
December 4, 2023, 7:11pm
2
The documentation you linked to is for restful sensors, not switches. Switches are documented here:
RyeGuy
(Ryan)
December 4, 2023, 7:33pm
3
It is my understanding if you have an API returning multiple sensors in the same “post” you could use the restful platform instead of the rest platform. The documentation you listed does work. I was trying to avoid calling the same API repeatedly for different switches and sensors and use the restful platform. According to the example in the restful documentation it seems switches and sensors can both be used. If I am misunderstanding this I would greatly appreciate somebody explaining this to me.
tom_l
December 4, 2023, 7:42pm
4
That example is incorrect. The rest integration only supports sensors and binary sensors:
https://www.home-assistant.io/integrations/rest/#sensor
I have created a PR to get that example corrected.
1 Like
RyeGuy
(Ryan)
December 4, 2023, 8:37pm
5
Thank you for the heads up, it’s much appreciated!
timr49
(Tim)
May 24, 2024, 9:44am
6
Do you know why the rest integration supports the sensor and binary_sensor platforms but not the switch platform? Looking at the code in components/rest, sensor.py and binary_sensor.py share common code (e.g. class RestData) to achieve this, but switch.py is fairly standalone. Similarly, the RESTful Switch integration is not as flexible as the RESTful Sensor and RESTful Binary Sensor integrations, e.g. RESTful Sensor and RESTful Binary Sensor support a template for ‘resource’ but RESTful Switch does not. Is this different treatment just a historical accident or is there a fundamental reason?
tom_l
May 24, 2024, 10:26am
7
Possibly because when @bdraco wrote this new functionality it was to fix an issue with multiple rest sensors using the same endpoint.
home-assistant:dev
← bdraco:rest_top_level
opened 09:10PM - 17 Feb 21 UTC
## Proposed change
<!--
Describe the big picture of your changes here to com… municate to the
maintainers why we should accept this pull request. If it fixes a bug
or resolves a feature request, be sure to link to that issue in the
additional information section.
-->
Add support for using a single endpoint for rest data
Bonus: `rest` now has 💯 line coverage.
## Type of change
<!--
What type of change does your PR introduce to Home Assistant?
NOTE: Please, check only 1! box!
If your PR requires multiple boxes to be checked, you'll most likely need to
split it into multiple PRs. This makes things easier and faster to code review.
-->
- [ ] Dependency upgrade
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New integration (thank you!)
- [x] New feature (which adds functionality to an existing integration)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [ ] Code quality improvements to existing code or addition of tests
## Example entry for `configuration.yaml`:
<!--
Supplying a configuration snippet, makes it easier for a maintainer to test
your PR. Furthermore, for new integrations, it gives an impression of how
the configuration would look like.
Note: Remove this section if this PR does not have an example entry.
-->
```yaml
# Example configuration.yaml
rest:
- resource: http://192.168.1.2:61208/api/3/all
binary_sensor:
- name: Horizon C Online
value_template: '{{ value_json[0].fs.online }}'
sensor:
- name: Horizon C Drive Used
value_template: '{{ value_json[0].fs.percent | round }}'
unit_of_measurement: "%"
- name: Horizon E Drive Used
value_template: '{{ value_json[0].fs.percent | round }}'
unit_of_measurement: "%"
- name: diskio_0
value_template: '{{ ( value_json[0].diskio ) }}'
json_attributes_path: "$.0"
json_attributes:
- time_since_update
- disk_name
- read_bytes
- write_bytes
- authentication: basic
username: admin
password: password
scan_interval: 60
resource: http://192.168.1.12/status.xml
sensor:
- name: Pool Data System
json_attributes_path: "$.response.system"
value_template: 'OK'
json_attributes:
- "runstate"
- "model"
- "opmode"
- "freeze"
- "time"
- "sensor1"
- "sensor2"
- "sensor3"
- "sensor4"
- "sensor5"
- "version"
- name: Pool Data Equipment
json_attributes_path: "$.response.equipment"
value_template: 'OK'
json_attributes:
- "circuit1"
- "circuit2"
- "circuit3"
- "circuit4"
- "circuit5"
- "circuit6"
- "circuit7"
- "circuit8"
- name: Pool Data Temp
json_attributes_path: "$.response.temp"
value_template: 'OK'
json_attributes:
- "htstatus"
- "poolsp"
- "spasp"
- "pooltemp"
- "spatemp"
- "airtemp"
```
## Additional information
<!--
Details are important, and help maintainers processing your PR.
Please be sure to fill out additional details, if applicable.
-->
- This PR fixes or closes issue: fixes #46182
- This PR is related to issue:
- Link to documentation pull request: https://github.com/home-assistant/home-assistant.io/pull/16632
## Checklist
<!--
Put an `x` in the boxes that apply. You can also fill these out after
creating the PR. If you're unsure about any of them, don't hesitate to ask.
We're here to help! This is simply a reminder of what we are going to look
for before merging your code.
-->
- [x] The code change is tested and works locally.
- [ ] Local tests pass. **Your PR cannot be merged unless tests pass**
- [ ] There is no commented out code in this PR.
- [ ] I have followed the [development checklist][dev-checklist]
- [ ] The code has been formatted using Black (`black --fast homeassistant tests`)
- [x] Tests have been added to verify that the new code works.
If user exposed functionality or configuration variables are added/changed:
- [x] Documentation added/updated for [www.home-assistant.io][docs-repository]
If the code communicates with devices, web services, or third-party tools:
- [ ] The [manifest file][manifest-docs] has all fields filled out correctly.
Updated and included derived files by running: `python3 -m script.hassfest`.
- [ ] New or updated dependencies have been added to `requirements_all.txt`.
Updated by running `python3 -m script.gen_requirements_all`.
- [ ] Untested files have been added to `.coveragerc`.
The integration reached or maintains the following [Integration Quality Scale][quality-scale]:
<!--
The Integration Quality Scale scores an integration on the code quality
and user experience. Each level of the quality scale consists of a list
of requirements. We highly recommend getting your integration scored!
-->
- [ ] No score or internal
- [ ] 🥈 Silver
- [ ] 🥇 Gold
- [ ] 🏆 Platinum
<!--
This project is very active and we have a high turnover of pull requests.
Unfortunately, the number of incoming pull requests is higher than what our
reviewers can review and merge so there is a long backlog of pull requests
waiting for review. You can help here!
By reviewing another pull request, you will help raise the code quality of
that pull request and the final review will be faster. This way the general
pace of pull request reviews will go up and your wait time will go down.
When picking a pull request to review, try to choose one that hasn't yet
been reviewed.
Thanks for helping out!
-->
To help with the load of incoming pull requests:
- [ ] I have reviewed two other [open pull requests][prs] in this repository.
[prs]: https://github.com/home-assistant/core/pulls?q=is%3Aopen+is%3Apr+-author%3A%40me+-draft%3Atrue+-label%3Awaiting-for-upstream+sort%3Acreated-desc+review%3Anone
<!--
Thank you for contributing <3
Below, some useful links you could explore:
-->
[dev-checklist]: https://developers.home-assistant.io/docs/en/development_checklist.html
[manifest-docs]: https://developers.home-assistant.io/docs/en/creating_integration_manifest.html
[quality-scale]: https://developers.home-assistant.io/docs/en/next/integration_quality_scale_index.html
[docs-repository]: https://github.com/home-assistant/home-assistant.io