JoDehli / PyLoxone

Python Loxone binding
Apache License 2.0
200 stars 42 forks source link

Cant get a connection #150

Closed buttjer closed 1 year ago

buttjer commented 1 year ago

PyLoxone Version

PyLoxone 0.4.0 and 0.3.16

Which Model do you use from loxone? Miniserver? Gen1 or Gen2?

I think Miniserver. But unsure. It is supplied by my Landlord. But Miniserver is mentioned in the Info Pane of the App.

Which software version does your loxone use?

Loxone Firmware 13.1.11.17

How did you install HomeAssistant? Over Hassio oder manual install?

HACS

Which Version do you use of HomeAssistant?

Home Assistant 2022.12.8 Supervisor 2022.12.1 Operating System 9.4 Frontend 20221213.1 - latest

Describe the bug

I cant manage to find the right connect configuration.

I tried:

IP: dns.loxonecloud.com/ Port: 80 (which is, in the browser a redirect to 2a01-4f8-c010-2f7--1..dyndns.loxonecloud.com:46688)

resulting in following error (I noted the HTTP 307 redirect code)

Logger: homeassistant.config_entries
Source: custom_components/loxone/api.py:84
Integration: PyLoxone ([documentation](https://github.com/JoDehli/PyLoxone), [issues](https://github.com/JoDehli/PyLoxone/issues))
First occurred: 17:15:48 (1 occurrences)
Last logged: 17:15:48

Error setting up entry PyLoxone for loxone
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 372, in async_setup
    result = await component.async_setup_entry(hass, self)
  File "/config/custom_components/loxone/__init__.py", line 195, in async_setup_entry
    if not await miniserver.async_setup():
  File "/config/custom_components/loxone/miniserver.py", line 140, in async_setup
    request_code = await self.lox_config.getJson()
  File "/config/custom_components/loxone/api.py", line 122, in getJson
    api_resp = await client.get("/jdev/cfg/apiKey")
  File "/usr/local/lib/python3.10/site-packages/httpx/_client.py", line 1757, in get
    return await self.request(
  File "/usr/local/lib/python3.10/site-packages/httpx/_client.py", line 1533, in request
    return await self.send(request, auth=auth, follow_redirects=follow_redirects)
  File "/usr/local/lib/python3.10/site-packages/httpx/_client.py", line 1620, in send
    response = await self._send_handling_auth(
  File "/usr/local/lib/python3.10/site-packages/httpx/_client.py", line 1648, in _send_handling_auth
    response = await self._send_handling_redirects(
  File "/usr/local/lib/python3.10/site-packages/httpx/_client.py", line 1706, in _send_handling_redirects
    raise exc
  File "/usr/local/lib/python3.10/site-packages/httpx/_client.py", line 1688, in _send_handling_redirects
    await hook(response)
  File "/config/custom_components/loxone/api.py", line 84, in raise_if_not_200
    raise LoxoneHTTPStatusError(
custom_components.loxone.api.LoxoneHTTPStatusError: Miniserver response code 307

and also the redirect, which in the browser is a webinterface, i didn't now it exists.

IP: 2a01-4f8-c010-2f7--1.*****.dyndns.loxonecloud.com Port: 46688

Logger: homeassistant.config_entries
Source: custom_components/loxone/api.py:122
Integration: PyLoxone ([documentation](https://github.com/JoDehli/PyLoxone), [issues](https://github.com/JoDehli/PyLoxone/issues))
First occurred: 17:04:52 (1 occurrences)
Last logged: 17:04:52

Error setting up entry PyLoxone for loxone
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/httpx/_transports/default.py", line 60, in map_httpcore_exceptions
    yield
  File "/usr/local/lib/python3.10/site-packages/httpx/_transports/default.py", line 353, in handle_async_request
    resp = await self._pool.handle_async_request(req)
  File "/usr/local/lib/python3.10/site-packages/httpcore/_async/connection_pool.py", line 253, in handle_async_request
    raise exc
  File "/usr/local/lib/python3.10/site-packages/httpcore/_async/connection_pool.py", line 237, in handle_async_request
    response = await connection.handle_async_request(request)
  File "/usr/local/lib/python3.10/site-packages/httpcore/_async/connection.py", line 90, in handle_async_request
    return await self._connection.handle_async_request(request)
  File "/usr/local/lib/python3.10/site-packages/httpcore/_async/http11.py", line 112, in handle_async_request
    raise exc
  File "/usr/local/lib/python3.10/site-packages/httpcore/_async/http11.py", line 91, in handle_async_request
    ) = await self._receive_response_headers(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/httpcore/_async/http11.py", line 155, in _receive_response_headers
    event = await self._receive_event(timeout=timeout)
  File "/usr/local/lib/python3.10/site-packages/httpcore/_async/http11.py", line 205, in _receive_event
    raise RemoteProtocolError(msg)
httpcore.RemoteProtocolError: Server disconnected without sending a response.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 372, in async_setup
    result = await component.async_setup_entry(hass, self)
  File "/config/custom_components/loxone/__init__.py", line 195, in async_setup_entry
    if not await miniserver.async_setup():
  File "/config/custom_components/loxone/miniserver.py", line 140, in async_setup
    request_code = await self.lox_config.getJson()
  File "/config/custom_components/loxone/api.py", line 122, in getJson
    api_resp = await client.get("/jdev/cfg/apiKey")
  File "/usr/local/lib/python3.10/site-packages/httpx/_client.py", line 1757, in get
    return await self.request(
  File "/usr/local/lib/python3.10/site-packages/httpx/_client.py", line 1533, in request
    return await self.send(request, auth=auth, follow_redirects=follow_redirects)
  File "/usr/local/lib/python3.10/site-packages/httpx/_client.py", line 1620, in send
    response = await self._send_handling_auth(
  File "/usr/local/lib/python3.10/site-packages/httpx/_client.py", line 1648, in _send_handling_auth
    response = await self._send_handling_redirects(
  File "/usr/local/lib/python3.10/site-packages/httpx/_client.py", line 1685, in _send_handling_redirects
    response = await self._send_single_request(request)
  File "/usr/local/lib/python3.10/site-packages/httpx/_client.py", line 1722, in _send_single_request
    response = await transport.handle_async_request(request)
  File "/usr/local/lib/python3.10/site-packages/httpx/_transports/default.py", line 352, in handle_async_request
    with map_httpcore_exceptions():
  File "/usr/local/lib/python3.10/contextlib.py", line 153, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/usr/local/lib/python3.10/site-packages/httpx/_transports/default.py", line 77, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.RemoteProtocolError: Server disconnected without sending a response.
JoDehli commented 1 year ago

If it is a gen 2 try to use local access only. But if you are not the admin you can not change it. You can try the local address otherwise you can not use pyloxone at the moment.

buttjer commented 1 year ago

I am no admin nor do I have access to the local network of the loxone server, because its installed for multiple Appartements by the landlord.

Its installed recently in the facility. So I suppose its quite new. How do I know for sure its gen1 or gen2?

I am a Developer myself. Can I help you supporting gen2? Any hints where to start? Have not looked into the sourcecode yet.

JoDehli commented 1 year ago

Do you have discord?

JoDehli commented 1 year ago

Here is a channel I made. We can discuss this topic there. https://discord.gg/Y3ZzQKfW

buttjer commented 1 year ago

Yeah sure

hasenbrau commented 1 year ago

Hey JoDehli,

If it is a gen 2 try to use local access only. But if you are not the admin you can not change it. You can try the local address otherwise you can not use pyloxone at the moment.

I have exactly the same problem as buttjer. I have a Gen2 and used the dns.loxonecloud.com method as written in the project documentation: "If you have the gen2 miniserver you must use your loxonecloud address". I'm now confused with your statement above. Shall it be local or cloud based?

Thanks!

JoDehli commented 1 year ago

@hasenbrau As I already mention I have a gen1 and I can not answer your questen 100 % as it is different on a gen1.

What I can say is that if you know your local ip adress of you gen2 server (for example 192.168.1.100) and port and if local access is enabled you should use this address.

If you only know you cloud address you can try this one but also with this address local access must be enabled. Otherwise you can not reach it by now. I am currently working on an a complete new communication and there I want to try to get it to work when local access is disabled. But for this I need users where I can test it on there machines.

hasenbrau commented 1 year ago

Funny, somehow the plugin once could connect locally and instatiate the entities but now fails again. Either why I get following exceptions:

On Local connection: Dieser Fehler wurde von einer benutzerdefinierten Integration verursacht

Logger: homeassistant.config_entries Source: custom_components/loxone/api.py:153 Integration: PyLoxone (documentation, issues) First occurred: 15:02:44 (1 occurrences) Last logged: 15:02:44

Error setting up entry PyLoxone for loxone Traceback (most recent call last): File "/usr/lib/python3.10/site-packages/httpx/_transports/default.py", line 60, in map_httpcore_exceptions yield File "/usr/lib/python3.10/site-packages/httpx/_transports/default.py", line 353, in handle_async_request resp = await self._pool.handle_async_request(req) File "/usr/lib/python3.10/site-packages/httpcore/_async/connection_pool.py", line 253, in handle_async_request raise exc File "/usr/lib/python3.10/site-packages/httpcore/_async/connection_pool.py", line 237, in handle_async_request response = await connection.handle_async_request(request) File "/usr/lib/python3.10/site-packages/httpcore/_async/connection.py", line 90, in handle_async_request return await self._connection.handle_async_request(request) File "/usr/lib/python3.10/site-packages/httpcore/_async/http11.py", line 112, in handle_async_request raise exc File "/usr/lib/python3.10/site-packages/httpcore/_async/http11.py", line 91, in handle_async_request ) = await self._receive_response_headers(**kwargs) File "/usr/lib/python3.10/site-packages/httpcore/_async/http11.py", line 155, in _receive_response_headers event = await self._receive_event(timeout=timeout) File "/usr/lib/python3.10/site-packages/httpcore/_async/http11.py", line 205, in _receive_event raise RemoteProtocolError(msg) httpcore.RemoteProtocolError: Server disconnected without sending a response.

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/usr/lib/python3.10/site-packages/homeassistant/config_entries.py", line 372, in async_setup result = await component.async_setup_entry(hass, self) File "/config/custom_components/loxone/init.py", line 156, in async_setup_entry if not await miniserver.async_setup(): File "/config/custom_components/loxone/miniserver.py", line 118, in async_setup request_code = await self.lox_config.getJson() File "/config/custom_components/loxone/api.py", line 153, in getJson my_response = await client.get(LOXAPPPATH) File "/usr/lib/python3.10/site-packages/httpx/_client.py", line 1757, in get return await self.request( File "/usr/lib/python3.10/site-packages/httpx/_client.py", line 1533, in request return await self.send(request, auth=auth, follow_redirects=follow_redirects) File "/usr/lib/python3.10/site-packages/httpx/_client.py", line 1620, in send response = await self._send_handling_auth( File "/usr/lib/python3.10/site-packages/httpx/_client.py", line 1648, in _send_handling_auth response = await self._send_handling_redirects( File "/usr/lib/python3.10/site-packages/httpx/_client.py", line 1685, in _send_handling_redirects response = await self._send_single_request(request) File "/usr/lib/python3.10/site-packages/httpx/_client.py", line 1722, in _send_single_request response = await transport.handle_async_request(request) File "/usr/lib/python3.10/site-packages/httpx/_transports/default.py", line 352, in handle_async_request with map_httpcore_exceptions(): File "/usr/lib/python3.10/contextlib.py", line 153, in exit self.gen.throw(typ, value, traceback) File "/usr/lib/python3.10/site-packages/httpx/_transports/default.py", line 77, in map_httpcore_exceptions raise mapped_exc(message) from exc httpx.RemoteProtocolError: Server disconnected without sending a response.

One remote connection: Dieser Fehler wurde von einer benutzerdefinierten Integration verursacht

Logger: homeassistant.config_entries Source: custom_components/loxone/api.py:84 Integration: PyLoxone (documentation, issues) First occurred: 15:08:17 (1 occurrences) Last logged: 15:08:17

Error setting up entry PyLoxone for loxone Traceback (most recent call last): File "/usr/lib/python3.10/site-packages/homeassistant/config_entries.py", line 372, in async_setup result = await component.async_setup_entry(hass, self) File "/config/custom_components/loxone/init.py", line 156, in async_setup_entry if not await miniserver.async_setup(): File "/config/custom_components/loxone/miniserver.py", line 118, in async_setup request_code = await self.lox_config.getJson() File "/config/custom_components/loxone/api.py", line 121, in getJson api_resp = await client.get("/jdev/cfg/apiKey") File "/usr/lib/python3.10/site-packages/httpx/_client.py", line 1757, in get return await self.request( File "/usr/lib/python3.10/site-packages/httpx/_client.py", line 1533, in request return await self.send(request, auth=auth, follow_redirects=follow_redirects) File "/usr/lib/python3.10/site-packages/httpx/_client.py", line 1620, in send response = await self._send_handling_auth( File "/usr/lib/python3.10/site-packages/httpx/_client.py", line 1648, in _send_handling_auth response = await self._send_handling_redirects( File "/usr/lib/python3.10/site-packages/httpx/_client.py", line 1706, in _send_handling_redirects raise exc File "/usr/lib/python3.10/site-packages/httpx/_client.py", line 1688, in _send_handling_redirects await hook(response) File "/config/custom_components/loxone/api.py", line 84, in raise_if_not_200 raise LoxoneHTTPStatusError( custom_components.loxone.api.LoxoneHTTPStatusError: Miniserver response code 307

Version of PyLoxone: 0.3.16 Loxone Miniserver: 13.1.11.17

hasenbrau commented 1 year ago

I see, sorry. Nevertheless, I'm trying local access but without success. Let me do some more debugging on my side and maybe see if I can find some more information. I have my own local Loxone installation and I have my HASS running in a docker container on a raspi. Maybe there something is going wrong.

JoDehli commented 1 year ago

@hasenbrau Yes I know this behavior. It is because the redirection is not implemented right. It can connect then loxone redirects to a other host. But this is not right implemented as I do not have a gen2.

JoDehli commented 1 year ago

@hasenbrau Do you know python?

hasenbrau commented 1 year ago

@JoDehli Well, I'm not a native python programmer but professional on other (C/ C++/ etc.). I do now an then some changes on some python scripts

hasenbrau commented 1 year ago

More than happy to support/ test.

JoDehli commented 1 year ago

@hasenbrau Aus deinem Log sehe ich dass du deutsch sprichst.

Hier ist ein Repo das ich zukünftig zur Kommunikation nutzen will. https://github.com/JoDehli/pyloxone-api

Wenn du den Branch rewrite auscheckst und die Anleitung unter "Development" durchgehst könntest du mal probieren ob du konstant log ausgaben bekommst. Also das poetry ist eine Verwaltungssoftware um Bibliotheken mit allen Abhängigkeiten zu installieren. Also nur um alles einfacher zu machen. Ist in Python ähnlich beschissen wie in C++. Aber wenn du es zum laufen bekommst oder auch in eine IDE bekommst (pycharm oder VSCODE) dann kannst du mal probieren ob das stabil funktioniert. Wie gesagt das versuche ich demnächst als grundlegende Kommunikation zu implementieren. Dann sollte das auch besser mit den gen2 funktionieren. Es ist nämlich nur der Aufbau der Websocket Verbindung. Alles andere ist gleich.

Hier noch eine kleine Hilfe wenn du das Python mit den Bibliotheken installiert bekommen hast:

A quick demo of the pyloxone_api module
From the command line, run:
> python -m pyloxone_api -u username -p password -a address -t port
where username, password address and port are your Loxone login credentials
hasenbrau commented 1 year ago

Cool - danke!, werde es heute versuchen. stay tuned!

JoDehli commented 1 year ago

@hasenbrau Aber das ist nur die Kommunikation. Ist also noch keine Homeassistant integration. Du wirst nur logs sehen. Aber wenn das funktioniert wird die Integration dann auch funktionieren.

Haupt File ist das main.py hier ist die main drin:

Wenn du wirkliche Werte wie Temperaturen oder sonstige Sachen ausgeben willst änderst du im main.py die folgende Zeilen ab:

image

Dann sollte die Log ungefähr so aussehen:

'0b734138-032f-0284-ffff403fb0c34b9e': 4.0, 
'0b734138-032f-0296-ffff403fb0c34b9e': 492.0, 
'0b734138-032f-0299-ffff403fb0c34b9e': 997.0, 
usw.
hasenbrau commented 1 year ago

Ja, habe das schon so verstanden. Ich melde mich wieder

hasenbrau commented 1 year ago

Ok, habs in einem WSL environment zum Laufen gebracht. Die Verbindung scheint zu funktionieren: image

JoDehli commented 1 year ago

@hasenbrau kommen auch konstant Werte an. Das was du hier siehst ist das sogenannte structure file.

hasenbrau commented 1 year ago

@JoDehli
Ich kann das Licht aus/ ein schalten und sehe dann folgenden Output: {'19efbbef-0334-d457-ffffbb40c6e6a189': 100.0, '19efbbef-0334-d3fe-ffffbb40c6e6a189': 777.0} {'19efbbef-0334-d3f6-ffffbb40c6e6a189': '[777]'} {'19efbbef-0334-d457-ffffbb40c6e6a189': 0.0, '19efbbef-0334-d3fe-ffffbb40c6e6a189': 778.0} {'19efbbef-0334-d3f6-ffffbb40c6e6a189': '[778]'} {'19efbbef-0334-d457-ffffbb40c6e6a189': 100.0, '19efbbef-0334-d3fe-ffffbb40c6e6a189': 777.0} {'19efbbef-0334-d3f6-ffffbb40c6e6a189': '[777]'} {'19efbbef-0334-d457-ffffbb40c6e6a189': 0.0, '19efbbef-0334-d3fe-ffffbb40c6e6a189': 778.0} {'19efbbef-0334-d3f6-ffffbb40c6e6a189': '[778]'}

JoDehli commented 1 year ago

@hasenbrau sehr gut. Damit kann dann pyloxone die Schalter richtig setzten.

hasenbrau commented 1 year ago

@JoDehli Sehr gut. Dann lass ich mal meine HASS/ Loxone Integration ruhen. Kann ich dich in irgendeiner Weise sonst noch unterstützen nebst PTN (Please Try Now :-))?

JoDehli commented 1 year ago

Im Moment nicht. Ich bin jetzt gerade dabei die neue Bibliothek einzubinden. Kann noch nicht genau sagen wie lange es dauert. Denke aber das ich relativ zeitnah sagen kann ob alles so klappt wie ich mir das vorstelle. Was noch nicht geht ist der automatische reconnect. Der muss unbedingt noch rein. Das heißt wenn die loxone nei startet sollte Homeassistent selbständig wieder die Kommunikation aufnehmen ohne Home assistant neu starten zu müssen. In der jetzigen integration geht das auch. Wir haben aber die websocket Bibliothek ausgetauscht von websockets zu aiohttp welches auch von Homeassistent verwendet wird.

Was mir dann helfen würde wenn ich Mal einen vernünftigen stand habe wenn du es testen könntest bevor ich es dann online stelle. Mittlerweile verwenden schon einige Leute die integration und das würde dann eine Menge von issues auslösen.

hasenbrau commented 1 year ago

@JoDehli Gerne doch. Mein Setup ist zwar noch sehr einfach, werde aber so gut testen wie ich kann. Du weisst wo du mich findest.

philjak commented 1 year ago

Hallo zusammen,

ich glaube, ich habe genau dasselbe Problem. Wenn ich mit die Integration mit meiner lokalen IP konfiguriere, dann geht alles super! Aber es stoppt leider auch irgendwann. Ein Reboot im HA bringt alles wieder zum Laufen. Verstehe ich richtig, dass dann der Loxone Miniserver neu gestartet hat und die Integration aktuell nicht reconnected.

Wenn ich es mit der dns.loxonecloud.com/**** mache, erhalte ich auch den Fehler 307.

Gibt es denn evtl. einen bekannten Workaround? Kann man den Loxone Server den Neustart verbieten? Oder reicht es den Remotezugang zu deaktivieren? Oder mit einem Shellscript erkennen, wann HA neugestartet werden muss?

Ich biete mich ebenfalls gerne zum Testen an!

Habt vielen Dank - insbesondere grundsätzlich für diese tolle Integration!

VG philjak

philjak commented 1 year ago

Hallo zusammen,

wie ich es ggf. auch schon in anderen Issues gelesen habe, hat es geholfen, die Remote Funktionalität von Loxone auszuschalten. Dann funktioniert alles gut, bis einmal die Netzwerkverbindung zu Loxone unterbrochen wird, z.B. durch einen Switch/Router Neustart. Dann verbindet sich HA nicht mehr automatisch. Man muss HA dann händisch neustarten.

@JoDehli Wenn ich irgendwie unterstützen kann bei der Implementierung oder beim Testen, lasse es mich gerne wissen.

Viele Grüße philjak

JoDehli commented 1 year ago

Kurz zur Klärung die remote Verbindung funktioniert nicht. Und wenn pyloxone die Verbindung verliert zur loxone versucht pyloxone mehrmals einen neuen Verbindungsaufbau. Ist das nicht erfolgreich wird pyloxone runtergefahren. Das wird sich sich mit der neuen Implementierung nicht ändern.

In absehbarer Zeit werde ich eine Beta Version an den Start bringen. Diese könntet ihr dann testen und Feedback geben. Bei mir läuft die Version schon seit 1-2 Wochen sehr stabil.

philjak commented 1 year ago

Vielen Dank für deine ganze Arbeit, @JoDehli !

Wir nutzen einen gen2 Server mit der lokalen IP und das funktioniert alles wunderbar! Einzig, wie gesagt, wenn wir den Switch, der HA und Loxone verbindet, nachts abschalten, dann kann am nächsten Morgen, nach dem Einschalten des Switches, HA Loxone nicht mehr steuern, die Verbindung scheint verloren. Sobald man HA neustartet, funktioniert wieder alles stabil. Würde deine Beta Version das beheben?