Exemple #1
0
    def visibility(self):
        """Return visibility."""
        vis_m = None
        if self.observation:
            vis_m = self.observation.get("visibility")
        if vis_m is None:
            return None

        if self.is_metric:
            vis = convert_distance(vis_m, LENGTH_METERS, LENGTH_KILOMETERS)
        else:
            vis = convert_distance(vis_m, LENGTH_METERS, LENGTH_MILES)
        return round(vis, 0)
Exemple #2
0
    def set_coordinates(self):
        """Weather data inialization - set the coordinates."""
        if self._config.get(CONF_TRACK_HOME, False):
            latitude = self.opp.config.latitude
            longitude = self.opp.config.longitude
            elevation = self.opp.config.elevation
        else:
            latitude = self._config[CONF_LATITUDE]
            longitude = self._config[CONF_LONGITUDE]
            elevation = self._config[CONF_ELEVATION]

        if not self._is_metric:
            elevation = int(
                round(convert_distance(elevation, LENGTH_FEET, LENGTH_METERS))
            )

        coordinates = {
            "lat": str(latitude),
            "lon": str(longitude),
            "msl": str(elevation),
        }
        if coordinates == self._coordinates:
            return False
        self._coordinates = coordinates

        self._weather_data = metno.MetWeatherData(
            coordinates, async_get_clientsession(self.opp), api_url=URL
        )
        return True
Exemple #3
0
 def forecast(self):
     """Return the forecast array."""
     if self._hourly:
         met_forecast = self.coordinator.data.hourly_forecast
     else:
         met_forecast = self.coordinator.data.daily_forecast
     required_keys = {ATTR_FORECAST_TEMP, ATTR_FORECAST_TIME}
     ha_forecast = []
     for met_item in met_forecast:
         if not set(met_item).issuperset(required_keys):
             continue
         ha_item = {
             k: met_item[v]
             for k, v in FORECAST_MAP.items()
             if met_item.get(v) is not None
         }
         if not self._is_metric and ATTR_FORECAST_PRECIPITATION in ha_item:
             precip_inches = convert_distance(
                 ha_item[ATTR_FORECAST_PRECIPITATION],
                 LENGTH_MILLIMETERS,
                 LENGTH_INCHES,
             )
             ha_item[ATTR_FORECAST_PRECIPITATION] = round(precip_inches, 2)
         if ha_item.get(ATTR_FORECAST_CONDITION):
             ha_item[ATTR_FORECAST_CONDITION] = format_condition(
                 ha_item[ATTR_FORECAST_CONDITION]
             )
         ha_forecast.append(ha_item)
     return ha_forecast
Exemple #4
0
 def state(self):
     """Return the state."""
     value = self._nws.observation.get(self._type)
     if value is None:
         return None
     if self._unit == SPEED_MILES_PER_HOUR:
         return round(
             convert_distance(value, LENGTH_KILOMETERS, LENGTH_MILES))
     if self._unit == LENGTH_MILES:
         return round(convert_distance(value, LENGTH_METERS, LENGTH_MILES))
     if self._unit == PRESSURE_INHG:
         return round(convert_pressure(value, PRESSURE_PA, PRESSURE_INHG),
                      2)
     if self._unit == TEMP_CELSIUS:
         return round(value, 1)
     if self._unit == PERCENTAGE:
         return round(value)
     return value
Exemple #5
0
    def wind_speed(self):
        """Return the wind speed."""
        speed_km_h = self.coordinator.data.current_weather_data.get(
            ATTR_MAP[ATTR_WEATHER_WIND_SPEED]
        )
        if self._is_metric or speed_km_h is None:
            return speed_km_h

        speed_mi_h = convert_distance(speed_km_h, LENGTH_KILOMETERS, LENGTH_MILES)
        return int(round(speed_mi_h))
Exemple #6
0
    def wind_speed(self):
        """Return the current windspeed."""
        wind_km_hr = None
        if self.observation:
            wind_km_hr = self.observation.get("windSpeed")
        if wind_km_hr is None:
            return None

        if self.is_metric:
            wind = wind_km_hr
        else:
            wind = convert_distance(wind_km_hr, LENGTH_KILOMETERS,
                                    LENGTH_MILES)
        return round(wind)
Exemple #7
0
    def forecast(self):
        """Return forecast."""
        if self._forecast is None:
            return None
        forecast = []
        for forecast_entry in self._forecast:
            data = {
                ATTR_FORECAST_DETAILED_DESCRIPTION:
                forecast_entry.get("detailedForecast"),
                ATTR_FORECAST_TEMP:
                forecast_entry.get("temperature"),
                ATTR_FORECAST_TIME:
                forecast_entry.get("startTime"),
            }

            if self.mode == DAYNIGHT:
                data[ATTR_FORECAST_DAYTIME] = forecast_entry.get("isDaytime")
            time = forecast_entry.get("iconTime")
            weather = forecast_entry.get("iconWeather")
            if time and weather:
                cond, precip = convert_condition(time, weather)
            else:
                cond, precip = None, None
            data[ATTR_FORECAST_CONDITION] = cond
            data[ATTR_FORECAST_PRECIPITATION_PROBABILITY] = precip

            data[ATTR_FORECAST_WIND_BEARING] = forecast_entry.get(
                "windBearing")
            wind_speed = forecast_entry.get("windSpeedAvg")
            if wind_speed is not None:
                if self.is_metric:
                    data[ATTR_FORECAST_WIND_SPEED] = round(
                        convert_distance(wind_speed, LENGTH_MILES,
                                         LENGTH_KILOMETERS))
                else:
                    data[ATTR_FORECAST_WIND_SPEED] = round(wind_speed)
            else:
                data[ATTR_FORECAST_WIND_SPEED] = None
            forecast.append(data)
        return forecast
Exemple #8
0
    "relativeHumidity": "10",
    "windSpeed": "10",
    "windGust": "20",
    "windDirection": "180",
    "barometricPressure": "100000",
    "seaLevelPressure": "100000",
    "visibility": "10000",
}

SENSOR_EXPECTED_OBSERVATION_IMPERIAL = {
    "dewpoint": str(round(convert_temperature(5, TEMP_CELSIUS, TEMP_FAHRENHEIT))),
    "temperature": str(round(convert_temperature(10, TEMP_CELSIUS, TEMP_FAHRENHEIT))),
    "windChill": str(round(convert_temperature(5, TEMP_CELSIUS, TEMP_FAHRENHEIT))),
    "heatIndex": str(round(convert_temperature(15, TEMP_CELSIUS, TEMP_FAHRENHEIT))),
    "relativeHumidity": "10",
    "windSpeed": str(round(convert_distance(10, LENGTH_KILOMETERS, LENGTH_MILES))),
    "windGust": str(round(convert_distance(20, LENGTH_KILOMETERS, LENGTH_MILES))),
    "windDirection": "180",
    "barometricPressure": str(
        round(convert_pressure(100000, PRESSURE_PA, PRESSURE_INHG), 2)
    ),
    "seaLevelPressure": str(
        round(convert_pressure(100000, PRESSURE_PA, PRESSURE_INHG), 2)
    ),
    "visibility": str(round(convert_distance(10000, LENGTH_METERS, LENGTH_MILES))),
}

WEATHER_EXPECTED_OBSERVATION_IMPERIAL = {
    ATTR_WEATHER_TEMPERATURE: round(
        convert_temperature(10, TEMP_CELSIUS, TEMP_FAHRENHEIT)
    ),