From ee248013a8e67a1e4d2b3e53dbd9fabc718fef3a Mon Sep 17 00:00:00 2001 From: Chris <1105672+firstof9@users.noreply.github.com> Date: Tue, 17 Sep 2024 08:16:23 -0700 Subject: [PATCH] fix: convert canadian prices from cents per liter to dollars per liter (#78) * fix: convert canadian prices from cents to dollars per liter * formatting --- custom_components/gasbuddy/sensor.py | 10 ++++++-- tests/conftest.py | 12 +++++++++- tests/const.py | 19 +++++++++++++++ tests/test_sensor.py | 35 ++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 3 deletions(-) diff --git a/custom_components/gasbuddy/sensor.py b/custom_components/gasbuddy/sensor.py index 18ddf7c..f346657 100644 --- a/custom_components/gasbuddy/sensor.py +++ b/custom_components/gasbuddy/sensor.py @@ -79,9 +79,15 @@ def native_value(self) -> Any: self._state = None if self._type in data.keys(): if self._cash and "cash_price" in data[self._type]: - self._state = data[self._type]["cash_price"] + if self.coordinator.data["unit_of_measure"] == "cents_per_liter": + self._state = data[self._type]["cash_price"] / 100 + else: + self._state = data[self._type]["cash_price"] else: - self._state = data[self._type]["price"] + if self.coordinator.data["unit_of_measure"] == "cents_per_liter": + self._state = data[self._type]["price"] / 100 + else: + self._state = data[self._type]["price"] _LOGGER.debug("Sensor [%s] updated value: %s", self._type, self._state) return self._state diff --git a/tests/conftest.py b/tests/conftest.py index 59a1d73..11fa7a9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,7 +4,7 @@ import pytest -from tests.const import COORDINATOR_DATA +from tests.const import COORDINATOR_DATA, COORDINATOR_DATA_CAD # This fixture enables loading custom integrations in all tests. @@ -35,3 +35,13 @@ def mock_gasbuddy(): ) as mock_value: mock_value.return_value = COORDINATOR_DATA yield + + +@pytest.fixture() +def mock_gasbuddy_cad(): + """Mock charger data.""" + with patch( + "custom_components.gasbuddy.GasBuddyUpdateCoordinator._async_update_data" + ) as mock_value: + mock_value.return_value = COORDINATOR_DATA_CAD + yield diff --git a/tests/const.py b/tests/const.py index bb24e22..c8b7c3f 100644 --- a/tests/const.py +++ b/tests/const.py @@ -68,3 +68,22 @@ "last_updated": "2023-12-10T17:31:01.856Z", }, } + +COORDINATOR_DATA_CAD = { + "station_id": "208656", + "unit_of_measure": "cents_per_liter", + "currency": "CAD", + "latitude": 33.459108, + "longitude": -112.502745, + "regular_gas": { + "credit": "Buddy_5bbkqrb1", + "price": 143.9, + "last_updated": "2023-12-10T17:48:46.584Z", + }, + "premium_gas": { + "credit": "Owner", + "price": 153.1, + "cash_price": 145.2, + "last_updated": "2023-12-10T17:31:01.856Z", + }, +} diff --git a/tests/test_sensor.py b/tests/test_sensor.py index 70ae364..0579319 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -77,3 +77,38 @@ async def test_sensors_no_uom(hass, mock_gasbuddy): state = hass.states.get("sensor.gas_station_premium_gas") assert state assert state.state == "3.45" + + +async def test_sensors_cad(hass, mock_gasbuddy_cad): + """Test setup_entry.""" + entry = MockConfigEntry( + domain=DOMAIN, + title="Gas Station", + data=CONFIG_DATA, + ) + + entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 8 + entries = hass.config_entries.async_entries(DOMAIN) + assert len(entries) == 1 + + assert DOMAIN in hass.config.components + + state = hass.states.get("sensor.gas_station_regular_gas") + assert state + assert state.state == "1.439" + assert state.attributes["unit_of_measurement"] == "CAD/liter" + assert state.attributes[ATTR_LATITUDE] == 33.459108 + assert state.attributes[ATTR_LONGITUDE] == -112.502745 + state = hass.states.get("sensor.gas_station_midgrade_gas") + assert state + assert state.state == "unavailable" + state = hass.states.get("sensor.gas_station_premium_gas") + assert state + assert state.state == "1.531" + state = hass.states.get("sensor.gas_station_premium_gas_cash") + assert state + assert state.state == "1.452"