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
This commit is contained in:
parent
3a0b0380c7
commit
189511724a
5 changed files with 119 additions and 31 deletions
|
@ -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):
|
||||
|
|
58
homeassistant/components/mazda/device_tracker.py
Normal file
58
homeassistant/components/mazda/device_tracker.py
Normal file
|
@ -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"]
|
30
tests/components/mazda/test_device_tracker.py
Normal file
30
tests/components/mazda/test_device_tracker.py
Normal file
|
@ -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"
|
|
@ -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"
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue