예제 #1
0
 async def validate_input(self, api_key: str, train_from: str,
                          train_to: str) -> None:
     """Validate input from user input."""
     web_session = async_get_clientsession(self.hass)
     train_api = TrafikverketTrain(web_session, api_key)
     await train_api.async_get_train_station(train_from)
     await train_api.async_get_train_station(train_to)
예제 #2
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up Trafikverket Train from a config entry."""

    http_session = async_get_clientsession(hass)
    train_api = TrafikverketTrain(http_session, entry.data[CONF_API_KEY])

    try:
        to_station = await train_api.async_get_train_station(
            entry.data[CONF_TO])
        from_station = await train_api.async_get_train_station(
            entry.data[CONF_FROM])
    except ValueError as error:
        if "Invalid authentication" in error.args[0]:
            raise ConfigEntryAuthFailed from error
        raise ConfigEntryNotReady(
            f"Problem when trying station {entry.data[CONF_FROM]} to {entry.data[CONF_TO]}. Error: {error} "
        ) from error

    hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
        CONF_TO: to_station,
        CONF_FROM: from_station,
        "train_api": train_api,
    }

    await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

    return True
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
    """Setup the departure sensor."""
    _LOGGER.debug("start async_setup_platform")
    from pytrafikverket import TrafikverketTrain
    httpsession = async_get_clientsession(hass)
    train_api = TrafikverketTrain(httpsession, config.get(CONF_API_KEY))
    sensors = []
    for train in config.get(CONF_TRAINS):
        from_sig = yield from train_api.async_get_train_station(train.get(CONF_FROM))
        to_sig = yield from train_api.async_get_train_station(train.get(CONF_TO))
        sensor = TrainSensor(train_api,
                              train.get(CONF_NAME),
                              from_sig,
                              to_sig,
                              train.get(CONF_TIME))
        #yield from sensor.async_update()
        sensors.append(sensor)

    # For some reason the function below is never called with `yield from` in the code base?
    async_add_devices(sensors, update_before_add=True)
    _LOGGER.debug("end async_setup_platform")
예제 #4
0
파일: sensor.py 프로젝트: 2Fake/core
async def async_setup_platform(
    hass: HomeAssistant,
    config: ConfigType,
    async_add_entities: AddEntitiesCallback,
    discovery_info: DiscoveryInfoType | None = None,
) -> None:
    """Set up the departure sensor."""
    httpsession = async_get_clientsession(hass)
    train_api = TrafikverketTrain(httpsession, config[CONF_API_KEY])
    sensors = []
    station_cache = {}
    for train in config[CONF_TRAINS]:
        try:
            trainstops = [train[CONF_FROM], train[CONF_TO]]
            for station in trainstops:
                if station not in station_cache:
                    station_cache[
                        station] = await train_api.async_get_train_station(
                            station)

        except ValueError as station_error:
            if "Invalid authentication" in station_error.args[0]:
                _LOGGER.error("Unable to set up up component: %s",
                              station_error)
                return
            _LOGGER.error(
                "Problem when trying station %s to %s. Error: %s ",
                train[CONF_FROM],
                train[CONF_TO],
                station_error,
            )
            continue

        sensor = TrainSensor(
            train_api,
            train[CONF_NAME],
            station_cache[train[CONF_FROM]],
            station_cache[train[CONF_TO]],
            train[CONF_WEEKDAY],
            train.get(CONF_TIME),
        )
        sensors.append(sensor)

    async_add_entities(sensors, update_before_add=True)
예제 #5
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry,
                            async_add_entities: AddEntitiesCallback) -> None:
    """Set up the Trafikverket sensor entry."""

    httpsession = async_get_clientsession(hass)
    train_api = TrafikverketTrain(httpsession, entry.data[CONF_API_KEY])

    try:
        to_station = await train_api.async_get_train_station(
            entry.data[CONF_TO])
        from_station = await train_api.async_get_train_station(
            entry.data[CONF_FROM])
    except ValueError as error:
        if "Invalid authentication" in error.args[0]:
            raise ConfigEntryAuthFailed from error
        raise ConfigEntryNotReady(
            f"Problem when trying station {entry.data[CONF_FROM]} to {entry.data[CONF_TO]}. Error: {error} "
        ) from error

    train_time = (dt.parse_time(entry.data.get(CONF_TIME, ""))
                  if entry.data.get(CONF_TIME) else None)

    async_add_entities(
        [
            TrainSensor(
                train_api,
                entry.data[CONF_NAME],
                from_station,
                to_station,
                entry.data[CONF_WEEKDAY],
                train_time,
                entry.entry_id,
            )
        ],
        True,
    )