From a6a0a0c9010d87b4fc3be1f388924e8ce688aba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Sun, 25 Feb 2024 16:04:09 +0100 Subject: [PATCH] Adjustment post move to WS in Traccar Server (#111337) * Adjustment post move to WS in Traccar Server * Use entry.async_create_background_task --- .../components/traccar_server/__init__.py | 6 +++ .../components/traccar_server/coordinator.py | 42 +++++-------------- .../components/traccar_server/entity.py | 5 +-- 3 files changed, 18 insertions(+), 35 deletions(-) diff --git a/homeassistant/components/traccar_server/__init__.py b/homeassistant/components/traccar_server/__init__.py index 5f37be16e04..dac54f5e3f8 100644 --- a/homeassistant/components/traccar_server/__init__.py +++ b/homeassistant/components/traccar_server/__init__.py @@ -75,6 +75,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: ) ) + entry.async_create_background_task( + hass=hass, + target=coordinator.subscribe(), + name="Traccar Server subscription", + ) + return True diff --git a/homeassistant/components/traccar_server/coordinator.py b/homeassistant/components/traccar_server/coordinator.py index 14889388d7e..960fdc01fa0 100644 --- a/homeassistant/components/traccar_server/coordinator.py +++ b/homeassistant/components/traccar_server/coordinator.py @@ -15,9 +15,8 @@ from pytraccar import ( ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import EVENT_HOMEASSISTANT_STOP from homeassistant.core import HomeAssistant -from homeassistant.helpers.dispatcher import dispatcher_send +from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.util import dt as dt_util @@ -66,7 +65,6 @@ class TraccarServerCoordinator(DataUpdateCoordinator[TraccarServerCoordinatorDat self.skip_accuracy_filter_for = skip_accuracy_filter_for self._geofences: list[GeofenceModel] = [] self._last_event_import: datetime | None = None - self._subscription: asyncio.Task | None = None self._should_log_subscription_error: bool = True async def _async_update_data(self) -> TraccarServerCoordinatorData: @@ -115,8 +113,6 @@ class TraccarServerCoordinator(DataUpdateCoordinator[TraccarServerCoordinatorDat "attributes": attr, } - await self.subscribe() - return data async def handle_subscription_data(self, data: SubscriptionData) -> None: @@ -163,7 +159,7 @@ class TraccarServerCoordinator(DataUpdateCoordinator[TraccarServerCoordinatorDat update_devices.add(device_id) for device_id in update_devices: - dispatcher_send(self.hass, f"{DOMAIN}_{device_id}") + async_dispatcher_send(self.hass, f"{DOMAIN}_{device_id}") async def import_events(self, _: datetime) -> None: """Import events from Traccar.""" @@ -203,33 +199,17 @@ class TraccarServerCoordinator(DataUpdateCoordinator[TraccarServerCoordinatorDat }, ) - async def unsubscribe(self, *args) -> None: - """Unsubscribe from Traccar Server.""" - if self._subscription is None: - return - self._should_log_subscription_error = False - self._subscription.cancel() - self._subscription = None - async def subscribe(self) -> None: """Subscribe to events.""" - if self._subscription is not None: - return - - async def _subscriber(): - try: - await self.client.subscribe(self.handle_subscription_data) - except TraccarException as ex: - if self._should_log_subscription_error: - self._should_log_subscription_error = False - LOGGER.error("Error while subscribing to Traccar: %s", ex) - # Retry after 10 seconds - await asyncio.sleep(10) - await _subscriber() - - self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self.unsubscribe) - self.config_entry.async_on_unload(self.unsubscribe) - self._subscription = asyncio.create_task(_subscriber()) + try: + await self.client.subscribe(self.handle_subscription_data) + except TraccarException as ex: + if self._should_log_subscription_error: + self._should_log_subscription_error = False + LOGGER.error("Error while subscribing to Traccar: %s", ex) + # Retry after 10 seconds + await asyncio.sleep(10) + await self.subscribe() def _return_custom_attributes_if_not_filtered_by_accuracy_configuration( self, diff --git a/homeassistant/components/traccar_server/entity.py b/homeassistant/components/traccar_server/entity.py index bb824cf77c8..1c32008d09b 100644 --- a/homeassistant/components/traccar_server/entity.py +++ b/homeassistant/components/traccar_server/entity.py @@ -34,10 +34,7 @@ class TraccarServerEntity(CoordinatorEntity[TraccarServerCoordinator]): @property def available(self) -> bool: """Return True if entity is available.""" - return ( - self.coordinator.last_update_success - and self.device_id in self.coordinator.data - ) + return bool(self.coordinator.data and self.device_id in self.coordinator.data) @property def traccar_device(self) -> DeviceModel: