1
mirror of https://github.com/home-assistant/core synced 2024-10-01 05:30:36 +02:00

Use length_util conversion (#67049)

This commit is contained in:
Kevin Stillhammer 2022-02-22 18:06:23 +01:00 committed by GitHub
parent d96c2df6a8
commit dbb8806b31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 5 deletions

View File

@ -14,6 +14,7 @@ from homeassistant.const import (
CONF_REGION,
CONF_UNIT_SYSTEM_IMPERIAL,
EVENT_HOMEASSISTANT_STARTED,
LENGTH_KILOMETERS,
TIME_MINUTES,
)
from homeassistant.core import CoreState, HomeAssistant
@ -21,6 +22,7 @@ from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.location import find_coordinates
from homeassistant.util.unit_system import IMPERIAL_SYSTEM
from .const import (
CONF_AVOID_FERRIES,
@ -237,7 +239,7 @@ class WazeTravelTimeData:
if units == CONF_UNIT_SYSTEM_IMPERIAL:
# Convert to miles.
self.distance = distance / 1.609
self.distance = IMPERIAL_SYSTEM.length(distance, LENGTH_KILOMETERS)
else:
self.distance = distance

View File

@ -3,7 +3,16 @@
from WazeRouteCalculator import WRCError
import pytest
from homeassistant.components.waze_travel_time.const import DOMAIN
from homeassistant.components.waze_travel_time.const import (
CONF_AVOID_FERRIES,
CONF_AVOID_SUBSCRIPTION_ROADS,
CONF_AVOID_TOLL_ROADS,
CONF_REALTIME,
CONF_UNITS,
CONF_VEHICLE_TYPE,
DOMAIN,
)
from homeassistant.const import CONF_UNIT_SYSTEM_IMPERIAL
from .const import MOCK_CONFIG
@ -11,11 +20,12 @@ from tests.common import MockConfigEntry
@pytest.fixture(name="mock_config")
async def mock_config_fixture(hass, data):
async def mock_config_fixture(hass, data, options):
"""Mock a Waze Travel Time config entry."""
config_entry = MockConfigEntry(
domain=DOMAIN,
data=data,
options=options,
entry_id="test",
)
config_entry.add_to_hass(hass)
@ -40,8 +50,8 @@ def mock_update_keyerror_fixture(mock_wrc):
@pytest.mark.parametrize(
"data",
[MOCK_CONFIG],
"data,options",
[(MOCK_CONFIG, {})],
)
@pytest.mark.usefixtures("mock_update", "mock_config")
async def test_sensor(hass):
@ -68,6 +78,28 @@ async def test_sensor(hass):
assert hass.states.get("sensor.waze_travel_time").attributes["icon"] == "mdi:car"
@pytest.mark.parametrize(
"data,options",
[
(
MOCK_CONFIG,
{
CONF_UNITS: CONF_UNIT_SYSTEM_IMPERIAL,
CONF_REALTIME: True,
CONF_VEHICLE_TYPE: "car",
CONF_AVOID_TOLL_ROADS: True,
CONF_AVOID_SUBSCRIPTION_ROADS: True,
CONF_AVOID_FERRIES: True,
},
)
],
)
@pytest.mark.usefixtures("mock_update", "mock_config")
async def test_imperial(hass):
"""Test that the imperial option works."""
assert hass.states.get("sensor.waze_travel_time").attributes["distance"] == 186.4113
@pytest.mark.usefixtures("mock_update_wrcerror")
async def test_sensor_failed_wrcerror(hass, caplog):
"""Test that sensor update fails with log message."""