Created
January 10, 2026 23:01
-
-
Save chrisboyle/bd674d6242c154fa0f15a51d7cc843bf to your computer and use it in GitHub Desktop.
Home Assistant addon API access via ingress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # This is an unsupported way to programmatically access endpoints within Home Assistant addons that use HA ingress. | |
| # I'd love to hear about any supported way to do this without opening ports for direct access to addons. | |
| # Based on https://github.com/patzly/grocy-android/blob/master/app/src/main/java/xyz/zedler/patrick/grocy/util/NetUtil.java | |
| import requests | |
| from homeassistant_api import WebsocketClient | |
| scheme = "ws" # wss if you have TLS | |
| homeassistant_addr = "homeassistant.local:8123" | |
| long_lived_token = "..." # create one at the bottom of https://my.home-assistant.io/redirect/profile_security/ | |
| addon_ingress_id = "..." # open a page from the addon in a new tab and this will become apparent | |
| path_within_addon = "/api/calendar/ical" # example from Grocy | |
| # Websocket because HTTP access to /api/hassio/ingress/session no longer works | |
| with WebsocketClient(f'{scheme}://{homeassistant_addr}/api/websocket', long_lived_token) as ws_client: | |
| cookie_result = ws_client.recv(ws_client.send('supervisor/api', method='post', endpoint='/ingress/session')) | |
| ingress_session = cookie_result.result['session'] | |
| response = requests.get(f'https://{homeassistant_addr}/api/hassio_ingress/{addon_ingress_id}{path_within_addon}', headers={'Cookie': f'ingress_session={ingress_session}'}) | |
| # If you're going to continue using the session for longer than a minute then you must call the validate session endpoint |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment