Home Assistant Web Proxy Library

0.0.7 · active · verified Tue Apr 14

hass-web-proxy-lib is a Python library designed to facilitate proxying web traffic through Home Assistant integrations. It provides utilities to generate proxy URLs and execute proxied requests, making external web services accessible securely within the Home Assistant ecosystem. The current version is 0.0.7, with an active development cadence typical for pre-1.0 libraries, meaning frequent updates and potential API changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate `WebProxy` and use its primary methods `async_get_proxy_url` and `async_proxy_request`. It provides mock objects for `hass` and `base_url` to make the code runnable without a full Home Assistant environment, but a live HA instance is required for actual web proxying to function correctly. The library is intended for use within Home Assistant custom components.

import asyncio
from hass_web_proxy.web_proxy import WebProxy

async def main():
    # In a real Home Assistant environment, 'hass' would be the HA instance
    # and 'base_url' would be the URL of the HA instance itself.
    # For this runnable example, we use mock objects to allow instantiation.
    # A real HA environment is required for actual proxying functionality.
    class MockHass:
        async def async_add_executor_job(self, func, *args):
            # Simulates an executor job, but doesn't run in a separate thread
            return await func(*args)
        
        @property
        def data(self):
            return {}

        class config:
            @staticmethod
            def path(resource):
                return "" # Dummy path

    mock_hass = MockHass()
    # In a real HA setup, this would be 'http://your_home_assistant_ip:8123'
    mock_base_url = "http://mocked-homeassistant.local:8123"

    # Instantiate the WebProxy. Requires a 'hass' object and the HA 'base_url'.
    # This library is specifically designed for Home Assistant custom components.
    proxy = WebProxy(hass=mock_hass, base_url=mock_base_url)

    # Example: Generating a proxy URL
    target_external_url = "https://api.example.com/data"
    integration_name = "my_custom_integration"
    proxy_request_path = "/external_api/get_data"

    proxy_url = proxy.async_get_proxy_url(
        integration_name=integration_name,
        external_url=target_external_url,
        request_path=proxy_request_path
    )
    print(f"Generated proxy URL: {proxy_url}")

    # Example: Making a proxied request
    # Note: This will likely fail outside a real Home Assistant instance
    # as it relies on HA's internal routing and aiohttp client.
    print("\nAttempting a proxied request (expected to fail without real HA):")
    try:
        response = await proxy.async_proxy_request(
            integration_name=integration_name,
            request_method="GET",
            request_path=proxy_request_path,
            request_headers={
                "User-Agent": "hass-web-proxy-lib-example",
                "Accept": "application/json"
            },
            request_body=None
        )
        print(f"Proxy request successful! Status: {response.status}")
        # You'd typically process response.text() or response.json() here
    except Exception as e:
        print(f"Proxy request failed as expected: {e}")

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →