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
29 changes: 14 additions & 15 deletions homeassistant/components/knx/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@

from homeassistant import config_entries
from homeassistant.components.weather import WeatherEntity
from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME, TEMP_CELSIUS, Platform
from homeassistant.const import (
CONF_ENTITY_CATEGORY,
CONF_NAME,
PRESSURE_PA,
SPEED_METERS_PER_SECOND,
TEMP_CELSIUS,
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType
Expand Down Expand Up @@ -69,6 +76,8 @@ class KNXWeather(KnxEntity, WeatherEntity):

_device: XknxWeather
_attr_temperature_unit = TEMP_CELSIUS
_attr_pressure_unit = PRESSURE_PA
_attr_wind_speed_unit = SPEED_METERS_PER_SECOND

def __init__(self, xknx: XKNX, config: ConfigType) -> None:
"""Initialize of a KNX sensor."""
Expand All @@ -83,13 +92,8 @@ def temperature(self) -> float | None:

@property
def pressure(self) -> float | None:
"""Return current air pressure."""
# KNX returns pA - HA requires hPa
return (
self._device.air_pressure / 100
if self._device.air_pressure is not None
else None
)
"""Return current air pressure in pA."""
return self._device.air_pressure

@property
def condition(self) -> str:
Expand All @@ -108,10 +112,5 @@ def wind_bearing(self) -> int | None:

@property
def wind_speed(self) -> float | None:
"""Return current wind speed in km/h."""
# KNX only supports wind speed in m/s
return (
self._device.wind_speed * 3.6
if self._device.wind_speed is not None
else None
)
"""Return current wind speed in m/s."""
return self._device.wind_speed
54 changes: 11 additions & 43 deletions homeassistant/components/met/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_NAME,
LENGTH_INCHES,
LENGTH_MILLIMETERS,
PRESSURE_HPA,
PRESSURE_INHG,
SPEED_KILOMETERS_PER_HOUR,
SPEED_MILES_PER_HOUR,
TEMP_CELSIUS,
)
from homeassistant.core import HomeAssistant
Expand All @@ -45,18 +42,8 @@
DataUpdateCoordinator,
T,
)
from homeassistant.util.distance import convert as convert_distance
from homeassistant.util.pressure import convert as convert_pressure
from homeassistant.util.speed import convert as convert_speed

from .const import (
ATTR_FORECAST_PRECIPITATION,
ATTR_MAP,
CONDITIONS_MAP,
CONF_TRACK_HOME,
DOMAIN,
FORECAST_MAP,
)

from .const import ATTR_MAP, CONDITIONS_MAP, CONF_TRACK_HOME, DOMAIN, FORECAST_MAP

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -133,6 +120,11 @@ def format_condition(condition: str) -> str:
class MetWeather(CoordinatorEntity, WeatherEntity):
"""Implementation of a Met.no weather condition."""

_attr_temperature_unit = TEMP_CELSIUS
_attr_pressure_unit = PRESSURE_HPA
_attr_wind_speed_unit = SPEED_KILOMETERS_PER_HOUR
_attr_precipitation_unit = LENGTH_MILLIMETERS

def __init__(
self,
coordinator: DataUpdateCoordinator[T],
Expand Down Expand Up @@ -196,21 +188,12 @@ def temperature(self) -> float | None:
ATTR_MAP[ATTR_WEATHER_TEMPERATURE]
)

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

@property
def pressure(self) -> float | None:
"""Return the pressure."""
pressure_hpa = self.coordinator.data.current_weather_data.get(
"""Return the pressure in hPa."""
return self.coordinator.data.current_weather_data.get(
ATTR_MAP[ATTR_WEATHER_PRESSURE]
)
if self._is_metric or pressure_hpa is None:
return pressure_hpa

return round(convert_pressure(pressure_hpa, PRESSURE_HPA, PRESSURE_INHG), 2)

@property
def humidity(self) -> float | None:
Expand All @@ -221,17 +204,10 @@ def humidity(self) -> float | None:

@property
def wind_speed(self) -> float | None:
"""Return the wind speed."""
speed_km_h = self.coordinator.data.current_weather_data.get(
"""Return the wind speed in km/h."""
return 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))

@property
def wind_bearing(self) -> float | str | None:
Expand Down Expand Up @@ -262,14 +238,6 @@ def forecast(self) -> list[Forecast] | None:
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:
if ha_item[ATTR_FORECAST_PRECIPITATION] is not None:
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]
Expand Down
43 changes: 9 additions & 34 deletions homeassistant/components/met_eireann/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from homeassistant.components.weather import (
ATTR_FORECAST_CONDITION,
ATTR_FORECAST_PRECIPITATION,
ATTR_FORECAST_TEMP,
ATTR_FORECAST_TIME,
WeatherEntity,
Expand All @@ -13,12 +12,9 @@
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_NAME,
LENGTH_INCHES,
LENGTH_MILLIMETERS,
PRESSURE_HPA,
PRESSURE_INHG,
SPEED_METERS_PER_SECOND,
SPEED_MILES_PER_HOUR,
TEMP_CELSIUS,
)
from homeassistant.core import HomeAssistant
Expand All @@ -27,9 +23,6 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import dt as dt_util
from homeassistant.util.distance import convert as convert_distance
from homeassistant.util.pressure import convert as convert_pressure
from homeassistant.util.speed import convert as convert_speed

from .const import ATTRIBUTION, CONDITION_MAP, DEFAULT_NAME, DOMAIN, FORECAST_MAP

Expand Down Expand Up @@ -67,6 +60,11 @@ async def async_setup_entry(
class MetEireannWeather(CoordinatorEntity, WeatherEntity):
"""Implementation of a Met Éireann weather condition."""

_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, coordinator, config, is_metric, hourly):
"""Initialise the platform with a data instance and site."""
super().__init__(coordinator)
Expand Down Expand Up @@ -113,19 +111,10 @@ def temperature(self):
"""Return the temperature."""
return self.coordinator.data.current_weather_data.get("temperature")

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

@property
def pressure(self):
"""Return the pressure."""
pressure_hpa = self.coordinator.data.current_weather_data.get("pressure")
if self._is_metric or pressure_hpa is None:
return pressure_hpa

return round(convert_pressure(pressure_hpa, PRESSURE_HPA, PRESSURE_INHG), 2)
"""Return the pressure in hPa."""
return self.coordinator.data.current_weather_data.get("pressure")

@property
def humidity(self):
Expand All @@ -134,15 +123,8 @@ def humidity(self):

@property
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))
"""Return the wind speed in m/s."""
return self.coordinator.data.current_weather_data.get("wind_speed")

@property
def wind_bearing(self):
Expand Down Expand Up @@ -171,13 +153,6 @@ def forecast(self):
ha_item = {
k: item[v] for k, v in FORECAST_MAP.items() if 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]
Expand Down
23 changes: 14 additions & 9 deletions homeassistant/components/meteo_france/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
WeatherEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_MODE, TEMP_CELSIUS
from homeassistant.const import (
CONF_MODE,
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
Expand Down Expand Up @@ -71,6 +77,11 @@ async def async_setup_entry(
class MeteoFranceWeather(CoordinatorEntity, WeatherEntity):
"""Representation of a weather condition."""

_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, coordinator: DataUpdateCoordinator, mode: str) -> None:
"""Initialise the platform with a data instance and station name."""
super().__init__(coordinator)
Expand Down Expand Up @@ -111,11 +122,6 @@ def temperature(self):
"""Return the temperature."""
return self.coordinator.data.current_forecast["T"]["value"]

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

@property
def pressure(self):
"""Return the pressure."""
Expand All @@ -128,9 +134,8 @@ def humidity(self):

@property
def wind_speed(self):
"""Return the wind speed."""
# convert from API m/s to km/h
return round(self.coordinator.data.current_forecast["wind"]["speed"] * 3.6)
"""Return the wind speed in m/s."""
return self.coordinator.data.current_forecast["wind"]["speed"]

@property
def wind_bearing(self):
Expand Down
17 changes: 11 additions & 6 deletions homeassistant/components/meteoclimatic/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

from homeassistant.components.weather import WeatherEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import TEMP_CELSIUS
from homeassistant.const import (
LENGTH_MILLIMETERS,
PRESSURE_HPA,
SPEED_KILOMETERS_PER_HOUR,
TEMP_CELSIUS,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo
Expand Down Expand Up @@ -38,6 +43,11 @@ async def async_setup_entry(
class MeteoclimaticWeather(CoordinatorEntity, WeatherEntity):
"""Representation of a weather condition."""

_attr_temperature_unit = TEMP_CELSIUS
_attr_pressure_unit = PRESSURE_HPA
_attr_wind_speed_unit = SPEED_KILOMETERS_PER_HOUR
_attr_precipitation_unit = LENGTH_MILLIMETERS

def __init__(self, coordinator: DataUpdateCoordinator) -> None:
"""Initialise the weather platform."""
super().__init__(coordinator)
Expand Down Expand Up @@ -75,11 +85,6 @@ def temperature(self):
"""Return the temperature."""
return self.coordinator.data["weather"].temp_current

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

@property
def humidity(self):
"""Return the humidity."""
Expand Down
11 changes: 5 additions & 6 deletions homeassistant/components/metoffice/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WeatherEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import TEMP_CELSIUS
from homeassistant.const import PRESSURE_HPA, SPEED_MILES_PER_HOUR, TEMP_CELSIUS
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
Expand Down Expand Up @@ -73,6 +73,10 @@ def _get_weather_condition(metoffice_code):
class MetOfficeWeather(CoordinatorEntity, WeatherEntity):
"""Implementation of a Met Office weather condition."""

_attr_temperature_unit = TEMP_CELSIUS
_attr_pressure_unit = PRESSURE_HPA
_attr_wind_speed_unit = SPEED_MILES_PER_HOUR

def __init__(self, coordinator, hass_data, use_3hourly):
"""Initialise the platform with a data instance."""
super().__init__(coordinator)
Expand Down Expand Up @@ -100,11 +104,6 @@ def temperature(self):
return self.coordinator.data.now.temperature.value
return None

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

@property
def visibility(self):
"""Return the platform visibility."""
Expand Down
Loading