Fix token retrieval issue for Yi devices with non-hexadecimal tokens#2795
Fix token retrieval issue for Yi devices with non-hexadecimal tokens#2795al-one merged 2 commits intoal-one:masterfrom
Conversation
- 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
Summary of ChangesHello, 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
Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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>
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