Alright I finally implemented a proper workaround, it’s still a little rough around the edges and needs proper documentation but I implemented my own addon to get around this issue.
This basically hosts a small node server which can proxy API calls and provide failover responses if the target API isn’t reachable. It’s still work in progress, but it’s effectively working.
If anyone is interested to try it out, you can add my development addon repository.
If you want to wait for a beta or stable release you can simply add my beta or main repository
# development
https://github.com/shawly/hassio-addons-dev
# beta
https://github.com/shawly/hassio-addons-beta
# stable
https://github.com/shawly/hassio-addons
After installation you can open the web UI to get the IP or domain of the api gateway.
A new folder named api-scapegoat will be created in your config/ folder with some example files so you get a basic idea of how to configure it.
For example this is a config for my TVs API:
---
route: /sony
target: 'http://192.168.0.200'
failover:
response:
status_code: 202
headers:
content-type: application/json
server: API Scapegoat
body: '{ "error": [7, "not power-on"], "id": 40 }'
This basically means that every request to “<id>-api-scapegoat.local:3000/sony/" will be forwarded to "http://192.168.0.200/sony/”, and if 192.168.0.200 is offline, the failover response will be sent instead.
And here is a sensor I use to get the current picture setting from the TV:
############# Current Scene Sensor ##########
- platform: rest
name: Sony Bravia TV Current Scene
resource_template: 'http://61b09487-api-scapegoat:3000/sony/videoScreen'
headers:
x-auth-psk: !secret bravia_psk
method: POST
payload: '{"method":"getSceneSetting","params":[""],"id":40,"version":"1.0"}'
value_template: >
{%- if value_json.result[0].error is defined -%}
error
{%- else -%}
{{- value_json.result[0].currentValue -}}
{%- endif -%}
This way the sensor will make the request against the internal address of the container of the addon, you can also always expose the port via the configuration and add your homeassistant IP or domain if you prefer.
Now if the TV is online and reachable, the API will simply forward the request to the TV and return the response 1:1 without any changes.
If the power of the TV is cut off, the API will instead return:
{ "error": [7, "not power-on"], "id": 40 }
With an http status code of 202, so the rest sensor will not interpret it as a failed request.
Et voilà, your home assistant log will not be spammed anymore.
I’m planning to experiment a little more with this addon, technically it’s much more powerful since you could transform requests of APIs as well.