mirror of
https://github.com/home-assistant/core.git
synced 2025-05-06 06:59:15 +00:00
Correct unit for here_travel_time distance sensor (#78303)
Signed-off-by: Kevin Stillhammer <kevin.stillhammer@gmail.com> Signed-off-by: Kevin Stillhammer <kevin.stillhammer@gmail.com>
This commit is contained in:
parent
38b087d220
commit
f59c8d985d
@ -23,6 +23,9 @@ from homeassistant.const import (
|
|||||||
CONF_MODE,
|
CONF_MODE,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
CONF_UNIT_SYSTEM,
|
CONF_UNIT_SYSTEM,
|
||||||
|
CONF_UNIT_SYSTEM_IMPERIAL,
|
||||||
|
LENGTH_KILOMETERS,
|
||||||
|
LENGTH_MILES,
|
||||||
TIME_MINUTES,
|
TIME_MINUTES,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
@ -139,12 +142,6 @@ def sensor_descriptions(travel_mode: str) -> tuple[SensorEntityDescription, ...]
|
|||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
native_unit_of_measurement=TIME_MINUTES,
|
native_unit_of_measurement=TIME_MINUTES,
|
||||||
),
|
),
|
||||||
SensorEntityDescription(
|
|
||||||
name="Distance",
|
|
||||||
icon=ICONS.get(travel_mode, ICON_CAR),
|
|
||||||
key=ATTR_DISTANCE,
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
|
||||||
),
|
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
name="Route",
|
name="Route",
|
||||||
icon="mdi:directions",
|
icon="mdi:directions",
|
||||||
@ -198,6 +195,7 @@ async def async_setup_entry(
|
|||||||
)
|
)
|
||||||
sensors.append(OriginSensor(entry_id, name, coordinator))
|
sensors.append(OriginSensor(entry_id, name, coordinator))
|
||||||
sensors.append(DestinationSensor(entry_id, name, coordinator))
|
sensors.append(DestinationSensor(entry_id, name, coordinator))
|
||||||
|
sensors.append(DistanceSensor(entry_id, name, coordinator))
|
||||||
async_add_entities(sensors)
|
async_add_entities(sensors)
|
||||||
|
|
||||||
|
|
||||||
@ -301,3 +299,29 @@ class DestinationSensor(HERETravelTimeSensor):
|
|||||||
ATTR_LONGITUDE: self.coordinator.data[ATTR_DESTINATION].split(",")[1],
|
ATTR_LONGITUDE: self.coordinator.data[ATTR_DESTINATION].split(",")[1],
|
||||||
}
|
}
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
class DistanceSensor(HERETravelTimeSensor):
|
||||||
|
"""Sensor holding information about the distance."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
unique_id_prefix: str,
|
||||||
|
name: str,
|
||||||
|
coordinator: HereTravelTimeDataUpdateCoordinator,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the sensor."""
|
||||||
|
sensor_description = SensorEntityDescription(
|
||||||
|
name="Distance",
|
||||||
|
icon=ICONS.get(coordinator.config.travel_mode, ICON_CAR),
|
||||||
|
key=ATTR_DISTANCE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
)
|
||||||
|
super().__init__(unique_id_prefix, name, sensor_description, coordinator)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_unit_of_measurement(self) -> str | None:
|
||||||
|
"""Return the unit of measurement of the sensor."""
|
||||||
|
if self.coordinator.config.units == CONF_UNIT_SYSTEM_IMPERIAL:
|
||||||
|
return LENGTH_MILES
|
||||||
|
return LENGTH_KILOMETERS
|
||||||
|
@ -37,10 +37,13 @@ from homeassistant.const import (
|
|||||||
ATTR_ICON,
|
ATTR_ICON,
|
||||||
ATTR_LATITUDE,
|
ATTR_LATITUDE,
|
||||||
ATTR_LONGITUDE,
|
ATTR_LONGITUDE,
|
||||||
|
ATTR_UNIT_OF_MEASUREMENT,
|
||||||
CONF_API_KEY,
|
CONF_API_KEY,
|
||||||
CONF_MODE,
|
CONF_MODE,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
EVENT_HOMEASSISTANT_START,
|
EVENT_HOMEASSISTANT_START,
|
||||||
|
LENGTH_KILOMETERS,
|
||||||
|
LENGTH_MILES,
|
||||||
TIME_MINUTES,
|
TIME_MINUTES,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
@ -58,7 +61,7 @@ from tests.common import MockConfigEntry
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"mode,icon,unit_system,arrival_time,departure_time,expected_duration,expected_distance,expected_duration_in_traffic",
|
"mode,icon,unit_system,arrival_time,departure_time,expected_duration,expected_distance,expected_duration_in_traffic,expected_distance_unit",
|
||||||
[
|
[
|
||||||
(
|
(
|
||||||
TRAVEL_MODE_CAR,
|
TRAVEL_MODE_CAR,
|
||||||
@ -69,6 +72,7 @@ from tests.common import MockConfigEntry
|
|||||||
"30",
|
"30",
|
||||||
23.903,
|
23.903,
|
||||||
"31",
|
"31",
|
||||||
|
LENGTH_KILOMETERS,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
TRAVEL_MODE_BICYCLE,
|
TRAVEL_MODE_BICYCLE,
|
||||||
@ -79,6 +83,7 @@ from tests.common import MockConfigEntry
|
|||||||
"30",
|
"30",
|
||||||
23.903,
|
23.903,
|
||||||
"30",
|
"30",
|
||||||
|
LENGTH_KILOMETERS,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
TRAVEL_MODE_PEDESTRIAN,
|
TRAVEL_MODE_PEDESTRIAN,
|
||||||
@ -89,6 +94,7 @@ from tests.common import MockConfigEntry
|
|||||||
"30",
|
"30",
|
||||||
14.85263,
|
14.85263,
|
||||||
"30",
|
"30",
|
||||||
|
LENGTH_MILES,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
TRAVEL_MODE_PUBLIC_TIME_TABLE,
|
TRAVEL_MODE_PUBLIC_TIME_TABLE,
|
||||||
@ -99,6 +105,7 @@ from tests.common import MockConfigEntry
|
|||||||
"30",
|
"30",
|
||||||
14.85263,
|
14.85263,
|
||||||
"30",
|
"30",
|
||||||
|
LENGTH_MILES,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
TRAVEL_MODE_TRUCK,
|
TRAVEL_MODE_TRUCK,
|
||||||
@ -109,6 +116,7 @@ from tests.common import MockConfigEntry
|
|||||||
"30",
|
"30",
|
||||||
23.903,
|
23.903,
|
||||||
"31",
|
"31",
|
||||||
|
LENGTH_KILOMETERS,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@ -123,6 +131,7 @@ async def test_sensor(
|
|||||||
expected_duration,
|
expected_duration,
|
||||||
expected_distance,
|
expected_distance,
|
||||||
expected_duration_in_traffic,
|
expected_duration_in_traffic,
|
||||||
|
expected_distance_unit,
|
||||||
):
|
):
|
||||||
"""Test that sensor works."""
|
"""Test that sensor works."""
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
@ -166,6 +175,10 @@ async def test_sensor(
|
|||||||
assert float(hass.states.get("sensor.test_distance").state) == pytest.approx(
|
assert float(hass.states.get("sensor.test_distance").state) == pytest.approx(
|
||||||
expected_distance
|
expected_distance
|
||||||
)
|
)
|
||||||
|
assert (
|
||||||
|
hass.states.get("sensor.test_distance").attributes.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||||
|
== expected_distance_unit
|
||||||
|
)
|
||||||
assert hass.states.get("sensor.test_route").state == (
|
assert hass.states.get("sensor.test_route").state == (
|
||||||
"US-29 - K St NW; US-29 - Whitehurst Fwy; "
|
"US-29 - K St NW; US-29 - Whitehurst Fwy; "
|
||||||
"I-495 N - Capital Beltway; MD-187 S - Old Georgetown Rd"
|
"I-495 N - Capital Beltway; MD-187 S - Old Georgetown Rd"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user