nickknissen / hass-monta

Monta Home Assistant custom component
MIT License
21 stars 2 forks source link

Invalid Credentials #50

Closed birkirb closed 6 months ago

birkirb commented 6 months ago

System Health details

None

Checklist

Describe the issue

The issue is the Invalid Credentials error that occurs (see debug logs below). The issue occurs in an Automation that turns on charging and then sets up a time delay trigger for a switch off a few hours later.

When the switch off is executed the error occurs.

Given what I know about the Monta API, and some of the symptoms I'd like to offer the following hypothesis:

Most likely an Oauth token is created for every new instance of the integration and this token has a very short lifetime (say less than two hours). The same instance is used for the lifetime of the Automation and when used like this the credentials are already expired when the next call happens.

This hypothesis has the additional observation that even as the Switch Off fails in the Automation, I can open the device view and press the switch off button and it will work there.

A possible resilient solution would be to try and capture errors like this, refresh the credentials and try again.

I left out the diagnostic dump and system health details, but will be happy to try and provide them if you deem the current info insufficient.

Many thanks for making your integration available to the Home Assistant Community! :muscle: :bow:

Reproduction steps

  1. Create automation with a Switch Turn Off for Monta
  2. Use said automation with a time delay
  3. See that automation executes Switch On successfully, wait a couple of hours, see that Switch Off execution fails ...

Debug logs

Logger: homeassistant.components.automation.automation_19
Source: helpers/script.py:468
Integration: Automation (documentation, issues)
First occurred: January 13, 2024 at 04:59:21 (2 occurrences)
Last logged: January 13, 2024 at 04:59:21

Monta Charge Start: If at step 3: Error executing script. Unexpected error for call_service at pos 2: Something really wrong happened!
Monta Charge Start: Error executing script. Unexpected error for if at pos 3: Something really wrong happened!
Traceback (most recent call last):
  File "/config/custom_components/monta/api.py", line 243, in _api_wrapper
    raise MontaApiClientAuthenticationError(
custom_components.monta.api.MontaApiClientAuthenticationError: Invalid credentials

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

Traceback (most recent call last):
  File "/config/custom_components/monta/coordinator.py", line 60, in async_stop_charge
    return await self.client.async_stop_charge(charges[0]["id"])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/monta/api.py", line 155, in async_stop_charge
    response = await self._api_wrapper(
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/monta/api.py", line 266, in _api_wrapper
    raise MontaApiClientError("Something really wrong happened!") from exception
custom_components.monta.api.MontaApiClientError: Something really wrong happened!

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

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 468, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 704, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 666, in _async_run_long_action
    return long_task.result()
           ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2067, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2104, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 272, in handle_service
    return await service.entity_service_call(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 878, in entity_service_call
    single_response = await _handle_entity_call(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 948, in _handle_entity_call
    result = await task
             ^^^^^^^^^^
  File "/config/custom_components/monta/switch.py", line 87, in async_turn_off
    await self.coordinator.async_stop_charge(self.charge_point_id)
  File "/config/custom_components/monta/coordinator.py", line 64, in async_stop_charge
    raise UpdateFailed(exception) from exception
homeassistant.helpers.update_coordinator.UpdateFailed: Something really wrong happened!

Diagnostics dump

None

nickknissen commented 6 months ago

It seems to be the same as https://github.com/nickknissen/hass-monta/issues/49 The problem is that Monta API is returning wrong ID's when the plugin tries to fetch charges.

I will close this issue because of duplication.

birkirb commented 6 months ago

It seems to be the same as #49 The problem is that Monta API is returning wrong ID's when the plugin tries to fetch charges.

Hmm... then why does the Switch Off work outside of a script? :thinking:

nickknissen commented 6 months ago

Can you maybe elabborate on that? Are there some cases where it works?

birkirb commented 6 months ago

Yes, when using this button. Selection_001

I'll enable the Debug and try to spot if there's any difference in the API calls. Sounds like you are certain it's not the token.

nickknissen commented 6 months ago

As reported in issue #49 there where some problems with stop charge which should be resolved now that Monta has fixed the list charges endpoint.

I have tested both start/stop charge via the controls button and service and both works on my end. If you still have the issue, please enable debug and forward the logs to me with a start and stop.