mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Clean up swiss public transport (#106245)
This commit is contained in:
parent
c91ac22d3c
commit
989a7e7b10
@ -1,9 +1,12 @@
|
|||||||
"""Support for transport.opendata.ch."""
|
"""Support for transport.opendata.ch."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Mapping
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from opendata_transport import OpendataTransport
|
||||||
from opendata_transport.exceptions import OpendataTransportError
|
from opendata_transport.exceptions import OpendataTransportError
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -53,13 +56,11 @@ async def async_setup_entry(
|
|||||||
"""Set up the sensor from a config entry created in the integrations UI."""
|
"""Set up the sensor from a config entry created in the integrations UI."""
|
||||||
opendata = hass.data[DOMAIN][config_entry.entry_id]
|
opendata = hass.data[DOMAIN][config_entry.entry_id]
|
||||||
|
|
||||||
start = config_entry.data[CONF_START]
|
|
||||||
destination = config_entry.data[CONF_DESTINATION]
|
|
||||||
name = config_entry.title
|
name = config_entry.title
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[SwissPublicTransportSensor(opendata, start, destination, name)],
|
[SwissPublicTransportSensor(opendata, name)],
|
||||||
update_before_add=True,
|
True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -112,37 +113,28 @@ class SwissPublicTransportSensor(SensorEntity):
|
|||||||
_attr_attribution = "Data provided by transport.opendata.ch"
|
_attr_attribution = "Data provided by transport.opendata.ch"
|
||||||
_attr_icon = "mdi:bus"
|
_attr_icon = "mdi:bus"
|
||||||
|
|
||||||
def __init__(self, opendata, start, destination, name):
|
def __init__(self, opendata: OpendataTransport, name: str) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._opendata = opendata
|
self._opendata = opendata
|
||||||
self._name = name
|
self._attr_name = name
|
||||||
self._from = start
|
self._remaining_time: timedelta | None = None
|
||||||
self._to = destination
|
|
||||||
self._remaining_time = None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def native_value(self) -> str:
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return (
|
return self._opendata.connections[0]["departure"]
|
||||||
self._opendata.connections[0]["departure"]
|
|
||||||
if self._opendata is not None
|
|
||||||
else None
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self) -> Mapping[str, Any]:
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
if self._opendata is None:
|
departure_time = dt_util.parse_datetime(
|
||||||
return
|
|
||||||
|
|
||||||
self._remaining_time = dt_util.parse_datetime(
|
|
||||||
self._opendata.connections[0]["departure"]
|
self._opendata.connections[0]["departure"]
|
||||||
) - dt_util.as_local(dt_util.utcnow())
|
)
|
||||||
|
if departure_time:
|
||||||
|
remaining_time = departure_time - dt_util.as_local(dt_util.utcnow())
|
||||||
|
else:
|
||||||
|
remaining_time = None
|
||||||
|
self._remaining_time = remaining_time
|
||||||
|
|
||||||
return {
|
return {
|
||||||
ATTR_TRAIN_NUMBER: self._opendata.connections[0]["number"],
|
ATTR_TRAIN_NUMBER: self._opendata.connections[0]["number"],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user