Add device_tracker platform for husqvarna_automower (#111403)
* Add device_tracker platform for husqvarna_automower * ruff * Add snapshot test * State test * Fix description * ruff * Optimize some docstrings * Update homeassistant/components/husqvarna_automower/device_tracker.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Adress review --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>pull/113817/head
parent
3a8494cb88
commit
ea443af557
|
@ -19,6 +19,7 @@ _LOGGER = logging.getLogger(__name__)
|
|||
|
||||
PLATFORMS: list[Platform] = [
|
||||
Platform.BINARY_SENSOR,
|
||||
Platform.DEVICE_TRACKER,
|
||||
Platform.LAWN_MOWER,
|
||||
Platform.SENSOR,
|
||||
Platform.SWITCH,
|
||||
|
|
|
@ -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
|
|
@ -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': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'device_tracker',
|
||||
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
|
||||
'entity_id': 'device_tracker.test_mower_1',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'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': <SourceType.GPS: 'gps'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'device_tracker.test_mower_1',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'not_home',
|
||||
})
|
||||
# ---
|
|
@ -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")
|
Loading…
Reference in New Issue