G-Two / subarulink

A python package for interacting with Subaru STARLINK remote vehicle services.
Apache License 2.0
86 stars 13 forks source link

Error when parsing time in controller.py #96

Open anoopknayak opened 8 months ago

anoopknayak commented 8 months ago

Hi,

I have been running subaru integration in Home assistant but I see the following error in the logs:

Traceback (most recent call last):
  File "/home/homeassistant/.pyenv/versions/3.11.6/lib/python3.11/site-packages/homeassistant/helpers/update_coordinator.py", line 290, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homeassistant/.pyenv/versions/3.11.6/lib/python3.11/site-packages/homeassistant/helpers/update_coordinator.py", line 246, in _async_update_data
    return await self.update_method()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homeassistant/.pyenv/versions/3.11.6/lib/python3.11/site-packages/homeassistant/components/subaru/__init__.py", line 75, in async_update_data
    return await refresh_subaru_data(entry, vehicle_info, controller)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homeassistant/.pyenv/versions/3.11.6/lib/python3.11/site-packages/homeassistant/components/subaru/__init__.py", line 125, in refresh_subaru_data
    await controller.fetch(vin, force=True)
  File "/mnt/dietpi_userdata/homeassistant/deps/lib/python3.11/site-packages/subarulink/controller.py", line 569, in fetch
    result = await self._fetch_status(vin)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/dietpi_userdata/homeassistant/deps/lib/python3.11/site-packages/subarulink/controller.py", line 1070, in _fetch_status
    status = await self._parse_condition(js_resp, vin)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/dietpi_userdata/homeassistant/deps/lib/python3.11/site-packages/subarulink/controller.py", line 1304, in _parse_condition
    keep_data[sc.TIMESTAMP] = datetime.strptime(data[api.API_LAST_UPDATED_DATE], api.API_TIMESTAMP_FMT)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homeassistant/.pyenv/versions/3.11.6/lib/python3.11/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homeassistant/.pyenv/versions/3.11.6/lib/python3.11/_strptime.py", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '2024-02-04T18:53:51.000+0000' does not match format '%Y-%m-%dT%H:%M:%S%z'

I can submit a patch if I get time, but I believe the right fix would be to iterate through all the known time formats obtained from subaru. Wdyt @G-Two?

G-Two commented 8 months ago

Are you using the latest release?

G-Two commented 8 months ago

There should be a try/except block now using the two known formats returned by the subaru API:

        try:
            keep_data[sc.TIMESTAMP] = datetime.strptime(data[api.API_LAST_UPDATED_DATE], api.API_TIMESTAMP_FMT)
        except ValueError:
            keep_data[sc.TIMESTAMP] = datetime.strptime(data[api.API_LAST_UPDATED_DATE], api.API_TIMESTAMP_FMT_OLD)
G-Two commented 8 months ago

Related https://github.com/G-Two/homeassistant-subaru/issues/101

anoopknayak commented 7 months ago

Hey @G-Two, sorry for the late reply. This missed my notifications. I need to check with the latest release of Home assistant but as of two weeks ago it wasn't working. I'm using the subaru plugin which comes with HA.