Make rmvtransport async (#17225)

* Make rmvtransport async

* Make rmv transport async

* Make async tests

* Update rmvtransport module version

* Remove unnecessary import

* Make rmvtransport async

* Make rmv transport async

* Make async tests

* Update rmvtransport module version

* Remove unnecessary import

* Update requirements

* Remove async loop

* Fix wrong import

* Fix stupidness

* Remove unnecessary import

* Bump upstream version

* Don't store the session

* Refactor tests

* Add test for no data

* Fix linter issues

* Fix stale docstring

* Fix stale docstring

* Remove unnecessary test code

* Remove unnecessary import

* Add configurable timeout

* Remove global variable
This commit is contained in:
cgtobi authored and Martin Hjelmare committed 2018-10-10 08:10:42 +02:00
1 parent 8310f4a1cf
commit cfc175d71d
4 files changed
+114 -94

No files matched your search

+37 -19
View File
@@ -5,15 +5,18 @@ For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.rmvtransport/
"""
import logging
from datetime import timedelta
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity import Entity
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (CONF_NAME, ATTR_ATTRIBUTION)
from homeassistant.util import Throttle
REQUIREMENTS = ['PyRMVtransport==0.1']
REQUIREMENTS = ['PyRMVtransport==0.1.3']
_LOGGER = logging.getLogger(__name__)
@@ -26,6 +29,7 @@ CONF_LINES = 'lines'
CONF_PRODUCTS = 'products'
CONF_TIME_OFFSET = 'time_offset'
CONF_MAX_JOURNEYS = 'max_journeys'
CONF_TIMEOUT = 'timeout'
DEFAULT_NAME = 'RMV Journey'
@@ -46,6 +50,8 @@ ICONS = {
}
ATTRIBUTION = "Data provided by opendata.rmv.de"
SCAN_INTERVAL = timedelta(seconds=60)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_NEXT_DEPARTURE): [{
vol.Required(CONF_STATION): cv.string,
@@ -59,16 +65,23 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.All(cv.ensure_list, [vol.In(VALID_PRODUCTS)]),
vol.Optional(CONF_TIME_OFFSET, default=0): cv.positive_int,
vol.Optional(CONF_MAX_JOURNEYS, default=5): cv.positive_int,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string}]
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string}],
vol.Optional(CONF_TIMEOUT, default=10): cv.positive_int
})
def setup_platform(hass, config, add_entities, discovery_info=None):
async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up the RMV departure sensor."""
timeout = config.get(CONF_TIMEOUT)
session = async_get_clientsession(hass)
sensors = []
for next_departure in config.get(CONF_NEXT_DEPARTURE):
sensors.append(
RMVDepartureSensor(
session,
next_departure[CONF_STATION],
next_departure.get(CONF_DESTINATIONS),
next_departure.get(CONF_DIRECTIONS),
@@ -76,21 +89,23 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
next_departure.get(CONF_PRODUCTS),
next_departure.get(CONF_TIME_OFFSET),
next_departure.get(CONF_MAX_JOURNEYS),
next_departure.get(CONF_NAME)))
add_entities(sensors, True)
next_departure.get(CONF_NAME),
timeout))
async_add_entities(sensors, True)
class RMVDepartureSensor(Entity):
"""Implementation of an RMV departure sensor."""
def __init__(self, station, destinations, directions,
lines, products, time_offset, max_journeys, name):
def __init__(self, session, station, destinations, directions, lines,
products, time_offset, max_journeys, name, timeout):
"""Initialize the sensor."""
self._station = station
self._name = name
self._state = None
self.data = RMVDepartureData(station, destinations, directions, lines,
products, time_offset, max_journeys)
self.data = RMVDepartureData(session, station, destinations,
directions, lines, products, time_offset,
max_journeys, timeout)
self._icon = ICONS[None]
@property
@@ -134,9 +149,10 @@ class RMVDepartureSensor(Entity):
"""Return the unit this state is expressed in."""
return "min"
def update(self):
async def async_update(self):
"""Get the latest data and update the state."""
self.data.update()
await self.data.async_update()
if not self.data.departures:
self._state = None
self._icon = ICONS[None]
@@ -151,10 +167,11 @@ class RMVDepartureSensor(Entity):
class RMVDepartureData:
"""Pull data from the opendata.rmv.de web page."""
def __init__(self, station_id, destinations, directions,
lines, products, time_offset, max_journeys):
def __init__(self, session, station_id, destinations, directions, lines,
products, time_offset, max_journeys, timeout):
"""Initialize the sensor."""
import RMVtransport
from RMVtransport import RMVtransport
self.station = None
self._station_id = station_id
self._destinations = destinations
@@ -163,15 +180,16 @@ class RMVDepartureData:
self._products = products
self._time_offset = time_offset
self._max_journeys = max_journeys
self.rmv = RMVtransport.RMVtransport()
self.rmv = RMVtransport(session, timeout)
self.departures = []
def update(self):
@Throttle(SCAN_INTERVAL)
async def async_update(self):
"""Update the connection data."""
try:
_data = self.rmv.get_departures(self._station_id,
products=self._products,
maxJourneys=50)
_data = await self.rmv.get_departures(self._station_id,
products=self._products,
maxJourneys=50)
except ValueError:
self.departures = []
_LOGGER.warning("Returned data not understood")