zabuldon / teslajsonpy

Apache License 2.0
56 stars 62 forks source link

fix: don't assume climate_state exists #404

Closed Megabytemb closed 1 year ago

Megabytemb commented 1 year ago

Home Assistant sometimes throws the following error on startup

Traceback (most recent call last):
  File "/config/custom_components/tesla_custom/teslamate.py", line 208, in msg_recieved
    ).result()
  File "/usr/local/python/lib/python3.10/concurrent/futures/_base.py", line 446, in result
    return self.__get_result()
  File "/usr/local/python/lib/python3.10/concurrent/futures/_base.py", line 391, in __get_result
    raise self._exception
  File "/config/custom_components/tesla_custom/teslamate.py", line 237, in async_handle_new_data
    coordinator.async_update_listeners()
  File "/usr/local/python/lib/python3.10/site-packages/homeassistant/helpers/update_coordinator.py", line 135, in async_update_listeners
    update_callback()
  File "/config/custom_components/tesla_custom/base.py", line 40, in refresh
    self.async_write_ha_state()
  File "/usr/local/python/lib/python3.10/site-packages/homeassistant/helpers/entity.py", line 559, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/local/python/lib/python3.10/site-packages/homeassistant/helpers/entity.py", line 600, in _async_write_ha_state
    state = self._stringify_state(available)
  File "/usr/local/python/lib/python3.10/site-packages/homeassistant/helpers/entity.py", line 565, in _stringify_state
    if (state := self.state) is None:
  File "/usr/local/python/lib/python3.10/site-packages/homeassistant/components/select/__init__.py", line 147, in state
    if self.current_option is None or self.current_option not in self.options:
  File "/config/custom_components/tesla_custom/select.py", line 156, in current_option
    current_value = self._car.get_seat_heater_status(
  File "/usr/local/python/lib/python3.10/site-packages/teslajsonpy/car.py", line 879, in get_seat_heater_status
    return self._vehicle_data.get("climate_state").get(seat_id)
AttributeError: 'NoneType' object has no attribute 'get'

This is because get_seat_heater_status doesn't return an empty dict if climate_state doesn't exist.

This fixes that error. All other calls in car.py return an empty dict instead of None

alandtse commented 1 year ago

Thanks. Can we add a test to avoid a regression?

Megabytemb commented 1 year ago

Done

alandtse commented 1 year ago

Thanks. I meant testing for the exception when climate state is None. Just do we don't accidentally revert this fix somehow.

Megabytemb commented 1 year ago

incoming :)