async def validate_input(self, sensor_api: str, station: str) -> str: """Validate input from user input.""" web_session = async_get_clientsession(self.hass) weather_api = TrafikverketWeather(web_session, sensor_api) try: await weather_api.async_get_weather(station) except ValueError as err: return str(err) return "connected"
def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None: """Initialize the Sensibo coordinator.""" super().__init__( hass, _LOGGER, name=DOMAIN, update_interval=TIME_BETWEEN_UPDATES, ) self._weather_api = TrafikverketWeather(async_get_clientsession(hass), entry.data[CONF_API_KEY]) self._station = entry.data[CONF_STATION]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> None: """Set up the Trafikverket sensor entry.""" web_session = async_get_clientsession(hass) weather_api = TrafikverketWeather(web_session, entry.data[CONF_API_KEY]) entities = [ TrafikverketWeatherStation(weather_api, entry.entry_id, entry.data[CONF_STATION], description) for description in SENSOR_TYPES ] async_add_entities(entities, True)
async def async_setup_platform( hass, config, async_add_entities, discovery_info=None): """Set up the Trafikverket sensor platform.""" from pytrafikverket.trafikverket_weather import TrafikverketWeather sensor_name = config[CONF_NAME] sensor_api = config[CONF_API_KEY] sensor_station = config[CONF_STATION] web_session = async_get_clientsession(hass) weather_api = TrafikverketWeather(web_session, sensor_api) dev = [] for condition in config[CONF_MONITORED_CONDITIONS]: dev.append(TrafikverketWeatherStation( weather_api, sensor_name, condition, sensor_station)) if dev: async_add_entities(dev, True)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the Trafikverket sensor platform.""" sensor_name = config[CONF_NAME] sensor_api = config[CONF_API_KEY] sensor_station = config[CONF_STATION] web_session = async_get_clientsession(hass) weather_api = TrafikverketWeather(web_session, sensor_api) monitored_conditions = config[CONF_MONITORED_CONDITIONS] entities = [ TrafikverketWeatherStation( weather_api, sensor_name, sensor_station, description ) for description in SENSOR_TYPES if description.key in monitored_conditions ] async_add_entities(entities, True)
async def async_main(loop): """Set up function to handle input and get data to present.""" async with aiohttp.ClientSession(loop=loop) as session: parser = argparse.ArgumentParser( description="CLI used to get data from trafikverket") parser.add_argument("-key", type=str, required=True) parser.add_argument( "-method", required=True, choices=( SEARCH_FOR_STATION, GET_TRAIN_STOP, GET_NEXT_TRAIN_STOP, GET_WEATHER, GET_FERRY_ROUTE, SEARCH_FOR_FERRY_ROUTE, GET_NEXT_FERRY_STOP, ), ) parser.add_argument("-station", type=str) parser.add_argument("-from-station", type=str) parser.add_argument("-to-station", type=str) parser.add_argument("-date-time", type=str) parser.add_argument("-route", type=str) parser.add_argument("-from-harbor", type=str) parser.add_argument("-to-harbor", type=str) args = parser.parse_args() train_api = TrafikverketTrain(session, args.key) weather_api = TrafikverketWeather(session, args.key) ferry_api = TrafikverketFerry(session, args.key) with async_timeout.timeout(10): if args.method == SEARCH_FOR_STATION: if args.station is None: raise ValueError("-station is required") stations = await train_api.async_search_train_stations( args.station) for station in stations: print(station.name + " " + station.signature) elif args.method == GET_TRAIN_STOP: from_station = await train_api.async_get_train_station( args.from_station) to_station = await train_api.async_get_train_station( args.to_station) print("from_station_signature: " + from_station.signature) print("to_station_signature: " + to_station.signature) time = datetime.strptime(args.date_time, Trafikverket.date_time_format) train_stop = await train_api.async_get_train_stop( from_station, to_station, time) print_values(train_stop) elif args.method == GET_NEXT_TRAIN_STOP: from_station = await train_api.async_get_train_station( args.from_station) to_station = await train_api.async_get_train_station( args.to_station) print("from_station_signature: " + from_station.signature) print("to_station_signature: " + to_station.signature) if args.date_time is not None: time = datetime.strptime(args.date_time, Trafikverket.date_time_format) else: time = datetime.now() train_stop = await train_api.async_get_next_train_stop( from_station, to_station, time) print_values(train_stop) elif args.method == GET_WEATHER: if args.station is None: raise ValueError( '-station is required with name of Weather station\ (ex. -station "Nöbbele")') weather = await weather_api.async_get_weather(args.station) print_values(weather) elif args.method == GET_FERRY_ROUTE: if args.route is None: raise ValueError( '-route is required with name of Ferry route\ (ex. -route "Ekeröleden")') route = await ferry_api.async_get_ferry_route(args.route) print_values(route) elif args.method == SEARCH_FOR_FERRY_ROUTE: if args.route is None: raise ValueError("-route is required") routes = await ferry_api.async_search_ferry_routes(args.route) for route in routes: print_values(route) print(route.name + " " + route.id) elif args.method == GET_NEXT_FERRY_STOP: if args.from_harbor is None: raise ValueError( '-from-harbor is required with name of Ferry harbor\ (ex. -from-harbor "Ekerö")') if args.date_time is not None: time = datetime.strptime(args.date_time, Trafikverket.date_time_format) else: time = datetime.now() ferry_stop = await ferry_api.async_get_next_ferry_stop( args.from_harbor, args.to_harbor, time) print_values(ferry_stop)
async def async_main(loop): async with aiohttp.ClientSession(loop=loop) as session: #parse arguments! parser = argparse.ArgumentParser( description="CLI used to get data from trafikverket") parser.add_argument("-key", type=str) parser.add_argument("-method", choices=(SEARCH_FOR_STATION, GET_TRAIN_STOP, GET_NEXT_TRAIN_STOP, GET_WEATHER)) parser.add_argument("-station", type=str) parser.add_argument("-from-station", type=str) parser.add_argument("-to-station", type=str) parser.add_argument("-date-time", type=str) args = parser.parse_args() train_api = TrafikverketTrain(session, args.key) weather_api = TrafikverketWeather(session, args.key) with async_timeout.timeout(10): if args.method == SEARCH_FOR_STATION: if args.station is None: raise ValueError("-station is required") stations = await train_api.async_search_train_stations( args.station) for station in stations: print(station.name + " " + station.signature) elif args.method == GET_TRAIN_STOP: from_station = await train_api.async_get_train_station( args.from_station) to_station = await train_api.async_get_train_station( args.to_station) print("from_station_signature: " + from_station.signature) print("to_station_signature: " + to_station.signature) time = datetime.strptime(args.date_time, Trafikverket.date_time_format) train_stop = await train_api.async_get_train_stop( from_station, to_station, time) print("id: %s, canceled: %s, advertised time: %s, " % (train_stop.id, train_stop.canceled, train_stop.advertised_time_at_location) + "estimated time: %s, time: %s, state: %s" % (train_stop.estimated_time_at_location, train_stop.time_at_location, train_stop.get_state())) elif args.method == GET_NEXT_TRAIN_STOP: from_station = await train_api.async_get_train_station( args.from_station) to_station = await train_api.async_get_train_station( args.to_station) print("from_station_signature: " + from_station.signature) print("to_station_signature: " + to_station.signature) if args.date_time is not None: time = datetime.strptime(args.date_time, Trafikverket.date_time_format) else: time = datetime.now() train_stop = await train_api.async_get_next_train_stop( from_station, to_station, time) print("id: %s, canceled: %s, advertised time: %s, " % (train_stop.id, train_stop.canceled, train_stop.advertised_time_at_location) + "estimated time: %s, time: %s, state: %s" % (train_stop.estimated_time_at_location, train_stop.time_at_location, train_stop.get_state())) elif args.method == GET_WEATHER: if args.station is None: raise ValueError( "-station is required with name of Weather station (ex. -station \"Nöbbele\")" ) weather = await weather_api.async_get_weather(args.station) print( "Name: %s, id: %s, road temp: %s , air temp: %s, " % (weather.station_name, weather.station_id, weather.road_temp, weather.air_temp) + "humidity: %s, precipitation: %s, wind direction %s degrees / " % (weather.humidity, weather.precipitationtype, weather.winddirection) + "%s, wind force: %s m/s (10 min avg)" % (weather.winddirectiontext, weather.windforce))