Skip to content

Decouple from Home Assistant with an AbstractAuth interface - #460

Open
Moustachauve wants to merge 3 commits into
dvd-dev:mainfrom
Moustachauve:refactor/abstract-auth
Open

Moustachauve wants to merge 3 commits into
dvd-dev:mainfrom
Moustachauve:refactor/abstract-auth

Conversation

@Moustachauve

@Moustachauve Moustachauve commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Replace Home Assistant OAuth2Session dependency with AbstractAuth.

pyhilo.api imported homeassistant at module level, but homeassistant is not declared as a package dependency. This caused ModuleNotFoundError whenever importing pyhilo in standard Python environments (such as CLI tools or isolated tests), and required artificial monkey-patching in conftest.py for local tests to pass.

To eliminate this circular coupling, API now relies on an AbstractAuth protocol following Home Assistant's library guidance. For compatibility with existing integration releases, passing oauth_session is temporarily retained and adapted via duck-typing, but logs a deprecation warning and should be removed after a couple of releases.

pyhilo.api imported homeassistant.helpers.config_entry_oauth2_flow at
module level, but homeassistant is not (and should not be) a dependency.
Importing pyhilo outside Home Assistant failed with ModuleNotFoundError,
and the test suite only passed because conftest.py injected fake
homeassistant modules into sys.modules.

API now takes an auth argument implementing pyhilo.auth.AbstractAuth,
following Home Assistant's recommended pattern for API libraries: the
library defines the interface, and the integration wraps its own
OAuth2Session. The URN is decoded from the last token returned by the
auth provider instead of reading the session directly.

The oauth_session argument is still accepted, adapted internally and
logged as deprecated, because already-released integration versions
declare python-hilo>= and would otherwise break on a fresh install. It
should be removed after a couple of releases.

The raw access token is no longer logged at DEBUG level, since it ended
up in Home Assistant logs.
Custom test stub classes have been replaced with standard unittest.mock
fixtures and AsyncMock/MagicMock assertions to eliminate boilerplate.

Edge cases for URN claim extraction and invalid auth initialization
parameters are now parametrized with matching error assertions.
@Moustachauve
Moustachauve marked this pull request as ready for review October 5, 2026 03:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant