Skip to content

Commit

Permalink
Add timeouthandler - partial messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneNulschDE committed Nov 28, 2024
1 parent d7f0c23 commit f8c778e
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions custom_components/mbapi2020/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

import asyncio
from datetime import datetime
from datetime import datetime, timedelta
import json
import logging
from pathlib import Path
Expand Down Expand Up @@ -64,7 +64,7 @@
LOGGER = logging.getLogger(__name__)

DEBUG_SIMULATE_PARTIAL_UPDATES_ONLY = False
GEOFENCING_MAX_RETRIES = 3
GEOFENCING_MAX_RETRIES = 2


class Client:
Expand Down Expand Up @@ -92,6 +92,9 @@ def __init__(
self._ws_connect_retry_counter: int = 0
self._ws_connect_retry_counter_reseted: bool = False
self._account_blocked: bool = False
self._first_vepupdates_processed: bool = False
self._vepupdates_timeout_reached: bool = False
self._vepupdates_timeout_seconds: int = 30

self.oauth: Oauth = Oauth(
hass=self._hass,
Expand Down Expand Up @@ -637,6 +640,10 @@ def _process_vep_updates(self, data):
vep_json = json.loads(MessageToJson(data, preserving_proto_field_name=True))
cars = vep_json["vepUpdates"]["updates"]

if not self._first_vepupdates_processed:
self._vepupdates_time_first_message = datetime.now()
self._first_vepupdates_processed = True

for vin in cars:
if vin in self.excluded_cars:
continue
Expand Down Expand Up @@ -688,6 +695,15 @@ def _process_vep_updates(self, data):
self._hass.async_create_task(self._on_dataload_complete())
self._dataload_complete_fired = True

if not self._dataload_complete_fired and (datetime.now() - self._vepupdates_time_first_message) > timedelta(
seconds=self._vepupdates_timeout_seconds
):
LOGGER.debug(
"_process_vep_updates - not all data received, timeout reached - fire event: _on_dataload_complete"
)
self._hass.async_create_task(self._on_dataload_complete())
self._dataload_complete_fired = True

def _process_assigned_vehicles(self, data):
if not self._dataload_complete_fired:
LOGGER.debug("Start _process_assigned_vehicles")
Expand Down

0 comments on commit f8c778e

Please sign in to comment.