Skip to content

Commit

Permalink
Make attributes' departure time timezone-aware (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
hkapanen authored Jan 5, 2025
1 parent 9fdf5b8 commit e0d39a4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions custom_components/digitransit/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.const import UnitOfTime

from zoneinfo import ZoneInfo
from .const import DOMAIN
from .coordinator import DigitransitDataUpdateCoordinator
from .entity import DigitransitEntity
Expand Down Expand Up @@ -64,12 +64,13 @@ def native_unit_of_measurement(self):
@property
def extra_state_attributes(self):
"""Attributes contain a list of the next few departures."""
timezone = ZoneInfo(self.hass.config.time_zone)
departure_list = (
self.coordinator.data.get("data")
.get("stop")
.get("stoptimesWithoutPatterns")
)
departure_list = list(map(formatDepartureRow, departure_list))
departure_list = [formatDepartureRow(row, timezone) for row in departure_list]
return {"departures": departure_list}

@property
Expand Down
8 changes: 4 additions & 4 deletions custom_components/digitransit/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Transform data for display."""
from datetime import datetime
from datetime import datetime, timedelta
import math

def formatDepartureRow(row):
def formatDepartureRow(row, timezone):
"""Simplify the departure information for use in the attribute."""
row['scheduledDeparture'] = datetime.fromtimestamp(row['serviceDay'] + row['scheduledDeparture'])
row['realtimeDeparture'] = datetime.fromtimestamp(row['serviceDay'] + row['realtimeDeparture'])
row['scheduledDeparture'] = datetime.fromtimestamp(row['serviceDay'], timezone) + timedelta(seconds=row['scheduledDeparture'])
row['realtimeDeparture'] = datetime.fromtimestamp(row['serviceDay'], timezone) + timedelta(seconds=row['realtimeDeparture'])
row['route'] = row.pop('trip')['routeShortName']
row.pop('serviceDay')
return row
Expand Down

0 comments on commit e0d39a4

Please sign in to comment.