diff --git a/homeassistant/components/waze_travel_time/sensor.py b/homeassistant/components/waze_travel_time/sensor.py index 62e103f9c1d..3471099b588 100644 --- a/homeassistant/components/waze_travel_time/sensor.py +++ b/homeassistant/components/waze_travel_time/sensor.py @@ -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 diff --git a/tests/components/waze_travel_time/test_sensor.py b/tests/components/waze_travel_time/test_sensor.py index 0409b037394..2b28190e430 100644 --- a/tests/components/waze_travel_time/test_sensor.py +++ b/tests/components/waze_travel_time/test_sensor.py @@ -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."""