Websocket API and unknown_error?

Hi guys, I’m trying to use the websocket API, I can authenticate just fine but when I try to send any subsequent command (except for get_config ) I get a unkwon_error response (example: { id: 1, type: 'subscribe_events' } . Am I missing something obvious?

If I send the get_config command I get back the config as expected.

In the logs I see:


Dec 22 10:59:28 home-assistant hass[16749]:   File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/websocket_api/connection.py", line 66, i
Dec 22 10:59:28 home-assistant hass[16749]:     handler(self.hass, self, schema(msg))
Dec 22 10:59:28 home-assistant hass[16749]:   File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/websocket_api/commands.py", line 161, in
Dec 22 10:59:28 home-assistant hass[16749]:     entity_perm = connection.user.permissions.check_entity
Dec 22 10:59:28 home-assistant hass[16749]: AttributeError: 'NoneType' object has no attribute 'permissions'

For completeness, here’s my code:

const ws = new WebSocket('wss://xxxxxxxxxx/api/websocket');
const j = JSON.stringify;

let id = 1;

ws.onmessage = e => {
  const data = JSON.parse(e.data);
  switch (data.type) {
    case 'auth_required':
      ws.send(
        j({
          type: 'auth',
          api_password: 'xxxxxxxxx',
        })
      );
      break;
    case 'auth_ok':
      ws.send(
        j({
          id: id++,
          type: 'subscribe_events',
        })
      );
      break;
    default:
      alert(e.data);
  }
};

I am seeing this regression as well. 0.83.3 is the last working version. My logs:

2018-12-22 10:11:36 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection.139957754689576] Received invalid command
2018-12-22 10:11:36 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection.139957754689576] Error handling message: {‘event_type’: ‘state_changed’, ‘type’: ‘subscribe_events’, ‘id’: 22}
Traceback (most recent call last):
File “/usr/local/lib/python3.6/dist-packages/homeassistant/components/websocket_api/connection.py”, line 66, in async_handle
handler(self.hass, self, schema(msg))
File “/usr/local/lib/python3.6/dist-packages/homeassistant/components/websocket_api/commands.py”, line 102, in handle_subscribe_events
if not connection.user.is_admin:
AttributeError: ‘NoneType’ object has no attribute ‘is_admin’

Thanks for the reply!

I noticed it works just fine if I use access_token “stolen” form the front-end.

Now I’m trying to implement a proper authentication system but after I get an authorization code and try to request an access_token I get unsupported_grant_type :frowning:

I experience the same ‘unknown error’ when I subscribe to event bus using api_password during authentication. This error occurs in my current version 0.84.6 (141) and it worked so far in my previous versions (last before upgrade was 0.82.1).

The issue can be easy simulated using Chrome extension, I use ‘Smart Websocket Client’. After making connection to your URL and using api_password to authenticate will throw the unknown error during the next step subscribe_events.

To resolve, you need to generate a long-lived token under frontend profile and use this token instead then the step subscribe_events works:

{
“type”: “auth”,
“access_token”: “ABCDEFGH”
}