mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 08:47:10 +00:00
Set proper sensor device class for swiss_public_transport (#106485)
set proper sensor device class
This commit is contained in:
parent
15e3af72d1
commit
598e18ca86
@ -1,7 +1,7 @@
|
|||||||
"""DataUpdateCoordinator for the swiss_public_transport integration."""
|
"""DataUpdateCoordinator for the swiss_public_transport integration."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import datetime, timedelta
|
||||||
import logging
|
import logging
|
||||||
from typing import TypedDict
|
from typing import TypedDict
|
||||||
|
|
||||||
@ -21,9 +21,9 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
class DataConnection(TypedDict):
|
class DataConnection(TypedDict):
|
||||||
"""A connection data class."""
|
"""A connection data class."""
|
||||||
|
|
||||||
departure: str
|
departure: datetime | None
|
||||||
next_departure: str
|
next_departure: str | None
|
||||||
next_on_departure: str
|
next_on_departure: str | None
|
||||||
duration: str
|
duration: str
|
||||||
platform: str
|
platform: str
|
||||||
remaining_time: str
|
remaining_time: str
|
||||||
@ -58,18 +58,35 @@ class SwissPublicTransportDataUpdateCoordinator(DataUpdateCoordinator[DataConnec
|
|||||||
)
|
)
|
||||||
raise UpdateFailed from e
|
raise UpdateFailed from e
|
||||||
|
|
||||||
departure_time = dt_util.parse_datetime(
|
departure_time = (
|
||||||
self._opendata.connections[0]["departure"]
|
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:
|
if departure_time:
|
||||||
remaining_time = departure_time - dt_util.as_local(dt_util.utcnow())
|
remaining_time = departure_time - dt_util.as_local(dt_util.utcnow())
|
||||||
else:
|
else:
|
||||||
remaining_time = None
|
remaining_time = None
|
||||||
|
|
||||||
return DataConnection(
|
return DataConnection(
|
||||||
departure=self._opendata.connections[0]["departure"],
|
departure=departure_time,
|
||||||
next_departure=self._opendata.connections[1]["departure"],
|
next_departure=next_departure_time.isoformat()
|
||||||
next_on_departure=self._opendata.connections[2]["departure"],
|
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"],
|
train_number=self._opendata.connections[0]["number"],
|
||||||
platform=self._opendata.connections[0]["platform"],
|
platform=self._opendata.connections[0]["platform"],
|
||||||
transfers=self._opendata.connections[0]["transfers"],
|
transfers=self._opendata.connections[0]["transfers"],
|
||||||
|
@ -1,14 +1,18 @@
|
|||||||
"""Support for transport.opendata.ch."""
|
"""Support for transport.opendata.ch."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import datetime, timedelta
|
||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import config_entries, core
|
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.config_entries import SOURCE_IMPORT
|
||||||
from homeassistant.const import CONF_NAME
|
from homeassistant.const import CONF_NAME
|
||||||
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant, callback
|
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant, callback
|
||||||
@ -107,6 +111,7 @@ class SwissPublicTransportSensor(
|
|||||||
_attr_icon = "mdi:bus"
|
_attr_icon = "mdi:bus"
|
||||||
_attr_has_entity_name = True
|
_attr_has_entity_name = True
|
||||||
_attr_translation_key = "departure"
|
_attr_translation_key = "departure"
|
||||||
|
_attr_device_class = SensorDeviceClass.TIMESTAMP
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@ -143,6 +148,6 @@ class SwissPublicTransportSensor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> str:
|
def native_value(self) -> datetime | None:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self.coordinator.data["departure"]
|
return self.coordinator.data["departure"]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user