Skip to content

Commit

Permalink
feat: add option to disable latitude/longitude entity attributes (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 authored Dec 19, 2024
1 parent 6eb2fb2 commit 80e06bb
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
7 changes: 7 additions & 0 deletions custom_components/gasbuddy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import (
CONF_GPS,
CONF_INTERVAL,
CONF_STATION_ID,
CONF_UOM,
Expand Down Expand Up @@ -53,6 +54,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
updated_config = config_entry.data.copy()
if CONF_UOM not in config_entry.data.keys():
updated_config[CONF_UOM] = True
if CONF_GPS not in config_entry.data.keys():
updated_config[CONF_GPS] = True

if updated_config != config_entry.data:
hass.config_entries.async_update_entry(config_entry, data=updated_config)
Expand Down Expand Up @@ -107,6 +110,10 @@ async def async_migrate_entry(hass, config_entry) -> bool:
if CONF_UOM not in updated_config.keys():
updated_config[CONF_UOM] = True

if version < 5:
if CONF_GPS not in updated_config.keys():
updated_config[CONF_GPS] = True

if updated_config != config_entry.data:
hass.config_entries.async_update_entry(
config_entry, data=updated_config, version=new_version
Expand Down
8 changes: 7 additions & 1 deletion custom_components/gasbuddy/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
from homeassistant.data_entry_flow import FlowResult

from .const import (
CONF_GPS,
CONF_INTERVAL,
CONF_NAME,
CONF_POSTAL,
CONF_STATION_ID,
CONF_UOM,
CONFIG_VER,
DEFAULT_NAME,
DOMAIN,
)
Expand Down Expand Up @@ -163,6 +165,7 @@ def _get_default(key: str, fallback_default: Any = None) -> Any | None:
{
vol.Required(CONF_INTERVAL, default=_get_default(CONF_INTERVAL, 3600)): int,
vol.Optional(CONF_UOM, default=_get_default(CONF_UOM)): bool,
vol.Optional(CONF_GPS, default=_get_default(CONF_GPS)): bool,
}
)

Expand All @@ -171,7 +174,7 @@ def _get_default(key: str, fallback_default: Any = None) -> Any | None:
class GasBuddyFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for GasBuddy."""

VERSION = 2
VERSION = CONFIG_VER
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL

def __init__(self):
Expand All @@ -193,6 +196,7 @@ async def async_step_manual(self, user_input=None):
if user_input is not None:
user_input[CONF_INTERVAL] = 3600
user_input[CONF_UOM] = True
user_input[CONF_GPS] = True
validate = await validate_station(user_input[CONF_STATION_ID])
if not validate:
self._errors[CONF_STATION_ID] = "station_id"
Expand Down Expand Up @@ -232,6 +236,7 @@ async def async_step_home(self, user_input=None):
if user_input is not None:
user_input[CONF_INTERVAL] = 3600
user_input[CONF_UOM] = True
user_input[CONF_GPS] = 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 @@ -278,6 +283,7 @@ async def async_step_station_list(self, user_input=None):
if user_input is not None:
user_input[CONF_INTERVAL] = 3600
user_input[CONF_UOM] = True
user_input[CONF_GPS] = 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
3 changes: 2 additions & 1 deletion custom_components/gasbuddy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
CONF_NAME = "name"
CONF_POSTAL = "zipcode"
CONF_UOM = "uom"
CONF_GPS = "gps"
DEFAULT_INTERVAL = 3600
DEFAULT_NAME = "Gas Station"
CONFIG_VER = 4
CONFIG_VER = 5

# hass.data attribues
ATTR_IMAGEURL = "image_url"
Expand Down
6 changes: 4 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 (
ATTR_IMAGEURL,
CONF_GPS,
CONF_NAME,
CONF_STATION_ID,
CONF_UOM,
Expand Down Expand Up @@ -119,8 +120,9 @@ def extra_state_attributes(self) -> Optional[dict]:
attrs[ATTR_ATTRIBUTION] = f"{credit} via GasBuddy"
attrs["last_updated"] = self.coordinator.data[self._type]["last_updated"]
attrs[CONF_STATION_ID] = self.coordinator.data[CONF_STATION_ID]
attrs[ATTR_LATITUDE] = self.coordinator.data[ATTR_LATITUDE]
attrs[ATTR_LONGITUDE] = self.coordinator.data[ATTR_LONGITUDE]
if self._config.data[CONF_GPS]:
attrs[ATTR_LATITUDE] = self.coordinator.data[ATTR_LATITUDE]
attrs[ATTR_LONGITUDE] = self.coordinator.data[ATTR_LONGITUDE]
return attrs

@property
Expand Down
6 changes: 6 additions & 0 deletions tests/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Constants for tests."""

from custom_components.gasbuddy.const import (
CONF_GPS,
CONF_INTERVAL,
CONF_NAME,
CONF_STATION_ID,
Expand All @@ -12,19 +13,22 @@
CONF_INTERVAL: 3600,
CONF_STATION_ID: 208656,
CONF_UOM: True,
CONF_GPS: True,
}

CONFIG_DATA_NO_UOM = {
CONF_NAME: "Gas Station",
CONF_INTERVAL: 3600,
CONF_STATION_ID: 208656,
CONF_UOM: False,
CONF_GPS: True,
}

CONFIG_DATA_V1 = {
CONF_NAME: "Gas Station",
CONF_INTERVAL: 3600,
CONF_STATION_ID: 208656,
CONF_GPS: True,
}

STATION_LIST = {
Expand Down Expand Up @@ -55,6 +59,7 @@
"image_url": "https://images.gasbuddy.io/b/122.png",
"unit_of_measure": "dollars_per_gallon",
"currency": "USD",
"gps": True,
"latitude": 33.459108,
"longitude": -112.502745,
"regular_gas": {
Expand Down Expand Up @@ -87,6 +92,7 @@
"image_url": None,
"unit_of_measure": "cents_per_liter",
"currency": "CAD",
"gps": True,
"latitude": 33.459108,
"longitude": -112.502745,
"regular_gas": {
Expand Down
6 changes: 6 additions & 0 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.gasbuddy.const import (
CONF_GPS,
CONF_INTERVAL,
CONF_NAME,
CONF_POSTAL,
Expand Down Expand Up @@ -37,6 +38,7 @@
"user",
DEFAULT_NAME,
{
CONF_GPS: True,
CONF_NAME: DEFAULT_NAME,
CONF_STATION_ID: "208656",
CONF_INTERVAL: 3600,
Expand Down Expand Up @@ -113,6 +115,7 @@ async def test_form_home(
CONF_STATION_ID: "208656",
CONF_INTERVAL: 3600,
CONF_UOM: True,
CONF_GPS: True,
},
),
],
Expand Down Expand Up @@ -191,6 +194,7 @@ async def test_form_postal(
CONF_STATION_ID: "208656",
CONF_INTERVAL: 3600,
CONF_UOM: True,
CONF_GPS: True,
},
),
],
Expand Down Expand Up @@ -254,6 +258,7 @@ async def test_form_manual(
CONF_STATION_ID: "208656",
CONF_INTERVAL: 3600,
CONF_UOM: True,
CONF_GPS: True,
},
),
],
Expand Down Expand Up @@ -317,6 +322,7 @@ async def test_form_home_no_stations(
CONF_STATION_ID: "208656",
CONF_INTERVAL: 3600,
CONF_UOM: True,
CONF_GPS: True,
},
),
],
Expand Down

0 comments on commit 80e06bb

Please sign in to comment.