fdebrus / hayward-ha

10 stars 1 forks source link

Authentifizierungsprobleme nach WLAN Abschaltung #12

Closed tobidou closed 4 weeks ago

tobidou commented 1 month ago

Reproduzierbar verbindet sich das Plugin nicht mehr mit dem Server wenn morgends das WLAN nach der Nachtabschaltung wieder aktiviert wurde. Der Zustand lässt sich nur durch einen Neustart von Home-Assistent bei mir lösen.

folke commented 1 month ago

Excuse me?

tobidou commented 1 month ago

Every Night i shot down my wlan. Next Morning there is no reconnect to the Havard Server. Every Action leads to false or missing authentification Message.

Hans1205 commented 1 month ago

Why do you switch off the WLAN at night? What do you want to achieve with it? To save power?

And how about connecting the HA via LAN?

In any case, the integration works perfectly for me! Thanks again 👍🏻

fdebrus commented 1 month ago

The integration refresh the token. It is not supposed that you loose connectivity to server and need to fully reconnect. As time allow, I can handle over a code update. will post a new release when ready.

fdebrus commented 1 month ago

How to handle ? it's true that the integration shall and will manage network cuts, I've implemented a 3 strikes rules where the integration tries 3 times with a timeout of 5 seconds to connect and re-connect. Then after those tries it consider the network as lost and stops. This is under testing.

Now, to this specific case where the network is lost for multiple hours, I do not see this being part of the integration, more of an automation that triggers as the network is available again and reset integration.

Not having network for hours is an exception, where HA restart should not be an issue, it is in this specific case as you trigger the exception every nights for hours.

I will keep looking at HA documentation on the best way to handle and revert here.

fdebrus commented 1 month ago

We could do something like testing for "google" every minutes,.... open for idea's, suggestions, do you have other integrations that re-connect ? so I can check how they do.

I'm busy testing the below

    async def check_connectivity(self, interval=60):
        """Periodically check internet connectivity and attempt reconnection if disconnected."""
        while True:
            try:
                async with self.aiohttp_session.get("https://www.google.com") as response:
                    if response.status == 200:
                        if not self.tokens:
                            _LOGGER.info("Internet connection restored, attempting to reauthenticate.")
                            await self.signin_with_retries()
                        else:
                            _LOGGER.debug("Internet connection is active.")
            except ClientError:
                _LOGGER.warning("Internet connection lost. Will retry...")
            await asyncio.sleep(interval)
fdebrus commented 1 month ago

0.1.2 is out as a Beta. I could not fully test it. have a try, revert with debuglog might issue arise and revert back to 0.1.1

tobidou commented 1 month ago

Thanks a lot. Will try and report asap.

tobidou commented 1 month ago

This was not the Solution. Maybe the issue, is that the wifi-module from Poolcontrol is loosing the Internet Connection and Not Home-Assistant itself.

fdebrus commented 1 month ago

I've shutdown wifi to HA for 30min, when enabled back, the Integration restored the connection. I will now disable the pool wifi, see if that trigger a error.

fdebrus commented 1 month ago

ok, found the json key, they call it "present" it's True when the pool is connected, False when the pool is offline.

Now the code was not build around checking for pool being connected or not, so need some re-work. Well if it keeps raining... should not take long 😄

    def get_pool_present(self, pool_id):
        """Return the present of the pool from document."""
        data_dict = self.data.to_dict()
        if data_dict.get("id") == pool_id:
            try:
                present =  data_dict["form"]["present"]
            except (KeyError, IndexError):
                present = "Unknown"
        else:
            _LOGGER.error(f"Pool ID {pool_id} does not match the document's ID.")
            present = "Unknown"
        return present
fdebrus commented 1 month ago

While I'm always willing to make the code more robust, I do not immediatly see how this would resolve your problem. I even trust it will not. Handling network disconnect and pool being offline will be part of the next release, but I still need to understand your issue, could you enable debuglog, trigger or wait for the issue to happen and send the log. thanks !

I've no problem using the integration after reconnecting the pool, so you problem lies somewhere else.

tobidou commented 1 month ago

Yeasterday Morning it was Fine, but today the issue is back again:

Logger: custom_components.aquarite.aquarite Quelle: custom_components/aquarite/aquarite.py:231 Integration: Aquarite (Dokumentation, Probleme) Erstmals aufgetreten: 07:15:54 (2 Vorkommnisse) Zuletzt protokolliert: 07:18:23

Failed to turn on switch for pool ID xxxxxxxxxxxxxxxxx: 401 Missing or invalid authentication.

tobidou commented 1 month ago

Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 241, in handle_call_service response = await hass.services.async_call( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/core.py", line 2741, in async_call response_data = await coro ^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/core.py", line 2784, in _execute_service return await target(service_call) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 977, in entity_service_call single_response = await _handle_entity_call( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 1049, in _handle_entity_call result = await task ^^^^^^^^^^ File "/config/custom_components/aquarite/switch.py", line 81, in async_turn_on await self._dataservice.api.turn_on_switch(self._pool_id, self._value_path) File "/config/custom_components/aquarite/aquarite.py", line 225, in turn_on_switch pool_data = await self.get_pool_as_json(pool_id) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/aquarite/aquarite.py", line 192, in __get_pool_as_json pool = await self.fetch_pool_data(pool_id) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/aquarite/aquarite.py", line 177, in fetch_pool_data self._pool_data = await asyncio.to_thread(self.client.collection("pools").document(pool_id).get) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/asyncio/threads.py", line 25, in to_thread return await loop.run_in_executor(None, func_call) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/google/cloud/firestore_v1/document.py", line 403, in get response_iter = self._client._firestore_api.batch_get_documents( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/google/cloud/firestore_v1/services/firestore/client.py", line 1150, in batch_get_documents response = rpc( ^^^^ File "/usr/local/lib/python3.12/site-packages/google/api_core/gapic_v1/method.py", line 131, in call__ return wrapped_func(*args, *kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/google/api_core/retry/retry_unary.py", line 293, in retry_wrapped_func return retry_target( ^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/google/api_core/retry/retry_unary.py", line 153, in retry_target _retry_error_helper( File "/usr/local/lib/python3.12/site-packages/google/api_core/retry/retry_base.py", line 212, in _retry_error_helper raise final_exc from source_exc File "/usr/local/lib/python3.12/site-packages/google/api_core/retry/retry_unary.py", line 144, in retry_target result = target() ^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/google/api_core/timeout.py", line 120, in func_with_timeout return func(args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/google/api_core/grpc_helpers.py", line 174, in error_remapped_callable raise exceptions.from_grpc_error(exc) from exc google.api_core.exceptions.Unauthenticated: 401 Missing or invalid authentication.

tobidou commented 1 month ago

And finaly i found this 5 hours ago:

Zuletzt protokolliert: 01:16:22

Token refresh failed on attempt 1/5: Cannot connect to host identitytoolkit.googleapis.com:443 ssl:default [None] Token refresh failed on attempt 2/5: Cannot connect to host identitytoolkit.googleapis.com:443 ssl:default [None] Token refresh failed on attempt 3/5: Cannot connect to host identitytoolkit.googleapis.com:443 ssl:default [None] Token refresh failed on attempt 4/5: Cannot connect to host identitytoolkit.googleapis.com:443 ssl:default [None] Token refresh failed on attempt 5/5: Cannot connect to host identitytoolkit.googleapis.com:443 ssl:default [None]

fdebrus commented 1 month ago

Expected behaviour as your network is shutdown, nothing wrong in those log.

I would need the debug log to further troubleshoot. go to the integration, click on "Enable debug logging" wait for the issue to arise, then go to system / logs as a filter use "aquarite" then click on load full log, copy/paste what you get from there. thanks.

image

Log filter set to "aquarite" then click "load full log"

image

tobidou commented 1 month ago

MainThread) [custom_components.aquarite.coordinator] Manually updated Aquarite data 2024-07-19 15:52:46.321 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 15:53:46.534 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 15:54:46.737 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 15:55:46.938 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 15:58:03.056 WARNING (MainThread) [custom_components.aquarite.aquarite] Internet connection lost. Will retry... 2024-07-19 15:58:14.254 DEBUG (MainThread) [custom_components.aquarite.aquarite] Token expired, refreshing... 2024-07-19 15:59:30.260 ERROR (MainThread) [custom_components.aquarite.aquarite] Token refresh failed on attempt 1/5: Cannot connect to host identitytoolkit.googleapis.com:443 ssl:default [None] 2024-07-19 15:59:35.262 DEBUG (MainThread) [custom_components.aquarite.aquarite] Token expired, refreshing... 2024-07-19 16:00:18.378 WARNING (MainThread) [custom_components.aquarite.aquarite] Internet connection lost. Will retry... 2024-07-19 16:00:50.455 ERROR (MainThread) [custom_components.aquarite.aquarite] Token refresh failed on attempt 2/5: Cannot connect to host identitytoolkit.googleapis.com:443 ssl:default [None] 2024-07-19 16:00:55.456 DEBUG (MainThread) [custom_components.aquarite.aquarite] Token expired, refreshing... 2024-07-19 16:02:10.656 ERROR (MainThread) [custom_components.aquarite.aquarite] Token refresh failed on attempt 3/5: Cannot connect to host identitytoolkit.googleapis.com:443 ssl:default [None] 2024-07-19 16:02:15.659 DEBUG (MainThread) [custom_components.aquarite.aquarite] Token expired, refreshing... 2024-07-19 16:02:33.718 WARNING (MainThread) [custom_components.aquarite.aquarite] Internet connection lost. Will retry... 2024-07-19 16:03:30.852 ERROR (MainThread) [custom_components.aquarite.aquarite] Token refresh failed on attempt 4/5: Cannot connect to host identitytoolkit.googleapis.com:443 ssl:default [None] 2024-07-19 16:03:35.857 DEBUG (MainThread) [custom_components.aquarite.aquarite] Token expired, refreshing... 2024-07-19 16:04:49.055 WARNING (MainThread) [custom_components.aquarite.aquarite] Internet connection lost. Will retry... 2024-07-19 16:04:51.059 ERROR (MainThread) [custom_components.aquarite.aquarite] Token refresh failed on attempt 5/5: Cannot connect to host identitytoolkit.googleapis.com:443 ssl:default [None] 2024-07-19 16:04:51.060 ERROR (MainThread) [custom_components.aquarite.aquarite] Error maintaining token: Cannot connect to host identitytoolkit.googleapis.com:443 ssl:default [None] 2024-07-19 16:07:04.295 WARNING (MainThread) [custom_components.aquarite.aquarite] Internet connection lost. Will retry... 2024-07-19 16:08:04.497 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 16:09:04.732 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 16:10:04.953 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 16:11:12.256 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 16:12:12.482 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 16:13:12.691 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 16:14:12.924 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 16:15:13.121 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 16:16:13.384 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 16:17:13.597 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 16:18:13.788 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 16:19:13.996 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 16:19:59.287 ERROR (MainThread) [custom_components.aquarite.aquarite] Failed to turn on switch for pool ID 05DF2D393636545343152E33: 401 Missing or invalid authentication. File "/config/custom_components/aquarite/light.py", line 71, in async_turn_on File "/config/custom_components/aquarite/aquarite.py", line 225, in turn_on_switch File "/config/custom_components/aquarite/aquarite.py", line 192, in __get_pool_as_json File "/config/custom_components/aquarite/aquarite.py", line 177, in fetch_pool_data 2024-07-19 16:20:14.187 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 16:21:15.356 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 16:22:15.575 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 16:23:15.778 DEBUG (MainThread) [custom_components.aquarite.aquarite] Internet connection is active. 2024-07-19 16:24:15.967 DEBUG (MainThread) [custom_components.aquarite.aquarite

tobidou commented 1 month ago

This is the log from a short internetdowntime.

fdebrus commented 1 month ago

thanks for the log, I've identified the issue. Please be patient, I will not fix 0.1.0 as I'm working on a full new release 0.2.0. Will update as it gets available.

fdebrus commented 1 month ago

I"m testing 0.2.0. Shall be able to release this week

fdebrus commented 1 month ago

0.2.2 is now available, have a try. I cannot test long period of non network connectivity. For short period, no issue detected.

tobidou commented 4 weeks ago

Till Today everything works like a charme. Thanks to you.

fdebrus commented 4 weeks ago

Good news, let’s wait a couple of days before to close. Btw, the integration is checking for connectivity every 5min. So following a network restore it could take up to 5min for the integration to reconnect

fdebrus commented 4 weeks ago

solved by 0.2.0 release