Home Assistant and Garmin Connect – 2FA workaround

How to Use the Garmin Connect Integration in Home Assistant with Two-Factor Authentication (2FA)

If you are using the cyberjunky/home-assistant-garmin_connect custom integration for Home Assistant, you may have noticed a significant issue: the configuration flow currently does not support the entry of a two-factor authentication (2FA) code. This limitation can be frustrating for users who rely on the added security of 2FA for their Garmin Connect accounts.

Simple Workaround for 2FA Authentication Issue

Fortunately, there is a straightforward workaround that does not require modifying the integration itself. This solution takes advantage of the existing capabilities within the integration, specifically its use of the garth library.

The underlying login function already integrates the garth library, which is a major advantage. The garth library not only supports saving session tokens but also allows the integration to automatically load these tokens when needed. This means you can still use the integration with 2FA enabled on your Garmin Connect account by following a few simple steps.

Step-by-Step Guide to Using the Integration with 2FA

  1. Save Session Tokens Using Garth

    First, you need to save your session tokens with the garth library. Here’s a simple Python script to help you do this. Make sure to install the garth library first by running pip install garth.

    import garth
    from getpass import getpass
    
    email = input("Enter email address: ")
    password = getpass("Enter password: ")
    
    # If there’s MFA (multi-factor authentication), you’ll be prompted during the login process
    garth.login(email, password)
    garth.save("~/.garth")

    This script will generate and save your session tokens to ~/.garth, a hidden directory in your home directory. These tokens are valid for one year.

  2. Make Session Tokens Accessible to Home Assistant

    Depending on how you are running Home Assistant, you will need to make these tokens accessible:

    If you are running Home Assistant in Docker:

    • Copy the contents of the ~/.garth directory to a desired location.
    • Mount that directory in the Docker container. For example: /srv/docks/hass/.garth:/config/.garth.
    • Add an environment variable GARMINTOKENS that points to the path inside the container.

    If you are running Home Assistant in any other way:

    • Simply add an environment variable GARMINTOKENS that points to the directory where the tokens are stored.
  3. Configure the Integration as Usual

    Now, configure the Garmin Connect integration as you normally would if 2FA were not enabled. The saved tokens will be used automatically to authenticate your account.

  4. Enjoy Full Functionality with 2FA Enabled!

    Congratulations! You have successfully configured the integration to work with 2FA enabled, without needing to make any changes to the integration itself.

Final Thoughts

This workaround allows you to maintain the security of your Garmin Connect account while using the cyberjunky/home-assistant-garmin_connect integration in Home Assistant. The process is quick and easy, leveraging the built-in functionality of the garth library to save and manage session tokens.

If you found this guide helpful, please consider sharing it with others in the Home Assistant community!

One thought on “Home Assistant and Garmin Connect – 2FA workaround

  1. Sinan

    Thank you for the post.

    I’ve followed the instructions carefully and successfully generated the two token files:

    /homeassistant/.passcode/.garth/oauth2_token.json
    /homeassistant/.passcode/.garth/oauth1_token.json
    I’ve also configured the GARMINTOKENS environment variable in my configuration.yaml file as follows:

    YAML
    default_config:
    environment_variable:
    GARMINTOKENS: /homeassistant/.passcode/.garth
    Use code with caution.

    I’ve installed the HACS integration and configured it according to the documentation, but I’m still unable to connect to my Garmin devices. I’ve tried removing and re-adding the integration, but the issue persists.

    I’m wondering if there’s anything else I might be missing. Any guidance would be greatly appreciated.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *