Exemple #1
0
async def test_nws_stations_observations(aiohttp_client, loop, mock_urls):
    app = setup_app()
    client = await aiohttp_client(app)
    nws = Nws(client, USERID, LATLON)
    assert nws
    with pytest.raises(NwsError):
        stations = await nws.get_stations_observations()
    nws.station = STATION
    observations = await nws.get_stations_observations()
    assert observations
    assert isinstance(observations, list)
Exemple #2
0
async def test_nws_alerts_forecast_zone(aiohttp_client, loop, mock_urls):
    app = setup_app()
    client = await aiohttp_client(app)
    nws = Nws(client, USERID, LATLON)
    assert nws
    alerts = await nws.get_alerts_forecast_zone()
    assert alerts
    assert isinstance(alerts, list)
Exemple #3
0
async def test_nws_points_stations(aiohttp_client, loop, mock_urls):
    app = setup_app()
    client = await aiohttp_client(app)
    nws = Nws(client, USERID, LATLON)
    assert nws
    stations = await nws.get_points_stations()
    assert stations
    assert isinstance(stations, list)
Exemple #4
0
async def test_nws_gridpoints_forecast_hourly(aiohttp_client, loop, mock_urls):
    app = setup_app()
    client = await aiohttp_client(app)
    nws = Nws(client, USERID, LATLON)
    assert nws
    forecast = await nws.get_gridpoints_forecast_hourly()
    assert nws.wfo
    assert forecast
    assert isinstance(forecast, list)
Exemple #5
0
async def async_setup_platform(hass,
                               config,
                               async_add_entities,
                               discovery_info=None):
    """Set up the NWS weather platform."""
    from pynws import Nws
    from metar import Metar
    latitude = config.get(CONF_LATITUDE, hass.config.latitude)
    longitude = config.get(CONF_LONGITUDE, hass.config.longitude)
    station = config.get(CONF_STATION)
    api_key = config[CONF_API_KEY]

    if None in (latitude, longitude):
        _LOGGER.error("Latitude/longitude not set in Home Assistant config")
        return ConfigEntryNotReady

    websession = async_get_clientsession(hass)
    # ID request as being from HA, pynws prepends the api_key in addition
    api_key_ha = '{} {}'.format(api_key, 'homeassistant')
    nws = Nws(websession,
              latlon=(float(latitude), float(longitude)),
              userid=api_key_ha)

    _LOGGER.debug("Setting up station: %s", station)
    if station is None:
        try:
            with async_timeout.timeout(10, loop=hass.loop):
                stations = await nws.stations()
        except ERRORS as status:
            _LOGGER.error("Error getting station list for %s: %s",
                          (latitude, longitude), status)
            raise PlatformNotReady
        _LOGGER.debug("Station list: %s", stations)
        nws.station = stations[0]
        _LOGGER.debug("Initialized for coordinates %s, %s -> station %s",
                      latitude, longitude, stations[0])
    else:
        nws.station = station
        _LOGGER.debug("Initialized station %s", station[0])

    async_add_entities(
        [NWSWeather(nws, Metar.Metar, hass.config.units, config)], True)
Exemple #6
0
async def test_nws_points(aiohttp_client, loop, mock_urls):
    app = setup_app()
    client = await aiohttp_client(app)
    nws = Nws(client, USERID, LATLON)
    assert nws
    points = await nws.get_points()
    assert points
    assert nws.wfo
    assert nws.x
    assert nws.y
    assert nws.forecast_zone
    assert nws.county_zone
    assert nws.fire_weather_zone