Skip to content

Commit

Permalink
bump pyfuelprices
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pantherale0 authored Nov 26, 2024
1 parent 98be68c commit b23c306
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion custom_components/fuel_prices/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 18 additions & 7 deletions custom_components/fuel_prices/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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."""
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
pyfuelprices==2024.11.6

0 comments on commit b23c306

Please sign in to comment.