Fix: REST API sends 'Bearer empty' when token not configured in addon…#742
Conversation
Reviewer's GuideUpdates Home Assistant REST API integration to resolve tokens and URLs more robustly, using SUPERVISOR_TOKEN and a default supervisor URL when addon configuration fields are set to "empty", and ensures REST calls no longer send an invalid "Bearer empty" Authorization header. Updated class diagram for RetrieveHass token and URL handlingclassDiagram
class RetrieveHass {
- hass_url : str
- long_lived_token : str
- ha_config : dict
- logger
+ get_ha_config() async
+ _get_data_rest_api(var_list, start, end, header_auth, header_accept) async
}
%% get_ha_config behavior
class GetHAConfigLogic {
+ resolve_token(long_lived_token) str
+ resolve_url(hass_url) str
+ build_headers(token, header_auth, header_accept) dict
}
%% _get_data_rest_api behavior
class RestAPILogic {
+ resolve_token(long_lived_token) str
+ build_headers(token, header_auth, header_accept) dict
}
RetrieveHass --> GetHAConfigLogic : uses
RetrieveHass --> RestAPILogic : uses
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In
_get_data_rest_apithe function is annotated to returnNonebut now returnsFalsein an error case; consider either normalizing this to always returnNoneor updating the type/usage so callers don’t have to handle mixedNone/boolreturn types. - The log message in
get_ha_configstill says 'No Home Assistant URL or Long Lived Token found' even though the early-exit condition only checks for a missing token (URL may be resolved fromhass_url); updating this message will make troubleshooting clearer.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `_get_data_rest_api` the function is annotated to return `None` but now returns `False` in an error case; consider either normalizing this to always return `None` or updating the type/usage so callers don’t have to handle mixed `None`/`bool` return types.
- The log message in `get_ha_config` still says 'No Home Assistant URL or Long Lived Token found' even though the early-exit condition only checks for a missing token (URL may be resolved from `hass_url`); updating this message will make troubleshooting clearer.
## Individual Comments
### Comment 1
<location path="src/emhass/retrieve_hass.py" line_range="194-196" />
<code_context>
+ url_to_use = hass_url # default: http://supervisor/core/api
+
+ # If we still have no token after checking env, skip HA config retrieval
+ if not token:
self.logger.info(
"No Home Assistant URL or Long Lived Token found. Using only local configuration file."
)
</code_context>
<issue_to_address>
**issue:** The log message no longer matches the condition it’s guarding, which may mislead users when only the token is missing.
Since the early return now depends only on `not token`, the URL is guaranteed to be set (from `self.hass_url` or the default `hass_url`). The current log implying both URL and token are missing is therefore misleading. Please update the message to reflect only the missing token, or conditionally log based on which value is actually absent.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| if not token: | ||
| self.logger.info( | ||
| "No Home Assistant URL or Long Lived Token found. Using only local configuration file." |
There was a problem hiding this comment.
issue: The log message no longer matches the condition it’s guarding, which may mislead users when only the token is missing.
Since the early return now depends only on not token, the URL is guaranteed to be set (from self.hass_url or the default hass_url). The current log implying both URL and token are missing is therefore misleading. Please update the message to reflect only the missing token, or conditionally log based on which value is actually absent.
|
Sorry guys 1st time uploading, and Claude is helping. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #742 +/- ##
==========================================
- Coverage 81.03% 80.92% -0.11%
==========================================
Files 10 10
Lines 5611 5626 +15
==========================================
+ Hits 4547 4553 +6
- Misses 1064 1073 +9 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Fix: REST API authentication fails when token is set to "empty" in addon mode
Problem
When running EMHASS as a Home Assistant addon with
hass_urlandlong_lived_tokenboth set to
"empty"(the documented default for addon users), the REST API callsfail with a 401 Unauthorized error.
Root Cause
In
retrieve_hass.py, theget_ha_config()method correctly checks for"empty"token and skips HA — but
_get_data_rest_api()does not perform this check.It blindly uses
self.long_lived_tokendirectly in the Authorization header:This results in
Authorization: Bearer emptybeing sent to the HA API, causing a401 Unauthorized rejection every time.
Additionally,
get_ha_config()would skip HA entirely when token is"empty",rather than falling back to the
SUPERVISOR_TOKENenvironment variable thatHome Assistant injects automatically into addon containers.
Fix
Both
get_ha_config()and_get_data_rest_api()now:long_lived_tokenisNone, empty, or"empty"SUPERVISOR_TOKENenvironment variable(automatically injected by HA supervisor into addon containers)
Impact
hass_urlandlong_lived_tokenas"empty"— this is the default recommended configurationtoken is still required)
Testing
Tested on Home Assistant OS 2026.3.1 with EMHASS addon v0.17.0 running as a
supervised addon with both
hass_urlandlong_lived_tokenleft at default"empty".Summary by Sourcery
Handle Home Assistant addon authentication correctly when long-lived tokens are unset or configured as "empty" by falling back to supervisor-provided credentials instead of sending invalid REST headers.
Bug Fixes: