mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 12:47:08 +00:00
Use _attr_* in delijn (#61344)
Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
parent
f4d66f67d5
commit
5b8f8772d2
@ -5,8 +5,12 @@ from pydelijn.api import Passages
|
|||||||
from pydelijn.common import HttpException
|
from pydelijn.common import HttpException
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
from homeassistant.components.sensor import (
|
||||||
from homeassistant.const import ATTR_ATTRIBUTION, CONF_API_KEY, DEVICE_CLASS_TIMESTAMP
|
PLATFORM_SCHEMA,
|
||||||
|
SensorDeviceClass,
|
||||||
|
SensorEntity,
|
||||||
|
)
|
||||||
|
from homeassistant.const import CONF_API_KEY
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
@ -60,72 +64,48 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
class DeLijnPublicTransportSensor(SensorEntity):
|
class DeLijnPublicTransportSensor(SensorEntity):
|
||||||
"""Representation of a Ruter sensor."""
|
"""Representation of a Ruter sensor."""
|
||||||
|
|
||||||
_attr_device_class = DEVICE_CLASS_TIMESTAMP
|
_attr_attribution = ATTRIBUTION
|
||||||
|
_attr_device_class = SensorDeviceClass.TIMESTAMP
|
||||||
|
_attr_icon = "mdi:bus"
|
||||||
|
|
||||||
def __init__(self, line):
|
def __init__(self, line):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self.line = line
|
self.line = line
|
||||||
self._attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
self._attr_extra_state_attributes = {}
|
||||||
self._name = None
|
|
||||||
self._state = None
|
|
||||||
self._available = True
|
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Get the latest data from the De Lijn API."""
|
"""Get the latest data from the De Lijn API."""
|
||||||
try:
|
try:
|
||||||
await self.line.get_passages()
|
await self.line.get_passages()
|
||||||
self._name = await self.line.get_stopname()
|
self._attr_name = await self.line.get_stopname()
|
||||||
except HttpException:
|
except HttpException:
|
||||||
self._available = False
|
self._attr_available = False
|
||||||
_LOGGER.error("De Lijn http error")
|
_LOGGER.error("De Lijn http error")
|
||||||
return
|
return
|
||||||
|
|
||||||
self._attributes["stopname"] = self._name
|
self._attr_extra_state_attributes["stopname"] = self._attr_name
|
||||||
|
|
||||||
if not self.line.passages:
|
if not self.line.passages:
|
||||||
self._available = False
|
self._attr_available = False
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
first = self.line.passages[0]
|
first = self.line.passages[0]
|
||||||
if first["due_at_realtime"] is not None:
|
if (first_passage := first["due_at_realtime"]) is None:
|
||||||
first_passage = first["due_at_realtime"]
|
|
||||||
else:
|
|
||||||
first_passage = first["due_at_schedule"]
|
first_passage = first["due_at_schedule"]
|
||||||
self._state = first_passage
|
self._attr_native_value = first_passage
|
||||||
self._attributes["line_number_public"] = first["line_number_public"]
|
self._attr_extra_state_attributes.update(
|
||||||
self._attributes["line_transport_type"] = first["line_transport_type"]
|
{
|
||||||
self._attributes["final_destination"] = first["final_destination"]
|
"line_number_public": first["line_number_public"],
|
||||||
self._attributes["due_at_schedule"] = first["due_at_schedule"]
|
"line_transport_type": first["line_transport_type"],
|
||||||
self._attributes["due_at_realtime"] = first["due_at_realtime"]
|
"final_destination": first["final_destination"],
|
||||||
self._attributes["is_realtime"] = first["is_realtime"]
|
"due_at_schedule": first["due_at_schedule"],
|
||||||
self._attributes["next_passages"] = self.line.passages
|
"due_at_realtime": first["due_at_realtime"],
|
||||||
self._available = True
|
"is_realtime": first["is_realtime"],
|
||||||
|
"next_passages": self.line.passages,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
self._attr_available = True
|
||||||
except (KeyError) as error:
|
except (KeyError) as error:
|
||||||
_LOGGER.error("Invalid data received from De Lijn: %s", error)
|
_LOGGER.error("Invalid data received from De Lijn: %s", error)
|
||||||
self._available = False
|
self._attr_available = False
|
||||||
|
|
||||||
@property
|
|
||||||
def available(self):
|
|
||||||
"""Return True if entity is available."""
|
|
||||||
return self._available
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the state of the sensor."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self):
|
|
||||||
"""Return the icon of the sensor."""
|
|
||||||
return "mdi:bus"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def extra_state_attributes(self):
|
|
||||||
"""Return attributes for the sensor."""
|
|
||||||
return self._attributes
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user