Skip to main content

ha-browser-companion

Python client for the Browser Companion add-on. This directory is the PyPI package; the add-on itself is ../browser_companion.

pip install ha-browser-companion

In manifest.json:

{
  "requirements": ["ha-browser-companion>=0.1.0"]
}

Depends on aiohttp (already in Home Assistant). Home Assistant itself is not a package dependency: discovery and the config-flow mixin import it only when those helpers run.

Config flow (recommended)

Most of the work is the progress/wait/retry UI. Subclass CompanionLoginFlow and implement two methods:

from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from ha_browser_companion import CompanionLoginFlow, CompanionStart, captured_query

from .const import DOMAIN, WAIT

class MyConfigFlow(CompanionLoginFlow, ConfigFlow, domain=DOMAIN):
    VERSION = 1
    companion_client_id = DOMAIN

    async def async_step_user(self, user_input=None) -> ConfigFlowResult:
        if not self.companion_supervisor_present():
            return self.async_abort(reason="companion_requires_supervisor")
        return await self.async_step_companion()

    async def async_companion_start(self) -> CompanionStart:
        return CompanionStart(
            start_url="https://example.com/login",
            wait=WAIT,  # same payload as POST /v1/sessions
        )

    async def async_companion_finish(self, captured: dict) -> ConfigFlowResult:
        code = captured_query(captured, "code")
        if not code:
            return await self.async_step_companion_failed()
        return self.async_create_entry(title="Example", data={"code": code})

Optional: navigate_after and success_message on CompanionStart; async_companion_on_close() for extra cleanup (close an OAuth helper, drop PKCE state, …).

Cookie capture (Allegro-style):

from ha_browser_companion import captured_cookie

cookie = captured_cookie(captured, "QXLSESSID")

Translations

The mixin uses these keys. Copy them and change the surrounding sentences:

{
  "config": {
    "step": {
      "companion": {
        "title": "Browser Companion",
        "description": "Open Browser Companion in a new window:\n\n[{companion_url}]({companion_href})\n\nSign in there. This screen waits until the add-on captures the result."
      },
      "companion_failed": {
        "title": "Browser Companion",
        "description": "Could not sign in with Browser Companion. Start the add-on, then submit to try again.\n\n[{companion_url}]({companion_href})"
      }
    },
    "progress": {
      "companion_wait": "Open Browser Companion in a new window:\n\n[{companion_url}]({companion_href})\n\nThis screen updates when the login is captured."
    },
    "abort": {
      "companion_requires_supervisor": "This integration needs Home Assistant OS or Supervised with the Browser Companion add-on."
    }
  }
}

Placeholders: companion_url (Ingress path or absolute URL), companion_href (my.home-assistant.io redirect that opens the sidebar panel).

Low-level client

If you already have a config flow and only need HTTP:

from ha_browser_companion import (
    CompanionError,
    async_create_session,
    async_delete_session,
    async_discover_companion,
    async_wait_captured,
)

found = await async_discover_companion(hass)
if not found:
    raise RuntimeError("Browser Companion add-on is not running")

created = await async_create_session(
    session,
    found.base_url,
    start_url=auth_url,
    wait={"event": "http_redirect", "location_prefixes": ["app://callback"]},
    client_id="my_integration",
)
captured = await async_wait_captured(session, found.base_url, created["id"])
code = captured["query"]["code"]
await async_delete_session(session, found.base_url, created["id"])

BrowserCompanionClient(session, base_url) is the same API as methods.

async_discover_companion lists Supervisor add-ons whose slug ends with browser_companion, then probes GET /v1/health. async_wait_captured polls about every 2 seconds until captured, or raises CompanionError (expired, timeout, browser_unavailable, …).

Wait rules

Same JSON as the add-on session API. Typical patterns:

OAuth redirect to a custom scheme

{"event": "http_redirect", "status_codes": [302], "location_prefixes": ["app://callback"]}

Navigation + cookies

{
    "event": "navigation",
    "url_prefixes": ["https://example.com/account"],
    "cookies": ["SESSION"],
}

Full request/response reference: add-on README.

Publish to PyPI

From python/ (after tests pass):

cd python
pip install build twine
python -m build
twine upload dist/*

Or create a GitHub Release: .github/workflows/publish.yml publishes with trusted publishing. On PyPI, add a trusted publisher for this GitHub repository (workflow publish.yml).

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ha_browser_companion-0.1.0.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ha_browser_companion-0.1.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file ha_browser_companion-0.1.0.tar.gz.

File metadata

  • Download URL: ha_browser_companion-0.1.0.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.3

File hashes

Hashes for ha_browser_companion-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2d9c6514845e71d23f195aded23f101c28a8c306dd98aa09454ab68bbef16935
MD5 5eda93d7baf13f59d67b6a57b98ab612
BLAKE2b-256 6bb22211521ef4e6ff0fc8e480ac230d5429f4058dca59044d13a90bca34b273

See more details on using hashes here.

File details

Details for the file ha_browser_companion-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ha_browser_companion-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9316f360996444a44fc0820c0a6f9a195c750af94414937ee59bf7de05342df2
MD5 b4497f00577c8bcb80bb8a6e77e3e4d6
BLAKE2b-256 5e84ef8d86ba222978f4e8faea8a1aa012edebb1adabaca6c56bcb0508d0ddcc

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.1

2 files

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page