Add HVV integration (Hamburg public transportation) (#31564)

Co-authored-by: springstan <46536646+springstan@users.noreply.github.com>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
authored and GitHub committed 2020-06-15 00:15:20 +02:00
1 parent 0b7d2aa4d7
commit 0331ebdd47
21 files changed
+1211

No files matched your search

@@ -0,0 +1 @@
"""Tests for the HVV Departures integration."""
@@ -0,0 +1,344 @@
"""Test the HVV Departures config flow."""
import json
from pygti.exceptions import CannotConnect, InvalidAuth
from homeassistant import data_entry_flow
from homeassistant.components.hvv_departures.const import (
CONF_FILTER,
CONF_REAL_TIME,
CONF_STATION,
DOMAIN,
)
from homeassistant.config_entries import CONN_CLASS_CLOUD_POLL, SOURCE_USER
from homeassistant.const import CONF_HOST, CONF_OFFSET, CONF_PASSWORD, CONF_USERNAME
from tests.async_mock import patch
from tests.common import MockConfigEntry, load_fixture
FIXTURE_INIT = json.loads(load_fixture("hvv_departures/init.json"))
FIXTURE_CHECK_NAME = json.loads(load_fixture("hvv_departures/check_name.json"))
FIXTURE_STATION_INFORMATION = json.loads(
load_fixture("hvv_departures/station_information.json")
)
FIXTURE_CONFIG_ENTRY = json.loads(load_fixture("hvv_departures/config_entry.json"))
FIXTURE_OPTIONS = json.loads(load_fixture("hvv_departures/options.json"))
FIXTURE_DEPARTURE_LIST = json.loads(load_fixture("hvv_departures/departure_list.json"))
async def test_user_flow(hass):
"""Test that config flow works."""
with patch(
"homeassistant.components.hvv_departures.hub.GTI.init",
return_value=FIXTURE_INIT,
), patch("pygti.gti.GTI.checkName", return_value=FIXTURE_CHECK_NAME,), patch(
"pygti.gti.GTI.stationInformation", return_value=FIXTURE_STATION_INFORMATION,
), patch(
"homeassistant.components.hvv_departures.async_setup", return_value=True
), patch(
"homeassistant.components.hvv_departures.async_setup_entry", return_value=True,
):
# step: user
result_user = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
data={
CONF_HOST: "api-test.geofox.de",
CONF_USERNAME: "test-username",
CONF_PASSWORD: "test-password",
},
)
assert result_user["step_id"] == "station"
# step: station
result_station = await hass.config_entries.flow.async_configure(
result_user["flow_id"], {CONF_STATION: "Wartenau"},
)
assert result_station["step_id"] == "station_select"
# step: station_select
result_station_select = await hass.config_entries.flow.async_configure(
result_user["flow_id"], {CONF_STATION: "Wartenau"},
)
assert result_station_select["type"] == "create_entry"
assert result_station_select["title"] == "Wartenau"
assert result_station_select["data"] == {
CONF_HOST: "api-test.geofox.de",
CONF_USERNAME: "test-username",
CONF_PASSWORD: "test-password",
CONF_STATION: {
"name": "Wartenau",
"city": "Hamburg",
"combinedName": "Wartenau",
"id": "Master:10901",
"type": "STATION",
"coordinate": {"x": 10.035515, "y": 53.56478},
"serviceTypes": ["bus", "u"],
"hasStationInformation": True,
},
}
async def test_user_flow_no_results(hass):
"""Test that config flow works when there are no results."""
with patch(
"homeassistant.components.hvv_departures.hub.GTI.init",
return_value=FIXTURE_INIT,
), patch(
"pygti.gti.GTI.checkName", return_value={"returnCode": "OK", "results": []},
), patch(
"homeassistant.components.hvv_departures.async_setup", return_value=True
), patch(
"homeassistant.components.hvv_departures.async_setup_entry", return_value=True,
):
# step: user
result_user = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
data={
CONF_HOST: "api-test.geofox.de",
CONF_USERNAME: "test-username",
CONF_PASSWORD: "test-password",
},
)
assert result_user["step_id"] == "station"
# step: station
result_station = await hass.config_entries.flow.async_configure(
result_user["flow_id"], {CONF_STATION: "non_existing_station"},
)
assert result_station["step_id"] == "station"
assert result_station["errors"]["base"] == "no_results"
async def test_user_flow_invalid_auth(hass):
"""Test that config flow handles invalid auth."""
with patch(
"homeassistant.components.hvv_departures.hub.GTI.init",
side_effect=InvalidAuth(
"ERROR_TEXT",
"Bei der Verarbeitung der Anfrage ist ein technisches Problem aufgetreten.",
"Authentication failed!",
),
):
# step: user
result_user = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
data={
CONF_HOST: "api-test.geofox.de",
CONF_USERNAME: "test-username",
CONF_PASSWORD: "test-password",
},
)
assert result_user["type"] == "form"
assert result_user["errors"] == {"base": "invalid_auth"}
async def test_user_flow_cannot_connect(hass):
"""Test that config flow handles connection errors."""
with patch(
"homeassistant.components.hvv_departures.hub.GTI.init",
side_effect=CannotConnect(),
):
# step: user
result_user = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
data={
CONF_HOST: "api-test.geofox.de",
CONF_USERNAME: "test-username",
CONF_PASSWORD: "test-password",
},
)
assert result_user["type"] == "form"
assert result_user["errors"] == {"base": "cannot_connect"}
async def test_user_flow_station(hass):
"""Test that config flow handles empty data on step station."""
with patch(
"homeassistant.components.hvv_departures.hub.GTI.init", return_value=True,
), patch(
"pygti.gti.GTI.checkName", return_value={"returnCode": "OK", "results": []},
):
# step: user
result_user = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
data={
CONF_HOST: "api-test.geofox.de",
CONF_USERNAME: "test-username",
CONF_PASSWORD: "test-password",
},
)
assert result_user["step_id"] == "station"
# step: station
result_station = await hass.config_entries.flow.async_configure(
result_user["flow_id"], None,
)
assert result_station["type"] == "form"
assert result_station["step_id"] == "station"
async def test_user_flow_station_select(hass):
"""Test that config flow handles empty data on step station_select."""
with patch(
"homeassistant.components.hvv_departures.hub.GTI.init", return_value=True,
), patch(
"pygti.gti.GTI.checkName", return_value=FIXTURE_CHECK_NAME,
):
result_user = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
data={
CONF_HOST: "api-test.geofox.de",
CONF_USERNAME: "test-username",
CONF_PASSWORD: "test-password",
},
)
result_station = await hass.config_entries.flow.async_configure(
result_user["flow_id"], {CONF_STATION: "Wartenau"},
)
# step: station_select
result_station_select = await hass.config_entries.flow.async_configure(
result_station["flow_id"], None,
)
assert result_station_select["type"] == "form"
assert result_station_select["step_id"] == "station_select"
async def test_options_flow(hass):
"""Test that options flow works."""
config_entry = MockConfigEntry(
version=1,
domain=DOMAIN,
title="Wartenau",
data=FIXTURE_CONFIG_ENTRY,
source="user",
connection_class=CONN_CLASS_CLOUD_POLL,
system_options={"disable_new_entities": False},
options=FIXTURE_OPTIONS,
unique_id="1234",
)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.hvv_departures.hub.GTI.init", return_value=True,
), patch(
"pygti.gti.GTI.departureList", return_value=FIXTURE_DEPARTURE_LIST,
):
assert await hass.config_entries.async_setup(config_entry.entry_id)
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={CONF_FILTER: ["0"], CONF_OFFSET: 15, CONF_REAL_TIME: False},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert config_entry.options == {
CONF_FILTER: [
{
"serviceID": "HHA-U:U1_HHA-U",
"stationIDs": ["Master:10902"],
"label": "Fuhlsbüttel Nord / Ochsenzoll / Norderstedt Mitte / Kellinghusenstraße / Ohlsdorf / Garstedt",
"serviceName": "U1",
}
],
CONF_OFFSET: 15,
CONF_REAL_TIME: False,
}
async def test_options_flow_invalid_auth(hass):
"""Test that options flow works."""
config_entry = MockConfigEntry(
version=1,
domain=DOMAIN,
title="Wartenau",
data=FIXTURE_CONFIG_ENTRY,
source="user",
connection_class=CONN_CLASS_CLOUD_POLL,
system_options={"disable_new_entities": False},
options=FIXTURE_OPTIONS,
unique_id="1234",
)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.hvv_departures.hub.GTI.init",
side_effect=InvalidAuth(
"ERROR_TEXT",
"Bei der Verarbeitung der Anfrage ist ein technisches Problem aufgetreten.",
"Authentication failed!",
),
):
assert await hass.config_entries.async_setup(config_entry.entry_id)
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "init"
assert result["errors"] == {"base": "invalid_auth"}
async def test_options_flow_cannot_connect(hass):
"""Test that options flow works."""
config_entry = MockConfigEntry(
version=1,
domain=DOMAIN,
title="Wartenau",
data=FIXTURE_CONFIG_ENTRY,
source="user",
connection_class=CONN_CLASS_CLOUD_POLL,
system_options={"disable_new_entities": False},
options=FIXTURE_OPTIONS,
unique_id="1234",
)
config_entry.add_to_hass(hass)
with patch(
"pygti.gti.GTI.departureList", side_effect=CannotConnect(),
):
assert await hass.config_entries.async_setup(config_entry.entry_id)
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "init"
assert result["errors"] == {"base": "cannot_connect"}
+15
View File
@@ -0,0 +1,15 @@
{
"returnCode": "OK",
"results": [
{
"name": "Wartenau",
"city": "Hamburg",
"combinedName": "Wartenau",
"id": "Master:10901",
"type": "STATION",
"coordinate": {"x": 10.035515, "y": 53.56478},
"serviceTypes": ["bus", "u"],
"hasStationInformation": true
}
]
}
+16
View File
@@ -0,0 +1,16 @@
{
"host": "api-test.geofox.de",
"username": "test-username",
"password": "test-password",
"station": {
"city": "Schmalfeld",
"combinedName": "Schmalfeld, Holstenstra\u00dfe",
"coordinate": {"x": 9.986115, "y": 53.874122},
"hasStationInformation": false,
"id": "Master:75279",
"name": "Holstenstra\u00dfe",
"serviceTypes": ["bus"],
"type": "STATION"
},
"stationInformation": {"returnCode": "OK"}
}
+162
View File
@@ -0,0 +1,162 @@
{
"returnCode": "OK",
"time": {"date": "26.01.2020", "time": "22:52"},
"departures": [
{
"line": {
"name": "U1",
"direction": "Großhansdorf",
"origin": "Norderstedt Mitte",
"type": {
"simpleType": "TRAIN",
"shortInfo": "U",
"longInfo": "U-Bahn",
"model": "DT4"
},
"id": "HHA-U:U1_HHA-U"
},
"timeOffset": 0,
"delay": 0,
"serviceId": 1482563187,
"station": {"combinedName": "Wartenau", "id": "Master:10901"},
"attributes": [{"isPlanned": true, "types": ["REALTIME", "ACCURATE"]}]
},
{
"line": {
"name": "25",
"direction": "Bf. Altona",
"origin": "U Burgstraße",
"type": {
"simpleType": "BUS",
"shortInfo": "Bus",
"longInfo": "Niederflur Metrobus",
"model": "Gelenkbus"
},
"id": "HHA-B:25_HHA-B"
},
"timeOffset": 1,
"delay": 0,
"serviceId": 74567,
"station": {"combinedName": "U Wartenau", "id": "Master:60015"},
"attributes": [{"isPlanned": true, "types": ["REALTIME", "ACCURATE"]}]
},
{
"line": {
"name": "25",
"direction": "U Burgstraße",
"origin": "Bf. Altona",
"type": {
"simpleType": "BUS",
"shortInfo": "Bus",
"longInfo": "Niederflur Metrobus",
"model": "Gelenkbus"
},
"id": "HHA-B:25_HHA-B"
},
"timeOffset": 5,
"delay": 0,
"serviceId": 74328,
"station": {"combinedName": "U Wartenau", "id": "Master:60015"},
"attributes": [{"isPlanned": true, "types": ["REALTIME", "ACCURATE"]}]
},
{
"line": {
"name": "U1",
"direction": "Norderstedt Mitte",
"origin": "Großhansdorf",
"type": {
"simpleType": "TRAIN",
"shortInfo": "U",
"longInfo": "U-Bahn",
"model": "DT4"
},
"id": "HHA-U:U1_HHA-U"
},
"timeOffset": 8,
"delay": 0,
"station": {"combinedName": "Wartenau", "id": "Master:10901"},
"attributes": [{"isPlanned": true, "types": ["REALTIME", "ACCURATE"]}]
},
{
"line": {
"name": "U1",
"direction": "Ohlstedt",
"origin": "Norderstedt Mitte",
"type": {
"simpleType": "TRAIN",
"shortInfo": "U",
"longInfo": "U-Bahn",
"model": "DT4"
},
"id": "HHA-U:U1_HHA-U"
},
"timeOffset": 10,
"delay": 0,
"station": {"combinedName": "Wartenau", "id": "Master:10901"},
"attributes": [{"isPlanned": true, "types": ["REALTIME", "ACCURATE"]}]
}
],
"filter": [
{
"serviceID": "HHA-U:U1_HHA-U",
"stationIDs": ["Master:10902"],
"label": "Fuhlsbüttel Nord / Ochsenzoll / Norderstedt Mitte / Kellinghusenstraße / Ohlsdorf / Garstedt",
"serviceName": "U1"
},
{
"serviceID": "HHA-U:U1_HHA-U",
"stationIDs": ["Master:60904"],
"label": "Volksdorf / Farmsen / Großhansdorf / Ohlstedt",
"serviceName": "U1"
},
{
"serviceID": "HHA-B:25_HHA-B",
"stationIDs": ["Master:10047"],
"label": "Sachsenstraße / U Burgstraße",
"serviceName": "25"
},
{
"serviceID": "HHA-B:25_HHA-B",
"stationIDs": ["Master:60029"],
"label": "Winterhuder Marktplatz / Bf. Altona",
"serviceName": "25"
},
{
"serviceID": "HHA-B:36_HHA-B",
"stationIDs": ["Master:10049"],
"label": "S Blankenese / Rathausmarkt",
"serviceName": "36"
},
{
"serviceID": "HHA-B:36_HHA-B",
"stationIDs": ["Master:60013"],
"label": "Berner Heerweg",
"serviceName": "36"
},
{
"serviceID": "HHA-B:606_HHA-B",
"stationIDs": ["Master:10047"],
"label": "S Landwehr (Ramazan-Avci-Platz) - Rathausmarkt",
"serviceName": "606"
},
{
"serviceID": "HHA-B:606_HHA-B",
"stationIDs": ["Master:60029"],
"label": "Uferstraße - Winterhuder Marktplatz / Uferstraße - S Hamburg Airport / Uferstraße - U Langenhorn Markt (Krohnstieg)",
"serviceName": "606"
},
{
"serviceID": "HHA-B:608_HHA-B",
"stationIDs": ["Master:10048"],
"label": "Rathausmarkt / S Reeperbahn",
"serviceName": "608"
},
{
"serviceID": "HHA-B:608_HHA-B",
"stationIDs": ["Master:60012"],
"label": "Bf. Rahlstedt (Amtsstraße) / Großlohe",
"serviceName": "608"
}
],
"serviceTypes": ["UBAHN", "BUS", "METROBUS", "SCHNELLBUS", "NACHTBUS"]
}
+10
View File
@@ -0,0 +1,10 @@
{
"returnCode": "OK",
"beginOfService": "04.06.2020",
"endOfService": "13.12.2020",
"id": "1.80.0",
"dataId": "32.55.01",
"buildDate": "04.06.2020",
"buildTime": "14:29:59",
"buildText": "Regelfahrplan 2020"
}
+12
View File
@@ -0,0 +1,12 @@
{
"filter": [
{
"label": "S Landwehr (Ramazan-Avci-Platz) - Rathausmarkt",
"serviceID": "HHA-B:606_HHA-B",
"serviceName": "606",
"stationIDs": ["Master:10047"]
}
],
"offset": 10,
"realtime": true
}
+32
View File
@@ -0,0 +1,32 @@
{
"returnCode": "OK",
"partialStations": [
{
"stationOutline": "http://www.geofox.de/images/mobi/stationDescriptions/U_Wartenau.ZM3.jpg",
"elevators": [
{
"label": "A",
"cabinWidth": 124,
"cabinLength": 147,
"doorWidth": 110,
"description": "Zugang Landwehr <-> Schalterhalle",
"elevatorType": "Durchlader",
"buttonType": "BRAILLE",
"state": "READY"
},
{
"lines": ["U1"],
"label": "B",
"cabinWidth": 123,
"cabinLength": 145,
"doorWidth": 90,
"description": "Schalterhalle <-> U1",
"elevatorType": "Durchlader",
"buttonType": "COMBI",
"state": "READY"
}
]
}
],
"lastUpdate": {"date": "26.01.2020", "time": "22:49"}
}