From 80e06bb8ebe4f4a01e5d8729c8e6c9fc23a498b9 Mon Sep 17 00:00:00 2001 From: Chris <1105672+firstof9@users.noreply.github.com> Date: Thu, 19 Dec 2024 13:35:17 -0700 Subject: [PATCH] feat: add option to disable latitude/longitude entity attributes (#122) --- custom_components/gasbuddy/__init__.py | 7 +++++++ custom_components/gasbuddy/config_flow.py | 8 +++++++- custom_components/gasbuddy/const.py | 3 ++- custom_components/gasbuddy/sensor.py | 6 ++++-- tests/const.py | 6 ++++++ tests/test_config_flow.py | 6 ++++++ 6 files changed, 32 insertions(+), 4 deletions(-) diff --git a/custom_components/gasbuddy/__init__.py b/custom_components/gasbuddy/__init__.py index f329512..8b406d4 100644 --- a/custom_components/gasbuddy/__init__.py +++ b/custom_components/gasbuddy/__init__.py @@ -18,6 +18,7 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from .const import ( + CONF_GPS, CONF_INTERVAL, CONF_STATION_ID, CONF_UOM, @@ -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) @@ -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 diff --git a/custom_components/gasbuddy/config_flow.py b/custom_components/gasbuddy/config_flow.py index f65e1fd..779e490 100644 --- a/custom_components/gasbuddy/config_flow.py +++ b/custom_components/gasbuddy/config_flow.py @@ -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, ) @@ -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, } ) @@ -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): @@ -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" @@ -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) @@ -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) diff --git a/custom_components/gasbuddy/const.py b/custom_components/gasbuddy/const.py index a5ab616..b0c15cc 100644 --- a/custom_components/gasbuddy/const.py +++ b/custom_components/gasbuddy/const.py @@ -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" diff --git a/custom_components/gasbuddy/sensor.py b/custom_components/gasbuddy/sensor.py index 0e739ed..63eafe1 100644 --- a/custom_components/gasbuddy/sensor.py +++ b/custom_components/gasbuddy/sensor.py @@ -12,6 +12,7 @@ from .const import ( ATTR_IMAGEURL, + CONF_GPS, CONF_NAME, CONF_STATION_ID, CONF_UOM, @@ -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 diff --git a/tests/const.py b/tests/const.py index 385ae4a..9914b1e 100644 --- a/tests/const.py +++ b/tests/const.py @@ -1,6 +1,7 @@ """Constants for tests.""" from custom_components.gasbuddy.const import ( + CONF_GPS, CONF_INTERVAL, CONF_NAME, CONF_STATION_ID, @@ -12,6 +13,7 @@ CONF_INTERVAL: 3600, CONF_STATION_ID: 208656, CONF_UOM: True, + CONF_GPS: True, } CONFIG_DATA_NO_UOM = { @@ -19,12 +21,14 @@ 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 = { @@ -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": { @@ -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": { diff --git a/tests/test_config_flow.py b/tests/test_config_flow.py index 7d633a0..4d6320e 100644 --- a/tests/test_config_flow.py +++ b/tests/test_config_flow.py @@ -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, @@ -37,6 +38,7 @@ "user", DEFAULT_NAME, { + CONF_GPS: True, CONF_NAME: DEFAULT_NAME, CONF_STATION_ID: "208656", CONF_INTERVAL: 3600, @@ -113,6 +115,7 @@ async def test_form_home( CONF_STATION_ID: "208656", CONF_INTERVAL: 3600, CONF_UOM: True, + CONF_GPS: True, }, ), ], @@ -191,6 +194,7 @@ async def test_form_postal( CONF_STATION_ID: "208656", CONF_INTERVAL: 3600, CONF_UOM: True, + CONF_GPS: True, }, ), ], @@ -254,6 +258,7 @@ async def test_form_manual( CONF_STATION_ID: "208656", CONF_INTERVAL: 3600, CONF_UOM: True, + CONF_GPS: True, }, ), ], @@ -317,6 +322,7 @@ async def test_form_home_no_stations( CONF_STATION_ID: "208656", CONF_INTERVAL: 3600, CONF_UOM: True, + CONF_GPS: True, }, ), ],