From b23c30630dc909f3a885c1d09117c1ca4530780d Mon Sep 17 00:00:00 2001 From: Jordan Harvey Date: Tue, 26 Nov 2024 11:53:52 +0000 Subject: [PATCH] bump pyfuelprices fix errors if fuel type for cheapest fuel sensor is invalid ensure device_class is only returned for valid monetary values ensure extra state attributes are returned even if cache is null --- custom_components/fuel_prices/manifest.json | 2 +- custom_components/fuel_prices/sensor.py | 25 +++++++++++++++------ requirements.txt | 2 +- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/custom_components/fuel_prices/manifest.json b/custom_components/fuel_prices/manifest.json index 7da2cca..75a2e67 100644 --- a/custom_components/fuel_prices/manifest.json +++ b/custom_components/fuel_prices/manifest.json @@ -13,7 +13,7 @@ "xmltodict", "brotli", "these-united-states==1.1.0.21", - "pyfuelprices==2024.11.5" + "pyfuelprices==2024.11.6" ], "ssdp": [], "version": "0.0.0", diff --git a/custom_components/fuel_prices/sensor.py b/custom_components/fuel_prices/sensor.py index 2ed0c93..eedbe2d 100644 --- a/custom_components/fuel_prices/sensor.py +++ b/custom_components/fuel_prices/sensor.py @@ -134,7 +134,6 @@ class CheapestFuelSensor(CheapestFuelEntity, SensorEntity): _last_update = None _next_update = datetime.now() _cached_data = None - _attr_device_class = SensorDeviceClass.MONETARY async def async_update(self) -> None: """Update device data.""" @@ -150,17 +149,27 @@ async def async_update(self) -> None: if len(data) >= (int(self._count)-1): self._last_update = datetime.now() self._next_update = datetime.now() + timedelta(minutes=5) - self._cached_data = data[int(self._count)-1] + if len(data) >= self._count: + self._cached_data = data[int(self._count)-1] + else: + self._cached_data = {} return True self._cached_data = None @property - def native_value(self) -> str | float: + def native_value(self) -> str | float | int: """Return state of entity.""" if self._cached_data is not None: - return self._cached_data["cost"] + return self._cached_data.get("cost", STATE_UNKNOWN) return STATE_UNKNOWN + @property + def device_class(self) -> SensorDeviceClass | None: + """Return device class.""" + if isinstance(self.native_value, float) or isinstance(self.native_value, int): + return SensorDeviceClass.MONETARY + return None + @property def name(self) -> str: """Name of the entity.""" @@ -169,21 +178,23 @@ def name(self) -> str: @property def native_unit_of_measurement(self) -> str: """Return unit of measurement.""" - if isinstance(self.native_value, float): + if isinstance(self.native_value, float) or isinstance(self.native_value, int): return self._cached_data["currency"] return None @property def state_class(self) -> str: """Return state type.""" - if isinstance(self.native_value, float): + if isinstance(self.native_value, float) or isinstance(self.native_value, int): return "total" return None @property def extra_state_attributes(self) -> Mapping[str, Any] | None: """Return extra state attributes.""" - data = self._cached_data + data = {} + if self._cached_data is not None: + data = self._cached_data data["area"] = self._area data["sensor_last_poll"] = self._last_update data["sensor_next_poll"] = self._next_update diff --git a/requirements.txt b/requirements.txt index fdf28eb..58a65ba 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,4 @@ colorlog==6.7.0 homeassistant==2024.11.0 pip>=21.0,<23.2 ruff==0.0.292 -pyfuelprices==2024.11.5 \ No newline at end of file +pyfuelprices==2024.11.6 \ No newline at end of file