From 189511724ac68f8a47a3c389f466897f5b4be18f Mon Sep 17 00:00:00 2001 From: Brandon Rothweiler Date: Sat, 17 Apr 2021 05:26:07 -0400 Subject: [PATCH] Add device tracker platform to Mazda integration (#47974) * Add device tracker platform for Mazda integration * Split device tests into a separate file * Address review comments --- homeassistant/components/mazda/__init__.py | 2 +- .../components/mazda/device_tracker.py | 58 +++++++++++++++++++ tests/components/mazda/test_device_tracker.py | 30 ++++++++++ tests/components/mazda/test_init.py | 29 ++++++++++ tests/components/mazda/test_sensor.py | 31 +--------- 5 files changed, 119 insertions(+), 31 deletions(-) create mode 100644 homeassistant/components/mazda/device_tracker.py create mode 100644 tests/components/mazda/test_device_tracker.py diff --git a/homeassistant/components/mazda/__init__.py b/homeassistant/components/mazda/__init__.py index 555cc9f3a00..a264ec24389 100644 --- a/homeassistant/components/mazda/__init__.py +++ b/homeassistant/components/mazda/__init__.py @@ -29,7 +29,7 @@ from .const import DATA_CLIENT, DATA_COORDINATOR, DOMAIN _LOGGER = logging.getLogger(__name__) -PLATFORMS = ["sensor"] +PLATFORMS = ["device_tracker", "sensor"] async def with_timeout(task, timeout_seconds=10): diff --git a/homeassistant/components/mazda/device_tracker.py b/homeassistant/components/mazda/device_tracker.py new file mode 100644 index 00000000000..ea05d2c8c8b --- /dev/null +++ b/homeassistant/components/mazda/device_tracker.py @@ -0,0 +1,58 @@ +"""Platform for Mazda device tracker integration.""" +from homeassistant.components.device_tracker import SOURCE_TYPE_GPS +from homeassistant.components.device_tracker.config_entry import TrackerEntity + +from . import MazdaEntity +from .const import DATA_COORDINATOR, DOMAIN + + +async def async_setup_entry(hass, config_entry, async_add_entities): + """Set up the device tracker platform.""" + coordinator = hass.data[DOMAIN][config_entry.entry_id][DATA_COORDINATOR] + + entities = [] + + for index, _ in enumerate(coordinator.data): + entities.append(MazdaDeviceTracker(coordinator, index)) + + async_add_entities(entities) + + +class MazdaDeviceTracker(MazdaEntity, TrackerEntity): + """Class for the device tracker.""" + + @property + def name(self): + """Return the name of the entity.""" + vehicle_name = self.get_vehicle_name() + return f"{vehicle_name} Device Tracker" + + @property + def unique_id(self): + """Return a unique identifier for this entity.""" + return self.vin + + @property + def icon(self): + """Return the icon to use in the frontend.""" + return "mdi:car" + + @property + def source_type(self): + """Return the source type, eg gps or router, of the device.""" + return SOURCE_TYPE_GPS + + @property + def force_update(self): + """All updates do not need to be written to the state machine.""" + return False + + @property + def latitude(self): + """Return latitude value of the device.""" + return self.coordinator.data[self.index]["status"]["latitude"] + + @property + def longitude(self): + """Return longitude value of the device.""" + return self.coordinator.data[self.index]["status"]["longitude"] diff --git a/tests/components/mazda/test_device_tracker.py b/tests/components/mazda/test_device_tracker.py new file mode 100644 index 00000000000..5e09c23ecd8 --- /dev/null +++ b/tests/components/mazda/test_device_tracker.py @@ -0,0 +1,30 @@ +"""The device tracker tests for the Mazda Connected Services integration.""" +from homeassistant.components.device_tracker import SOURCE_TYPE_GPS +from homeassistant.components.device_tracker.const import ATTR_SOURCE_TYPE +from homeassistant.const import ( + ATTR_FRIENDLY_NAME, + ATTR_ICON, + ATTR_LATITUDE, + ATTR_LONGITUDE, +) +from homeassistant.helpers import entity_registry as er + +from tests.components.mazda import init_integration + + +async def test_device_tracker(hass): + """Test creation of the device tracker.""" + await init_integration(hass) + + entity_registry = er.async_get(hass) + + state = hass.states.get("device_tracker.my_mazda3_device_tracker") + assert state + assert state.attributes.get(ATTR_FRIENDLY_NAME) == "My Mazda3 Device Tracker" + assert state.attributes.get(ATTR_ICON) == "mdi:car" + assert state.attributes.get(ATTR_LATITUDE) == 1.234567 + assert state.attributes.get(ATTR_LONGITUDE) == -2.345678 + assert state.attributes.get(ATTR_SOURCE_TYPE) == SOURCE_TYPE_GPS + entry = entity_registry.async_get("device_tracker.my_mazda3_device_tracker") + assert entry + assert entry.unique_id == "JM000000000000000" diff --git a/tests/components/mazda/test_init.py b/tests/components/mazda/test_init.py index ebd118260bc..fe5b96096f1 100644 --- a/tests/components/mazda/test_init.py +++ b/tests/components/mazda/test_init.py @@ -14,6 +14,7 @@ from homeassistant.config_entries import ( ) from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_REGION from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr from homeassistant.util import dt as dt_util from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture @@ -109,3 +110,31 @@ async def test_unload_config_entry(hass: HomeAssistant) -> None: await hass.config_entries.async_unload(entry.entry_id) await hass.async_block_till_done() assert entry.state == ENTRY_STATE_NOT_LOADED + + +async def test_device_nickname(hass): + """Test creation of the device when vehicle has a nickname.""" + await init_integration(hass, use_nickname=True) + + device_registry = dr.async_get(hass) + reg_device = device_registry.async_get_device( + identifiers={(DOMAIN, "JM000000000000000")}, + ) + + assert reg_device.model == "2021 MAZDA3 2.5 S SE AWD" + assert reg_device.manufacturer == "Mazda" + assert reg_device.name == "My Mazda3" + + +async def test_device_no_nickname(hass): + """Test creation of the device when vehicle has no nickname.""" + await init_integration(hass, use_nickname=False) + + device_registry = dr.async_get(hass) + reg_device = device_registry.async_get_device( + identifiers={(DOMAIN, "JM000000000000000")}, + ) + + assert reg_device.model == "2021 MAZDA3 2.5 S SE AWD" + assert reg_device.manufacturer == "Mazda" + assert reg_device.name == "2021 MAZDA3 2.5 S SE AWD" diff --git a/tests/components/mazda/test_sensor.py b/tests/components/mazda/test_sensor.py index d5f25bce2f3..179ad96d533 100644 --- a/tests/components/mazda/test_sensor.py +++ b/tests/components/mazda/test_sensor.py @@ -1,6 +1,5 @@ """The sensor tests for the Mazda Connected Services integration.""" -from homeassistant.components.mazda.const import DOMAIN from homeassistant.const import ( ATTR_FRIENDLY_NAME, ATTR_ICON, @@ -10,40 +9,12 @@ from homeassistant.const import ( PERCENTAGE, PRESSURE_PSI, ) -from homeassistant.helpers import device_registry as dr, entity_registry as er +from homeassistant.helpers import entity_registry as er from homeassistant.util.unit_system import IMPERIAL_SYSTEM from tests.components.mazda import init_integration -async def test_device_nickname(hass): - """Test creation of the device when vehicle has a nickname.""" - await init_integration(hass, use_nickname=True) - - device_registry = dr.async_get(hass) - reg_device = device_registry.async_get_device( - identifiers={(DOMAIN, "JM000000000000000")}, - ) - - assert reg_device.model == "2021 MAZDA3 2.5 S SE AWD" - assert reg_device.manufacturer == "Mazda" - assert reg_device.name == "My Mazda3" - - -async def test_device_no_nickname(hass): - """Test creation of the device when vehicle has no nickname.""" - await init_integration(hass, use_nickname=False) - - device_registry = dr.async_get(hass) - reg_device = device_registry.async_get_device( - identifiers={(DOMAIN, "JM000000000000000")}, - ) - - assert reg_device.model == "2021 MAZDA3 2.5 S SE AWD" - assert reg_device.manufacturer == "Mazda" - assert reg_device.name == "2021 MAZDA3 2.5 S SE AWD" - - async def test_sensors(hass): """Test creation of the sensors.""" await init_integration(hass)