diff --git a/homeassistant/components/husqvarna_automower/__init__.py b/homeassistant/components/husqvarna_automower/__init__.py index 2145b3a134e..a6c9e46dbed 100644 --- a/homeassistant/components/husqvarna_automower/__init__.py +++ b/homeassistant/components/husqvarna_automower/__init__.py @@ -19,6 +19,7 @@ _LOGGER = logging.getLogger(__name__) PLATFORMS: list[Platform] = [ Platform.BINARY_SENSOR, + Platform.DEVICE_TRACKER, Platform.LAWN_MOWER, Platform.SENSOR, Platform.SWITCH, diff --git a/homeassistant/components/husqvarna_automower/device_tracker.py b/homeassistant/components/husqvarna_automower/device_tracker.py new file mode 100644 index 00000000000..a32fd8758bd --- /dev/null +++ b/homeassistant/components/husqvarna_automower/device_tracker.py @@ -0,0 +1,52 @@ +"""Creates the device tracker entity for the mower.""" + +from homeassistant.components.device_tracker import SourceType, TrackerEntity +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from .const import DOMAIN +from .coordinator import AutomowerDataUpdateCoordinator +from .entity import AutomowerBaseEntity + + +async def async_setup_entry( + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback +) -> None: + """Set up device tracker platform.""" + coordinator: AutomowerDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] + async_add_entities( + AutomowerDeviceTrackerEntity(mower_id, coordinator) + for mower_id in coordinator.data + if coordinator.data[mower_id].capabilities.position + ) + + +class AutomowerDeviceTrackerEntity(AutomowerBaseEntity, TrackerEntity): + """Defining the AutomowerDeviceTrackerEntity.""" + + _attr_name = None + + def __init__( + self, + mower_id: str, + coordinator: AutomowerDataUpdateCoordinator, + ) -> None: + """Initialize AutomowerDeviceTracker.""" + super().__init__(mower_id, coordinator) + self._attr_unique_id = mower_id + + @property + def source_type(self) -> SourceType: + """Return the source type of the device.""" + return SourceType.GPS + + @property + def latitude(self) -> float: + """Return latitude value of the device.""" + return self.mower_attributes.positions[0].latitude + + @property + def longitude(self) -> float: + """Return longitude value of the device.""" + return self.mower_attributes.positions[0].longitude diff --git a/tests/components/husqvarna_automower/snapshots/test_device_tracker.ambr b/tests/components/husqvarna_automower/snapshots/test_device_tracker.ambr new file mode 100644 index 00000000000..636b6599380 --- /dev/null +++ b/tests/components/husqvarna_automower/snapshots/test_device_tracker.ambr @@ -0,0 +1,50 @@ +# serializer version: 1 +# name: test_device_tracker_snapshot[device_tracker.test_mower_1-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'device_tracker', + 'entity_category': , + 'entity_id': 'device_tracker.test_mower_1', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': None, + 'platform': 'husqvarna_automower', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'c7233734-b219-4287-a173-08e3643f89f0', + 'unit_of_measurement': None, + }) +# --- +# name: test_device_tracker_snapshot[device_tracker.test_mower_1-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Test Mower 1', + 'gps_accuracy': 0, + 'latitude': 35.5402913, + 'longitude': -82.5527055, + 'source_type': , + }), + 'context': , + 'entity_id': 'device_tracker.test_mower_1', + 'last_changed': , + 'last_updated': , + 'state': 'not_home', + }) +# --- diff --git a/tests/components/husqvarna_automower/test_device_tracker.py b/tests/components/husqvarna_automower/test_device_tracker.py new file mode 100644 index 00000000000..d9cab0d5074 --- /dev/null +++ b/tests/components/husqvarna_automower/test_device_tracker.py @@ -0,0 +1,38 @@ +"""Tests for the device tracker platform.""" + +from unittest.mock import AsyncMock, patch + +from syrupy import SnapshotAssertion + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import setup_integration + +from tests.common import MockConfigEntry + + +async def test_device_tracker_snapshot( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + mock_automower_client: AsyncMock, + mock_config_entry: MockConfigEntry, + snapshot: SnapshotAssertion, +) -> None: + """Test device tracker with a snapshot.""" + with patch( + "homeassistant.components.husqvarna_automower.PLATFORMS", + [Platform.DEVICE_TRACKER], + ): + await setup_integration(hass, mock_config_entry) + entity_entries = er.async_entries_for_config_entry( + entity_registry, mock_config_entry.entry_id + ) + + assert entity_entries + for entity_entry in entity_entries: + assert hass.states.get(entity_entry.entity_id) == snapshot( + name=f"{entity_entry.entity_id}-state" + ) + assert entity_entry == snapshot(name=f"{entity_entry.entity_id}-entry")