robinostlund / volkswagencarnet

A python library for volkswagen carnet
GNU General Public License v3.0
67 stars 38 forks source link

Information parsing fails in is_parking_light_supported #243

Closed raresv closed 10 months ago

raresv commented 10 months ago

One of my VW cars (Tiguan) has their VW services expired (I can still see it in the mobile app, but some of the services are not available anymore). As a result of this, I think some attributes have some values which are not parsed correctly. Or maybe there is just something with the vehicleLights sensor: Although... for the other VW car I own (eGolf) , all works fine.

See trace below for Tiguan::

DEBUG:volkswagencarnet.vw_dashboard:WVGZZZ........797 API trips is supported
DEBUG:volkswagencarnet.vw_dashboard:WVGZZZ........797 API selectivestatus is supported
DEBUG:volkswagencarnet.vw_dashboard:WVGZZZ........797 API parkingposition is supported
DEBUG:volkswagencarnet.vw_dashboard:WVGZZZ........797 API token is supported
DEBUG:volkswagencarnet.vw_dashboard:WVGZZZ........797 External power (BinarySensor:external_power) is not supported
DEBUG:volkswagencarnet.vw_dashboard:WVGZZZ........797 Energy flow (BinarySensor:energy_flow) is not supported
Traceback (most recent call last):
  File "C:\.....\volkswagencarnet\sample.py", line 100, in <module>
    loop.run_until_complete(main())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\Lib\asyncio\base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\.....\volkswagencarnet\sample.py", line 78, in main
    dashboard = vehicle.dashboard(mutable=True)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\myusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\volkswagencarnet\vw_vehicle.py", line 656, in dashboard
    return Dashboard(self, **config)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\myusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\volkswagencarnet\vw_dashboard.py", line 1132, in __init__
    self.instruments = [instrument for instrument in create_instruments() if instrument.setup(vehicle, **config)]
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\myusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\volkswagencarnet\vw_dashboard.py", line 1132, in <listcomp>
    self.instruments = [instrument for instrument in create_instruments() if instrument.setup(vehicle, **config)]
                                                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\myusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\volkswagencarnet\vw_dashboard.py", line 57, in setup
    if not self.is_supported:
           ^^^^^^^^^^^^^^^^^
  File "C:\Users\myusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\volkswagencarnet\vw_dashboard.py", line 103, in is_supported
    if hasattr(self.vehicle, supported):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\myusername\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\volkswagencarnet\vw_vehicle.py", line 769, in is_parking_light_supported
    return self.attrs.get("vehicleLights", False) and "lights" in self.attrs.get("vehicleLights").get(
                                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: argument of type 'NoneType' is not iterable

See extract below for the eGolf which works (parking lights parsing was ok):

DEBUG:volkswagencarnet.vw_dashboard:WVWZZZAUZKW....12 Energy flow (BinarySensor:energy_flow) is not supported
DEBUG:volkswagencarnet.vw_dashboard:WVWZZZAUZKW....12 Parking light is supported
raresv commented 10 months ago

I changed the code to this one, and it seems to work: Like this:

@property
def is_parking_light_supported(self) -> bool:
    """Return true if parking light is supported."""
    vehicle_lights = self.attrs.get("vehicleLights")
    if vehicle_lights is None:
        return False

    lights_status = vehicle_lights.get("lightsStatus")
    if lights_status is None:
        return False

    value = lights_status.get("value")
    return "lights" in value if value is not None else False

Maybe this helps, I did not test it much. Sorry, I should have made a pull request but I just figured I could try and solve it while reporting it :).

stickpin commented 10 months ago

Hi @raresv,

All three cars I have access to do have vehicleLights.lightsStatus.value.lights present. One of them is without paid service as well, so it shouldn't be related. If you can, please run the plugin in the debug mode and share the response from selectivestatus API. In the log it should be something like this: DEBUG (MainThread) [volkswagencarnet.vw_connection] Request for "https://emea.bff.cariad.digital/vehicle/v1/vehicles/MYCARVINNUMBER/selectivestatus?jobs=access,fuelStatus,vehicleLights,vehicleHealthInspection,measurements,charging,climatisation,automation" returned with status code [207], response: Please remove your VIN before sharing it here. Another option is to share your VW credentials with me (stickpins at gmail dot com) and I will check where the problem is.

raresv commented 10 months ago

I've run it but this is all it shows on that front:

DEBUG:volkswagencarnet.vw_vehicle:API endpoints: {'access': {'active': False}, 'tripStatistics': {'active': False}, 'measurements': {'active': False}, 'honkAndFlash': {'active': False}, 'parkingPosition': {'active': False}}
DEBUG:volkswagencarnet.vw_connection:HTTP GET "https://emea.bff.cariad.digital/vehicle/v1/vehicles/WVGZZZ...7/selectivestatus?jobs=access,fuelStatus,vehicleLights,vehicleHealthInspection,measurements,charging,climatisation,automation"
DEBUG:volkswagencarnet.vw_connection:HTTP GET "https://emea.bff.cariad.digital/vehicle/v2/vehicles"
DEBUG:volkswagencarnet.vw_connection:Request for "https://emea.bff.cariad.digital/vehicle/v1/vehicles/WVGZZZ....7/selectivestatus?jobs=access,fuelStatus,vehicleLights,vehicleHealthInspection,measurements,charging,climatisation,automation" returned with status code [207]

(there is no ", response:" at the end of the log item) I'll try to dig a bit more..

raresv commented 10 months ago

I did a bit of poor-man's debugging (added some logging in the code above): and apparently there is a nice error in the response instead of a good value:

Here is the vehicleLights value:

{'lightsStatus': {'error': {'message': 'Bad Gateway', 'errorTimeStamp': datetime.datetime(2024, 1, 21, 17, 6, 31, tzinfo=datetime.timezone.utc), 'info': 'Upstream service responded with an unexpected status. If the problem persists, please contact our support.', 'code': 4007, 'group': 2, 'retry': True}}} which leads to lightStatus not being ok: lightsStatus value:

{'error': {'message': 'Bad Gateway', 'errorTimeStamp': datetime.datetime(2024, 1, 21, 17, 6, 31, tzinfo=datetime.timezone.utc), 'info': 'Upstream service responded with an unexpected status. If the problem persists, please contact our support.', 'code': 4007, 'group': 2, 'retry': True}} which then means trying to get the "value" item from lightStatus returns None

For context, for the other car I have the API responds nicely: (i use the sample code from you to run the library, so it does both cars in the same run)

vehicleLights value: {'lightsStatus': {'value': {'carCapturedTimestamp': datetime.datetime(2024, 1, 21, 11, 26, 55, tzinfo=datetime.timezone.utc), 'lights': [{'name': 'right', 'status': 'off'}, {'name': 'left', 'status': 'off'}]}}} lightsStatus value: {'value': {'carCapturedTimestamp': datetime.datetime(2024, 1, 21, 11, 26, 55, tzinfo=datetime.timezone.utc), 'lights': [{'name': 'right', 'status': 'off'}, {'name': 'left', 'status': 'off'}]}}

so the "value" is nicely: {'carCapturedTimestamp': datetime.datetime(2024, 1, 21, 11, 26, 55, tzinfo=datetime.timezone.utc), 'lights': [{'name': 'right', 'status': 'off'}, {'name': 'left', 'status': 'off'}]}

stickpin commented 10 months ago

@raresv thanks for the details, I will fix it in the next beta. In any case, it's very strange that VW API responds this way, have no clue what could be the reason.

raresv commented 10 months ago

I think this happened for a long time already, it's not temporary. I use the home assistant addon based on this and due to this fluke it hangs while configuring the second car

stickpin commented 10 months ago

@raresv please try a new beta https://github.com/robinostlund/homeassistant-volkswagencarnet/releases/tag/v4.5.1-beta4. The issue should be fixed.

raresv commented 10 months ago

I just installed the beta and I can confirm the HA integration now connects properly and loads the car information also (does not load much information, though... but that's either normal or something for me to dig into it more). But.. well done, thank you!