mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 04:08:19 -04:00
Set up Tractive trackers that have no position yet (#184567)
This commit is contained in:
@@ -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]
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user