Skip to content
Permalink
Browse files

Fix Delijn sensor naming (#35789)

  • Loading branch information
Emilv2 authored and pvizeli committed May 20, 2020
1 parent f3e5d03 commit e979b5441389ad5510e9097afeb314c4d19d7ac2
Showing with 30 additions and 22 deletions.
  1. +1 −1 homeassistant/components/delijn/manifest.json
  2. +28 −20 homeassistant/components/delijn/sensor.py
  3. +1 −1 requirements_all.txt
@@ -3,5 +3,5 @@
"name": "De Lijn",
"documentation": "https://www.home-assistant.io/integrations/delijn",
"codeowners": ["@bollewolle"],
"requirements": ["pydelijn==0.5.1"]
"requirements": ["pydelijn==0.6.0"]
}
@@ -2,6 +2,7 @@
import logging

from pydelijn.api import Passages
from pydelijn.common import HttpException
import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA
@@ -37,61 +38,68 @@
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Create the sensor."""
api_key = config[CONF_API_KEY]
name = DEFAULT_NAME

session = async_get_clientsession(hass)

sensors = []
for nextpassage in config[CONF_NEXT_DEPARTURE]:
stop_id = nextpassage[CONF_STOP_ID]
number_of_departures = nextpassage[CONF_NUMBER_OF_DEPARTURES]
line = Passages(
hass.loop, stop_id, number_of_departures, api_key, session, True
sensors.append(
DeLijnPublicTransportSensor(
Passages(
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)


class DeLijnPublicTransportSensor(Entity):
"""Representation of a Ruter sensor."""

def __init__(self, line, name):
def __init__(self, line):
"""Initialize the sensor."""
self.line = line
self._attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
self._name = name
self._name = None
self._state = None
self._available = False
self._available = True

async def async_update(self):
"""Get the latest data from the De Lijn API."""
await self.line.get_passages()
if self.line.passages is None:
_LOGGER.warning("No data received from De Lijn")
try:
await self.line.get_passages()
self._name = await self.line.get_stopname()
except HttpException:
self._available = False
_LOGGER.error("De Lijn http error")
return

self._attributes["stopname"] = self._name
for passage in self.line.passages:
passage["stopname"] = self._name

try:
first = self.line.passages[0]
if first["due_at_realtime"] is not None:
first_passage = first["due_at_realtime"]
else:
first_passage = first["due_at_schedule"]
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_transport_type"] = first["line_transport_type"]
self._attributes["final_destination"] = first["final_destination"]
self._attributes["due_at_schedule"] = first["due_at_schedule"]
self._attributes["due_at_realtime"] = first["due_at_realtime"]
self._attributes["next_passages"] = self.line.passages
self._available = True
except (KeyError, IndexError) as error:
_LOGGER.debug("Error getting data from De Lijn: %s", error)
except (KeyError, IndexError):
_LOGGER.error("Invalid data received from De Lijn")
self._available = False

@property
@@ -1272,7 +1272,7 @@ pydanfossair==0.1.0
pydeconz==70

# homeassistant.components.delijn
pydelijn==0.5.1
pydelijn==0.6.0

# homeassistant.components.zwave
pydispatcher==2.0.5

0 comments on commit e979b54

Please sign in to comment.
You can’t perform that action at this time.