mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Fix Delijn sensor naming (#35789)
This commit is contained in:
parent
4f317353e0
commit
53a9d39a81
@ -3,5 +3,5 @@
|
|||||||
"name": "De Lijn",
|
"name": "De Lijn",
|
||||||
"documentation": "https://www.home-assistant.io/integrations/delijn",
|
"documentation": "https://www.home-assistant.io/integrations/delijn",
|
||||||
"codeowners": ["@bollewolle"],
|
"codeowners": ["@bollewolle"],
|
||||||
"requirements": ["pydelijn==0.5.1"]
|
"requirements": ["pydelijn==0.6.0"]
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from pydelijn.api import Passages
|
from pydelijn.api import Passages
|
||||||
|
from pydelijn.common import HttpException
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
@ -37,22 +38,23 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||||
"""Create the sensor."""
|
"""Create the sensor."""
|
||||||
api_key = config[CONF_API_KEY]
|
api_key = config[CONF_API_KEY]
|
||||||
name = DEFAULT_NAME
|
|
||||||
|
|
||||||
session = async_get_clientsession(hass)
|
session = async_get_clientsession(hass)
|
||||||
|
|
||||||
sensors = []
|
sensors = []
|
||||||
for nextpassage in config[CONF_NEXT_DEPARTURE]:
|
for nextpassage in config[CONF_NEXT_DEPARTURE]:
|
||||||
stop_id = nextpassage[CONF_STOP_ID]
|
sensors.append(
|
||||||
number_of_departures = nextpassage[CONF_NUMBER_OF_DEPARTURES]
|
DeLijnPublicTransportSensor(
|
||||||
line = Passages(
|
Passages(
|
||||||
hass.loop, stop_id, number_of_departures, api_key, session, True
|
hass.loop,
|
||||||
|
nextpassage[CONF_STOP_ID],
|
||||||
|
nextpassage[CONF_NUMBER_OF_DEPARTURES],
|
||||||
|
api_key,
|
||||||
|
session,
|
||||||
|
True,
|
||||||
|
)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
await line.get_passages()
|
|
||||||
if line.passages is None:
|
|
||||||
_LOGGER.warning("No data received from De Lijn")
|
|
||||||
return
|
|
||||||
sensors.append(DeLijnPublicTransportSensor(line, name))
|
|
||||||
|
|
||||||
async_add_entities(sensors, True)
|
async_add_entities(sensors, True)
|
||||||
|
|
||||||
@ -60,20 +62,28 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
class DeLijnPublicTransportSensor(Entity):
|
class DeLijnPublicTransportSensor(Entity):
|
||||||
"""Representation of a Ruter sensor."""
|
"""Representation of a Ruter sensor."""
|
||||||
|
|
||||||
def __init__(self, line, name):
|
def __init__(self, line):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self.line = line
|
self.line = line
|
||||||
self._attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
self._attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
||||||
self._name = name
|
self._name = None
|
||||||
self._state = None
|
self._state = None
|
||||||
self._available = False
|
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."""
|
||||||
await self.line.get_passages()
|
try:
|
||||||
if self.line.passages is None:
|
await self.line.get_passages()
|
||||||
_LOGGER.warning("No data received from De Lijn")
|
self._name = await self.line.get_stopname()
|
||||||
|
except HttpException:
|
||||||
|
self._available = False
|
||||||
|
_LOGGER.error("De Lijn http error")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
self._attributes["stopname"] = self._name
|
||||||
|
for passage in self.line.passages:
|
||||||
|
passage["stopname"] = self._name
|
||||||
|
|
||||||
try:
|
try:
|
||||||
first = self.line.passages[0]
|
first = self.line.passages[0]
|
||||||
if first["due_at_realtime"] is not None:
|
if first["due_at_realtime"] is not None:
|
||||||
@ -81,8 +91,6 @@ class DeLijnPublicTransportSensor(Entity):
|
|||||||
else:
|
else:
|
||||||
first_passage = first["due_at_schedule"]
|
first_passage = first["due_at_schedule"]
|
||||||
self._state = first_passage
|
self._state = first_passage
|
||||||
self._name = first["stopname"]
|
|
||||||
self._attributes["stopname"] = first["stopname"]
|
|
||||||
self._attributes["line_number_public"] = first["line_number_public"]
|
self._attributes["line_number_public"] = first["line_number_public"]
|
||||||
self._attributes["line_transport_type"] = first["line_transport_type"]
|
self._attributes["line_transport_type"] = first["line_transport_type"]
|
||||||
self._attributes["final_destination"] = first["final_destination"]
|
self._attributes["final_destination"] = first["final_destination"]
|
||||||
@ -90,8 +98,8 @@ class DeLijnPublicTransportSensor(Entity):
|
|||||||
self._attributes["due_at_realtime"] = first["due_at_realtime"]
|
self._attributes["due_at_realtime"] = first["due_at_realtime"]
|
||||||
self._attributes["next_passages"] = self.line.passages
|
self._attributes["next_passages"] = self.line.passages
|
||||||
self._available = True
|
self._available = True
|
||||||
except (KeyError, IndexError) as error:
|
except (KeyError, IndexError):
|
||||||
_LOGGER.debug("Error getting data from De Lijn: %s", error)
|
_LOGGER.error("Invalid data received from De Lijn")
|
||||||
self._available = False
|
self._available = False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -1272,7 +1272,7 @@ pydanfossair==0.1.0
|
|||||||
pydeconz==70
|
pydeconz==70
|
||||||
|
|
||||||
# homeassistant.components.delijn
|
# homeassistant.components.delijn
|
||||||
pydelijn==0.5.1
|
pydelijn==0.6.0
|
||||||
|
|
||||||
# homeassistant.components.zwave
|
# homeassistant.components.zwave
|
||||||
pydispatcher==2.0.5
|
pydispatcher==2.0.5
|
||||||
|
Loading…
x
Reference in New Issue
Block a user