itchannel/fordpass-ha

Corrupted fordpass_token.txt file

Opened this issue · 7 comments

The @_fordpass_token.txt file often gets corupted with an additional } at the end occationally, at least a couple of times a month. Trying to figure out if this is common, or if this is a local file system issue on my system. Symptom is sensors becomes unavailable and setup fails after failing to refresh token. Temporary solution is always to remove the additional } at the end and save the file.

I cannot find any reason in the code for this, my theory has been that it happens if there is less characters when re-writing file than before, and it does not clear the file, but as far as I can see this should not happen.

Anyone else seeing this issue, or is it just me? This is on the 1.70 beta branch

{"access_token": "...", "refresh_token": "...", "expires_in": 1800, "refresh_expires_in": 31556952, "expiry_date": 1720862042.6590693, "auto_token": "...", "auto_refresh": "....", "auto_expiry": 1720860542.326344}}

Should be:
{"access_token": "...", "refresh_token": "...", "expires_in": 1800, "refresh_expires_in": 31556952, "expiry_date": 1720862042.6590693, "auto_token": "...", "auto_refresh": "....", "auto_expiry": 1720860542.326344}

I just checked and mine at 5} on the end. Removed it and reloaded and everything started working again.

I'm trying to look into this issue, from first glance I see correct usage of the functions to read/write the token file. However, in the read_token, I see a retry mechanism that is trying to re-authenticate, which can rewrite the token file and retry reading it.

Could you provide the full corrupt token file (so I can try to reproduce in test code) or debugging logs? I am wondering if the line "Fixing malformed token" is in there...

I'm trying to look into this issue, from first glance I see correct usage of the functions to read/write the token file. However, in the read_token, I see a retry mechanism that is trying to re-authenticate, which can rewrite the token file and retry reading it.

Could you provide the full corrupt token file (so I can try to reproduce in test code) or debugging logs? I am wondering if the line "Fixing malformed token" is in there...

I'll turn on debugging for the app and keep tabs the next few days.

I was running into this as well... First time after authenticating it works, after it tries to reauthenticate, it fails with an extra }

Before I found this issue, I just reauthenticated and it worked again... for a day or 2, now deleting the } fixes it as well.. I just enabled debugging as well for wehn it happens next time

So I had the error again and did not seem to have fixing malformed token in the logs.... do have this stuff in logs:

`
2024-08-12 07:29:49.374 WARNING (MainThread) [homeassistant.helpers.frame] Detected that custom integration 'fordpass' calls async_forward_entry_setup for integration, fordpass with title: 2017 Ford Fusion (removed) and entry_id: removed, which is deprecated and will stop working in Home Assistant 2025.6, await async_forward_entry_setups instead at custom_components/fordpass/init.py, line 83: hass.async_create_task(, please create a bug report at https://github.com/itchannel/fordpass-ha/issues
2024-08-12 07:30:06.264 WARNING (MainThread) [homeassistant.helpers.frame] Detected code that calls async_forward_entry_setup for integration fordpass with title: 2017 Ford Fusion Energi (removed) and entry_id: removed, during setup without awaiting async_forward_entry_setup, which can cause the setup lock to be released before the setup is done. This will stop working in Home Assistant 2025.1. Please report this issue.
Stack (most recent call last):
File "", line 198, in _run_module_as_main
File "", line 88, in _run_code
File "/usr/src/homeassistant/homeassistant/main.py", line 223, in
sys.exit(main())
File "/usr/src/homeassistant/homeassistant/main.py", line 209, in main
exit_code = runner.run(runtime_conf)
File "/usr/src/homeassistant/homeassistant/runner.py", line 190, in run
return loop.run_until_complete(setup_and_run_hass(runtime_config))
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 674, in run_until_complete
self.run_forever()
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 641, in run_forever
self._run_once()
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 1990, in _run_once
handle._run()
File "/usr/local/lib/python3.12/asyncio/events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "/usr/src/homeassistant/homeassistant/config_entries.py", line 2161, in async_forward_entry_setup
_report_non_awaited_platform_forwards(entry, "async_forward_entry_setup")
File "/usr/src/homeassistant/homeassistant/config_entries.py", line 1201, in _report_non_awaited_platform_forwards
report(
File "/usr/src/homeassistant/homeassistant/helpers/frame.py", line 151, in report
_LOGGER.warning(msg, stack_info=True)
2024-08-12 07:30:06.272 WARNING (MainThread) [homeassistant.helpers.frame] Detected code that calls async_forward_entry_setup for integration fordpass with title: 2020 Ford Transit (removed) and entry_id: removed, during setup without awaiting async_forward_entry_setup, which can cause the setup lock to be released before the setup is done. This will stop working in Home Assistant 2025.1. Please report this issue.
Stack (most recent call last):
File "", line 198, in _run_module_as_main
File "", line 88, in _run_code
File "/usr/src/homeassistant/homeassistant/main.py", line 223, in
sys.exit(main())
File "/usr/src/homeassistant/homeassistant/main.py", line 209, in main
exit_code = runner.run(runtime_conf)
File "/usr/src/homeassistant/homeassistant/runner.py", line 190, in run
return loop.run_until_complete(setup_and_run_hass(runtime_config))
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 674, in run_until_complete
self.run_forever()
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 641, in run_forever
self._run_once()
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 1990, in _run_once
handle._run()
File "/usr/local/lib/python3.12/asyncio/events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "/usr/src/homeassistant/homeassistant/config_entries.py", line 2161, in async_forward_entry_setup
_report_non_awaited_platform_forwards(entry, "async_forward_entry_setup")
File "/usr/src/homeassistant/homeassistant/config_entries.py", line 1201, in _report_non_awaited_platform_forwards
report(
File "/usr/src/homeassistant/homeassistant/helpers/frame.py", line 151, in report
_LOGGER.warning(msg, stack_info=True)
2024-08-12 07:30:06.287 WARNING (MainThread) [homeassistant.helpers.frame] Detected code that calls async_forward_entry_setup for integration fordpass with title: 2017 Ford Fusion Energi (removed) and entry_id: removed, during setup without awaiting async_forward_entry_setup, which can cause the setup lock to be released before the setup is done. This will stop working in Home Assistant 2025.1. Please report this issue.
Stack (most recent call last):
File "", line 198, in _run_module_as_main
File "", line 88, in _run_code
File "/usr/src/homeassistant/homeassistant/main.py", line 223, in
sys.exit(main())
File "/usr/src/homeassistant/homeassistant/main.py", line 209, in main
exit_code = runner.run(runtime_conf)
File "/usr/src/homeassistant/homeassistant/runner.py", line 190, in run
return loop.run_until_complete(setup_and_run_hass(runtime_config))
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 674, in run_until_complete
self.run_forever()
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 641, in run_forever
self._run_once()
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 1990, in _run_once
handle._run()
File "/usr/local/lib/python3.12/asyncio/events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "/usr/src/homeassistant/homeassistant/config_entries.py", line 2161, in async_forward_entry_setup
_report_non_awaited_platform_forwards(entry, "async_forward_entry_setup")
File "/usr/src/homeassistant/homeassistant/config_entries.py", line 1201, in _report_non_awaited_platform_forwards
report(
File "/usr/src/homeassistant/homeassistant/helpers/frame.py", line 151, in report
_LOGGER.warning(msg, stack_info=True)
2024-08-12 07:30:06.290 WARNING (MainThread) [homeassistant.helpers.frame] Detected code that calls async_forward_entry_setup for integration fordpass with title: 2020 Ford Transit (removed) and entry_id: removed, during setup without awaiting async_forward_entry_setup, which can cause the setup lock to be released before the setup is done. This will stop working in Home Assistant 2025.1. Please report this issue.
Stack (most recent call last):
File "", line 198, in _run_module_as_main
File "", line 88, in _run_code
File "/usr/src/homeassistant/homeassistant/main.py", line 223, in
sys.exit(main())
File "/usr/src/homeassistant/homeassistant/main.py", line 209, in main
exit_code = runner.run(runtime_conf)
File "/usr/src/homeassistant/homeassistant/runner.py", line 190, in run
return loop.run_until_complete(setup_and_run_hass(runtime_config))
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 674, in run_until_complete
self.run_forever()
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 641, in run_forever
self._run_once()
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 1990, in _run_once
handle._run()
File "/usr/local/lib/python3.12/asyncio/events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "/usr/src/homeassistant/homeassistant/config_entries.py", line 2161, in async_forward_entry_setup
_report_non_awaited_platform_forwards(entry, "async_forward_entry_setup")
File "/usr/src/homeassistant/homeassistant/config_entries.py", line 1201, in _report_non_awaited_platform_forwards
report(
File "/usr/src/homeassistant/homeassistant/helpers/frame.py", line 151, in report
_LOGGER.warning(msg, stack_info=True)

2024-08-12 22:34:01.940 WARNING (SyncWorker_52) [urllib3.connectionpool] Retrying (Retry(total=5, connect=3, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:1000)'))': /v1.0/endpoint/default/authorize?redirect_uri=fordapp://userauthorized&response_type=code&scope=openid&max_age=3600&client_id=removed&code_challenge=removed&code_challenge_method=S256
2024-08-12 22:34:04.097 ERROR (MainThread) [custom_components.spook] Unexpected exception from <function AbstractSpookRepair.async_activate.._async_inspect at 0x763c699b04a0>
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/debounce.py", line 137, in _handle_timer_finish
await task
File "/config/custom_components/spook/repairs.py", line 158, in _async_inspect
await self.async_inspect()
File "/config/custom_components/spook/ectoplasms/automation/repairs/unknown_device_references.py", line 46, in async_inspect
device_ids=entity.referenced_devices,
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/functools.py", line 995, in get
val = self.func(instance)
^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/automation/init.py", line 584, in referenced_devices
referenced = self.action_script.referenced_devices
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/functools.py", line 995, in get
val = self.func(instance)
^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1594, in referenced_devices
Script._find_referenced_devices(referenced_devices, self.sequence)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1610, in _find_referenced_devices
_referenced_extract_ids(data, ATTR_DEVICE_ID, referenced)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1370, in _referenced_extract_ids
for item_id in item_ids:
TypeError: 'int' object is not iterable
2024-08-12 22:34:10.870 WARNING (MainThread) [custom_components.fordpass]
2024-08-12 22:34:10.870 WARNING (MainThread) [custom_components.fordpass] Error communicating with FordPass for removed

2024-08-12 22:34:10.870 ERROR (MainThread) [custom_components.fordpass] Error fetching fordpass data: Error communicating with FordPass for removed
2024-08-12 22:34:10.875 WARNING (MainThread) [custom_components.fordpass]
2024-08-12 22:34:10.876 WARNING (MainThread) [custom_components.fordpass] Error communicating with FordPass for removed
2024-08-12 22:34:10.876 ERROR (MainThread) [custom_components.fordpass] Error fetching fordpass data: Error communicating with FordPass for removed
`

Actually I restarted HA like 5 times in a row and ended up with both the extra } and the last character, so I had to delete the last 2 characters to get it working again.

The first 4 errors are because of API changes on Home Assistant side, and was acually already reported in issue #520 and resolved with pull request #521, so updating your data to the latest 1.70 branch state will resolve these errors.

Unfortunately I can't make sense of the last error at this moment.