mbillow / ha-chargepoint

Home Assistant ChargePoint EV Charger Integration
MIT License
44 stars 6 forks source link

Can't add integration: KeyError: 'last_connected_at' #19

Closed SeanPM5 closed 1 year ago

SeanPM5 commented 1 year ago

Yesterday morning I installed the ha-chargepoint integration and it worked fine showing me one entity (account balance). Then in the afternoon I had ChargePoint Home Flex unit installed and it broke this integration for me.

Error in logs:

This error originated from a custom integration.

Logger: custom_components.chargepoint
Source: custom_components/chargepoint/__init__.py:153
Integration: ChargePoint (documentation, issues)
First occurred: February 3, 2023 at 5:43:25 PM (929 occurrences)
Last logged: 10:31:06 AM

Unexpected error fetching chargepoint data: 'last_connected_at'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 239, in _async_refresh
    self.data = await self._async_update_data()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 195, in _async_update_data
    return await self.update_method()
  File "/config/custom_components/chargepoint/__init__.py", line 153, in async_update_data
    await hass.async_add_executor_job(
  File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.10/site-packages/python_chargepoint/client.py", line 42, in check_login
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/python_chargepoint/client.py", line 329, in get_home_charger_technical_info
    return HomeChargerTechnicalInfo.from_json(
  File "/usr/local/lib/python3.10/site-packages/python_chargepoint/types.py", line 150, in from_json
    json["last_connected_at"] / 1000, tz=timezone.utc
KeyError: 'last_connected_at'

HA devices page (retries this constantly): error

Home Charger -> Settings -> About Charger screen: image

Guessing it has to do with the last line there saying December 31, 1969. I don't know why that is not updating, the charger and Wi-Fi connection and everything works 100% fine besides that (I see my home charger in the Charging Activity tab and everything). But this seems to prevent the integration from being added at all.

Literally purchased the charger because of this great looking HA integration so hope I can get it added (even if the last connected entity doesn't work or whatever). Would appreciate any help. Thanks.

GeekGamerTV commented 1 year ago

I also am having the same issue. It's probably due to the date in the past with the "Last Contact" entry. Either we probably need to see a push from Chargepoint (to update the date) or something in the plugin to acknowledge this date in the past.

mbillow commented 1 year ago

Literally purchased the charger because of this great looking HA integration so hope I can get it added (even if the last connected entity doesn't work or whatever).

Man, I should really get a percentage of that sale. 😉 haha

Last contact: Dec 31, 1969 at 7:00:00 PM

Mmmm. Epoch. There are so many parts of this API that can just return garbage data at any point. I'll see what I can do to get you up and running. 😄

mbillow commented 1 year ago

So, as you might have been able to tell from the light hearted animosity in my last comment, this API is a lot more fragile that I initially expected it to be. 😓 That makes this kind of issue far more common than I'd like because I initially built the API library to rigidly read from the API responses.

Instead of continuing this game or whack-a-mole, I went ahead and replaced all the direct dictionary accessors with calls to get() with Falsey defaults. This maintains our type hinting signatures but allows the API to respond with garbage and our clients not break.

If you update to v0.5.0, you should be able to get the plugin to work now. 😄

GeekGamerTV commented 1 year ago

Thank you for updating @mbillow - Everything is now working again as described.

Much appreciated.