Skip to content

Commit

Permalink
feat: add toggle to unit of measure
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 committed Dec 13, 2023
1 parent 5a04aa0 commit 56c9878
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions custom_components/gasbuddy/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
CONF_NAME,
CONF_POSTAL,
CONF_STATION_ID,
CONF_UOM,
DEFAULT_NAME,
DOMAIN,
)
Expand Down Expand Up @@ -158,6 +159,7 @@ def _get_default(key: str, fallback_default: Any = None) -> Any | None:
return vol.Schema(
{
vol.Required(CONF_INTERVAL, default=_get_default(CONF_INTERVAL, 3600)): int,
vol.Optional(CONF_UOM, default=_get_default(CONF_UOM)): bool,
}
)

Expand Down Expand Up @@ -188,6 +190,7 @@ async def async_step_manual(self, user_input=None):
if user_input is not None:
user_input[CONF_NAME] = slugify(user_input[CONF_NAME].lower())
user_input[CONF_INTERVAL] = 3600
user_input[CONF_UOM] = True
validate = await validate_station(user_input[CONF_STATION_ID])
if not validate:
self._errors[CONF_STATION_ID] = "station_id"
Expand Down Expand Up @@ -227,6 +230,7 @@ async def async_step_home(self, user_input=None):
if user_input is not None:
user_input[CONF_NAME] = slugify(user_input[CONF_NAME].lower())
user_input[CONF_INTERVAL] = 3600
user_input[CONF_UOM] = True
self._data.update(user_input)
return self.async_create_entry(title=self._data[CONF_NAME], data=self._data)
return await self._show_config_home(user_input)
Expand Down Expand Up @@ -270,6 +274,7 @@ async def async_step_station_list(self, user_input=None):
if user_input is not None:
user_input[CONF_NAME] = slugify(user_input[CONF_NAME].lower())
user_input[CONF_INTERVAL] = 3600
user_input[CONF_UOM] = True
self._data.pop(CONF_POSTAL)
self._data.update(user_input)
return self.async_create_entry(title=self._data[CONF_NAME], data=self._data)
Expand Down
1 change: 1 addition & 0 deletions custom_components/gasbuddy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
CONF_INTERVAL = "interval"
CONF_NAME = "name"
CONF_POSTAL = "zipcode"
CONF_UOM = "uom"
DEFAULT_INTERVAL = 3600
DEFAULT_NAME = "Gas Station"

Expand Down
8 changes: 6 additions & 2 deletions custom_components/gasbuddy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .const import (
CONF_NAME,
CONF_STATION_ID,
CONF_UOM,
COORDINATOR,
DOMAIN,
SENSOR_TYPES,
Expand Down Expand Up @@ -90,8 +91,11 @@ def native_unit_of_measurement(self) -> Any:
"""Return the unit of measurement."""
uom = self.coordinator.data["unit_of_measure"]
currency = self.coordinator.data["currency"]
if uom is not None and currency is not None:
return f"{currency}/{UNIT_OF_MEASURE[uom]}"
if self._config.data[CONF_UOM]:
if uom is not None and currency is not None:
return f"{currency}/{UNIT_OF_MEASURE[uom]}"
elif currency is not None:
return currency
return None

@property
Expand Down
3 changes: 2 additions & 1 deletion custom_components/gasbuddy/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"step": {
"init": {
"data": {
"interval": "Polling interval"
"interval": "Polling interval",
"uom": "Show per liter/gallon in unit of measure"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/const.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""Constants for tests."""

from custom_components.gasbuddy.const import CONF_INTERVAL, CONF_NAME, CONF_STATION_ID
from custom_components.gasbuddy.const import CONF_INTERVAL, CONF_NAME, CONF_STATION_ID, CONF_UOM

CONFIG_DATA = {
CONF_NAME: "Gas Station",
CONF_INTERVAL: 3600,
CONF_STATION_ID: 208656,
CONF_UOM: True,
}

STATION_LIST = {
Expand Down
4 changes: 4 additions & 0 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
CONF_NAME,
CONF_POSTAL,
CONF_STATION_ID,
CONF_UOM,
DEFAULT_NAME,
DOMAIN,
)
Expand All @@ -35,6 +36,7 @@
CONF_NAME: "gas_station",
CONF_STATION_ID: "208656",
CONF_INTERVAL: 3600,
CONF_UOM: True,
},
),
],
Expand Down Expand Up @@ -102,6 +104,7 @@ async def test_form_home(
CONF_NAME: "gas_station",
CONF_STATION_ID: "208656",
CONF_INTERVAL: 3600,
CONF_UOM: True,
},
),
],
Expand Down Expand Up @@ -175,6 +178,7 @@ async def test_form_postal(
CONF_NAME: "gas_station",
CONF_STATION_ID: "208656",
CONF_INTERVAL: 3600,
CONF_UOM: True,
},
),
],
Expand Down

0 comments on commit 56c9878

Please sign in to comment.