G-Two / homeassistant-subaru

Subaru STARLINK custom component for Home Assistant.
Apache License 2.0
54 stars 6 forks source link

Getting "Unhandled API error code DEVICE_NOT_AUTHENTICATED" #104

Closed jam313 closed 6 months ago

jam313 commented 6 months ago

I had the integration working with my vehicle on a previous installation of HA, on docker with and a manual installation. I recently migrated to HA OS and installed the integration via HACS.

After setting up the integration, I entered my pin and it was accepted, but shows the message "Failed to setup"

In the log I see two errors (below), but I it indicates that one is cause by the first not being handled.

I've confirmed it is still working through the Subaru app and I know the vehicle should be supported as it was connected before.

Error 1

This error originated from a custom integration.

Logger: subarulink.controller
Source: custom_components/subaru/__init__.py:218
Integration: Subaru (HACS) (documentation, issues)
First occurred: 2:48:43 PM (3 occurrences)
Last logged: 3:18:26 PM

Unhandled API error code DEVICE_NOT_AUTHENTICATED

Error 2

Logger: homeassistant.config_entries
Source: config_entries.py:402
First occurred: 2:48:43 PM (3 occurrences)
Last logged: 3:18:26 PM

Error setting up entry [MY EMAIL] for subaru
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 402, in async_setup
    result = await component.async_setup_entry(hass, self)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/subaru/__init__.py", line 103, in async_setup_entry
    vehicles[vin] = await _get_vehicle_info(controller, vin)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/subaru/__init__.py", line 218, in _get_vehicle_info
    VEHICLE_HAS_POWER_WINDOWS: await controller.has_power_windows(vin),
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/subarulink/controller.py", line 286, in has_power_windows
    await self.get_data(vin)
  File "/usr/local/lib/python3.11/site-packages/subarulink/controller.py", line 409, in get_data
    await self.fetch(vin)
  File "/usr/local/lib/python3.11/site-packages/subarulink/controller.py", line 569, in fetch
    result = await self._fetch_status(vin)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/subarulink/controller.py", line 1081, in _fetch_status
    raise err
  File "/usr/local/lib/python3.11/site-packages/subarulink/controller.py", line 1067, in _fetch_status
    js_resp = await self._remote_query(vin, api.API_CONDITION)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/subarulink/controller.py", line 975, in _remote_query
    js_resp = await self._get(cmd.replace("api_gen", api_gen))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/subarulink/controller.py", line 910, in _get
    self._check_error_code(js_resp)
  File "/usr/local/lib/python3.11/site-packages/subarulink/controller.py", line 934, in _check_error_code
    raise SubaruException(f"Unhandled API error: {error} - {js_resp}")
subarulink.exceptions.SubaruException
G-Two commented 6 months ago

That error means that your HA instance is successfully authenticating to the Subaru API, but the device ID it is using isn't on the list of devices authorized with 2FA. Authorizing via 2FA occurs during initial setup, but if the device is ever deleted on the Subaru side, it will result in a broken installation. It should be possible to recognize this situation from the error code and automatically present the user with the option to reauthorize with 2FA, but I haven't figured out how to initiate a config flow step outside of an initial setup context.

In the mean time, you should be able to get your HA instance working again by deleting the Subaru device entry you have and then running the Subaru setup again. It should generate an new device ID and require you to complete the 2FA process in the config flow.

jam313 commented 6 months ago

Removing the Entity and re-adding fixed the issue for me! not sure why it had an issue in the first place though.