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:
1 parent
8310f4a1cf
commit
cfc175d71d
4 files changed
+114
-94
No files matched your search
@@ -1,14 +1,17 @@
|
||||
"""The tests for the rmvtransport platform."""
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
import datetime
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.common import mock_coro
|
||||
|
||||
VALID_CONFIG_MINIMAL = {'sensor': {'platform': 'rmvtransport',
|
||||
'next_departure': [{'station': '3000010'}]}}
|
||||
|
||||
VALID_CONFIG_MINIMAL = {'sensor': {
|
||||
'platform': 'rmvtransport',
|
||||
'next_departure': [
|
||||
{'station': '3000010'}
|
||||
]}}
|
||||
|
||||
VALID_CONFIG_NAME = {'sensor': {
|
||||
'platform': 'rmvtransport',
|
||||
@@ -41,8 +44,7 @@ VALID_CONFIG_DEST = {'sensor': {
|
||||
]}}
|
||||
|
||||
|
||||
def get_departuresMock(stationId, maxJourneys,
|
||||
products): # pylint: disable=invalid-name
|
||||
def get_departures_mock():
|
||||
"""Mock rmvtransport departures loading."""
|
||||
data = {'station': 'Frankfurt (Main) Hauptbahnhof',
|
||||
'stationId': '3000010', 'filter': '11111111111', 'journeys': [
|
||||
@@ -97,77 +99,77 @@ def get_departuresMock(stationId, maxJourneys,
|
||||
return data
|
||||
|
||||
|
||||
def get_errDeparturesMock(stationId, maxJourneys,
|
||||
products): # pylint: disable=invalid-name
|
||||
"""Mock rmvtransport departures erroneous loading."""
|
||||
raise ValueError
|
||||
def get_no_departures_mock():
|
||||
"""Mock no departures in results."""
|
||||
data = {'station': 'Frankfurt (Main) Hauptbahnhof',
|
||||
'stationId': '3000010',
|
||||
'filter': '11111111111',
|
||||
'journeys': []}
|
||||
return data
|
||||
|
||||
|
||||
class TestRMVtransportSensor(unittest.TestCase):
|
||||
"""Test the rmvtransport sensor."""
|
||||
async def test_rmvtransport_min_config(hass):
|
||||
"""Test minimal rmvtransport configuration."""
|
||||
with patch('RMVtransport.RMVtransport.get_departures',
|
||||
return_value=mock_coro(get_departures_mock())):
|
||||
assert await async_setup_component(hass, 'sensor',
|
||||
VALID_CONFIG_MINIMAL) is True
|
||||
|
||||
def setUp(self):
|
||||
"""Set up things to run when tests begin."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.config = VALID_CONFIG_MINIMAL
|
||||
self.reference = {}
|
||||
self.entities = []
|
||||
state = hass.states.get('sensor.frankfurt_main_hauptbahnhof')
|
||||
assert state.state == '7'
|
||||
assert state.attributes['departure_time'] == \
|
||||
datetime.datetime(2018, 8, 6, 14, 21)
|
||||
assert state.attributes['direction'] == \
|
||||
'Frankfurt (Main) Hugo-Junkers-Straße/Schleife'
|
||||
assert state.attributes['product'] == 'Tram'
|
||||
assert state.attributes['line'] == 12
|
||||
assert state.attributes['icon'] == 'mdi:tram'
|
||||
assert state.attributes['friendly_name'] == 'Frankfurt (Main) Hauptbahnhof'
|
||||
|
||||
def tearDown(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
@patch('RMVtransport.RMVtransport.get_departures',
|
||||
side_effect=get_departuresMock)
|
||||
def test_rmvtransport_min_config(self, mock_get_departures):
|
||||
"""Test minimal rmvtransport configuration."""
|
||||
assert setup_component(self.hass, 'sensor', VALID_CONFIG_MINIMAL)
|
||||
state = self.hass.states.get('sensor.frankfurt_main_hauptbahnhof')
|
||||
self.assertEqual(state.state, '7')
|
||||
self.assertEqual(state.attributes['departure_time'],
|
||||
datetime.datetime(2018, 8, 6, 14, 21))
|
||||
self.assertEqual(state.attributes['direction'],
|
||||
'Frankfurt (Main) Hugo-Junkers-Straße/Schleife')
|
||||
self.assertEqual(state.attributes['product'], 'Tram')
|
||||
self.assertEqual(state.attributes['line'], 12)
|
||||
self.assertEqual(state.attributes['icon'], 'mdi:tram')
|
||||
self.assertEqual(state.attributes['friendly_name'],
|
||||
'Frankfurt (Main) Hauptbahnhof')
|
||||
async def test_rmvtransport_name_config(hass):
|
||||
"""Test custom name configuration."""
|
||||
with patch('RMVtransport.RMVtransport.get_departures',
|
||||
return_value=mock_coro(get_departures_mock())):
|
||||
assert await async_setup_component(hass, 'sensor', VALID_CONFIG_NAME)
|
||||
|
||||
@patch('RMVtransport.RMVtransport.get_departures',
|
||||
side_effect=get_departuresMock)
|
||||
def test_rmvtransport_name_config(self, mock_get_departures):
|
||||
"""Test custom name configuration."""
|
||||
assert setup_component(self.hass, 'sensor', VALID_CONFIG_NAME)
|
||||
state = self.hass.states.get('sensor.my_station')
|
||||
self.assertEqual(state.attributes['friendly_name'], 'My Station')
|
||||
state = hass.states.get('sensor.my_station')
|
||||
assert state.attributes['friendly_name'] == 'My Station'
|
||||
|
||||
@patch('RMVtransport.RMVtransport.get_departures',
|
||||
side_effect=get_errDeparturesMock)
|
||||
def test_rmvtransport_err_config(self, mock_get_departures):
|
||||
"""Test erroneous rmvtransport configuration."""
|
||||
assert setup_component(self.hass, 'sensor', VALID_CONFIG_MINIMAL)
|
||||
|
||||
@patch('RMVtransport.RMVtransport.get_departures',
|
||||
side_effect=get_departuresMock)
|
||||
def test_rmvtransport_misc_config(self, mock_get_departures):
|
||||
"""Test misc configuration."""
|
||||
assert setup_component(self.hass, 'sensor', VALID_CONFIG_MISC)
|
||||
state = self.hass.states.get('sensor.frankfurt_main_hauptbahnhof')
|
||||
self.assertEqual(state.attributes['friendly_name'],
|
||||
'Frankfurt (Main) Hauptbahnhof')
|
||||
self.assertEqual(state.attributes['line'], 21)
|
||||
async def test_rmvtransport_misc_config(hass):
|
||||
"""Test misc configuration."""
|
||||
with patch('RMVtransport.RMVtransport.get_departures',
|
||||
return_value=mock_coro(get_departures_mock())):
|
||||
assert await async_setup_component(hass, 'sensor', VALID_CONFIG_MISC)
|
||||
|
||||
@patch('RMVtransport.RMVtransport.get_departures',
|
||||
side_effect=get_departuresMock)
|
||||
def test_rmvtransport_dest_config(self, mock_get_departures):
|
||||
"""Test misc configuration."""
|
||||
assert setup_component(self.hass, 'sensor', VALID_CONFIG_DEST)
|
||||
state = self.hass.states.get('sensor.frankfurt_main_hauptbahnhof')
|
||||
self.assertEqual(state.state, '11')
|
||||
self.assertEqual(state.attributes['direction'],
|
||||
'Frankfurt (Main) Hugo-Junkers-Straße/Schleife')
|
||||
self.assertEqual(state.attributes['line'], 12)
|
||||
self.assertEqual(state.attributes['minutes'], 11)
|
||||
self.assertEqual(state.attributes['departure_time'],
|
||||
datetime.datetime(2018, 8, 6, 14, 25))
|
||||
state = hass.states.get('sensor.frankfurt_main_hauptbahnhof')
|
||||
assert state.attributes['friendly_name'] == 'Frankfurt (Main) Hauptbahnhof'
|
||||
assert state.attributes['line'] == 21
|
||||
|
||||
|
||||
async def test_rmvtransport_dest_config(hass):
|
||||
"""Test destination configuration."""
|
||||
with patch('RMVtransport.RMVtransport.get_departures',
|
||||
return_value=mock_coro(get_departures_mock())):
|
||||
assert await async_setup_component(hass, 'sensor', VALID_CONFIG_DEST)
|
||||
|
||||
state = hass.states.get('sensor.frankfurt_main_hauptbahnhof')
|
||||
assert state.state == '11'
|
||||
assert state.attributes['direction'] == \
|
||||
'Frankfurt (Main) Hugo-Junkers-Straße/Schleife'
|
||||
assert state.attributes['line'] == 12
|
||||
assert state.attributes['minutes'] == 11
|
||||
assert state.attributes['departure_time'] == \
|
||||
datetime.datetime(2018, 8, 6, 14, 25)
|
||||
|
||||
|
||||
async def test_rmvtransport_no_departures(hass):
|
||||
"""Test for no departures."""
|
||||
with patch('RMVtransport.RMVtransport.get_departures',
|
||||
return_value=mock_coro(get_no_departures_mock())):
|
||||
assert await async_setup_component(hass, 'sensor',
|
||||
VALID_CONFIG_MINIMAL)
|
||||
|
||||
state = hass.states.get('sensor.frankfurt_main_hauptbahnhof')
|
||||
assert not state
|
||||
Reference in new issue
Block a user