{"record":{"id":"a7e9b532234d52b9","repo":"AlexxIT/go2rtc","slug":"hass-wrong-type","errorCode":null,"errorMessage":"hass: wrong type: ","messagePattern":"hass: wrong type: ","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/hass/api.go","lineNumber":36,"sourceCode":"\t}\n\n\tapi := &API{ws: ws}\n\tif err = api.Auth(token); err != nil {\n\t\t_ = ws.Close()\n\t\treturn nil, err\n\t}\n\n\treturn api, nil\n}\n\nfunc (a *API) Auth(token string) error {\n\tvar res ResponseAuth\n\n\tif err := a.ws.ReadJSON(&res); err != nil {\n\t\treturn err\n\t}\n\tif res.Type != \"auth_required\" {\n\t\treturn errors.New(\"hass: wrong type: \" + res.Type)\n\t}\n\n\ts := `{\"type\":\"auth\",\"access_token\":\"` + token + `\"}`\n\tif err := a.ws.WriteMessage(websocket.TextMessage, []byte(s)); err != nil {\n\t\treturn err\n\t}\n\tif err := a.ws.ReadJSON(&res); err != nil {\n\t\treturn err\n\t}\n\tif res.Type != \"auth_ok\" {\n\t\treturn errors.New(\"hass: wrong type: \" + res.Type)\n\t}\n\n\treturn nil\n}\n\nfunc (a *API) Close() error {\n\treturn a.ws.Close()","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/AlexxIT/go2rtc/blob/c245815e75e2a5fd60b4290f12bfc04e55a984d3/pkg/hass/api.go#L18-L54","documentation":"hass.API.Auth performs the Home Assistant WebSocket auth handshake and expects the first message type to be \"auth_required\". If the server sends any other type, Auth returns this error including the received type, indicating the connection is not in the expected authentication state.","triggerScenarios":"Connecting to a URL that is not the Home Assistant WebSocket endpoint (e.g. the HTTP API root, or behind a proxy serving HTML/error pages that fail differently), or an already-authenticated connection that skips auth_required.","commonSituations":"Wrong ws:// or wss:// URL (missing /api/websocket path); reverse proxy intercepting the handshake; connecting to a different Home Assistant integration expecting another protocol; token supplied to a connection that already completed auth.","solutions":["Verify the WebSocket URL points to Home Assistant's API websocket endpoint (path /api/websocket).","Log the received res.Type value to see what the server actually sent.","Bypass or correctly configure reverse proxies so the raw WebSocket reaches Home Assistant.","Ensure you are not reusing an already-authenticated connection."],"exampleFix":"// before\napi, err := hass.NewAPI(\"ws://homeassistant.local:8123/api\")\n// after\napi, err := hass.NewAPI(\"ws://homeassistant.local:8123/api/websocket\")","handlingStrategy":"try-catch","validationCode":"u, err := url.Parse(wsURL)\nif err != nil || !strings.HasSuffix(u.Path, \"/api/websocket\") {\n    return fmt.Errorf(\"invalid Home Assistant websocket URL: %s\", wsURL)\n}","typeGuard":null,"tryCatchPattern":"api, err := hass.NewAPI(wsURL, token)\nif err != nil {\n    if strings.Contains(err.Error(), \"hass: wrong type\") {\n        return fmt.Errorf(\"endpoint did not send auth_required; check URL points to /api/websocket: %w\", err)\n    }\n    return err\n}","preventionTips":["Always append /api/websocket to the Home Assistant base URL.","Test the websocket endpoint with a raw client before wiring it into the app.","Check reverse-proxy WebSocket upgrade configuration (Connection/Upgrade headers)."],"tags":["home-assistant","websocket","authentication","protocol"],"backgroundTag":"unexpected-api-response-shape","analyzedSha":"c245815e75e2a5fd60b4290f12bfc04e55a984d3","analyzedAt":"2026-09-07T11:47:02.965Z","contentChangedAt":"2026-09-07T11:47:02.965Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}