Skip to content

Fix token retrieval issue for Yi devices with non-hexadecimal tokens#2795

Merged
al-one merged 2 commits intoal-one:masterfrom
kuanpak:master
Mar 9, 2026
Merged

Fix token retrieval issue for Yi devices with non-hexadecimal tokens#2795
al-one merged 2 commits intoal-one:masterfrom
kuanpak:master

Conversation

@kuanpak
Copy link

@kuanpak kuanpak commented Mar 9, 2026

Fix below errors for Yi cameras and similar devices, the top-level token may not be valid hex, check if token is valid hexadecimal, otherwise try extra.token

ERROR (MainThread) [homeassistant.config_entries] Error setting up entry Xiaomi: ******** for xiaomi_miot
 Traceback (most recent call last):
   File "/usr/src/homeassistant/homeassistant/config_entries.py", line 769, in __async_setup_with_context
     result = await component.async_setup_entry(hass, self)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/config/custom_components/xiaomi_miot/__init__.py", line 233, in async_setup_entry
     await async_setup_xiaomi_cloud(hass, config_entry)
   File "/config/custom_components/xiaomi_miot/__init__.py", line 279, in async_setup_xiaomi_cloud
     device = await entry.new_device(d)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/config/custom_components/xiaomi_miot/core/hass_entry.py", line 99, in new_device
     await device.async_init()
   File "/config/custom_components/xiaomi_miot/core/device.py", line 184, in async_init
     self.local = MiotDevice.from_device(self)
                  ~~~~~~~~~~~~~~~~~~~~~~^^^^^^
   File "/config/custom_components/xiaomi_miot/core/device.py", line 1369, in from_device
     miio = AsyncMiIO(host, token)
   File "/config/custom_components/xiaomi_miot/core/mini_miio.py", line 34, in __init__
     self.token = bytes.fromhex(token)
                  ~~~~~~~~~~~~~^^^^^^^
 ValueError: non-hexadecimal number found in fromhex() arg at position 1

- fix Error setting up entry Xiaomi with non-hexadecimal number found in fromhex() arg
- error details: ERROR (MainThread) [homeassistant.config_entries] Error setting up entry Xiaomi: **masked_id** for xiaomi_miot
 Traceback (most recent call last):
   File "/usr/src/homeassistant/homeassistant/config_entries.py", line 769, in __async_setup_with_context
     result = await component.async_setup_entry(hass, self)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/config/custom_components/xiaomi_miot/__init__.py", line 233, in async_setup_entry
     await async_setup_xiaomi_cloud(hass, config_entry)
   File "/config/custom_components/xiaomi_miot/__init__.py", line 279, in async_setup_xiaomi_cloud
     device = await entry.new_device(d)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/config/custom_components/xiaomi_miot/core/hass_entry.py", line 99, in new_device
     await device.async_init()
   File "/config/custom_components/xiaomi_miot/core/device.py", line 184, in async_init
     self.local = MiotDevice.from_device(self)
                  ~~~~~~~~~~~~~~~~~~~~~~^^^^^^
   File "/config/custom_components/xiaomi_miot/core/device.py", line 1369, in from_device
     miio = AsyncMiIO(host, token)
   File "/config/custom_components/xiaomi_miot/core/mini_miio.py", line 34, in __init__
     self.token = bytes.fromhex(token)
                  ~~~~~~~~~~~~~^^^^^^^
 ValueError: non-hexadecimal number found in fromhex() arg at position 1
@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical issue where certain Xiaomi Miio devices, particularly Yi cameras, failed to initialize due to their primary token not being a valid hexadecimal string. The changes enhance the token retrieval logic by adding validation and a fallback mechanism, ensuring that devices can be set up correctly even with non-standard token formats, thereby improving device compatibility and system robustness.

Highlights

  • Token Validation: Implemented a check to validate if the retrieved device token is a valid hexadecimal string, preventing ValueError exceptions during device setup.
  • Fallback Mechanism: Introduced a fallback to use the token from the extra field if the primary token is found to be non-hexadecimal, specifically addressing issues with devices like Yi cameras.
Changelog
  • custom_components/xiaomi_miot/core/device.py
    • Modified the token property to include validation for hexadecimal format and a fallback to extra.token.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request effectively addresses the issue of non-hexadecimal tokens causing ValueError exceptions during device initialization for Yi cameras and similar devices. The implemented logic attempts to validate the primary token and falls back to an extra.token if the primary one is invalid. However, there's a minor logical flaw in the fallback mechanism that could still lead to an invalid token being returned.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@al-one al-one merged commit 57a164d into al-one:master Mar 9, 2026
5 checks passed
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.

2 participants