Skip to content

feat: optional client argument to IdasenDesk#492

Open
pedropombeiro wants to merge 2 commits into
newAM:mainfrom
pedropombeiro:pr/client-injection
Open

feat: optional client argument to IdasenDesk#492
pedropombeiro wants to merge 2 commits into
newAM:mainfrom
pedropombeiro:pr/client-injection

Conversation

@pedropombeiro
Copy link
Copy Markdown
Contributor

Summary

Add an optional client: BleakClient | None = None parameter to IdasenDesk.__init__. When provided, the supplied client is used as-is instead of letting the constructor create its own; otherwise the existing behavior is preserved.

Why

Downstream wrappers that manage their own BLE connection lifecycle currently have to subclass IdasenDesk (or reach into _client) to swap out the underlying BleakClient. The most common case is using bleak-retry-connector's establish_connection(), which returns a pre-connected client with caching and retry logic — useful for connections through Bluetooth proxies.

Letting IdasenDesk accept a pre-configured client removes the need for subclassing and makes the relationship explicit rather than fragile.

This was requested by abmantis/idasen-ha, which is the Home Assistant helper library that wraps this package.

Changes

In IdasenDesk.__init__:

  • Add client: Optional[BleakClient] = None argument
  • If client is None, the existing branch runs — a fresh BleakClient is created with the supplied disconnected_callback
  • If client is provided, it is stored as self._client as-is; disconnected_callback is ignored on the assumption that the caller has already configured it on the client they pass in (documented in the docstring)
  • self._mac is still derived from the mac argument so logging is unchanged

No other code paths are modified.

Tests

  • test_init_creates_default_client — without client, a BleakClient is created
  • test_init_uses_injected_client — when client is provided, the same instance is stored on self._client
  • test_init_ignores_disconnected_callback_when_client_provideddisconnected_callback is silently ignored when client is provided

All existing tests continue to pass.

Local CI checks:

$ uv run ruff check
All checks passed!
$ uv run ruff format --check
7 files already formatted
$ uv run mypy idasen tests
Success: no issues found in 5 source files
$ uv run pytest -vvv --cov=idasen --doctest-modules
95 passed

Notes

Add an optional `client: BleakClient | None = None` parameter to
`IdasenDesk.__init__`. When provided, the supplied client is used
as-is instead of letting the constructor create its own; otherwise
the existing behavior is preserved.

This is useful for downstream wrappers that manage their own BLE
connection lifecycle (e.g. via `bleak-retry-connector`'s
`establish_connection()`), and removes the need to subclass
`IdasenDesk` purely to swap out the underlying client.

When `client` is provided, `disconnected_callback` is ignored: the
caller is expected to configure that on the client they pass in.
This is documented in the docstring.
@coveralls
Copy link
Copy Markdown

coveralls commented May 26, 2026

Coverage Status

coverage: 89.136% (+0.05%) from 89.082% — pedropombeiro:pr/client-injection into newAM:main

Copy link
Copy Markdown
Owner

@newAM newAM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, just a minor formatting change. Do you need a release for this when merged? Do you have any other planned changes?

Comment thread CHANGELOG.md Outdated
Comment on lines +10 to +14
- Added an optional ``client`` argument to ``IdasenDesk.__init__`` for
injecting a pre-configured ``BleakClient``. Useful for downstream
wrappers that manage their own connection lifecycle (e.g. via
``bleak-retry-connector``). When provided, ``disconnected_callback``
is ignored.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be single backticks for markdown.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the backticks. @newAM sorry for missing this required change in the first PR. Yes, I'd need a new release with this change, there should be no more changes needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — fixed in 92e5608.

CHANGELOG.md is markdown; double-backtick reST inline-literal syntax
renders as literal backticks rather than inline code.
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.

3 participants