skodaconnect / homeassistant-myskoda

Homeassistant integration for MySkoda.
89 stars 15 forks source link

update position whenever departure event is published to MQTT #289

Closed prvakt closed 1 week ago

prvakt commented 1 week ago

made some tests already and seems to work correctly just Skoda API doesn't provide GPS coordinates immediately after car is stopped..

Test 1: Started the car and moved a little Result: position was immediately (after debounce) changed to VEHICLE_IN_MOTION

Test 2: Stopped the car Result: there was departure event publish on MQTT, but position received from API was still VEHICLE_IN_MOTION.. by next periodical update, it was changed to GPS coordinates

WebSpider commented 1 week ago

How much time was there between stopping the car and updating to GPS coordinates for test 2?

prvakt commented 1 week ago

How much time was there between stopping the car and updating to GPS coordinates for test 2?

cca 30s after departure-status-changed came from Departure topic (because of debounce).. Can't leave the house at the moment so just started the car, moved 1 meter front, 1 meter back, stayed in the car and shut it off after a minute.. After another 1minute there was periodical update of all sensors where VEHICLE_IN_MOTION was still present.. and after another 5minutes I got GPS coordinates during periodical update of all sensors

Will check tomorrow how fast the coordinates are updated after driving (will configure polling to 1 minute). But I think this also can be the security aspect that they not provide GPS imediatelly after stopping, or maybe my GPS was not yet available because car was in motion state for only a minute

WebSpider commented 1 week ago

I will marge this as draft in the mean time, so we dont accidentally merge it

dvx76 commented 1 week ago

How much time was there between stopping the car and updating to GPS coordinates for test 2?

cca 30s after departure-status-changed came from Departure topic (because of debounce).

I guess we could delay by 30s (or maybe 60s) in this case?

LGTM otherwise. Nice work.

Is the MQTT event the same on departure and arrival? Do you have a dump of the events?

prvakt commented 1 week ago

How much time was there between stopping the car and updating to GPS coordinates for test 2?

cca 30s after departure-status-changed came from Departure topic (because of debounce).

I guess we could delay by 30s (or maybe 60s) in this case?

LGTM otherwise. Nice work.

Is the MQTT event the same on departure and arrival? Do you have a dump of the events?

yes its same topic (departure) and same name (departure-status-changed).. Started car and moved a little

2024-11-15 14:14:42.579 DEBUG (MainThread) [myskoda.mqtt] Received PUBLISH (d0, q0, r0, m0), 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/TMB00000000000000/service-event/departure', ...  (238 bytes)
2024-11-15 14:14:42.579 DEBUG (MainThread) [myskoda.mqtt] Message (service-event) received for TMB00000000000000 on topic departure: b'{"version":1,"traceId":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","timestamp":"2024-11-15T13:14:41Z","producer":"SKODA_MHUB","name":"departure-status-changed","data":{"userId":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","vin":"TMB00000000000000"}}'
2024-11-15 14:15:09.231 DEBUG (MainThread) [custom_components.myskoda.coordinator] Updating positions for TMB00000000000000
2024-11-15 14:15:12.233 DEBUG (MainThread) [myskoda.myskoda] Trace: GET https://mysmob.api.connect.skoda-auto.cz/api/v1/maps/positio - response: 200 (93 bytes) {"positions":[],"errors":[{"type":"VEHICLE_IN_MOTION","description":"Vehicle is in motion"}]}

After circa minute I have stopped the car

2024-11-15 14:15:44.957 DEBUG (MainThread) [myskoda.mqtt] Received PUBLISH (d0, q0, r0, m0), 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/TMB00000000000000/service-event/departure', ...  (238 bytes)
2024-11-15 14:15:44.957 DEBUG (MainThread) [myskoda.mqtt] Message (service-event) received for TMB00000000000000 on topic departure: b'{"version":1,"traceId":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","timestamp":"2024-11-15T13:15:43Z","producer":"SKODA_MHUB","name":"departure-status-changed","data":{"userId":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","vin":"TMB00000000000000"}}'
2024-11-15 14:16:14.957 DEBUG (MainThread) [custom_components.myskoda.coordinator] Updating positions for TMB00000000000000
2024-11-15 14:16:16.626 DEBUG (MainThread) [myskoda.myskoda] Trace: GET https://mysmob.api.connect.skoda-auto.cz/api/v1/maps/positio - response: 200 (93 bytes) {"positions":[],"errors":[{"type":"VEHICLE_IN_MOTION","description":"Vehicle is in motion"}]}

after 5 minutes I got GPS coordinates during periodical update

2024-11-15 14:21:29.120 DEBUG (MainThread) [myskoda.myskoda] Trace: GET https://mysmob.api.connect.skoda-auto.cz/api/v1/maps/positio - response: 200 (241 bytes) {"positions":[{"type":"VEHICLE","gpsCoordinates":{"latitude":xx.xxxxx,"longitude":xx.xxxxxx},"address":{"city":"xxxxxxx","country":"xxxxxxx","countryCode":"xxxxxxx","street":"xxxxxxx.","houseNumber":"xxxxxxx","zipCode":"xxxxxxx"}}],"errors":[]}

I will set polling to 1 minute tomorrow and check how fast I can get coordinates... maybe my "test ride" was too short for car to update GPS position within one minute. I have to go to the store tomorrow, so my drive will be longer than today 😄

prvakt commented 1 week ago

Bellow are my findings 1) When I went to the store, there was no departure event when I started the car and also no departure event when I stopped the car. I can just guess that car was not awake? 2) Seems like GPS is updated within 2 minutes after after car is stopped..

2024-11-16 08:17:21.525 GPS AVAILABLE
NO DEPARTURE EVENT RECEIVED (started car)
2024-11-16 08:18:24.260 VEHICLE_IN_MOTION 
2024-11-16 08:20:32.629 VEHICLE_IN_MOTION 
NO DEPARTURE EVENT RECEIVED (stopped car)
2024-11-16 08:21:35.706 VEHICLE_IN_MOTION 
2024-11-16 08:23:00.757 GPS AVAILABLE
2024-11-16 08:24:05.736 GPS AVAILABLE
2024-11-16 08:24:13.297 DEPARTURE EVENT (started car) 
2024-11-16 08:24:43.843 VEHICLE_IN_MOTION 
2024-11-16 08:25:45.159 VEHICLE_IN_MOTION
2024-11-16 08:26:50.940 VEHICLE_IN_MOTION
2024-11-16 08:27:30.895 DEPARTURE EVENT (stopped car)
2024-11-16 08:28:01.435 VEHICLE_IN_MOTION
2024-11-16 08:29:24.668 GPS AVAILABLE
dvx76 commented 1 week ago

So what do we do with this? I'm not bothered by the events not being consistent/reliable. If we do get an event it's still beneficial to do a refresh. If we don't, well then it's no worse then it is today.

But I'm not sure how to handle the significant delay between the departure event (on arrival) being receive and the new gps position being returned by the API.

WebSpider commented 1 week ago

Agreed with the former part. Events should be seen as additional, if we dont get it we fallback to polling.

But I'm not sure how to handle the significant delay between the departure event (on arrival) being receive and the new gps position being returned by the API.

All i can think of is add an async waitstate, so we get the new position more reliable. No idea how that works with locking in HA tho.

prvakt commented 1 week ago

As soon as I can log in to MySkoda again (response 429 🙈 ), I will test to wait more than 30seconds to get positions after departure event. Maybe I can find a value which will result in more stable result

prvakt commented 1 week ago

performed 2 tests today with waiting 60s + debounce for positions update after departure event and seems promising.. just want to test it a little more tomorrow

prvakt commented 1 week ago

today results => 5 out 5 departure events (stopped car) ended up with valid GPS coordinates after waiting 60s before requesting positions.. Only noticed that departure event is not published when car is not awake (mostly the first ride in the morning)