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
577 stars 99 forks source link

Sync doors open/close from Teslamate MQTT #707

Open KrzysztofHajdamowicz opened 1 year ago

KrzysztofHajdamowicz commented 1 year ago

Is your feature request related to a problem? Please describe. User wants to automate something car-related, where automation will be triggered by opening and closing car doors. Currently, door status is polled from Tesla servers, instead of using Streaming API/Teslamate+MQTT.

Describe the solution you'd like sensor.doors entity updated from Teslamate MQTT.

Describe alternatives you've considered Current workaround is using MQTT triggers, but automation gets triggered every dozen of seconds as Teslamate periodically re-sends "doors closed" MQTT message.

trigger:
  - platform: mqtt
    topic: teslamate/cars/1/doors_open
    payload: "false"
    id: closed
  - platform: mqtt
    topic: teslamate/cars/1/doors_open
    payload: "true"
    id: open

Additional context Task is simmilar to #698 , just another entity needs to be synced.

KrzysztofHajdamowicz commented 12 months ago

I spend an hour digging through the code and Teslamate MQTT topics.

So far, I understand that Tesla API shows door status with granularity {d,p}{f,r)={true,false} and Teslamate publishes teslamate/cars/$TESLAMATE_ID/doors_open={true,false} MQTT message. That means, that from API you can get status of individual doors, while Teslamate exposes "are any doors open?" Next, in file binary_sensor.py#L169 there is casting of Tesla API granularity into generic {true/false}

Unfortunately, I have no idea how to merge teslamate.py: MAP_VEHICLE_STATE["doors_open"] with binary_sensor.py#L169

alandtse commented 12 months ago

Probably change it to store the value of the or statement and let teslamate set it true or false. The polling with granularity would then update when it has new info.