Trouble with Websocket_api

Hi! I’m trying to connect HA with another service (“Multisystem reactor” aka MSR, similiar to Node red). But when MSR tries to connect to HA I only get this:

2021-03-14T15:20:27.492Z HassController:NOTICE HassController#hass attempting reconnection to ws://192.168.50.167:8123/api/websocket
2021-03-14T15:20:27.492Z HassController:NOTICE HassController#hass connecting to ws://192.168.50.167:8123/api/websocket
2021-03-14T15:20:27.501Z HassController:WARN HassController#hass socket error during open: Error: socket hang up
2021-03-14T15:20:27.502Z HassController:NOTICE HassController#hass socket to ws://192.168.50.167:8123/api/websocket closed during open
2021-03-14T15:20:27.505Z HassController:ERR HassController#hass connect error with ws://192.168.50.167:8123/api/websocket: websocket error
2021-03-14T15:20:27.505Z HassController:WARN HassController#hass failed to connect/initialize communication: websocket error

In my configuration.yaml I have the line:
websocket_api:

I have all the latest versions of everything:

Version core-2021.3.4
Installation Type Home Assistant OS
Development false
Supervisor true
Docker true
Virtual Environment false
Python Version 3.8.7
Operating System Family Linux
Operating System Version 5.4.99
CPU Architecture x86_64
Timezone Europe/Stockholm
Host Operating System Home Assistant OS 5.12
Update Channel stable
Supervisor Version supervisor-2021.03.6
Docker Version 19.03.13
Disk Total 30.8 GB
Disk Used 16.8 GB
Healthy true
Supported true
Board ova
Supervisor API ok
Version API ok

Why wont it work? What am I missing? Thanks in advance!
Best regards,
/Fanan

HA’s websocket implementation includes an authentication phase as the first connection. So, whatever tool you are using has to be able to support that initial authentication phase:

Server states#

    Client connects.
    Authentication phase starts.
        Server sends auth_required message.
        Client sends auth message.
        If auth message correct: go to 3.
        Server sends auth_invalid. Go to 6.
    Send auth_ok message
    Authentication phase ends.
    Command phase starts.
        Client can send commands.
        Server can send results of previous commands.
    Client or server disconnects session.

Hi @code-in-progress! MSR is used by many more, and they don’t have this problem - so MSR is fully capable. I have followed every guide and every step. I didn’t mention, but I have created a long time access token, which should do the trick. I just can’t get my head around this…

Huh… From the log, it looks like it can’t open the port.

Can you open https://www.websocket.org/echo.html and test your HA websocket connection? Down in location, enter in ws://192.168.50.167:8123/api/websocket and see if you can connect to it.

No, it won’t connect. Another question related to this; do I have to install any integration or add-on to make this work? I have only added the single line to the configuration.yaml file.
I’m using duckdns and Lets encrypt. Maybe thats messing with my websocket?
I feel like a total newbee here…

Sorry about the delay! I didn’t see your reply.

No, you shouldn’t need any additional configuration. When you tried the websocket.org page, did you see any error messages pop up in your logs?

Can you try the test again and then when you hit the connect button, look at your logs and see if any error messages pop up?

Hi @code-in-progress! No need for excuses! I’m grateful for all help I can get!

When I test:
ws://192.168.50.167:8123/api/websocket
nothing happens - it´s like I don’t have pushed the connect button.
The example: wss://echo.websocket.org work just fine. So it´s something wrong with my adress I guess. I have portforwarded 192.168.50.167:8123 in my router, so that shouldn’t be the error. Getting crazy here…

Welcome to HA? :wink:

You shouldn’t have to forward anything. The code on that page actually runs locally through javascript.

Ok. Let’s try another approach: On WebSocket API | Home Assistant Developer Docs there are three links near the top. Click on the Python link. It’ll open a new tab with some raw python in it. Copy that to a file on your local system and name it something like haws.py. Change the token in the line starting with ACCESS_TOKEN to use your token and change the line websocket = await asyncws.connect('ws://localhost:8123/api/websocket') to the IP address of your HA server. Then, run the file using python3 haws.py. If all goes well, you’ll see something like this:

Ok. I typed in the command in the Terminal in HA. This is what came out:
image

Try running pip install asyncio (you might need to run it as pip3 install asyncio) and then running the command again.

I did, and I even ran pip install asyncws.
This is the result:

Hmmmm… are you certain that the IP is correct? If that python code fails, then there’s no socket listening at that port. What IP do you use to hit HA in your browser? Is it the same?

Yes, it’s the same. Here is a snapshot of my browser:
image

Hmmmm, ok. Let me check a couple of things because that websocket should be opened by default.

1 Like

@code-in-progress - did any lightbulbs turn on above your head? :slight_smile:
Have a great day!

Hi. When I look in the Supervisor log, this is what I see:

`21-04-06 05:56:47 ERROR (MainThread) [asyncio] Task exception was never retrieved
future: <Task finished name='Task-48091' coro=<HomeAssistantWebSocket.async_send_command() done, defined at /usr/src/supervisor/supervisor/homeassistant/websocket.py:101> exception=TypeError('Received message 257:None is not str')>
Traceback (most recent call last):
  File "/usr/src/supervisor/supervisor/homeassistant/websocket.py", line 129, in async_send_command
    return await self._client.async_send_command(message)
  File "/usr/src/supervisor/supervisor/homeassistant/websocket.py", line 46, in async_send_command
    response = await self.client.receive_json()
  File "/usr/local/lib/python3.8/site-packages/aiohttp/client_ws.py", line 291, in receive_json
    data = await self.receive_str(timeout=timeout)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/client_ws.py", line 276, in receive_str
    raise TypeError(f"Received message {msg.type}:{msg.data!r} is not str")
TypeError: Received message 257:None is not str`

Don’t know if it´s to any help to understand the problem…
I´m so greatful for any help. Thanks!
/Fanan

Fanan, i have almost the same error as yours in the supervisor log, and im not using any MSR service. So i dont think is something non related to that svc.

Too bad I don’t have any answers…

If I only knew why.

Hi @code-in-progress! Did you find anything?
My HA installation was on a VM on a PC. I the installed it on a RPi 4 - and that was ok as long as I didn’t restore a snapshot. Then it had the same issues.

From your browser screenshot, it seems you have https enabled. Is it correct?

If so, the protocol is wss://, and, as it seems you use some self-signed certificate, you have to be sure to ignore the certificate errors in whatever client.