Skip to content

Commit

Permalink
Merge pull request #12 from warrior25/more-data-attributes
Browse files Browse the repository at this point in the history
More sensor attributes
  • Loading branch information
warrior25 authored Nov 24, 2024
2 parents 2dc6729 + 12bad1b commit 9418b98
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,33 @@ The integration can be set up from the frontend by searching for `Nysse`.

## Usage

Each station creates a sensor which contains data for departures from that station. Explanations for non self-explanatory attributes are listed below.
Each station creates a sensor which contains data for departures from that station. Explanations for attributes are listed below.

`realtime` - Indicates if the data is pulled from realtime vehicle monitoring or timetable data
### General

| Attribute | Description |
| ------------ | ----------------------------------------------------------------------------------- |
| last_refresh | Timestamp (ISO 8601 format) indicating when real-time departures were last fetched. |
| departures | A list of departure objects representing the next available departures. |
| station_name | Name of the monitored stop. |

### Departures

| Attribute | Description |
| --------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| destination | Name of the line's destination stop. |
| line | Reference identifier for the line, such as `4` or `36A`. |
| departure | Departure time in `%H:%M` (24-hour format), e.g., `16:09`. When _realtime_ is `false`, this value is taken directly from the timetable. When _realtime_ is `true`, it is calculated based on the real-time position of the vehicle. |
| time_to_station | Remaining time in **whole minutes** until the vehicle departs from the stop. Rounded down (e.g., `1 min 0 sec` to `1 min 59 sec` displays as `1`). When _realtime_ is `false`, this value is taken directly from the timetable. When _realtime_ is `true`, it is calculated based on the real-time position of the vehicle. |
| icon | Icon representing the vehicle operating the line. Possible values are `mdi:tram` or `mdi:bus`. |
| realtime | Boolean (`true` or `false`) indicating whether the data is based on real-time vehicle position or a static timetable. Real-time vehicle data is used whenever available. |

### Realtime departure specific

| Attribute | Description |
| --------------- | ------------------------------------------------------------------------------------------------------------------------ |
| aimed_departure | Scheduled departure time according to the timetable, in `%H:%M` (24-hour format), e.g., `16:09`. |
| delay | Number of seconds the vehicle is ahead of or behind schedule. Negative values indicate the vehicle is ahead of schedule. |

## Frontend examples

Expand Down
2 changes: 1 addition & 1 deletion custom_components/nysse/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"issue_tracker": "https://github.com/warrior25/HA-Nysse/issues",
"requirements": [],
"ssdp": [],
"version": "1.2.3",
"version": "1.3.0",
"zeroconf": []
}
20 changes: 20 additions & 0 deletions custom_components/nysse/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging

from dateutil import parser
import isodate

from homeassistant import config_entries, core
from homeassistant.components.sensor import SensorEntity
Expand Down Expand Up @@ -148,6 +149,7 @@ def _format_departures(self, departures):
),
"departure_time": departure["call"]["expectedDepartureTime"],
"aimed_departure_time": departure["call"]["aimedDepartureTime"],
"delay": departure["delay"],
"realtime": True,
}
if (
Expand Down Expand Up @@ -233,6 +235,12 @@ def _data_to_display_format(self, data):
"icon": self._get_line_icon(item["route_id"]),
"realtime": item["realtime"] if "realtime" in item else False,
}
if "aimed_departure_time" in item:
departure["aimed_departure"] = parser.parse(
item["aimed_departure_time"]
).strftime("%H:%M")
if "delay" in item:
departure["delay"] = self._delay_to_display_format(item["delay"])
formatted_data.append(departure)
return sorted(formatted_data, key=lambda x: x["time_to_station"])
except (OSError, ValueError) as err:
Expand All @@ -259,6 +267,18 @@ def _time_to_station(self, item):
)
return 0

def _delay_to_display_format(self, item):
try:
delay = isodate.parse_duration(item)
return int(delay.total_seconds())
except (OSError, ValueError) as err:
_LOGGER.debug(
"%s: Failed to format delay: %s",
self._stop_code,
err,
)
return 0

def _get_stop_name(self, stop_id):
try:
return next(
Expand Down

0 comments on commit 9418b98

Please sign in to comment.