mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 03:37:07 +00:00
Move data instance to setup (#11350)
This commit is contained in:
parent
2e582a4597
commit
3203849b60
@ -5,17 +5,17 @@ For more details about this platform, please refer to the documentation at
|
|||||||
https://home-assistant.io/components/sensor.swiss_public_transport/
|
https://home-assistant.io/components/sensor.swiss_public_transport/
|
||||||
"""
|
"""
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
|
||||||
import homeassistant.util.dt as dt_util
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
from homeassistant.const import CONF_NAME, ATTR_ATTRIBUTION
|
from homeassistant.const import ATTR_ATTRIBUTION, CONF_NAME
|
||||||
from homeassistant.helpers.entity import Entity
|
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.helpers.entity import Entity
|
||||||
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
REQUIREMENTS = ['python_opendata_transport==0.0.3']
|
REQUIREMENTS = ['python_opendata_transport==0.0.3']
|
||||||
|
|
||||||
@ -51,36 +51,36 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||||
"""Set up the Swiss public transport sensor."""
|
"""Set up the Swiss public transport sensor."""
|
||||||
|
from opendata_transport import OpendataTransport, exceptions
|
||||||
|
|
||||||
name = config.get(CONF_NAME)
|
name = config.get(CONF_NAME)
|
||||||
start = config.get(CONF_START)
|
start = config.get(CONF_START)
|
||||||
destination = config.get(CONF_DESTINATION)
|
destination = config.get(CONF_DESTINATION)
|
||||||
|
|
||||||
connection = SwissPublicTransportSensor(hass, start, destination, name)
|
session = async_get_clientsession(hass)
|
||||||
yield from connection.async_update()
|
opendata = OpendataTransport(start, destination, hass.loop, session)
|
||||||
|
|
||||||
if connection.state is None:
|
try:
|
||||||
|
yield from opendata.async_get_data()
|
||||||
|
except exceptions.OpendataTransportError:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
"Check at http://transport.opendata.ch/examples/stationboard.html "
|
"Check at http://transport.opendata.ch/examples/stationboard.html "
|
||||||
"if your station names are valid")
|
"if your station names are valid")
|
||||||
return False
|
return
|
||||||
|
|
||||||
async_add_devices([connection])
|
async_add_devices(
|
||||||
|
[SwissPublicTransportSensor(opendata, start, destination, name)])
|
||||||
|
|
||||||
|
|
||||||
class SwissPublicTransportSensor(Entity):
|
class SwissPublicTransportSensor(Entity):
|
||||||
"""Implementation of an Swiss public transport sensor."""
|
"""Implementation of an Swiss public transport sensor."""
|
||||||
|
|
||||||
def __init__(self, hass, start, destination, name):
|
def __init__(self, opendata, start, destination, name):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
from opendata_transport import OpendataTransport
|
self._opendata = opendata
|
||||||
|
|
||||||
self.hass = hass
|
|
||||||
self._name = name
|
self._name = name
|
||||||
self._from = start
|
self._from = start
|
||||||
self._to = destination
|
self._to = destination
|
||||||
self._websession = async_get_clientsession(self.hass)
|
|
||||||
self._opendata = OpendataTransport(
|
|
||||||
self._from, self._to, self.hass.loop, self._websession)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -131,4 +131,3 @@ class SwissPublicTransportSensor(Entity):
|
|||||||
yield from self._opendata.async_get_data()
|
yield from self._opendata.async_get_data()
|
||||||
except OpendataTransportError:
|
except OpendataTransportError:
|
||||||
_LOGGER.error("Unable to retrieve data from transport.opendata.ch")
|
_LOGGER.error("Unable to retrieve data from transport.opendata.ch")
|
||||||
self._opendata = None
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user