Thomas55555 / husqvarna_automower

Custom component for Home Assistant to monitor and control your Husqvarna Automower
https://github.com/Thomas55555/husqvarna_automower
MIT License
105 stars 28 forks source link

Invalid credentials #493

Closed Thomas55555 closed 1 year ago

Thomas55555 commented 1 year ago

Prerequirements [x] I have connected my mower(s) to the Automower Connect App [x] I have connected my Husqvanra developers account to the Authentication API [x] I have connected my Husqvarna developers account to the Automower Connect API Used Version: This was the last commit in my production environement: https://github.com/Thomas55555/husqvarna_automower/pull/485/commits/b342395a4912d95eb25d0ebd4f8680e6359c9194

Describe the bug I got this error message after the system was running for about 12h:

2023-06-16 08:25:06.450 ERROR (MainThread) [aioautomower.rest] Response mower data: {'errors': [{'id': '56d07d65-b0f7-4a3e-81db-f93e2c71b649', 'status': '403', 'code': 'invalid.credentials', 'title': 'Invalid credentials', 'detail': 'The supplied credentials are invalid.'}]}
2023-06-16 08:25:06.456 ERROR (MainThread) [homeassistant] Error doing job: Exception in callback AutomowerEntity.async_added_to_hass.<locals>.<lambda>({'errors': [{'code': 'invalid.credentials', 'detail': 'The supplied... are invalid.', 'id': '56d07d65-b0f...-f93e2c71b649', 'status': '403', ...}]}) at /config/custom_components/husqvarna_automower/entity.py:52
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/config/custom_components/husqvarna_automower/entity.py", line 52, in <lambda>
    lambda _: self.async_write_ha_state(), schedule_immediately=True
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 590, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 654, in _async_write_ha_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 596, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/binary_sensor/__init__.py", line 211, in state
    if (is_on := self.is_on) is None:
                 ^^^^^^^^^^
  File "/config/custom_components/husqvarna_automower/binary_sensor.py", line 52, in is_on
    mower_attributes = AutomowerEntity.get_mower_attributes(self)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/husqvarna_automower/entity.py", line 37, in get_mower_attributes
    return self.coordinator.session.data["data"][self.idx]["attributes"]
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
KeyError: 'data'

Expected behavior This shouldn't happen, and everything should just work

Screenshots If applicable, add screenshots to help explain your problem.

Additional context I've never had an connection issue like that so far. As I'm right now testing the PR #485 I'm courious if it is related somehow to that, especially because thre was the same error on an other user after testing this: https://github.com/Thomas55555/husqvarna_automower/pull/485#issuecomment-1594178007

And no other error like this was reported before. But maybe it's just something with the Husqvarna API.

The solution was just to reload the integration.

prairiesnpr commented 1 year ago

I just looked at the logs on my two instances, these are both running variations of #485, no errors were in the logs on the production instance (core install in a venv) and it's been up since the 12th. I did find a single instance of the same error on the dev instance (Home assistant OS on a VM) also on the 12th, but no reload was evidently required because it's been working every time I've logged in to check it.

Thomas55555 commented 1 year ago

Seems to be a one time issue

chmeeedalf commented 1 year ago

I've seen this several times. It might be just the server side rebooting.

Thomas55555 commented 1 year ago

I've seen this several times. It might be just the server side rebooting.

Can you tell, when this occured the last time to you? Maybe it was at the same time.... I had the problem again yesterday:

2023-06-28 08:21:13.951 ERROR (MainThread) [aioautomower.rest] Response mower data: {'errors': [{'id': '8a268a8f-3602-4aff-8806-48c3e1e6a004', 'status': '403', 'code': 'invalid.credentials', 'title': 'Invalid credentials', 'detail': 'The supplied credentials are invalid.'}]}
2023-06-28 08:21:13.954 ERROR (MainThread) [homeassistant] Error doing job: Exception in callback AutomowerCamera.__init__.<locals>.<lambda>({'errors': [{'code': 'invalid.credentials', 'detail': 'The supplied... are invalid.', 'id': '8a268a8f-360...-48c3e1e6a004', 'status': '403', ...}]}) at /config/custom_components/husqvarna_automower/camera.py:102

It makes me wonder that it was nearly at the same time as 12 days before.

chmeeedalf commented 1 year ago

From my logs: 2023-06-27 17:01:08 2023-06-28 00:52:08 2023-06-28 08:03:08

mulcmu commented 1 year ago

I had a few occurrences in my logs. Seem to get a 403 but there is a 200 that was logged either right before or right after the 403. I haven't noticed any loss of functionality with the integration.

Logger: aioautomower.rest
Source: runner.py:179
First occurred: June 25, 2023 at 8:08:27 PM (1039 occurrences)
Last logged: 10:12:25 AM

Response mower data: {'errors': [{'id': '63472ab0-7168-4bdc-bc54-fea43bef1559', 'status': '403', 'code': 'invalid.credentials', 'title': 'Invalid credentials', 'detail': 'The supplied credentials are invalid.'}]}
Response mower data: {'errors': [{'id': '18fdf98c-d1ca-42bc-a72a-f2e947d415a7', 'status': '403', 'code': 'invalid.credentials', 'title': 'Invalid credentials', 'detail': 'The supplied credentials are invalid.'}]}
Response mower data: {'errors': [{'id': '191f9cc1-dd78-4f39-9fc9-91843c0d4a27', 'status': '403', 'code': 'invalid.credentials', 'title': 'Invalid credentials', 'detail': 'The supplied credentials are invalid.'}]}
Response mower data: {'errors': [{'id': '840d667f-55ff-4fcd-9e81-c1b043e061f2', 'status': '403', 'code': 'invalid.credentials', 'title': 'Invalid credentials', 'detail': 'The supplied credentials are invalid.'}]}
Response mower data: {'errors': [{'id': 'f3362c99-062c-4fb9-93f6-02b7251be097', 'status': '403', 'code': 'invalid.credentials', 'title': 'Invalid credentials', 'detail': 'The supplied credentials are invalid.'}]}
2023-06-25 20:08:27.643 DEBUG (MainThread) [aioautomower.rest] Response mower data: <ClientResponse(https://api.amc.husqvarna.dev/v1/mowers/) [200 OK]>
<CIMultiDictProxy('Content-Type': 'application/vnd.api+json', 'Content-Length': '3304', 'Connection': 'keep-alive', 'Date': 'Mon, 26 Jun 2023 00:08:27 GMT', 'x-amzn-RequestId': 'ce7a20b3-e413-4de7-a64a-fe7bda8a3090', 'x-amzn-Remapped-Content-Length': '3304', 'x-amzn-Remapped-Connection': 'keep-alive', 'x-amz-apigw-id': 'HGaHWGy9DoEF5oQ=', 'x-amzn-Remapped-Date': 'Mon, 26 Jun 2023 00:08:27 GMT', 'X-Cache': 'Miss from cloudfront', 'Via': '1.1 8301cc1793fefdf5e4bdf622a2e7ba06.cloudfront.net (CloudFront)', 'X-Amz-Cf-Pop': 'ORD58-P7', 'X-Amz-Cf-Id': 'QjJBzFQN6pa8lQviNvsk0mD4Zq_WevRrMeVgDL8CbqYPGW8EjC5CsQ==')>
2023-06-25 20:08:27.643 DEBUG (MainThread) [aioautomower.rest] Result mower data: {'data': [{'type': 'mower', 'id': '664f0889-2c1b-4bac-b9bb-9c4a7bea6ad5', 'attributes': {'system': {'name': 'Automower', 'model': 'HUSQVARNA AUTOMOWER® 430XH', 'serialNumber': 192103824}, 'battery': {'batteryPercent': 100}, 'mower': {'mode': 'MAIN_AREA', 'activity': 'PARKED_IN_CS', 'state': 'RESTRICTED', 'errorCode': 0, 'errorCodeTimestamp': 0}, 'calendar': {'tasks': [{'start': 705, 'duration': 585, 'monday': True, 'tuesday': True, 'wednesday': True, 'thursday': True, 'friday': True, 'saturday': True, 'sunday': True}]}, 'planner': {'nextStartTimestamp': 1687779900000, 'override': {'action': 'NOT_ACTIVE'}, 'restrictedReason': 'PARK_OVERRIDE'}, 'metadata': {'connected': True, 'statusTimestamp': 1687737283388}, 'positions': redacted, 'statistics': {'numberOfChargingCycles': 747, 'numberOfCollisions': 5867, 'totalChargingTime': 2469600, 'totalCuttingTime': 6559200, 'totalRunningTime': 6714000, 'totalSearchingTime': 154800}, 'cuttingHeight': 6, 'headlight': {'mode': 'EVENING_AND_NIGHT'}}}]}
2023-06-25 20:08:27.645 DEBUG (MainThread) [aioautomower.session] LE: False
2023-06-25 20:08:27.687 DEBUG (MainThread) [aioautomower.rest] Response mower data: <ClientResponse(https://api.amc.husqvarna.dev/v1/mowers/) [403 Forbidden]>
<CIMultiDictProxy('Content-Type': 'application/vnd.api+json', 'Content-Length': '181', 'Connection': 'keep-alive', 'Date': 'Mon, 26 Jun 2023 00:08:27 GMT', 'x-amzn-RequestId': '9279ba74-0fdb-40bd-b6fa-8acdc756d379', 'x-amzn-Remapped-Content-Length': '181', 'x-amzn-Remapped-Connection': 'keep-alive', 'x-amz-apigw-id': 'HGaHWEI1DoEFcNA=', 'x-amzn-Remapped-Date': 'Mon, 26 Jun 2023 00:08:27 GMT', 'X-Cache': 'Error from cloudfront', 'Via': '1.1 311a3438d86211891ae98ce35673353a.cloudfront.net (CloudFront)', 'X-Amz-Cf-Pop': 'ORD58-P7', 'X-Amz-Cf-Id': 'm6AR0OPhaHkSgsA0xFdw0PFQ6pYsZTtaWqmf8zdA7dzuJBcUuEbnng==')>
2023-06-25 20:08:27.687 ERROR (MainThread) [aioautomower.rest] Response mower data: {'errors': [{'id': 'd70b95de-08a9-4e18-8dd8-a8454d36fed7', 'status': '403', 'code': 'invalid.credentials', 'title': 'Invalid credentials', 'detail': 'The supplied credentials are invalid.'}]}
2023-06-25 20:08:27.692 DEBUG (MainThread) [aioautomower.session] LE: False
2023-06-25 20:09:03.388 DEBUG (MainThread) [aioautomower.session] Age in sec: 860
2023-06-25 20:09:03.388 DEBUG (MainThread) [aioautomower.session] No ws updates anymore and mower connected, ws probably down or mower shortly before disconnecting
2023-06-25 20:13:28.185 DEBUG (MainThread) [aioautomower.rest] Response mower data: <ClientResponse(https://api.amc.husqvarna.dev/v1/mowers/) [403 Forbidden]>
<CIMultiDictProxy('Content-Type': 'application/vnd.api+json', 'Content-Length': '181', 'Connection': 'keep-alive', 'Date': 'Mon, 26 Jun 2023 00:13:28 GMT', 'x-amzn-RequestId': 'cac3cc04-db11-4527-ae3d-9c931166b6ef', 'x-amzn-Remapped-Content-Length': '181', 'x-amzn-Remapped-Connection': 'keep-alive', 'x-amz-apigw-id': 'HGa2TE-HDoEFlgg=', 'x-amzn-Remapped-Date': 'Mon, 26 Jun 2023 00:13:28 GMT', 'X-Cache': 'Error from cloudfront', 'Via': '1.1 bfcd4a19db828cc95af34fc79f0fe0ce.cloudfront.net (CloudFront)', 'X-Amz-Cf-Pop': 'PIT50-P1', 'X-Amz-Cf-Id': 'mV6Tbe54r_E91-RK-gM0l4YUVuWqrkXyo2orVORQiwbre-iQHxcFfA==')>
2023-06-25 20:13:28.185 ERROR (MainThread) [aioautomower.rest] Response mower data: {'errors': [{'id': '41f0b356-dc9f-41f0-bbc5-188f6b6ad77f', 'status': '403', 'code': 'invalid.credentials', 'title': 'Invalid credentials', 'detail': 'The supplied credentials are invalid.'}]}
2023-06-25 20:13:28.187 DEBUG (MainThread) [aioautomower.session] LE: False
2023-06-25 20:13:28.281 DEBUG (MainThread) [aioautomower.rest] Response mower data: <ClientResponse(https://api.amc.husqvarna.dev/v1/mowers/) [200 OK]>
<CIMultiDictProxy('Content-Type': 'application/vnd.api+json', 'Content-Length': '3301', 'Connection': 'keep-alive', 'Date': 'Mon, 26 Jun 2023 00:13:28 GMT', 'x-amzn-RequestId': '4c913f48-b893-44d1-8b46-a29eda3031b3', 'x-amzn-Remapped-Content-Length': '3301', 'x-amzn-Remapped-Connection': 'keep-alive', 'x-amz-apigw-id': 'HGa2TEn6joEFyTw=', 'x-amzn-Remapped-Date': 'Mon, 26 Jun 2023 00:13:28 GMT', 'X-Cache': 'Miss from cloudfront', 'Via': '1.1 48c826f03f551ee8df23ba7911e47e48.cloudfront.net (CloudFront)', 'X-Amz-Cf-Pop': 'PIT50-P1', 'X-Amz-Cf-Id': 'pgXtx8wOVXH0xfsFH3hQDwRwu2dfZifmVrTPvivasCgDTqJPlleLdw==')>

config_entry-husqvarna_automower-29f9fd212d0a7318cb6470d180694958.json.txt

Din-BH commented 1 year ago

I am getting these now as of a few hours, and it is not going away - integration effectively broken for me right now. Also, when I try to press CONFIGURE in the Settings page I get a "Config flow could not be loaded: 500 Internal Server Error Server got itself in trouble", which gives below in logs;

2023-07-01 20:35:59.985 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/aiohttp/web_protocol.py", line 433, in _handle_request
resp = await request_handler(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/aiohttp/web_app.py", line 504, in _handle
resp = await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/aiohttp/web_middlewares.py", line 117, in impl
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 85, in security_filter_middleware
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/http/forwarded.py", line 100, in forwarded_middleware
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 28, in request_context_middleware
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 80, in ban_middleware
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 236, in auth_middleware
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 148, in handle
result = await handler(request, **request.match_info)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/config/config_entries.py", line 219, in post
return await super().post(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/http/data_validator.py", line 72, in wrapper
result = await method(view, request, data, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/data_entry_flow.py", line 71, in post
result = await self._flow_mgr.async_init(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 233, in async_init
flow = await self.async_create_flow(handler, context=context, data=data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/config_entries.py", line 1830, in async_create_flow
return HANDLERS[entry.domain].async_get_options_flow(entry)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/husqvarna_automower/config_flow.py", line 112, in async_get_options_flow
return OptionsFlowHandler(config_entry)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/husqvarna_automower/config_flow.py", line 128, in __init__
self.hass.data[DOMAIN][entity].session.data["data"]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
KeyError: 'data'

Before that there is an issue setting up the integration - probably since the auth is failing.

2023-07-01 20:16:14.372 ERROR (MainThread) [homeassistant.components.binary_sensor] Error while setting up husqvarna_automower platform for binary_sensor
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 320, in _async_setup_platform
await asyncio.shield(task)
File "/config/custom_components/husqvarna_automower/binary_sensor.py", line 25, in async_setup_entry
for idx, ent in enumerate(coordinator.session.data["data"])
~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
KeyError: 'data'
Din-BH commented 1 year ago

Or is my issue maybe slightly different? I am getting

2023-07-01 20:21:14.261 ERROR (MainThread) [aioautomower.rest] Response mower data: {'Message': 'User is not authorized to access this resource with an explicit deny'}

Din-BH commented 1 year ago

One more update. In the same state, I can also not download diagnostics. Error trace below.

Any quick suggestion for restarting the auth flow to try to recover, since it is not starting automatically?

Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/aiohttp/web_protocol.py", line 433, in _handle_request
    resp = await request_handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/web_app.py", line 504, in _handle
    resp = await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/web_middlewares.py", line 117, in impl
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 85, in security_filter_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/forwarded.py", line 100, in forwarded_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 28, in request_context_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 80, in ban_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 236, in auth_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 148, in handle
    result = await handler(request, **request.match_info)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/diagnostics/__init__.py", line 249, in get
    data = await info.config_entry_diagnostics(hass, config_entry)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/husqvarna_automower/diagnostics.py", line 38, in async_get_config_entry_diagnostics
    coordinator.session.data["data"], TO_REDACT
    ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
KeyError: 'data'
Din-BH commented 1 year ago

Further update; loaded the updated main, and did a re-authentication. Not helping..

Thomas55555 commented 1 year ago

Or is my issue maybe slightly different? I am getting

2023-07-01 20:21:14.261 ERROR (MainThread) [aioautomower.rest] Response mower data: {'Message': 'User is not authorized to access this resource with an explicit deny'}

Yes it's different. Your issue is more related to #426 For me everything works at the moment.

Thomas55555 commented 1 year ago

Regarding the original issue with the invalid credentials. I've got information from Husqvarna, that this was just a temporary problem, because someone spammed the server.