From bc7a61671c4ea74daa6c40b9b583d4cae6700a6a Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Wed, 7 Oct 2026 04:23:53 +0200 Subject: [PATCH] Set up Tractive trackers that have no position yet (#184567) --- homeassistant/components/tractive/__init__.py | 2 +- .../components/tractive/device_tracker.py | 10 +++-- .../tractive/test_device_tracker.py | 38 ++++++++++++++++++- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/tractive/__init__.py b/homeassistant/components/tractive/__init__.py index b0c65eea9185..63594c86ed64 100644 --- a/homeassistant/components/tractive/__init__.py +++ b/homeassistant/components/tractive/__init__.py @@ -61,7 +61,7 @@ class Trackables: trackable: dict[str, Any] tracker_details: dict[str, Any] hw_info: dict[str, Any] - pos_report: dict[str, Any] + pos_report: dict[str, Any] | None health_overview: dict[str, Any] diff --git a/homeassistant/components/tractive/device_tracker.py b/homeassistant/components/tractive/device_tracker.py index 0b3880152e96..236947e7f040 100644 --- a/homeassistant/components/tractive/device_tracker.py +++ b/homeassistant/components/tractive/device_tracker.py @@ -49,10 +49,12 @@ class TractiveDeviceTracker(TractiveEntity, TrackerEntity): f"{TRACKER_POSITION_UPDATED}-{item.tracker_details['_id']}", ) - self._attr_latitude = item.pos_report["latlong"][0] - self._attr_longitude = item.pos_report["latlong"][1] - self._attr_location_accuracy: float = item.pos_report["pos_uncertainty"] - self._source_type: str = item.pos_report["sensor_used"] + # A tracker that has been switched off for a while has no position + pos_report = item.pos_report or {} + if latlong := pos_report.get("latlong"): + self._attr_latitude, self._attr_longitude = latlong + self._attr_location_accuracy: float = pos_report.get("pos_uncertainty") or 0 + self._source_type: str | None = pos_report.get("sensor_used") self._attr_unique_id = item.trackable["_id"] @property diff --git a/tests/components/tractive/test_device_tracker.py b/tests/components/tractive/test_device_tracker.py index 8e3d818f5f17..9d22c0f24854 100644 --- a/tests/components/tractive/test_device_tracker.py +++ b/tests/components/tractive/test_device_tracker.py @@ -2,11 +2,12 @@ from unittest.mock import AsyncMock, patch +import pytest from syrupy.assertion import SnapshotAssertion from homeassistant.components.device_tracker import SourceType from homeassistant.components.tractive.const import DOMAIN -from homeassistant.const import Platform +from homeassistant.const import STATE_UNKNOWN, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er @@ -115,3 +116,38 @@ async def test_device_tracker_device_assignment( entry = entity_registry.async_get("device_tracker.tracker_device_id_123") assert entry is not None assert entry.device_id == tracker_device.id + + +@pytest.mark.parametrize( + "pos_report", + [ + pytest.param(None, id="none"), + pytest.param({}, id="empty"), + pytest.param( + {"latlong": None, "pos_uncertainty": None, "sensor_used": None}, + id="no_location", + ), + ], +) +async def test_device_tracker_without_position( + hass: HomeAssistant, + mock_tractive_client: AsyncMock, + mock_config_entry: MockConfigEntry, + pos_report: dict[str, None] | None, +) -> None: + """Test a tracker without a position, like a switched off one, is set up.""" + mock_tractive_client.tracker.return_value.pos_report.return_value = pos_report + with patch( + "homeassistant.components.tractive.PLATFORMS", [Platform.DEVICE_TRACKER] + ): + await init_integration(hass, mock_config_entry) + + state = hass.states.get("device_tracker.tracker_device_id_123") + assert state + assert state.state == STATE_UNKNOWN + + mock_tractive_client.send_position_event(mock_config_entry) + await hass.async_block_till_done() + + state = hass.states.get("device_tracker.tracker_device_id_123") + assert state.state != STATE_UNKNOWN