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.1"]
}

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 and companion_href are the same sidebar panel URL (/{slug}, or an absolute URL from Home Assistant get_url).

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.1.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.1-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ha_browser_companion-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 58278d4191658344f84d8dfd543a584712660f2dc6ed23624c786ff7c3f3eeda
MD5 ad51de52c44b96e1ba597dcd0e44b5d3
BLAKE2b-256 5e041b177ea16a270c3d34871b413f14a6a7c200ca656e91a0bdf00ccad8ac5e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ha_browser_companion-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 066b1b9e43f607a30f5ec54fd35c5783dbca766261caf34f4fa2bcf0d2573fb6
MD5 e78d06e6fa575c0a7dcb7219d8736ae5
BLAKE2b-256 5a57c3a49afdbeded487f52d3344af2960a2b4c1b03ae72b10f6c0d2f8c4fba3

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 files

0.1.0

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