maxbec / hassio-diyHue

Fully configurable diyHue add-on for home-assistant.
30 stars 17 forks source link

unable to connect to HA Websocket #12

Closed Ergerbolt closed 3 years ago

Ergerbolt commented 3 years ago

im running version

diyHue-beta Current version: 1.1.8

in Hass.io from https://github.com/MaxBec/hassio-diyHue

Im running HA in Version 2020.12.7

It seems that diyhue cant connect to the HA API.

my config.json:

"emulator": { "alarm": { "email": "", "lasttriggered": 100000, "on": false }, "esphome": { "enabled": false }, "homeassistant": { "enabled": true, "homeAssistantIncludeByDefault": true, "homeAssistantIp": "192.168.178.106", "homeAssistantPort": 8123, "homeAssistantToken": "....." }, "hyperion": { "enabled": false }, "lights": {}, "mqtt": { "discoveryPrefix": "homeassistant", "enabled": false, "mqttPassword": "", "mqttPort": 1883, "mqttServer": "mqtt", "mqttUser": "" }, "network_scan": { "enabled": true }, "sensors": {}, "shelly": { "enabled": false }, "tasmota": { "enabled": false }, "wled": { "enabled": false }, "yeelight": { "enabled": false } },

The logs tell me:

2021-01-06 23:55:49,973 - root - INFO - IP range for light discovery: 0-255
2021-01-06 23:55:49,973 - root - INFO - 127.0.0.1
2021-01-06 23:55:49,973 - root - INFO - Online Discovery/Remote API Enabled!
2021-01-06 23:55:49,975 - root - INFO - Config loaded
2021-01-06 23:55:49,985 - root - WARNING - Home Assistant Web Socket Client disconnected trying to (re)connect
2021-01-06 23:55:49,989 - root - ERROR - Error connecting to Home Assistant WebSocket
Traceback (most recent call last):
  File "/opt/hue-emulator/protocols/homeassistant_ws.py", line 223, in create_websocket_client
    homeassistant_ws_client.connect()
  File "/usr/local/lib/python3.8/site-packages/ws4py/client/__init__.py", line 233, in connect
    raise HandshakeError("Invalid response")
ws4py.exc.HandshakeError: Invalid response
2021-01-06 23:55:49,993 - root - DEBUG - start ssdp broadcast
2021-01-06 23:55:49,994 - root - DEBUG - starting ssdp...
2021-01-06 23:55:49,996 - root - INFO - sync with lights
2021-01-06 23:55:50,003 - root - INFO - Starting httpd...
Ergerbolt commented 3 years ago

Hi, i found another log - maybe this helps.. it seems to be a problem with the http and https connection... maybe?!

2021-01-07 00:39:12,529 - urllib3.connection - ERROR - Certificate did not match expected hostname: 127.0.0.1. Certificate: {'subject': ((('commonName', '....duckdns.org'),),), 'issuer': ((('countryName', 'US'),), (('organizationName', "Let's Encrypt"),), (('commonName', 'R3'),)), 'version': 3, 'serialNumber': '04A474DFF45A9FBF698A87CC53B6D543A621', 'notBefore': 'Jan 4 17:07:06 2021 GMT', 'notAfter': 'Apr 4 17:07:06 2021 GMT', 'subjectAltName': (('DNS', '.....duckdns.org'),), 'OCSP': ('http://r3.o.lencr.org',), 'caIssuers': ('http://r3.i.lencr.org/',)}

maxbec commented 3 years ago

Hey @Ergerbolt,

thank you for the report. We are investigating this https issue at the moment.

foxy82 commented 3 years ago

Hi - from the log it looks like you are running Home Assistant as https on port 8123?

If so the websocket connection is only supported under HTTP. You'd need to raise with the Home Assistant developers to get the Web Socket supporting ssl.

Ergerbolt commented 3 years ago

Hi, thanks for your answer.

I just tried some requests with postman via http - its not working. Its only working via https through the internet - even when i curl from localhost.

foxy82 commented 3 years ago

Home Assistant by default should serve http on port 8123. So this suggests you have some setup that has changed this?

vandalon commented 3 years ago

Can it be that home assistant now does support web socket via ssl? i can only connect to was and not ws when I test: I have enabled ssl in the http home assistant component.

  diyhue wsdump.py wss://<hostname>:8123/api/websocket
Press Ctrl+C to quit
< {"type": "auth_required", "ha_version": "2021.5.1"}
> #
  diyhue wsdump.py ws://<hostname>:8123/api/websocket
Connection is already closed.
  diyhue
vandalon commented 3 years ago

i've tested by changing the following line:

    homeassistant_url = f'ws://{homeassistant_ip}:{homeAssistant_port}/api/websocket'

to

    homeassistant_url = f'wss://{homeassistant_ip}:{homeAssistant_port}/api/websocket'

and it works , a valid cert is needed though.

onkytonk commented 3 years ago

Hello, I'm experiencing the same issue. Has this been resolved please? I'm running diyhue 1.4.1 on HA 2021.06.6

foxy82 commented 3 years ago

Hi. Basically when you put home assistant into secure mode instead of using a wss socket it uses ws over Https which I've not managed to get working. The easiest work around ismise something like nginx for the SSL layer and not home assistants built in version

Talk2Giuseppe commented 2 years ago

And this is the proposed solution for the end user? Can someone please explain how the end user would be able to implement this solution? And while on the subject, why is the current application still needing this hack to get it to work? Is the app an abandoned project considering this issue has been reported for well over a year now?

foxy82 commented 2 years ago

The issue is with how Home Assistant handles web sockets when in secure mode. If you submit a fix upstream to them I'm happy to see if it is easy to get working. Or use NGINX like I do.

Talk2Giuseppe commented 2 years ago

The issue is with how Home Assistant handles web sockets when in secure mode. If you submit a fix upstream to them I'm happy to see if it is easy to get working. Or use NGINX like I do.

I appreciate your assistance. But quite honestly, this hack should be absolutely unacceptable. Installing work arounds like NGINX is not an easy fix for end users. Why the AI isn't put into the app or the core of HA to handle this properly is crazy. It'll be easier for me to throw my wife's crappy iphone into the sea and buy her a different device than to spend more time trying to fix this.

Again, it worked flawlessly for nearly 2 years. And this bug has been around for some time now. Yet it remains. Go figure!