Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 32 additions & 71 deletions homeassistant/components/nws/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
from homeassistant.const import (
CONF_LATITUDE,
CONF_LONGITUDE,
LENGTH_KILOMETERS,
LENGTH_METERS,
LENGTH_MILES,
PRESSURE_HPA,
PRESSURE_INHG,
PRESSURE_PA,
SPEED_KILOMETERS_PER_HOUR,
SPEED_MILES_PER_HOUR,
Expand All @@ -28,9 +24,7 @@
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.distance import convert as convert_distance
from homeassistant.util.dt import utcnow
from homeassistant.util.pressure import convert as convert_pressure
from homeassistant.util.speed import convert as convert_speed
from homeassistant.util.temperature import convert as convert_temperature

Expand Down Expand Up @@ -100,6 +94,11 @@ async def async_setup_entry(
class NWSWeather(WeatherEntity):
"""Representation of a weather condition."""

_attr_temperature_unit = TEMP_CELSIUS
_attr_pressure_unit = PRESSURE_PA
_attr_wind_speed_unit = SPEED_KILOMETERS_PER_HOUR
_attr_visibility_unit = LENGTH_METERS

def __init__(self, entry_data, hass_data, mode, units):
"""Initialise the platform with a data instance and station name."""
self.nws = hass_data[NWS_DATA]
Expand Down Expand Up @@ -156,67 +155,38 @@ def name(self):

@property
def temperature(self):
"""Return the current temperature."""
temp_c = None
"""Return the current temperature in degrees C."""
if self.observation:
temp_c = self.observation.get("temperature")
if temp_c is not None:
return convert_temperature(temp_c, TEMP_CELSIUS, TEMP_FAHRENHEIT)
return self.observation.get("temperature")
return None

@property
def pressure(self):
"""Return the current pressure."""
pressure_pa = None
"""Return the current pressure in Pa."""
if self.observation:
pressure_pa = self.observation.get("seaLevelPressure")
if pressure_pa is None:
return None
if self.is_metric:
pressure = convert_pressure(pressure_pa, PRESSURE_PA, PRESSURE_HPA)
pressure = round(pressure)
else:
pressure = convert_pressure(pressure_pa, PRESSURE_PA, PRESSURE_INHG)
pressure = round(pressure, 2)
return pressure
return self.observation.get("seaLevelPressure")
return None

@property
def humidity(self):
"""Return the name of the sensor."""
humidity = None
"""Return the current humidity as a percentage."""
if self.observation:
humidity = self.observation.get("relativeHumidity")
return humidity
return self.observation.get("relativeHumidity")
return None

@property
def wind_speed(self):
"""Return the current windspeed."""
wind_km_hr = None
"""Return the current windspeed in km/h."""
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)
return self.observation.get("windSpeed")
return None

@property
def wind_bearing(self):
"""Return the current wind bearing (degrees)."""
wind_bearing = None
if self.observation:
wind_bearing = self.observation.get("windDirection")
return wind_bearing

@property
def temperature_unit(self):
"""Return the unit of measurement."""
return TEMP_FAHRENHEIT
return self.observation.get("windDirection")
return None

@property
def condition(self):
Expand All @@ -233,18 +203,10 @@ def condition(self):

@property
def visibility(self):
"""Return visibility."""
vis_m = None
"""Return visibility in meters."""
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)
return self.observation.get("visibility")
return None

@property
def forecast(self):
Expand All @@ -257,7 +219,6 @@ def forecast(self):
ATTR_FORECAST_DETAILED_DESCRIPTION: forecast_entry.get(
"detailedForecast"
),
ATTR_FORECAST_TEMP: forecast_entry.get("temperature"),
ATTR_FORECAST_TIME: forecast_entry.get("startTime"),
}

Expand All @@ -273,18 +234,18 @@ def forecast(self):
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)
if (temperature := forecast_entry.get("temperature")) is not None:
data[ATTR_FORECAST_TEMP] = convert_temperature(
temperature, TEMP_FAHRENHEIT, TEMP_CELSIUS
)
else:
data[ATTR_FORECAST_WIND_SPEED] = None
data[
ATTR_FORECAST_TEMP
] = None # data[ATTR_FORECAST_TEMP] must be defined
if (wind_speed := forecast_entry.get("windSpeedAvg")) is not None:
data[ATTR_FORECAST_WIND_SPEED] = convert_speed(
wind_speed, SPEED_MILES_PER_HOUR, SPEED_KILOMETERS_PER_HOUR
)
forecast.append(data)
return forecast

Expand Down
27 changes: 13 additions & 14 deletions homeassistant/components/openweathermap/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@

from homeassistant.components.weather import Forecast, WeatherEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PRESSURE_HPA, PRESSURE_INHG, TEMP_CELSIUS
from homeassistant.const import (
LENGTH_MILLIMETERS,
PRESSURE_HPA,
SPEED_METERS_PER_SECOND,
TEMP_CELSIUS,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.pressure import convert as pressure_convert

from .const import (
ATTR_API_CONDITION,
Expand Down Expand Up @@ -50,6 +54,9 @@ class OpenWeatherMapWeather(WeatherEntity):
_attr_attribution = ATTRIBUTION
_attr_should_poll = False
_attr_temperature_unit = TEMP_CELSIUS
_attr_pressure_unit = PRESSURE_HPA
_attr_wind_speed_unit = SPEED_METERS_PER_SECOND
_attr_precipitation_unit = LENGTH_MILLIMETERS

def __init__(
self,
Expand Down Expand Up @@ -80,13 +87,8 @@ def temperature(self) -> float | None:

@property
def pressure(self) -> float | None:
"""Return the pressure."""
pressure = self._weather_coordinator.data[ATTR_API_PRESSURE]
# OpenWeatherMap returns pressure in hPA, so convert to
# inHg if we aren't using metric.
if not self.hass.config.units.is_metric and pressure:
return pressure_convert(pressure, PRESSURE_HPA, PRESSURE_INHG)
return pressure
"""Return the pressure in hPA."""
return self._weather_coordinator.data[ATTR_API_PRESSURE]

@property
def humidity(self) -> float | None:
Expand All @@ -95,11 +97,8 @@ def humidity(self) -> float | None:

@property
def wind_speed(self) -> float | None:
"""Return the wind speed."""
wind_speed = self._weather_coordinator.data[ATTR_API_WIND_SPEED]
if self.hass.config.units.name == "imperial":
return round(wind_speed * 2.24, 2)
return round(wind_speed * 3.6, 2)
"""Return the wind speed in m/s."""
return self._weather_coordinator.data[ATTR_API_WIND_SPEED]

@property
def wind_bearing(self) -> float | str | None:
Expand Down
27 changes: 18 additions & 9 deletions homeassistant/components/smhi/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@
WeatherEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME, TEMP_CELSIUS
from homeassistant.const import (
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_NAME,
LENGTH_KILOMETERS,
LENGTH_MILLIMETERS,
PRESSURE_HPA,
SPEED_METERS_PER_SECOND,
TEMP_CELSIUS,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand Down Expand Up @@ -100,6 +109,12 @@ async def async_setup_entry(
class SmhiWeather(WeatherEntity):
"""Representation of a weather entity."""

_attr_temperature_unit = TEMP_CELSIUS
_attr_pressure_unit = PRESSURE_HPA
_attr_wind_speed_unit = SPEED_METERS_PER_SECOND
_attr_visibility_unit = LENGTH_KILOMETERS
_attr_precipitation_unit = LENGTH_MILLIMETERS

def __init__(
self,
name: str,
Expand Down Expand Up @@ -157,11 +172,6 @@ def temperature(self) -> int | None:
return self._forecasts[0].temperature
return None

@property
def temperature_unit(self) -> str:
"""Return the unit of measurement."""
return TEMP_CELSIUS

@property
def humidity(self) -> int | None:
"""Return the humidity."""
Expand All @@ -171,10 +181,9 @@ def humidity(self) -> int | None:

@property
def wind_speed(self) -> float | None:
"""Return the wind speed."""
"""Return the wind speed in m/s."""
if self._forecasts is not None:
# Convert from m/s to km/h
return round(self._forecasts[0].wind_speed * 18 / 5)
return self._forecasts[0].wind_speed
return None

@property
Expand Down
18 changes: 12 additions & 6 deletions homeassistant/components/zamg/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@
PLATFORM_SCHEMA,
WeatherEntity,
)
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME, TEMP_CELSIUS
from homeassistant.const import (
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_NAME,
PRESSURE_HPA,
SPEED_KILOMETERS_PER_HOUR,
TEMP_CELSIUS,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand Down Expand Up @@ -80,6 +87,10 @@ def setup_platform(
class ZamgWeather(WeatherEntity):
"""Representation of a weather condition."""

_attr_temperature_unit = TEMP_CELSIUS
_attr_pressure_unit = PRESSURE_HPA
_attr_wind_speed_unit = SPEED_KILOMETERS_PER_HOUR

def __init__(self, zamg_data, stationname=None):
"""Initialise the platform with a data instance and station name."""
self.zamg_data = zamg_data
Expand Down Expand Up @@ -108,11 +119,6 @@ def temperature(self):
"""Return the platform temperature."""
return self.zamg_data.get_data(ATTR_WEATHER_TEMPERATURE)

@property
def temperature_unit(self):
"""Return the unit of measurement."""
return TEMP_CELSIUS

@property
def pressure(self):
"""Return the pressure."""
Expand Down
31 changes: 15 additions & 16 deletions tests/components/nws/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
LENGTH_KILOMETERS,
LENGTH_METERS,
LENGTH_MILES,
PRESSURE_HPA,
PRESSURE_INHG,
PRESSURE_PA,
PRESSURE_PSI,
SPEED_KILOMETERS_PER_HOUR,
SPEED_METERS_PER_SECOND,
SPEED_MILES_PER_HOUR,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
Expand Down Expand Up @@ -104,26 +105,22 @@
convert_temperature(10, TEMP_CELSIUS, TEMP_FAHRENHEIT)
),
ATTR_WEATHER_WIND_BEARING: 180,
ATTR_WEATHER_WIND_SPEED: round(
convert_speed(10, SPEED_KILOMETERS_PER_HOUR, SPEED_MILES_PER_HOUR)
),
ATTR_WEATHER_PRESSURE: round(
convert_pressure(100000, PRESSURE_PA, PRESSURE_INHG), 2
),
ATTR_WEATHER_VISIBILITY: round(
convert_distance(10000, LENGTH_METERS, LENGTH_MILES)
ATTR_WEATHER_WIND_SPEED: convert_speed(
10, SPEED_KILOMETERS_PER_HOUR, SPEED_MILES_PER_HOUR
),
ATTR_WEATHER_PRESSURE: convert_pressure(100000, PRESSURE_PA, PRESSURE_PSI),
ATTR_WEATHER_VISIBILITY: convert_distance(10000, LENGTH_METERS, LENGTH_MILES),
ATTR_WEATHER_HUMIDITY: 10,
}

WEATHER_EXPECTED_OBSERVATION_METRIC = {
ATTR_WEATHER_TEMPERATURE: 10,
ATTR_WEATHER_WIND_BEARING: 180,
ATTR_WEATHER_WIND_SPEED: 10,
ATTR_WEATHER_PRESSURE: round(convert_pressure(100000, PRESSURE_PA, PRESSURE_HPA)),
ATTR_WEATHER_VISIBILITY: round(
convert_distance(10000, LENGTH_METERS, LENGTH_KILOMETERS)
ATTR_WEATHER_WIND_SPEED: convert_speed(
10, SPEED_KILOMETERS_PER_HOUR, SPEED_METERS_PER_SECOND
),
ATTR_WEATHER_PRESSURE: convert_pressure(100000, PRESSURE_PA, PRESSURE_PA),
ATTR_WEATHER_VISIBILITY: convert_distance(10000, LENGTH_METERS, LENGTH_KILOMETERS),
ATTR_WEATHER_HUMIDITY: 10,
}

Expand Down Expand Up @@ -157,9 +154,11 @@
EXPECTED_FORECAST_METRIC = {
ATTR_FORECAST_CONDITION: ATTR_CONDITION_LIGHTNING_RAINY,
ATTR_FORECAST_TIME: "2019-08-12T20:00:00-04:00",
ATTR_FORECAST_TEMP: round(convert_temperature(10, TEMP_FAHRENHEIT, TEMP_CELSIUS)),
ATTR_FORECAST_WIND_SPEED: round(
convert_speed(10, SPEED_MILES_PER_HOUR, SPEED_KILOMETERS_PER_HOUR)
ATTR_FORECAST_TEMP: round(
convert_temperature(10, TEMP_FAHRENHEIT, TEMP_CELSIUS), 1
),
ATTR_FORECAST_WIND_SPEED: convert_speed(
10, SPEED_MILES_PER_HOUR, SPEED_METERS_PER_SECOND
),
ATTR_FORECAST_WIND_BEARING: 180,
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 90,
Expand Down
Loading