Skip to content

Commit

Permalink
fix: convert canadian prices from cents per liter to dollars per liter (
Browse files Browse the repository at this point in the history
#78)

* fix: convert canadian prices from cents to dollars per liter

* formatting
  • Loading branch information
firstof9 authored Sep 17, 2024
1 parent 0cad9d4 commit ee24801
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
10 changes: 8 additions & 2 deletions custom_components/gasbuddy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
19 changes: 19 additions & 0 deletions tests/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}
35 changes: 35 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit ee24801

Please sign in to comment.