Skip to content

Fix librouteros login_method argument#489

Closed
alvarodgarcia wants to merge 1 commit into
tomaae:masterfrom
alvarodgarcia:codex/librouteros-login-method
Closed

Fix librouteros login_method argument#489
alvarodgarcia wants to merge 1 commit into
tomaae:masterfrom
alvarodgarcia:codex/librouteros-login-method

Conversation

@alvarodgarcia
Copy link
Copy Markdown

@alvarodgarcia alvarodgarcia commented May 5, 2026

Summary

  • Update the RouterOS API connection kwargs from login_methods to login_method.
  • Convert configured string login methods (plain/token) to the functions expected by newer librouteros.

Why

Newer librouteros versions reject the old login_methods keyword with:

connect() got an unexpected keyword argument 'login_methods'. Did you mean 'login_method'?

This prevents the integration from connecting after Home Assistant loads a newer librouteros in the environment.

Validation

  • Reproduced on Home Assistant Core 2026.4.4 with librouteros exposing login_method.
  • Applied the same patch locally and verified the integration reconnects and Mikrotik sensors update.
  • Ran python3 -m py_compile custom_components/mikrotik_router/mikrotikapi.py.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 5, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Update Mikrotik Router integration to use the new librouteros login_method API and map configured string login-method values to the expected callable functions, restoring compatibility with newer librouteros versions.

Sequence diagram for MikrotikApi connect using new librouteros login_method

sequenceDiagram
    actor HomeAssistant
    participant MikrotikApi
    participant librouteros
    participant librouteros_login as librouteros_login_module

    HomeAssistant->>MikrotikApi: connect()
    MikrotikApi->>MikrotikApi: read _login_method
    alt login_method is plain string
        MikrotikApi->>librouteros_login: resolve plain
        librouteros_login-->>MikrotikApi: plain function
    else login_method is token string
        MikrotikApi->>librouteros_login: resolve token
        librouteros_login-->>MikrotikApi: token function
    else login_method already function
        MikrotikApi->>MikrotikApi: use existing callable
    end
    MikrotikApi->>librouteros: connect(encoding, login_method, port)
    librouteros-->>MikrotikApi: connection
    MikrotikApi-->>HomeAssistant: connection established
Loading

Class diagram for MikrotikApi connect login_method handling

classDiagram
    class MikrotikApi {
        - _encoding
        - _login_method
        - _port
        - _connected
        - _connection_epoch
        + connect() bool
    }

    class librouteros_login {
        + plain
        + token
    }

    class librouteros_client_factory {
        + connect(encoding, login_method, port)
    }

    MikrotikApi ..> librouteros_login : uses
    MikrotikApi ..> librouteros_client_factory : calls connect with login_method
Loading

File-Level Changes

Change Details Files
Switch RouterOS connection code to use the new login_method kwarg with function objects instead of the deprecated login_methods kwarg with string values.
  • Import the plain and token login functions from librouteros.login.
  • Translate the stored string login method ("plain" or "token") to the corresponding librouteros.login function before connecting.
  • Update the connection kwargs to use the singular login_method key instead of login_methods, passing the resolved function object.
custom_components/mikrotik_router/mikrotikapi.py

Possibly linked issues

  • #[Bug] Cannot connect to Mikrotik API: unexpected keyword 'login_methods': PR updates kwargs and maps string login methods to callables, exactly fixing the described librouteros breaking change.
  • #[Bug] Problems with last versions: PR updates librouteros connection to use login_method, directly fixing the reported login_methods argument error.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Consider handling unexpected self._login_method values (e.g., logging a warning or falling back to a default) so misconfigurations don’t silently result in unusable login_method arguments.
  • If self._login_method can already be a callable (e.g., set elsewhere in code), you may want to skip the string-to-function mapping when it’s not a string to avoid unnecessarily overriding a valid login function.
  • You might want to normalize/resolve self._login_method to the appropriate function once at initialization or configuration time instead of on every connect() call to keep connection logic simpler and avoid repeated mapping.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider handling unexpected `self._login_method` values (e.g., logging a warning or falling back to a default) so misconfigurations don’t silently result in unusable `login_method` arguments.
- If `self._login_method` can already be a callable (e.g., set elsewhere in code), you may want to skip the string-to-function mapping when it’s not a string to avoid unnecessarily overriding a valid login function.
- You might want to normalize/resolve `self._login_method` to the appropriate function once at initialization or configuration time instead of on every `connect()` call to keep connection logic simpler and avoid repeated mapping.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@alvarodgarcia alvarodgarcia force-pushed the codex/librouteros-login-method branch from 85e67fb to 52ffd91 Compare May 5, 2026 12:33
carpenike added a commit to carpenike/homeassistant-mikrotik_router that referenced this pull request May 7, 2026
librouteros 4.0 (released 2026-04) made two breaking changes that prevent
this integration from connecting:

1. The 'login_methods' (plural) keyword argument was renamed to
   'login_method' (singular). Old code raises:
     connect() got an unexpected keyword argument 'login_methods'.
     Did you mean 'login_method'?

2. The value must now be a callable (e.g. librouteros.login.plain), not
   the legacy string 'plain'. Old code raises:
     'str' object is not callable

Anyone with a fresh HA environment will resolve librouteros 4.x via pip
because our requirement is '>=3.4.1' with no upper bound. Without this
fix the integration is unusable on those installs.

Fix (adapted from upstream PR tomaae#489 by @alvarodgarcia):
  - Import librouteros.login.plain and librouteros.login.token at module
    load (these symbols also exist in librouteros 3.x).
  - In connect(), if self._login_method is still a string from saved
    config, translate 'plain'/'token' to the matching callable. Unknown
    string values log a warning and fall back to plain. Already-callable
    values pass through unchanged.
  - Rename the kwarg to 'login_method' (singular) — the canonical name
    in both librouteros 3.x and 4.x.

DEFAULT_LOGIN_METHOD in const.py is left as the string 'plain' on
purpose: it is what gets persisted into existing config entries, so
keeping the string + runtime translation preserves backward-compat for
all existing installs without a migration.
@github-actions
Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@github-actions github-actions Bot added the stale label May 20, 2026
@github-actions github-actions Bot closed this May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant