core: Music Assistant: cannot connect when MA server is correctly SSL-wrapped
UPDATE: it was an SSL CA error. The error is not logged or displayed. It should be. I think this ticket should be closed once the error is displayed.
(For comparison, I had the same error with the Frigate custom integration, and yes, it logs the error to the HA log, although it also says “Cannot connect” and gives no further details in the UI, just like this integration.)
The problem
I’ve got Music Assistant correctly running with HTTPS proxying of its frontend / server on port 443 (decrypting and proxy-passing to port 8095 locally), served as https://music.dragonfear. I can set up MASS custom integration no problem with that. However, the recently-released built-in HA integration does not seem to support this, and I think I’ve traced the problem to this code in music_assistant_client/connection.py:
def get_websocket_url(url: str) -> str:
"""Extract Websocket URL from (base) Music Assistant URL."""
if not url or "://" not in url:
msg = f"{url} is not a valid url"
raise RuntimeError(msg)
ws_url = url.replace("http", "ws")
if not ws_url.endswith("/ws"):
ws_url += "/ws"
return ws_url.replace("//ws", "/ws")
class WebsocketsConnection:
"""Websockets connection to a Music Assistant Server."""
def __init__(self, server_url: str, aiohttp_session: ClientSession | None) -> None:
"""Initialize."""
self.ws_server_url = get_websocket_url(server_url)
self._aiohttp_session_provided = aiohttp_session is not None
self._aiohttp_session: ClientSession | None = aiohttp_session or ClientSession()
self._ws_client: ClientWebSocketResponse | None = None
As result of get_websocket_url(), the URL is never actually set up to be wss when the URL passed on the configuration interface is https.
However, wss://music.dragonfear/ws doesn’t work either. Nor does wss://music.dragonfear.
I can verify that the HA machine can retrieve https://music.dragonfear just fine, even from the Python used in the HA setup. No errors appear on the log either. It would be fantastic if the actual error was in the log!
What version of Home Assistant Core has the issue?
core-2024.12.1
What was the last working version of Home Assistant Core?
No response
What type of installation are you running?
Home Assistant Core
Integration causing the issue
music_assistant
Link to integration documentation on our website
No response
Diagnostics information
No response
Example YAML snippet
No response
Anything in the logs that might be useful for us?
Sadly no. The only debug log entry from the integration that I get is `Trying to connect`. Nothing useful happens after that.
Additional information
No response
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 16 (2 by maintainers)
Wanted to chime in here that I wasted my entire evening on this as well. I have Home Assistant and Music Assistant running on Kubernetes with ingress-nginx and cert-manager providing reverse proxying and TLS certificates issued by my internal CA. Actually went down the same debugging rabbit hole, finding that it worked fine with
curl wss://my-ma.domainas well as from a minimal Pythonwebsocketsexample. I was also looking with suspicion at that logic to construct the websocket URL, but found that it worked fine in the end. I had already spent some time writing aninitContainerscript to add my root CA certificate to the system trust store without root access to the pod, but hadn’t circled back to theREQUESTS_CA_BUNDLEvariable: didn’t realize it was using avenvand didn’t realize that avenvwon’t respect the same certificates. In the end, I simply had to point that environment variable at the existing system trust store, and the MA integration is working perfectly. My thanks to @Rudd-O for filling in the last piece for me 🖤So the error in the integration config flow in the UI just says “Failed to connect” with no additional information. This error appears if I enter
https://my-ma.domainorwss://my-ma.domainorhttps://my-ma.domain/ws, or any permutation of those, or if I enter nonsense likeoisdhgapsdifjpoij. Same error in the UI. I spent a lot of time trying to get the Home Assistant logger to give me any information about what was happening, but eventually realized that the logs were going to stay silent and I’d have to debug the issue completely in the dark.Agree that it would be great for HA to make this error visible to users; I’d be happy to provide more context on this thread or wherever the issue ends up, and perhaps contribute a fix, though I’ve never contributed to HA. I’m mostly leaving this very verbose comment so that the next unfortunate person struggling with a “Failed to connect” error while setting up the Music Assistant integration with an SSL/TLS reverse proxy can find this thread more easily ^-^
The error is being swallowed and not logged, but what was going on is that the HTTPS certificate was not trusted by the Python virtual environment (created with
-m venv). Normal system Python has no issue.Setting
ENVIRONMENT=REQUESTS_CA_BUNDLE=/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pemin the Home Assistant service file solved the problem.I keep this bug open because this type of error should be logged to the system log in detail, instead of being swallowed.