mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Removed rounding of durations in Here Travel Time sensors (#146838)
* Removed rounding of durations * Set duration sensors unit to seconds * Updated Here Travel Time tests * Update homeassistant/components/here_travel_time/sensor.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/here_travel_time/sensor.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Updated Here Travel Time tests --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
parent
c5d93e5456
commit
ad3dac0373
@ -133,8 +133,8 @@ class HERERoutingDataUpdateCoordinator(DataUpdateCoordinator[HERETravelTimeData]
|
|||||||
def _parse_routing_response(self, response: dict[str, Any]) -> HERETravelTimeData:
|
def _parse_routing_response(self, response: dict[str, Any]) -> HERETravelTimeData:
|
||||||
"""Parse the routing response dict to a HERETravelTimeData."""
|
"""Parse the routing response dict to a HERETravelTimeData."""
|
||||||
distance: float = 0.0
|
distance: float = 0.0
|
||||||
duration: float = 0.0
|
duration: int = 0
|
||||||
duration_in_traffic: float = 0.0
|
duration_in_traffic: int = 0
|
||||||
|
|
||||||
for section in response["routes"][0]["sections"]:
|
for section in response["routes"][0]["sections"]:
|
||||||
distance += DistanceConverter.convert(
|
distance += DistanceConverter.convert(
|
||||||
@ -167,8 +167,8 @@ class HERERoutingDataUpdateCoordinator(DataUpdateCoordinator[HERETravelTimeData]
|
|||||||
destination_name = names[0]["value"]
|
destination_name = names[0]["value"]
|
||||||
return HERETravelTimeData(
|
return HERETravelTimeData(
|
||||||
attribution=None,
|
attribution=None,
|
||||||
duration=round(duration / 60),
|
duration=duration,
|
||||||
duration_in_traffic=round(duration_in_traffic / 60),
|
duration_in_traffic=duration_in_traffic,
|
||||||
distance=distance,
|
distance=distance,
|
||||||
origin=f"{mapped_origin_lat},{mapped_origin_lon}",
|
origin=f"{mapped_origin_lat},{mapped_origin_lon}",
|
||||||
destination=f"{mapped_destination_lat},{mapped_destination_lon}",
|
destination=f"{mapped_destination_lat},{mapped_destination_lon}",
|
||||||
@ -271,13 +271,13 @@ class HERETransitDataUpdateCoordinator(
|
|||||||
UnitOfLength.METERS,
|
UnitOfLength.METERS,
|
||||||
UnitOfLength.KILOMETERS,
|
UnitOfLength.KILOMETERS,
|
||||||
)
|
)
|
||||||
duration: float = sum(
|
duration: int = sum(
|
||||||
section["travelSummary"]["duration"] for section in sections
|
section["travelSummary"]["duration"] for section in sections
|
||||||
)
|
)
|
||||||
return HERETravelTimeData(
|
return HERETravelTimeData(
|
||||||
attribution=attribution,
|
attribution=attribution,
|
||||||
duration=round(duration / 60),
|
duration=duration,
|
||||||
duration_in_traffic=round(duration / 60),
|
duration_in_traffic=duration,
|
||||||
distance=distance,
|
distance=distance,
|
||||||
origin=f"{mapped_origin_lat},{mapped_origin_lon}",
|
origin=f"{mapped_origin_lat},{mapped_origin_lon}",
|
||||||
destination=f"{mapped_destination_lat},{mapped_destination_lon}",
|
destination=f"{mapped_destination_lat},{mapped_destination_lon}",
|
||||||
|
@ -56,7 +56,8 @@ def sensor_descriptions(travel_mode: str) -> tuple[SensorEntityDescription, ...]
|
|||||||
key=ATTR_DURATION,
|
key=ATTR_DURATION,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
device_class=SensorDeviceClass.DURATION,
|
device_class=SensorDeviceClass.DURATION,
|
||||||
native_unit_of_measurement=UnitOfTime.MINUTES,
|
native_unit_of_measurement=UnitOfTime.SECONDS,
|
||||||
|
suggested_unit_of_measurement=UnitOfTime.MINUTES,
|
||||||
),
|
),
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
translation_key="duration_in_traffic",
|
translation_key="duration_in_traffic",
|
||||||
@ -64,7 +65,8 @@ def sensor_descriptions(travel_mode: str) -> tuple[SensorEntityDescription, ...]
|
|||||||
key=ATTR_DURATION_IN_TRAFFIC,
|
key=ATTR_DURATION_IN_TRAFFIC,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
device_class=SensorDeviceClass.DURATION,
|
device_class=SensorDeviceClass.DURATION,
|
||||||
native_unit_of_measurement=UnitOfTime.MINUTES,
|
native_unit_of_measurement=UnitOfTime.SECONDS,
|
||||||
|
suggested_unit_of_measurement=UnitOfTime.MINUTES,
|
||||||
),
|
),
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
translation_key="distance",
|
translation_key="distance",
|
||||||
|
@ -150,10 +150,10 @@ async def test_sensor(
|
|||||||
duration = hass.states.get("sensor.test_duration")
|
duration = hass.states.get("sensor.test_duration")
|
||||||
assert duration.attributes.get("unit_of_measurement") == UnitOfTime.MINUTES
|
assert duration.attributes.get("unit_of_measurement") == UnitOfTime.MINUTES
|
||||||
assert duration.attributes.get(ATTR_ICON) == icon
|
assert duration.attributes.get(ATTR_ICON) == icon
|
||||||
assert duration.state == "26"
|
assert duration.state == "26.1833333333333"
|
||||||
|
|
||||||
assert float(hass.states.get("sensor.test_distance").state) == pytest.approx(13.682)
|
assert float(hass.states.get("sensor.test_distance").state) == pytest.approx(13.682)
|
||||||
assert hass.states.get("sensor.test_duration_in_traffic").state == "30"
|
assert hass.states.get("sensor.test_duration_in_traffic").state == "29.6"
|
||||||
assert hass.states.get("sensor.test_origin").state == "22nd St NW"
|
assert hass.states.get("sensor.test_origin").state == "22nd St NW"
|
||||||
assert (
|
assert (
|
||||||
hass.states.get("sensor.test_origin").attributes.get(ATTR_LATITUDE)
|
hass.states.get("sensor.test_origin").attributes.get(ATTR_LATITUDE)
|
||||||
@ -501,13 +501,13 @@ async def test_restore_state(hass: HomeAssistant) -> None:
|
|||||||
"1234",
|
"1234",
|
||||||
attributes={
|
attributes={
|
||||||
ATTR_LAST_RESET: last_reset,
|
ATTR_LAST_RESET: last_reset,
|
||||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTime.MINUTES,
|
ATTR_UNIT_OF_MEASUREMENT: UnitOfTime.SECONDS,
|
||||||
ATTR_STATE_CLASS: SensorStateClass.MEASUREMENT,
|
ATTR_STATE_CLASS: SensorStateClass.MEASUREMENT,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
{
|
{
|
||||||
"native_value": 1234,
|
"native_value": 1234,
|
||||||
"native_unit_of_measurement": UnitOfTime.MINUTES,
|
"native_unit_of_measurement": UnitOfTime.SECONDS,
|
||||||
"icon": "mdi:car",
|
"icon": "mdi:car",
|
||||||
"last_reset": last_reset,
|
"last_reset": last_reset,
|
||||||
},
|
},
|
||||||
@ -518,13 +518,13 @@ async def test_restore_state(hass: HomeAssistant) -> None:
|
|||||||
"5678",
|
"5678",
|
||||||
attributes={
|
attributes={
|
||||||
ATTR_LAST_RESET: last_reset,
|
ATTR_LAST_RESET: last_reset,
|
||||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTime.MINUTES,
|
ATTR_UNIT_OF_MEASUREMENT: UnitOfTime.SECONDS,
|
||||||
ATTR_STATE_CLASS: SensorStateClass.MEASUREMENT,
|
ATTR_STATE_CLASS: SensorStateClass.MEASUREMENT,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
{
|
{
|
||||||
"native_value": 5678,
|
"native_value": 5678,
|
||||||
"native_unit_of_measurement": UnitOfTime.MINUTES,
|
"native_unit_of_measurement": UnitOfTime.SECONDS,
|
||||||
"icon": "mdi:car",
|
"icon": "mdi:car",
|
||||||
"last_reset": last_reset,
|
"last_reset": last_reset,
|
||||||
},
|
},
|
||||||
@ -596,12 +596,12 @@ async def test_restore_state(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
# restore from cache
|
# restore from cache
|
||||||
state = hass.states.get("sensor.test_duration")
|
state = hass.states.get("sensor.test_duration")
|
||||||
assert state.state == "1234"
|
assert state.state == "20.5666666666667"
|
||||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfTime.MINUTES
|
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfTime.MINUTES
|
||||||
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT
|
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT
|
||||||
|
|
||||||
state = hass.states.get("sensor.test_duration_in_traffic")
|
state = hass.states.get("sensor.test_duration_in_traffic")
|
||||||
assert state.state == "5678"
|
assert state.state == "94.6333333333333"
|
||||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfTime.MINUTES
|
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfTime.MINUTES
|
||||||
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT
|
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT
|
||||||
|
|
||||||
@ -799,10 +799,12 @@ async def test_multiple_sections(
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
duration = hass.states.get("sensor.test_duration")
|
duration = hass.states.get("sensor.test_duration")
|
||||||
assert duration.state == "18"
|
assert duration.state == "18.4833333333333"
|
||||||
|
|
||||||
assert float(hass.states.get("sensor.test_distance").state) == pytest.approx(3.583)
|
assert float(hass.states.get("sensor.test_distance").state) == pytest.approx(3.583)
|
||||||
assert hass.states.get("sensor.test_duration_in_traffic").state == "18"
|
assert (
|
||||||
|
hass.states.get("sensor.test_duration_in_traffic").state == "18.4833333333333"
|
||||||
|
)
|
||||||
assert hass.states.get("sensor.test_origin").state == "Chemin de Halage"
|
assert hass.states.get("sensor.test_origin").state == "Chemin de Halage"
|
||||||
assert (
|
assert (
|
||||||
hass.states.get("sensor.test_origin").attributes.get(ATTR_LATITUDE)
|
hass.states.get("sensor.test_origin").attributes.get(ATTR_LATITUDE)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user