alandtse / tesla

Tesla custom integration for Home Assistant. This requires a refresh token be generated by third-party apps to login.
Apache License 2.0
555 stars 99 forks source link

`return data` statement code path where data is not set #907

Closed maruel closed 3 months ago

maruel commented 3 months ago

Version of the custom_component

HEAD as of now; 1c59bc28e2091737998906d52db38c87d488c5c7

Describe the bug

https://github.com/alandtse/tesla/blob/1c59bc28e2091737998906d52db38c87d488c5c7/custom_components/tesla_custom/__init__.py#L484

data is set on line 461 but if IncompleteCredentials triggers, it does a reload and still return data that is not existing. I think a return after lin 474 would fix the exception.

Debug log

2024-03-13 21:29:00.273 ERROR (MainThread) [custom_components.tesla_custom] Unexpected error fetching tesla_custom data: cannot access local variable 'data' where it is not associated with a value     
Traceback (most recent call last):                                                                                                                                                                       
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 313, in _async_refresh                                                                                                 
    self.data = await self._async_update_data()                                                                                                                                                          
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                          
  File "/config/custom_components/tesla_custom/__init__.py", line 484, in _async_update_data                                                                                                                 return data                                                                                                                                                                                          
           ^^^^                                                                                                                                                                                          UnboundLocalError: cannot access local variable 'data' where it is not associated with a value 
alandtse commented 3 months ago

Please go ahead and submit a PR if you see a solution.