Skip to content

Commit

Permalink
fix #25
Browse files Browse the repository at this point in the history
  • Loading branch information
pantherale0 authored Nov 22, 2024
1 parent adb214a commit bb7c5ba
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
4 changes: 2 additions & 2 deletions custom_components/fuel_prices/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

from homeassistant.helpers.entity import Entity
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .coordinator import FuelPricesCoordinator
Expand Down Expand Up @@ -35,13 +36,12 @@ def unique_id(self) -> str | None:
return f"fuelprices_{self._fuel_station_id}_{self._entity_id}"


class CheapestFuelEntity(CoordinatorEntity):
class CheapestFuelEntity(Entity):
"""Represents a fuel."""

def __init__(
self, coordinator: FuelPricesCoordinator, count: str, area: str, fuel: str, coords: tuple, radius: float):
"""Initialize."""
super().__init__(coordinator)
self.coordinator: FuelPricesCoordinator = coordinator
self._count = count
self._area = area
Expand Down
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.2"
"pyfuelprices==2024.11.4"
],
"ssdp": [],
"version": "0.0.0",
Expand Down
25 changes: 17 additions & 8 deletions custom_components/fuel_prices/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL = timedelta(minutes=1)


async def async_setup_entry(
hass: HomeAssistant, entry: FuelPricesConfigEntry, async_add_entities: AddEntitiesCallback
Expand Down Expand Up @@ -119,6 +121,7 @@ def state_class(self) -> str:
class CheapestFuelSensor(CheapestFuelEntity, SensorEntity):
"""A entity that shows the cheapest fuel for an area."""

_attr_force_update = True
_attr_should_poll = True # we need to query the module for this data
_last_update = None
_next_update = datetime.now()
Expand All @@ -136,11 +139,11 @@ async def async_update(self) -> None:
radius=self._radius
)
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]
return True
self._cached_data = None
self._last_update = datetime.now()
self._next_update = datetime.now() + timedelta(minutes=5)

@property
def native_value(self) -> str | float:
Expand All @@ -154,6 +157,13 @@ def name(self) -> str:
"""Name of the entity."""
return f"{self._area} cheapest {self._fuel} {self._count}"

@property
def native_unit_of_measurement(self) -> str:
"""Return unit of measurement."""
if isinstance(self.native_value, float):
return self._cached_data["currency"]
return None

@property
def state_class(self) -> str:
"""Return state type."""
Expand All @@ -164,9 +174,8 @@ def state_class(self) -> str:
@property
def extra_state_attributes(self) -> Mapping[str, Any] | None:
"""Return extra state attributes."""
return {
"area": self._area,
**self._cached_data,
"last_updated": self._last_update,
"next_update": self._next_update
}
data = self._cached_data
data["area"] = self._area
data["last_updated"] = self._last_update
data["next_update"] = self._next_update
return data
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
colorlog==6.7.0
git+https://github.com/home-assistant/core@dev
# git+https://github.com/home-assistant/core@dev
homeassistant==2024.11.0
pip>=21.0,<23.2
ruff==0.0.292
pyfuelprices==2024.11.2
pyfuelprices==2024.11.4

0 comments on commit bb7c5ba

Please sign in to comment.