Esempio n. 1
0
    def wind_speed(self) -> float | None:
        """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_speed(speed_km_h, SPEED_KILOMETERS_PER_HOUR,
                                   SPEED_MILES_PER_HOUR)
        return int(round(speed_mi_h))
Esempio n. 2
0
    def wind_speed(self):
        """Return the wind speed."""
        speed_m_s = self.coordinator.data.current_weather_data.get(
            "wind_speed")
        if self._is_metric or speed_m_s is None:
            return speed_m_s

        speed_mi_h = convert_speed(speed_m_s, SPEED_METERS_PER_SECOND,
                                   SPEED_MILES_PER_HOUR)
        return int(round(speed_mi_h))
Esempio n. 3
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_speed(wind_km_hr, SPEED_KILOMETERS_PER_HOUR,
                                 SPEED_MILES_PER_HOUR)
        return round(wind)
Esempio n. 4
0
 def native_value(self):
     """Return the state."""
     value = self._nws.observation.get(self.entity_description.key)
     if value is None:
         return None
     # Set alias to unit property -> prevent unnecessary hasattr calls
     unit_of_measurement = self.native_unit_of_measurement
     if unit_of_measurement == SPEED_MILES_PER_HOUR:
         return round(
             convert_speed(value, SPEED_KILOMETERS_PER_HOUR,
                           SPEED_MILES_PER_HOUR))
     if unit_of_measurement == LENGTH_MILES:
         return round(convert_distance(value, LENGTH_METERS, LENGTH_MILES))
     if unit_of_measurement == PRESSURE_INHG:
         return round(convert_pressure(value, PRESSURE_PA, PRESSURE_INHG),
                      2)
     if unit_of_measurement == TEMP_CELSIUS:
         return round(value, 1)
     if unit_of_measurement == PERCENTAGE:
         return round(value)
     return value
Esempio n. 5
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_speed(wind_speed, SPEED_MILES_PER_HOUR,
                                      SPEED_KILOMETERS_PER_HOUR))
                else:
                    data[ATTR_FORECAST_WIND_SPEED] = round(wind_speed)
            else:
                data[ATTR_FORECAST_WIND_SPEED] = None
            forecast.append(data)
        return forecast
Esempio n. 6
0
async def test_wind_speed_conversion(
    hass,
    enable_custom_integrations,
    unit_system,
):
    """Test wind speed conversion."""
    hass.config.units = unit_system
    native_value = 10
    native_unit = SPEED_METERS_PER_SECOND

    entity0 = await create_entity(
        hass, wind_speed=native_value, wind_speed_unit=native_unit
    )

    state = hass.states.get(entity0.entity_id)
    forecast = state.attributes[ATTR_FORECAST][0]

    expected = convert_speed(native_value, native_unit, unit_system.wind_speed_unit)
    assert float(state.attributes[ATTR_WEATHER_WIND_SPEED]) == approx(
        expected, rel=1e-2
    )
    assert float(forecast[ATTR_FORECAST_WIND_SPEED]) == approx(expected, rel=1e-2)
Esempio n. 7
0
async def test_wind_speed(
    hass: HomeAssistant,
    enable_custom_integrations,
    native_unit: str,
    state_unit: str,
    unit_system,
):
    """Test wind speed."""
    hass.config.units = unit_system
    native_value = 10
    state_value = convert_speed(native_value, native_unit, state_unit)

    entity0 = await create_entity(hass,
                                  native_wind_speed=native_value,
                                  native_wind_speed_unit=native_unit)

    state = hass.states.get(entity0.entity_id)
    forecast = state.attributes[ATTR_FORECAST][0]

    expected = state_value
    assert float(state.attributes[ATTR_WEATHER_WIND_SPEED]) == approx(expected,
                                                                      rel=1e-2)
    assert float(forecast[ATTR_FORECAST_WIND_SPEED]) == approx(expected,
                                                               rel=1e-2)
Esempio n. 8
0
File: const.py Progetto: jbouwh/core
    "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_speed(10, SPEED_KILOMETERS_PER_HOUR, SPEED_MILES_PER_HOUR))
    ),
    "windGust": str(
        round(convert_speed(20, SPEED_KILOMETERS_PER_HOUR, SPEED_MILES_PER_HOUR))
    ),
    "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 = {
Esempio n. 9
0
async def test_backwards_compatibility_convert_values(
        hass: HomeAssistant, enable_custom_integrations) -> None:
    """Test backward compatibility for converting values."""
    wind_speed_value = 5
    wind_speed_unit = SPEED_METERS_PER_SECOND
    pressure_value = 110000
    pressure_unit = PRESSURE_PA
    temperature_value = 20
    temperature_unit = TEMP_CELSIUS
    visibility_value = 11
    visibility_unit = LENGTH_KILOMETERS
    precipitation_value = 1
    precipitation_unit = LENGTH_MILLIMETERS

    hass.config.units = IMPERIAL_SYSTEM

    platform: WeatherPlatform = getattr(hass.components, "test.weather")
    platform.init(empty=True)
    platform.ENTITIES.append(
        platform.MockWeatherMockForecastCompat(
            name="Test",
            condition=ATTR_CONDITION_SUNNY,
            temperature=temperature_value,
            temperature_unit=temperature_unit,
            wind_speed=wind_speed_value,
            wind_speed_unit=wind_speed_unit,
            pressure=pressure_value,
            pressure_unit=pressure_unit,
            visibility=visibility_value,
            visibility_unit=visibility_unit,
            precipitation=precipitation_value,
            precipitation_unit=precipitation_unit,
            unique_id="very_unique",
        ))

    entity0 = platform.ENTITIES[0]
    assert await async_setup_component(hass, "weather",
                                       {"weather": {
                                           "platform": "test"
                                       }})
    await hass.async_block_till_done()

    state = hass.states.get(entity0.entity_id)

    expected_wind_speed = round(
        convert_speed(wind_speed_value, wind_speed_unit, SPEED_MILES_PER_HOUR),
        ROUNDING_PRECISION,
    )
    expected_temperature = convert_temperature(temperature_value,
                                               temperature_unit,
                                               TEMP_FAHRENHEIT)
    expected_pressure = round(
        convert_pressure(pressure_value, pressure_unit, PRESSURE_INHG),
        ROUNDING_PRECISION,
    )
    expected_visibility = round(
        convert_distance(visibility_value, visibility_unit, LENGTH_MILES),
        ROUNDING_PRECISION,
    )
    expected_precipitation = round(
        convert_distance(precipitation_value, precipitation_unit,
                         LENGTH_INCHES),
        ROUNDING_PRECISION,
    )

    assert state.attributes == {
        ATTR_FORECAST: [{
            ATTR_FORECAST_PRECIPITATION:
            approx(expected_precipitation, rel=0.1),
            ATTR_FORECAST_PRESSURE:
            approx(expected_pressure, rel=0.1),
            ATTR_FORECAST_TEMP:
            approx(expected_temperature, rel=0.1),
            ATTR_FORECAST_TEMP_LOW:
            approx(expected_temperature, rel=0.1),
            ATTR_FORECAST_WIND_BEARING:
            None,
            ATTR_FORECAST_WIND_SPEED:
            approx(expected_wind_speed, rel=0.1),
        }],
        ATTR_FRIENDLY_NAME:
        "Test",
        ATTR_WEATHER_PRECIPITATION_UNIT:
        LENGTH_INCHES,
        ATTR_WEATHER_PRESSURE:
        approx(expected_pressure, rel=0.1),
        ATTR_WEATHER_PRESSURE_UNIT:
        PRESSURE_INHG,
        ATTR_WEATHER_TEMPERATURE:
        approx(expected_temperature, rel=0.1),
        ATTR_WEATHER_TEMPERATURE_UNIT:
        TEMP_FAHRENHEIT,
        ATTR_WEATHER_VISIBILITY:
        approx(expected_visibility, rel=0.1),
        ATTR_WEATHER_VISIBILITY_UNIT:
        LENGTH_MILES,
        ATTR_WEATHER_WIND_SPEED:
        approx(expected_wind_speed, rel=0.1),
        ATTR_WEATHER_WIND_SPEED_UNIT:
        SPEED_MILES_PER_HOUR,
    }
Esempio n. 10
0
async def test_custom_units(hass: HomeAssistant,
                            enable_custom_integrations) -> None:
    """Test custom unit."""
    wind_speed_value = 5
    wind_speed_unit = SPEED_METERS_PER_SECOND
    pressure_value = 110
    pressure_unit = PRESSURE_HPA
    temperature_value = 20
    temperature_unit = TEMP_CELSIUS
    visibility_value = 11
    visibility_unit = LENGTH_KILOMETERS
    precipitation_value = 1.1
    precipitation_unit = LENGTH_MILLIMETERS

    set_options = {
        "wind_speed_unit": SPEED_MILES_PER_HOUR,
        "precipitation_unit": LENGTH_INCHES,
        "pressure_unit": PRESSURE_INHG,
        "temperature_unit": TEMP_FAHRENHEIT,
        "visibility_unit": LENGTH_MILES,
    }

    entity_registry = er.async_get(hass)

    entry = entity_registry.async_get_or_create("weather", "test",
                                                "very_unique")
    entity_registry.async_update_entity_options(entry.entity_id, "weather",
                                                set_options)
    await hass.async_block_till_done()

    platform: WeatherPlatform = getattr(hass.components, "test.weather")
    platform.init(empty=True)
    platform.ENTITIES.append(
        platform.MockWeatherMockForecast(
            name="Test",
            condition=ATTR_CONDITION_SUNNY,
            native_temperature=temperature_value,
            native_temperature_unit=temperature_unit,
            native_wind_speed=wind_speed_value,
            native_wind_speed_unit=wind_speed_unit,
            native_pressure=pressure_value,
            native_pressure_unit=pressure_unit,
            native_visibility=visibility_value,
            native_visibility_unit=visibility_unit,
            native_precipitation=precipitation_value,
            native_precipitation_unit=precipitation_unit,
            unique_id="very_unique",
        ))

    entity0 = platform.ENTITIES[0]
    assert await async_setup_component(hass, "weather",
                                       {"weather": {
                                           "platform": "test"
                                       }})
    await hass.async_block_till_done()

    state = hass.states.get(entity0.entity_id)
    forecast = state.attributes[ATTR_FORECAST][0]

    expected_wind_speed = round(
        convert_speed(wind_speed_value, wind_speed_unit, SPEED_MILES_PER_HOUR),
        ROUNDING_PRECISION,
    )
    expected_temperature = convert_temperature(temperature_value,
                                               temperature_unit,
                                               TEMP_FAHRENHEIT)
    expected_pressure = round(
        convert_pressure(pressure_value, pressure_unit, PRESSURE_INHG),
        ROUNDING_PRECISION,
    )
    expected_visibility = round(
        convert_distance(visibility_value, visibility_unit, LENGTH_MILES),
        ROUNDING_PRECISION,
    )
    expected_precipitation = round(
        convert_distance(precipitation_value, precipitation_unit,
                         LENGTH_INCHES),
        ROUNDING_PRECISION,
    )

    assert float(state.attributes[ATTR_WEATHER_WIND_SPEED]) == approx(
        expected_wind_speed)
    assert float(state.attributes[ATTR_WEATHER_TEMPERATURE]) == approx(
        expected_temperature, rel=0.1)
    assert float(
        state.attributes[ATTR_WEATHER_PRESSURE]) == approx(expected_pressure)
    assert float(state.attributes[ATTR_WEATHER_VISIBILITY]) == approx(
        expected_visibility)
    assert float(forecast[ATTR_FORECAST_PRECIPITATION]) == approx(
        expected_precipitation, rel=1e-2)

    assert (state.attributes[ATTR_WEATHER_PRECIPITATION_UNIT] ==
            set_options["precipitation_unit"])
    assert state.attributes[ATTR_WEATHER_PRESSURE_UNIT] == set_options[
        "pressure_unit"]
    assert (state.attributes[ATTR_WEATHER_TEMPERATURE_UNIT] ==
            set_options["temperature_unit"])
    assert (state.attributes[ATTR_WEATHER_VISIBILITY_UNIT] ==
            set_options["visibility_unit"])
    assert (state.attributes[ATTR_WEATHER_WIND_SPEED_UNIT] ==
            set_options["wind_speed_unit"])