Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow entities to be updated from TeslaMate via MQTT #564

Merged
merged 7 commits into from
Apr 9, 2023

Conversation

Megabytemb
Copy link
Contributor

As a way of getting more real-time updates for entities, this pull request allows you to configure the tesla_custom integration to use the the MQTT integration to get updates from TeslaMate.

This means a select few entities updates once a second, as TeslaMate publishes them via MQTT.

https://docs.teslamate.org/docs/integrations/mqtt/

This pull request add an option to the Config entry options to enable the overall connection.
It also adds a new Text entity, which is disabled by default, so you can set the TeslaMate ID for the car (aka device) in this integration.

The TeslaMate class also checks to ensure you also have the MQTT integration configured, and simply logs a warning if it doesn't.

@Megabytemb Megabytemb changed the title Allow entities to be updated from TeslaMate via MQTT Feat: Allow entities to be updated from TeslaMate via MQTT Apr 5, 2023
@Megabytemb Megabytemb changed the title Feat: Allow entities to be updated from TeslaMate via MQTT feat: Allow entities to be updated from TeslaMate via MQTT Apr 5, 2023
@alandtse
Copy link
Owner

alandtse commented Apr 9, 2023

Thanks! I don't have anyway to test this, so I'll push it to dev. Can you see if you can get some testers to take the dev build? If after a week, it seems like nothing bad happens, I'll push to main.

@alandtse alandtse merged commit 36713fb into alandtse:dev Apr 9, 2023
@petermnt
Copy link

Thanks @Megabytemb, this is a great addition. It will allow me to get rid of some double sensors.

I have just switched to dev. Will report back if something weird happens.

@petermnt
Copy link

petermnt commented Apr 14, 2023

Seems to work. But it wasn't clear to me that I had to enable the text field and fill it it before it starts working.

Might not be a bad idea to default to 1, as the vast majority of people will only have 1 car linked, or at least to add it to the docs.

@Megabytemb
Copy link
Contributor Author

@petermnt
Copy link

I did not, I only saw the README.md change in this PR.

That is indeed very clear.

Maybe for easy discovery this page could be linked in the line about Teslamate in the README.

@elgeniskogen
Copy link

elgeniskogen commented Apr 21, 2023

GREAT new feature :-) But the usage of "Teslamate id" is not crystal clear to me. So if I have sensor like "sensor.tesla_usable_battery_level" from Teslamate I should input "tesla" as "Teslamate id" in order for the Tesla integration to read the right sensor values from MQTT? But if I change it it returns to "unknown" after very short time.

@Megabytemb
Copy link
Contributor Author

Megabytemb commented Apr 21, 2023 via email

@fishredleung
Copy link

why couldn't I save my tesla mate ID? After I enter 1 and click outside the box, it still showed 'unknown'.

@Megabytemb
Copy link
Contributor Author

Hey @fishredleung

Did you end up with it working?

If not, anything in the Home Assistant Log?
Have you enabled Teslamate in the Config options for the intergration?
Is there a /config/.storage/tesla_custom_teslamte file? if so, what is its contents? (feel free to obscure your VIN)

@drjjr2
Copy link

drjjr2 commented Apr 23, 2023

Hello,
So I think I have this set up correctly and I do have the /config/.storage/tesla_custom_teslamate file with VINs and IDs that are correct. Enabling debug logging doesn't show me anything MQTT specific but I do get plenty of:

2023-04-23 12:18:08.338 DEBUG (MainThread) [custom_components.tesla_custom] Running controller.update()
2023-04-23 12:18:08.379 DEBUG (MainThread) [custom_components.tesla_custom] Running controller.update()
2023-04-23 12:18:23.508 DEBUG (MainThread) [custom_components.tesla_custom] Finished fetching tesla_custom data in 15.170 seconds (success: True)
2023-04-23 12:18:23.516 DEBUG (MainThread) [custom_components.tesla_custom] Finished fetching tesla_custom data in 15.137 seconds (success: True)
2023-04-23 12:18:33.272 DEBUG (MainThread) [custom_components.tesla_custom] Running controller.update()
2023-04-23 12:18:33.301 DEBUG (MainThread) [custom_components.tesla_custom] Running controller.update()

Does this mean it's working?

@hanabal2
Copy link

ive got the same issue as @fishredleung. the ID is always showing unknown. I had a look and there is no such file I can find using file explorer.

There is some stuff in the log that sounds like its sudscribing to something

2023-04-23 17:06:03.777 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting battery_level from MQTT
2023-04-23 17:06:03.779 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting time_to_full_charge from MQTT
2023-04-23 17:06:03.784 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting locked from MQTT
2023-04-23 17:06:03.790 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting odometer from MQTT
2023-04-23 17:06:03.794 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting tpms_pressure_fr from MQTT
2023-04-23 17:06:03.800 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting sentry_mode from MQTT
2023-04-23 17:06:03.803 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting tpms_pressure_rr from MQTT

@Megabytemb
Copy link
Contributor Author

ive got the same issue as @fishredleung. the ID is always showing unknown. I had a look and there is no such file I can find using file explorer.

There is some stuff in the log that sounds like its sudscribing to something

2023-04-23 17:06:03.777 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting battery_level from MQTT
2023-04-23 17:06:03.779 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting time_to_full_charge from MQTT
2023-04-23 17:06:03.784 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting locked from MQTT
2023-04-23 17:06:03.790 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting odometer from MQTT
2023-04-23 17:06:03.794 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting tpms_pressure_fr from MQTT
2023-04-23 17:06:03.800 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting sentry_mode from MQTT
2023-04-23 17:06:03.803 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting tpms_pressure_rr from MQTT

Hey @hanabal2

Yours is working, but i'm not sure why you still get "unknown" whilst still getting MQTT sub messages in the logs.

is there any error in the logs when you set the car ID?
Can you toggle the config entry option "Sync Data from TeslaMate via MQTT" off, save, on, save and share the logs?

@Woutch
Copy link

Woutch commented Apr 24, 2023

Same "issue" here. Set the id to 1 but keeps showing unknown. Reloaded the Tesla integration and it shows 1. Working as expected. Very nice feature, was running teslamate separate, love to see it integrated!!!

Many thanks!!

@Megabytemb
Copy link
Contributor Author

Hello, So I think I have this set up correctly and I do have the /config/.storage/tesla_custom_teslamate file with VINs and IDs that are correct. Enabling debug logging doesn't show me anything MQTT specific but I do get plenty of:

2023-04-23 12:18:08.338 DEBUG (MainThread) [custom_components.tesla_custom] Running controller.update()
2023-04-23 12:18:08.379 DEBUG (MainThread) [custom_components.tesla_custom] Running controller.update()
2023-04-23 12:18:23.508 DEBUG (MainThread) [custom_components.tesla_custom] Finished fetching tesla_custom data in 15.170 seconds (success: True)
2023-04-23 12:18:23.516 DEBUG (MainThread) [custom_components.tesla_custom] Finished fetching tesla_custom data in 15.137 seconds (success: True)
2023-04-23 12:18:33.272 DEBUG (MainThread) [custom_components.tesla_custom] Running controller.update()
2023-04-23 12:18:33.301 DEBUG (MainThread) [custom_components.tesla_custom] Running controller.update()

Does this mean it's working?

HEy @drjjr2
I'm not seeting any MQTT logs in yours, so I don't think its fully set up yet (or, there's just nothig from MQTT to set at that moment). The logs you've shared are just the normal logs for the intergration.

When you start home assistant, are there logs like "Subscribed to topic x" from the tesla intergration?
you could also do the same as above, Can you toggle the config entry option "Sync Data from TeslaMate via MQTT" off, save, on, save and share the logs? and it should generate some logs

@drjjr2
Copy link

drjjr2 commented Apr 24, 2023

Toggling the option off and then on seemed to do it:

2023-04-24 06:54:13.009 INFO (MainThread) [custom_components.tesla_custom.teslamate] Un-subbing from MQTT Topics.
2023-04-24 06:54:13.274 DEBUG (MainThread) [custom_components.tesla_custom] Running controller.update()
2023-04-24 06:54:18.656 DEBUG (MainThread) [custom_components.tesla_custom] Finished fetching tesla_custom data in 15.356 seconds (success: True)
2023-04-24 06:54:18.661 DEBUG (MainThread) [custom_components.tesla_custom] Finished fetching tesla_custom data in 5.386 seconds (success: True)
2023-04-24 06:54:28.284 DEBUG (MainThread) [custom_components.tesla_custom] Running controller.update()
2023-04-24 06:54:28.285 DEBUG (MainThread) [custom_components.tesla_custom] Finished fetching tesla_custom data in 0.001 seconds (success: True)
2023-04-24 06:54:28.301 DEBUG (MainThread) [custom_components.tesla_custom] Running controller.update()
2023-04-24 06:54:31.834 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting up MQTT subs for Teslamate
2023-04-24 06:54:31.834 INFO (MainThread) [custom_components.tesla_custom.teslamate] Un-subbing from MQTT Topics.
2023-04-24 06:54:31.857 INFO (MainThread) [custom_components.tesla_custom.teslamate] Subscribed to topic: teslamate/cars/1/#
2023-04-24 06:54:31.867 INFO (MainThread) [custom_components.tesla_custom.teslamate] Subscribed to topic: teslamate/cars/2/#
2023-04-24 06:54:32.030 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting latitude from MQTT
2023-04-24 06:54:32.056 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting odometer from MQTT
2023-04-24 06:54:32.070 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting battery_level from MQTT
2023-04-24 06:54:32.083 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting longitude from MQTT
2023-04-24 06:54:32.100 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting usable_battery_level from MQTT
2023-04-24 06:54:32.108 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting outside_temp from MQTT
2023-04-24 06:54:32.117 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting inside_temp from MQTT
2023-04-24 06:54:32.124 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting tpms_pressure_rl from MQTT
2023-04-24 06:54:32.142 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting tpms_pressure_fr from MQTT
2023-04-24 06:54:32.150 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting charger_actual_current from MQTT
2023-04-24 06:54:32.156 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting time_to_full_charge from MQTT
2023-04-24 06:54:32.163 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting sentry_mode from MQTT
2023-04-24 06:54:32.171 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting charger_voltage from MQTT
2023-04-24 06:54:32.177 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting charge_limit_soc from MQTT
2023-04-24 06:54:32.184 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting heading from MQTT
2023-04-24 06:54:32.190 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting locked from MQTT
2023-04-24 06:54:32.205 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting charge_energy_added from MQTT
2023-04-24 06:54:32.212 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting tpms_pressure_rr from MQTT
2023-04-24 06:54:32.218 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting tpms_pressure_fl from MQTT
2023-04-24 06:54:32.224 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting charger_power from MQTT
2023-04-24 06:54:32.233 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting is_climate_on from MQTT
2023-04-24 06:54:32.239 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting speed from MQTT

@drjjr2
Copy link

drjjr2 commented Apr 24, 2023

However, now I am seeing these errors for one of the cars:

2023-04-24 07:10:43.504 ERROR (SyncWorker_11) [homeassistant.util.logging] Exception in msg_recieved when handling msg on 'teslamate/cars/2/longitude': '-95.591385'
Traceback (most recent call last):
  File "/config/custom_components/tesla_custom/teslamate.py", line 207, in msg_recieved
    ).result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 458, in result
    return self.__get_result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
    raise self._exception
  File "/config/custom_components/tesla_custom/teslamate.py", line 236, in async_handle_new_data
    coordinator.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 142, 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/src/homeassistant/homeassistant/helpers/entity.py", line 568, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 632, in _async_write_ha_state
    state = self._stringify_state(available)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 574, in _stringify_state
    if (state := self.state) is None:
  File "/usr/src/homeassistant/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/lib/python3.10/site-packages/teslajsonpy/car.py", line 877, in get_seat_heater_status
    return self._vehicle_data.get("climate_state").get(seat_id)
AttributeError: 'NoneType' object has no attribute 'get'

2023-04-24 07:10:43.505 ERROR (SyncWorker_62) [homeassistant.util.logging] Exception in msg_recieved when handling msg on 'teslamate/cars/2/usable_battery_level': '85'
Traceback (most recent call last):
  File "/config/custom_components/tesla_custom/teslamate.py", line 207, in msg_recieved
    ).result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 458, in result
    return self.__get_result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
    raise self._exception
  File "/config/custom_components/tesla_custom/teslamate.py", line 254, in async_handle_new_data
    coordinator.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 142, 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/src/homeassistant/homeassistant/helpers/entity.py", line 568, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 632, in _async_write_ha_state
    state = self._stringify_state(available)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 574, in _stringify_state
    if (state := self.state) is None:
  File "/usr/src/homeassistant/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/lib/python3.10/site-packages/teslajsonpy/car.py", line 877, in get_seat_heater_status
    return self._vehicle_data.get("climate_state").get(seat_id)
AttributeError: 'NoneType' object has no attribute 'get'

2023-04-24 07:10:43.526 ERROR (SyncWorker_41) [homeassistant.util.logging] Exception in msg_recieved when handling msg on 'teslamate/cars/2/battery_level': '85'
Traceback (most recent call last):
  File "/config/custom_components/tesla_custom/teslamate.py", line 207, in msg_recieved
    ).result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 458, in result
    return self.__get_result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
    raise self._exception
  File "/config/custom_components/tesla_custom/teslamate.py", line 254, in async_handle_new_data
    coordinator.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 142, 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/src/homeassistant/homeassistant/helpers/entity.py", line 568, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 632, in _async_write_ha_state
    state = self._stringify_state(available)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 574, in _stringify_state
    if (state := self.state) is None:
  File "/usr/src/homeassistant/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/lib/python3.10/site-packages/teslajsonpy/car.py", line 877, in get_seat_heater_status
    return self._vehicle_data.get("climate_state").get(seat_id)
AttributeError: 'NoneType' object has no attribute 'get'

2023-04-24 07:10:43.545 ERROR (SyncWorker_53) [homeassistant.util.logging] Exception in msg_recieved when handling msg on 'teslamate/cars/2/odometer': '783.41'
Traceback (most recent call last):
  File "/config/custom_components/tesla_custom/teslamate.py", line 207, in msg_recieved
    ).result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 458, in result
    return self.__get_result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
    raise self._exception
  File "/config/custom_components/tesla_custom/teslamate.py", line 242, in async_handle_new_data
    coordinator.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 142, 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/src/homeassistant/homeassistant/helpers/entity.py", line 568, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 632, in _async_write_ha_state
    state = self._stringify_state(available)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 574, in _stringify_state
    if (state := self.state) is None:
  File "/usr/src/homeassistant/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/lib/python3.10/site-packages/teslajsonpy/car.py", line 877, in get_seat_heater_status
    return self._vehicle_data.get("climate_state").get(seat_id)
AttributeError: 'NoneType' object has no attribute 'get'

@rubin110
Copy link

Hi there. Thanks for this awesome update. Before I set this up, would it make sense to disable some of the entities I'm already grabbing from TeslaMate's MQTT updates so they're not doubled up? Thanks.

@drjjr2
Copy link

drjjr2 commented Apr 24, 2023

Update: After re-enabling all the devices in the custom integration and restarting, everything seems to be subscribed and I know Teslamate is sending data because the sensors I set up manually work. Hoewver, I don't see any messages about MQTT updating anything. Just the same ones from above.
Also can I request that we add the Teslamate ID for the car being updated to the debug logs? I saw a single temperature sensor update message and I wonder if that's car 2.
Could the problem be that I have two cars? So far I've only driven one of them to see if there are log entries.

Edit: Definitely only see log entries for car 2.

2023-04-24 15:27:10.013 DEBUG (MainThread) [custom_components.tesla_custom] Finished fetching tesla_custom data in 2.601 seconds (success: True)
2023-04-24 15:27:11.304 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting shift_state from MQTT
2023-04-24 15:27:11.364 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting inside_temp from MQTT
2023-04-24 15:27:11.387 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting locked from MQTT
2023-04-24 15:27:11.399 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting is_climate_on from MQTT
2023-04-24 15:27:11.408 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting speed from MQTT
2023-04-24 15:27:11.418 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting outside_temp from MQTT

@Megabytemb
Copy link
Contributor Author

However, now I am seeing these errors for one of the cars:

2023-04-24 07:10:43.504 ERROR (SyncWorker_11) [homeassistant.util.logging] Exception in msg_recieved when handling msg on 'teslamate/cars/2/longitude': '-95.591385'
Traceback (most recent call last):
  File "/config/custom_components/tesla_custom/teslamate.py", line 207, in msg_recieved
    ).result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 458, in result
    return self.__get_result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
    raise self._exception
  File "/config/custom_components/tesla_custom/teslamate.py", line 236, in async_handle_new_data
    coordinator.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 142, 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/src/homeassistant/homeassistant/helpers/entity.py", line 568, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 632, in _async_write_ha_state
    state = self._stringify_state(available)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 574, in _stringify_state
    if (state := self.state) is None:
  File "/usr/src/homeassistant/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/lib/python3.10/site-packages/teslajsonpy/car.py", line 877, in get_seat_heater_status
    return self._vehicle_data.get("climate_state").get(seat_id)
AttributeError: 'NoneType' object has no attribute 'get'

2023-04-24 07:10:43.505 ERROR (SyncWorker_62) [homeassistant.util.logging] Exception in msg_recieved when handling msg on 'teslamate/cars/2/usable_battery_level': '85'
Traceback (most recent call last):
  File "/config/custom_components/tesla_custom/teslamate.py", line 207, in msg_recieved
    ).result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 458, in result
    return self.__get_result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
    raise self._exception
  File "/config/custom_components/tesla_custom/teslamate.py", line 254, in async_handle_new_data
    coordinator.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 142, 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/src/homeassistant/homeassistant/helpers/entity.py", line 568, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 632, in _async_write_ha_state
    state = self._stringify_state(available)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 574, in _stringify_state
    if (state := self.state) is None:
  File "/usr/src/homeassistant/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/lib/python3.10/site-packages/teslajsonpy/car.py", line 877, in get_seat_heater_status
    return self._vehicle_data.get("climate_state").get(seat_id)
AttributeError: 'NoneType' object has no attribute 'get'

2023-04-24 07:10:43.526 ERROR (SyncWorker_41) [homeassistant.util.logging] Exception in msg_recieved when handling msg on 'teslamate/cars/2/battery_level': '85'
Traceback (most recent call last):
  File "/config/custom_components/tesla_custom/teslamate.py", line 207, in msg_recieved
    ).result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 458, in result
    return self.__get_result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
    raise self._exception
  File "/config/custom_components/tesla_custom/teslamate.py", line 254, in async_handle_new_data
    coordinator.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 142, 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/src/homeassistant/homeassistant/helpers/entity.py", line 568, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 632, in _async_write_ha_state
    state = self._stringify_state(available)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 574, in _stringify_state
    if (state := self.state) is None:
  File "/usr/src/homeassistant/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/lib/python3.10/site-packages/teslajsonpy/car.py", line 877, in get_seat_heater_status
    return self._vehicle_data.get("climate_state").get(seat_id)
AttributeError: 'NoneType' object has no attribute 'get'

2023-04-24 07:10:43.545 ERROR (SyncWorker_53) [homeassistant.util.logging] Exception in msg_recieved when handling msg on 'teslamate/cars/2/odometer': '783.41'
Traceback (most recent call last):
  File "/config/custom_components/tesla_custom/teslamate.py", line 207, in msg_recieved
    ).result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 458, in result
    return self.__get_result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
    raise self._exception
  File "/config/custom_components/tesla_custom/teslamate.py", line 242, in async_handle_new_data
    coordinator.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 142, 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/src/homeassistant/homeassistant/helpers/entity.py", line 568, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 632, in _async_write_ha_state
    state = self._stringify_state(available)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 574, in _stringify_state
    if (state := self.state) is None:
  File "/usr/src/homeassistant/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/lib/python3.10/site-packages/teslajsonpy/car.py", line 877, in get_seat_heater_status
    return self._vehicle_data.get("climate_state").get(seat_id)
AttributeError: 'NoneType' object has no attribute 'get'

I just made a pull request in teslajsonpy to fix this.
zabuldon/teslajsonpy#404

will get this into the integration soon

@Megabytemb
Copy link
Contributor Author

Hi there. Thanks for this awesome update. Before I set this up, would it make sense to disable some of the entities I'm already grabbing from TeslaMate's MQTT updates so they're not doubled up? Thanks.

You don't need to, you can keep both for a bit to ensure its updating correctly

@drjjr2
Copy link

drjjr2 commented Apr 25, 2023

I just made a pull request in teslajsonpy to fix this. zabuldon/teslajsonpy#404

will get this into the integration soon

Excellent. Any thoughts as to why I can never get data for car 1 but I do for car 2. I can see that it has subscribed to both:

2023-04-25 00:30:17.384 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting up MQTT subs for Teslamate
2023-04-25 00:30:17.384 INFO (MainThread) [custom_components.tesla_custom.teslamate] Un-subbing from MQTT Topics.
2023-04-25 00:30:17.400 INFO (MainThread) [custom_components.tesla_custom.teslamate] Subscribed to topic: teslamate/cars/1/#
2023-04-25 00:30:17.438 INFO (MainThread) [custom_components.tesla_custom.teslamate] Subscribed to topic: teslamate/cars/2/#
2023-04-25 00:30:17.616 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting latitude from MQTT
2023-04-25 00:30:17.633 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting odometer from MQTT
2023-04-25 00:30:17.655 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting battery_level from MQTT
2023-04-25 00:30:17.665 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting longitude from MQTT
2023-04-25 00:30:17.672 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting usable_battery_level from MQTT
2023-04-25 00:30:17.678 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting outside_temp from MQTT
2023-04-25 00:30:17.697 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting inside_temp from MQTT
2023-04-25 00:30:17.705 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting tpms_pressure_rl from MQTT
2023-04-25 00:30:17.722 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting tpms_pressure_fr from MQTT
2023-04-25 00:30:17.728 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting charger_actual_current from MQTT
2023-04-25 00:30:17.736 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting time_to_full_charge from MQTT
2023-04-25 00:30:17.770 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting sentry_mode from MQTT
2023-04-25 00:30:17.775 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting charger_voltage from MQTT
2023-04-25 00:30:17.781 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting charge_limit_soc from MQTT
2023-04-25 00:30:17.786 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting heading from MQTT
2023-04-25 00:30:17.795 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting locked from MQTT
2023-04-25 00:30:17.802 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting charge_energy_added from MQTT
2023-04-25 00:30:17.808 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting tpms_pressure_rr from MQTT
2023-04-25 00:30:17.812 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting tpms_pressure_fl from MQTT
2023-04-25 00:30:17.819 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting charger_power from MQTT
2023-04-25 00:30:17.824 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting is_climate_on from MQTT
2023-04-25 00:30:17.831 INFO (MainThread) [custom_components.tesla_custom.teslamate] Setting speed from MQTT

@Megabytemb
Copy link
Contributor Author

I've just worked on a update to the teslamate class with way more logging to help us fix that.
Sadly, i don't have 2 cars to test with.

Any chance you could replace the teslamate.py file in config/custom_components/tesla_custom with the following.
https://raw.githubusercontent.com/Megabytemb/tesla/telslamate_updates/custom_components/tesla_custom/teslamate.py

@Megabytemb
Copy link
Contributor Author

@drjjr2 Please continue the discussion in the Issue. We probably shouldn't be having this conversation here

@jonferreira
Copy link

Just stopped by to say this is awesome, thanks.

On a different note, does this means we can / should change / disable polling all together?

@Megabytemb
Copy link
Contributor Author

Megabytemb commented May 6, 2023 via email

@rubin110
Copy link

rubin110 commented May 9, 2023

So I finally had a little bit of time to do some HA maintenance.

First observation I had is once you restart after updating to this version of the extension you must wake your car up, otherwise the device id entity will remain unavailable.

Regarding @Megabytemb answering my question:

Hi there. Thanks for this awesome update. Before I set this up, would it make sense to disable some of the entities I'm already grabbing from TeslaMate's MQTT updates so they're not doubled up? Thanks.

You don't need to, you can keep both for a bit to ensure its updating correctly

I don't see entities double up more than just having this Tesla integration and MQTT coming in from Teslamate. My thought was there would be remnants of entities from the prior version of the integration from the previous method of grabbing data directly from Tesla's API.

@Megabytemb
Copy link
Contributor Author

Hey @rubin110
To be clear, when you have the TeslaMate connection enabled, this integration uses data from TeslaMate in addition to the existing methods of using the Tesla API directly. It does not remove or create any entities within this Tesla intergration. It also continues to use the polling rate set in the configuration (defaults to 660 seconds).

When I first read your message, I thought you had manually set up entities in Home Assistant via the MQTT integration to capture the messages from TeslaMate manually (which meant you would now have some duplicate entries - some from the Tesla integration, some from the MQTT integration).

Does that clear everything up?

@rubin110
Copy link

@Megabytemb yes 100% thank you.

One thing I have been noticing and maybe it's more of a Teslamate issues, if the car has been asleep for a bit a lot of the sensor turn unavailable.

@Megabytemb
Copy link
Contributor Author

Yeah, i noticed the same.

That's not a teslamate issue, but overall how the integration handles data.

There is a "force data update" button you can press (or use an automation to press) if you want to refresh the data from Tesla.
There's also a "last data update" sensor, that could be used in automations too.

@rubin110
Copy link

Is there a reason why the integration doesn't just hold onto the stale data until the next refresh, instead of going to unavailable?

@drjjr2
Copy link

drjjr2 commented May 15, 2023

Is there a reason why the integration doesn't just hold onto the stale data until the next refresh, instead of going to unavailable?

I agree. This would be a preferred behavior.

@alandtse
Copy link
Owner

It's a HA spec to report unavailability if a data refresh fails. Absent a change in HA, we're not going to change that behavior.

@rubin110
Copy link

rubin110 commented May 15, 2023 via email

@alandtse
Copy link
Owner

Yes, if the car is asleep, it should just preserve the last data point we had. Unavailable should only appear if we tried to do a request to the car and it failed to respond to us. Please note there is a transient 408 error where the car just refuses to respond even if it's connected. We schedule 5 retries but typically the car won't respond to the retries while in this state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants