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.

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).

Metadata

Release files for ha-browser-companion 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ha-browser-companion 0.1.1
File Size Uploaded Provenance Actions
ha_browser_companion-0.1.1.tar.gz 12.2 kB Without provenance Details for ha_browser_companion-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ha-browser-companion 0.1.1
File Interpreter ABI Platform Actions
ha_browser_companion-0.1.1-py3-none-any.whl Python 3 none any Details for ha_browser_companion-0.1.1-py3-none-any.whl

Total release size: 23.5 kB

Release files / ha_browser_companion-0.1.1.tar.gz

Download URL ha_browser_companion-0.1.1.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
58278d4191658344f84d8dfd543a584712660f2dc6ed23624c786ff7c3f3eeda
BLAKE2b-256 checksum
How to use checksums
5e041b177ea16a270c3d34871b413f14a6a7c200ca656e91a0bdf00ccad8ac5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / ha_browser_companion-0.1.1-py3-none-any.whl

Download URL ha_browser_companion-0.1.1-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
066b1b9e43f607a30f5ec54fd35c5783dbca766261caf34f4fa2bcf0d2573fb6
BLAKE2b-256 checksum
How to use checksums
5a57c3a49afdbeded487f52d3344af2960a2b4c1b03ae72b10f6c0d2f8c4fba3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for ha-browser-companion 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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