Skip to content

Commit

Permalink
Fix the since days calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Aohzan committed Nov 24, 2023
1 parent 4e6027a commit 8995881
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 3.1.1

- Fix the since days calculation

## 3.1.0

- Add Auchan logo
Expand Down
18 changes: 12 additions & 6 deletions custom_components/prix_carburant/sensor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Prix Carburant sensor platform."""
from __future__ import annotations

from datetime import datetime
from datetime import UTC, datetime
import logging

import voluptuous as vol
Expand Down Expand Up @@ -127,7 +127,9 @@ def __init__(self, station_id, station_info, fuel, coordinator) -> None:
case "Carrefour Market":
self._attr_entity_picture = "https://upload.wikimedia.org/wikipedia/commons/4/4f/Carrefour_market_logo.svg"
case "Auchan":
self._attr_entity_picture = "https://upload.wikimedia.org/wikipedia/commons/4/4f/Auchan_A.svg"
self._attr_entity_picture = (
"https://upload.wikimedia.org/wikipedia/commons/4/4f/Auchan_A.svg"
)

self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self.station_id)},
Expand Down Expand Up @@ -159,14 +161,18 @@ def native_value(self) -> float | None:
ATTR_UPDATED_DATE
]
try:
delay = datetime.now() - datetime.strptime(
fuel[ATTR_UPDATED_DATE], "%Y-%m-%d %H:%M:%S"
delay = datetime.now(tz=UTC) - datetime.strptime(
fuel[ATTR_UPDATED_DATE], "%Y-%m-%dT%H:%M:%S%z"
)
self._attr_extra_state_attributes[
ATTR_DAYS_SINCE_LAST_UPDATE
] = delay.days
except ValueError:
_LOGGER.warning("Cannot calculate days between last update")
except ValueError as err:
_LOGGER.warning(
"Cannot calculate days for %s since last update: %s",
self._attr_name,
err,
)
# return price
return float(fuel[ATTR_PRICE])
return None

0 comments on commit 8995881

Please sign in to comment.