Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set proper sensor device class for swiss_public_transport #106485

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions homeassistant/components/swiss_public_transport/coordinator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""DataUpdateCoordinator for the swiss_public_transport integration."""
from __future__ import annotations

from datetime import timedelta
from datetime import datetime, timedelta
import logging
from typing import TypedDict

Expand All @@ -21,9 +21,9 @@
class DataConnection(TypedDict):
"""A connection data class."""

departure: str
next_departure: str
next_on_departure: str
departure: datetime | None
next_departure: str | None
next_on_departure: str | None
duration: str
platform: str
remaining_time: str
Expand Down Expand Up @@ -58,18 +58,35 @@ async def _async_update_data(self) -> DataConnection:
)
raise UpdateFailed from e

departure_time = dt_util.parse_datetime(
self._opendata.connections[0]["departure"]
departure_time = (
dt_util.parse_datetime(self._opendata.connections[0]["departure"])
if self._opendata.connections[0] is not None
else None
)
next_departure_time = (
dt_util.parse_datetime(self._opendata.connections[1]["departure"])
if self._opendata.connections[1] is not None
else None
)
next_on_departure_time = (
dt_util.parse_datetime(self._opendata.connections[2]["departure"])
if self._opendata.connections[2] is not None
else None
)

if departure_time:
remaining_time = departure_time - dt_util.as_local(dt_util.utcnow())
else:
remaining_time = None

return DataConnection(
departure=self._opendata.connections[0]["departure"],
next_departure=self._opendata.connections[1]["departure"],
next_on_departure=self._opendata.connections[2]["departure"],
departure=departure_time,
next_departure=next_departure_time.isoformat()
if next_departure_time is not None
else None,
next_on_departure=next_on_departure_time.isoformat()
if next_on_departure_time is not None
else None,
train_number=self._opendata.connections[0]["number"],
platform=self._opendata.connections[0]["platform"],
transfers=self._opendata.connections[0]["transfers"],
Expand Down
11 changes: 8 additions & 3 deletions homeassistant/components/swiss_public_transport/sensor.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
"""Support for transport.opendata.ch."""
from __future__ import annotations

from datetime import timedelta
from datetime import datetime, timedelta
import logging
from typing import TYPE_CHECKING

import voluptuous as vol

from homeassistant import config_entries, core
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity,
)
from homeassistant.config_entries import SOURCE_IMPORT
from homeassistant.const import CONF_NAME
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant, callback
Expand Down Expand Up @@ -107,6 +111,7 @@ class SwissPublicTransportSensor(
_attr_icon = "mdi:bus"
_attr_has_entity_name = True
_attr_translation_key = "departure"
_attr_device_class = SensorDeviceClass.TIMESTAMP

def __init__(
self,
Expand All @@ -133,6 +138,6 @@ def _handle_coordinator_update(self) -> None:
return super()._handle_coordinator_update()

@property
def native_value(self) -> str:
def native_value(self) -> datetime | None:
"""Return the state of the sensor."""
return self.coordinator.data["departure"]