Adjust distance unit check in google travel time (#80232)

* Cleanup unused option in google travel time

* Adjust tests

* Adjust to use local constant

* Tweak logic

* Reduce size of PR

* Add tests

* Use system compare

* Use is not ==

* Adjust to use local constant again
pull/80084/head
epenet 2022-10-14 12:13:47 +02:00 committed by GitHub
parent 849688f71f
commit 0b7c84edbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 17 deletions

View File

@ -1,6 +1,4 @@
"""Constants for Google Travel Time."""
from homeassistant.const import CONF_UNIT_SYSTEM_IMPERIAL, CONF_UNIT_SYSTEM_METRIC
DOMAIN = "google_travel_time"
ATTRIBUTION = "Powered by Google"
@ -84,4 +82,8 @@ TRANSIT_PREFS = ["less_walking", "fewer_transfers"]
TRANSPORT_TYPE = ["bus", "subway", "train", "tram", "rail"]
TRAVEL_MODE = ["driving", "walking", "bicycling", "transit"]
TRAVEL_MODEL = ["best_guess", "pessimistic", "optimistic"]
UNITS = [CONF_UNIT_SYSTEM_METRIC, CONF_UNIT_SYSTEM_IMPERIAL]
# googlemaps library uses "metric" or "imperial" terminology in distance_matrix
UNITS_METRIC = "metric"
UNITS_IMPERIAL = "imperial"
UNITS = [UNITS_METRIC, UNITS_IMPERIAL]

View File

@ -23,6 +23,7 @@ from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.location import find_coordinates
import homeassistant.util.dt as dt_util
from homeassistant.util.unit_system import IMPERIAL_SYSTEM
from .const import (
ATTRIBUTION,
@ -35,6 +36,8 @@ from .const import (
CONF_UNITS,
DEFAULT_NAME,
DOMAIN,
UNITS_IMPERIAL,
UNITS_METRIC,
)
_LOGGER = logging.getLogger(__name__)
@ -63,7 +66,9 @@ async def async_setup_entry(
options = new_data.pop(CONF_OPTIONS, {})
if CONF_UNITS not in options:
options[CONF_UNITS] = hass.config.units.name
options[CONF_UNITS] = UNITS_METRIC
if hass.config.units is IMPERIAL_SYSTEM:
options[CONF_UNITS] = UNITS_IMPERIAL
if CONF_TRAVEL_MODE in new_data:
wstr = (

View File

@ -19,13 +19,9 @@ from homeassistant.components.google_travel_time.const import (
DEFAULT_NAME,
DEPARTURE_TIME,
DOMAIN,
UNITS_IMPERIAL,
)
from homeassistant.const import (
CONF_API_KEY,
CONF_MODE,
CONF_NAME,
CONF_UNIT_SYSTEM_IMPERIAL,
)
from homeassistant.const import CONF_API_KEY, CONF_MODE, CONF_NAME
from .const import MOCK_CONFIG
@ -79,7 +75,7 @@ async def test_invalid_config_entry(hass):
{
CONF_MODE: "driving",
CONF_ARRIVAL_TIME: "test",
CONF_UNITS: CONF_UNIT_SYSTEM_IMPERIAL,
CONF_UNITS: UNITS_IMPERIAL,
},
)
],
@ -100,7 +96,7 @@ async def test_options_flow(hass, mock_config):
CONF_MODE: "driving",
CONF_LANGUAGE: "en",
CONF_AVOID: "tolls",
CONF_UNITS: CONF_UNIT_SYSTEM_IMPERIAL,
CONF_UNITS: UNITS_IMPERIAL,
CONF_TIME_TYPE: ARRIVAL_TIME,
CONF_TIME: "test",
CONF_TRAFFIC_MODEL: "best_guess",
@ -114,7 +110,7 @@ async def test_options_flow(hass, mock_config):
CONF_MODE: "driving",
CONF_LANGUAGE: "en",
CONF_AVOID: "tolls",
CONF_UNITS: CONF_UNIT_SYSTEM_IMPERIAL,
CONF_UNITS: UNITS_IMPERIAL,
CONF_ARRIVAL_TIME: "test",
CONF_TRAFFIC_MODEL: "best_guess",
CONF_TRANSIT_MODE: "train",
@ -125,7 +121,7 @@ async def test_options_flow(hass, mock_config):
CONF_MODE: "driving",
CONF_LANGUAGE: "en",
CONF_AVOID: "tolls",
CONF_UNITS: CONF_UNIT_SYSTEM_IMPERIAL,
CONF_UNITS: UNITS_IMPERIAL,
CONF_ARRIVAL_TIME: "test",
CONF_TRAFFIC_MODEL: "best_guess",
CONF_TRANSIT_MODE: "train",
@ -153,7 +149,7 @@ async def test_options_flow_departure_time(hass, mock_config):
CONF_MODE: "driving",
CONF_LANGUAGE: "en",
CONF_AVOID: "tolls",
CONF_UNITS: CONF_UNIT_SYSTEM_IMPERIAL,
CONF_UNITS: UNITS_IMPERIAL,
CONF_TIME_TYPE: DEPARTURE_TIME,
CONF_TIME: "test",
CONF_TRAFFIC_MODEL: "best_guess",
@ -167,7 +163,7 @@ async def test_options_flow_departure_time(hass, mock_config):
CONF_MODE: "driving",
CONF_LANGUAGE: "en",
CONF_AVOID: "tolls",
CONF_UNITS: CONF_UNIT_SYSTEM_IMPERIAL,
CONF_UNITS: UNITS_IMPERIAL,
CONF_DEPARTURE_TIME: "test",
CONF_TRAFFIC_MODEL: "best_guess",
CONF_TRANSIT_MODE: "train",
@ -178,7 +174,7 @@ async def test_options_flow_departure_time(hass, mock_config):
CONF_MODE: "driving",
CONF_LANGUAGE: "en",
CONF_AVOID: "tolls",
CONF_UNITS: CONF_UNIT_SYSTEM_IMPERIAL,
CONF_UNITS: UNITS_IMPERIAL,
CONF_DEPARTURE_TIME: "test",
CONF_TRAFFIC_MODEL: "best_guess",
CONF_TRANSIT_MODE: "train",

View File

@ -10,6 +10,9 @@ from homeassistant.components.google_travel_time.const import (
CONF_TRAVEL_MODE,
DOMAIN,
)
from homeassistant.const import CONF_UNIT_SYSTEM_IMPERIAL, CONF_UNIT_SYSTEM_METRIC
from homeassistant.core import HomeAssistant
from homeassistant.util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM, UnitSystem
from .const import MOCK_CONFIG
@ -218,3 +221,35 @@ async def test_sensor_deprecation_warning(hass, caplog):
"add mode to the options dictionary instead!"
)
assert wstr in caplog.text
@pytest.mark.parametrize(
"unit_system, expected_unit_option",
[
(METRIC_SYSTEM, CONF_UNIT_SYSTEM_METRIC),
(IMPERIAL_SYSTEM, CONF_UNIT_SYSTEM_IMPERIAL),
],
)
async def test_sensor_unit_system(
hass: HomeAssistant,
unit_system: UnitSystem,
expected_unit_option: str,
) -> None:
"""Test that sensor works."""
hass.config.units = unit_system
config_entry = MockConfigEntry(
domain=DOMAIN,
data=MOCK_CONFIG,
options={},
entry_id="test",
)
config_entry.add_to_hass(hass)
with patch("homeassistant.components.google_travel_time.sensor.Client"), patch(
"homeassistant.components.google_travel_time.sensor.distance_matrix"
) as distance_matrix_mock:
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
distance_matrix_mock.assert_called_once()
assert distance_matrix_mock.call_args.kwargs["units"] == expected_unit_option