Update NSAPI to version 3.0.0 (#30599)
* Updated NSAPI to version 3.0.0 * Fixed code style error * Restructured API access * Improved construction of attributes * Fixed handling of API output * Added @YarmoM as code owner * Updated CODEOWNERS * Reverted changes for full backwards compatibility * Fixed bad conditional * Simplify conditionalpull/30713/head
parent
5f5e8d81e1
commit
96bf8bc395
|
@ -219,6 +219,7 @@ homeassistant/components/msteams/* @peroyvind
|
||||||
homeassistant/components/mysensors/* @MartinHjelmare
|
homeassistant/components/mysensors/* @MartinHjelmare
|
||||||
homeassistant/components/mystrom/* @fabaff
|
homeassistant/components/mystrom/* @fabaff
|
||||||
homeassistant/components/neato/* @dshokouhi @Santobert
|
homeassistant/components/neato/* @dshokouhi @Santobert
|
||||||
|
homeassistant/components/nederlandse_spoorwegen/* @YarmoM
|
||||||
homeassistant/components/nello/* @pschmitt
|
homeassistant/components/nello/* @pschmitt
|
||||||
homeassistant/components/ness_alarm/* @nickw444
|
homeassistant/components/ness_alarm/* @nickw444
|
||||||
homeassistant/components/nest/* @awarecan
|
homeassistant/components/nest/* @awarecan
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
"domain": "nederlandse_spoorwegen",
|
"domain": "nederlandse_spoorwegen",
|
||||||
"name": "Nederlandse Spoorwegen (NS)",
|
"name": "Nederlandse Spoorwegen (NS)",
|
||||||
"documentation": "https://www.home-assistant.io/integrations/nederlandse_spoorwegen",
|
"documentation": "https://www.home-assistant.io/integrations/nederlandse_spoorwegen",
|
||||||
"requirements": ["nsapi==2.7.4"],
|
"requirements": ["nsapi==3.0.0"],
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
"codeowners": []
|
"codeowners": ["@YarmoM"]
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import requests
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
from homeassistant.const import ATTR_ATTRIBUTION, CONF_EMAIL, CONF_NAME, CONF_PASSWORD
|
from homeassistant.const import ATTR_ATTRIBUTION, CONF_API_KEY, CONF_NAME
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.util import Throttle
|
||||||
|
@ -37,18 +37,14 @@ ROUTE_SCHEMA = vol.Schema(
|
||||||
ROUTES_SCHEMA = vol.All(cv.ensure_list, [ROUTE_SCHEMA])
|
ROUTES_SCHEMA = vol.All(cv.ensure_list, [ROUTE_SCHEMA])
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{
|
{vol.Required(CONF_API_KEY): cv.string, vol.Optional(CONF_ROUTES): ROUTES_SCHEMA}
|
||||||
vol.Required(CONF_EMAIL): cv.string,
|
|
||||||
vol.Required(CONF_PASSWORD): cv.string,
|
|
||||||
vol.Optional(CONF_ROUTES): ROUTES_SCHEMA,
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the departure sensor."""
|
"""Set up the departure sensor."""
|
||||||
|
|
||||||
nsapi = ns_api.NSAPI(config.get(CONF_EMAIL), config.get(CONF_PASSWORD))
|
nsapi = ns_api.NSAPI(config[CONF_API_KEY])
|
||||||
try:
|
try:
|
||||||
stations = nsapi.get_stations()
|
stations = nsapi.get_stations()
|
||||||
except (
|
except (
|
||||||
|
@ -128,40 +124,59 @@ class NSDepartureSensor(Entity):
|
||||||
for k in self._trips[0].trip_parts:
|
for k in self._trips[0].trip_parts:
|
||||||
route.append(k.destination)
|
route.append(k.destination)
|
||||||
|
|
||||||
return {
|
# Static attributes
|
||||||
|
attributes = {
|
||||||
"going": self._trips[0].going,
|
"going": self._trips[0].going,
|
||||||
"departure_time_planned": self._trips[0].departure_time_planned.strftime(
|
"departure_time_planned": self._trips[0].departure_time_planned.strftime(
|
||||||
"%H:%M"
|
"%H:%M"
|
||||||
),
|
),
|
||||||
"departure_time_actual": self._trips[0].departure_time_actual.strftime(
|
"departure_time_actual": None,
|
||||||
"%H:%M"
|
"departure_delay": False,
|
||||||
),
|
"departure_platform_planned": self._trips[0].departure_platform_planned,
|
||||||
"departure_delay": self._trips[0].departure_time_planned
|
"departure_platform_actual": None,
|
||||||
!= self._trips[0].departure_time_actual,
|
|
||||||
"departure_platform": self._trips[0].trip_parts[0].stops[0].platform,
|
|
||||||
"departure_platform_changed": self._trips[0]
|
|
||||||
.trip_parts[0]
|
|
||||||
.stops[0]
|
|
||||||
.platform_changed,
|
|
||||||
"arrival_time_planned": self._trips[0].arrival_time_planned.strftime(
|
"arrival_time_planned": self._trips[0].arrival_time_planned.strftime(
|
||||||
"%H:%M"
|
"%H:%M"
|
||||||
),
|
),
|
||||||
"arrival_time_actual": self._trips[0].arrival_time_actual.strftime("%H:%M"),
|
"arrival_time_actual": None,
|
||||||
"arrival_delay": self._trips[0].arrival_time_planned
|
"arrival_delay": False,
|
||||||
!= self._trips[0].arrival_time_actual,
|
"arrival_platform_platform": self._trips[0].arrival_platform_planned,
|
||||||
"arrival_platform": self._trips[0].trip_parts[0].stops[-1].platform,
|
"arrival_platform_actual": None,
|
||||||
"arrival_platform_changed": self._trips[0]
|
"next": None,
|
||||||
.trip_parts[0]
|
|
||||||
.stops[-1]
|
|
||||||
.platform_changed,
|
|
||||||
"next": self._trips[1].departure_time_actual.strftime("%H:%M"),
|
|
||||||
"status": self._trips[0].status.lower(),
|
"status": self._trips[0].status.lower(),
|
||||||
"transfers": self._trips[0].nr_transfers,
|
"transfers": self._trips[0].nr_transfers,
|
||||||
"route": route,
|
"route": route,
|
||||||
"remarks": [r.message for r in self._trips[0].trip_remarks],
|
"remarks": None,
|
||||||
ATTR_ATTRIBUTION: ATTRIBUTION,
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Departure attributes
|
||||||
|
if self._trips[0].departure_time_actual is not None:
|
||||||
|
attributes["departure_time_actual"] = self._trips[
|
||||||
|
0
|
||||||
|
].departure_time_actual.strftime("%H:%M")
|
||||||
|
attributes["departure_delay"] = True
|
||||||
|
attributes["departure_platform_actual"] = self._trips[
|
||||||
|
0
|
||||||
|
].departure_platform_actual
|
||||||
|
|
||||||
|
# Arrival attributes
|
||||||
|
if self._trips[0].arrival_time_actual is not None:
|
||||||
|
attributes["arrival_time_actual"] = self._trips[
|
||||||
|
0
|
||||||
|
].arrival_time_actual.strftime("%H:%M")
|
||||||
|
attributes["arrival_delay"] = True
|
||||||
|
attributes["arrival_platform_actual"] = self._trips[
|
||||||
|
0
|
||||||
|
].arrival_platform_actual
|
||||||
|
|
||||||
|
# Next attributes
|
||||||
|
if self._trips[1].departure_time_actual is not None:
|
||||||
|
attributes["next"] = self._trips[1].departure_time_actual.strftime("%H:%M")
|
||||||
|
elif self._trips[1].departure_time_planned is not None:
|
||||||
|
attributes["next"] = self._trips[1].departure_time_planned.strftime("%H:%M")
|
||||||
|
|
||||||
|
return attributes
|
||||||
|
|
||||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the trip information."""
|
"""Get the trip information."""
|
||||||
|
@ -173,10 +188,15 @@ class NSDepartureSensor(Entity):
|
||||||
self._heading,
|
self._heading,
|
||||||
True,
|
True,
|
||||||
0,
|
0,
|
||||||
|
2,
|
||||||
)
|
)
|
||||||
if self._trips:
|
if self._trips:
|
||||||
actual_time = self._trips[0].departure_time_actual
|
if self._trips[0].departure_time_actual is None:
|
||||||
self._state = actual_time.strftime("%H:%M")
|
planned_time = self._trips[0].departure_time_planned
|
||||||
|
self._state = planned_time.strftime("%H:%M")
|
||||||
|
else:
|
||||||
|
actual_time = self._trips[0].departure_time_actual
|
||||||
|
self._state = actual_time.strftime("%H:%M")
|
||||||
except (
|
except (
|
||||||
requests.exceptions.ConnectionError,
|
requests.exceptions.ConnectionError,
|
||||||
requests.exceptions.HTTPError,
|
requests.exceptions.HTTPError,
|
||||||
|
|
|
@ -905,7 +905,7 @@ niko-home-control==0.2.1
|
||||||
niluclient==0.1.2
|
niluclient==0.1.2
|
||||||
|
|
||||||
# homeassistant.components.nederlandse_spoorwegen
|
# homeassistant.components.nederlandse_spoorwegen
|
||||||
nsapi==2.7.4
|
nsapi==3.0.0
|
||||||
|
|
||||||
# homeassistant.components.nsw_fuel_station
|
# homeassistant.components.nsw_fuel_station
|
||||||
nsw-fuel-api-client==1.0.10
|
nsw-fuel-api-client==1.0.10
|
||||||
|
|
Loading…
Reference in New Issue