mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 10:17:51 +00:00
Use built in unit handling for nws weather (#73981)
use built in unit handling for nws
This commit is contained in:
parent
a94579107c
commit
e32c7dbf92
@ -3,22 +3,18 @@ from homeassistant.components.weather import (
|
|||||||
ATTR_CONDITION_CLEAR_NIGHT,
|
ATTR_CONDITION_CLEAR_NIGHT,
|
||||||
ATTR_CONDITION_SUNNY,
|
ATTR_CONDITION_SUNNY,
|
||||||
ATTR_FORECAST_CONDITION,
|
ATTR_FORECAST_CONDITION,
|
||||||
|
ATTR_FORECAST_NATIVE_TEMP,
|
||||||
|
ATTR_FORECAST_NATIVE_WIND_SPEED,
|
||||||
ATTR_FORECAST_PRECIPITATION_PROBABILITY,
|
ATTR_FORECAST_PRECIPITATION_PROBABILITY,
|
||||||
ATTR_FORECAST_TEMP,
|
|
||||||
ATTR_FORECAST_TIME,
|
ATTR_FORECAST_TIME,
|
||||||
ATTR_FORECAST_WIND_BEARING,
|
ATTR_FORECAST_WIND_BEARING,
|
||||||
ATTR_FORECAST_WIND_SPEED,
|
|
||||||
WeatherEntity,
|
WeatherEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_LATITUDE,
|
CONF_LATITUDE,
|
||||||
CONF_LONGITUDE,
|
CONF_LONGITUDE,
|
||||||
LENGTH_KILOMETERS,
|
|
||||||
LENGTH_METERS,
|
LENGTH_METERS,
|
||||||
LENGTH_MILES,
|
|
||||||
PRESSURE_HPA,
|
|
||||||
PRESSURE_INHG,
|
|
||||||
PRESSURE_PA,
|
PRESSURE_PA,
|
||||||
SPEED_KILOMETERS_PER_HOUR,
|
SPEED_KILOMETERS_PER_HOUR,
|
||||||
SPEED_MILES_PER_HOUR,
|
SPEED_MILES_PER_HOUR,
|
||||||
@ -28,9 +24,7 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
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.dt import utcnow
|
||||||
from homeassistant.util.pressure import convert as convert_pressure
|
|
||||||
from homeassistant.util.speed import convert as convert_speed
|
from homeassistant.util.speed import convert as convert_speed
|
||||||
from homeassistant.util.temperature import convert as convert_temperature
|
from homeassistant.util.temperature import convert as convert_temperature
|
||||||
|
|
||||||
@ -155,68 +149,54 @@ class NWSWeather(WeatherEntity):
|
|||||||
return f"{self.station} {self.mode.title()}"
|
return f"{self.station} {self.mode.title()}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def temperature(self):
|
def native_temperature(self):
|
||||||
"""Return the current temperature."""
|
"""Return the current temperature."""
|
||||||
temp_c = None
|
|
||||||
if self.observation:
|
if self.observation:
|
||||||
temp_c = self.observation.get("temperature")
|
return self.observation.get("temperature")
|
||||||
if temp_c is not None:
|
|
||||||
return convert_temperature(temp_c, TEMP_CELSIUS, TEMP_FAHRENHEIT)
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def pressure(self):
|
def native_temperature_unit(self):
|
||||||
|
"""Return the current temperature unit."""
|
||||||
|
return TEMP_CELSIUS
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_pressure(self):
|
||||||
"""Return the current pressure."""
|
"""Return the current pressure."""
|
||||||
pressure_pa = None
|
|
||||||
if self.observation:
|
if self.observation:
|
||||||
pressure_pa = self.observation.get("seaLevelPressure")
|
return self.observation.get("seaLevelPressure")
|
||||||
if pressure_pa is None:
|
return None
|
||||||
return None
|
|
||||||
if self.is_metric:
|
@property
|
||||||
pressure = convert_pressure(pressure_pa, PRESSURE_PA, PRESSURE_HPA)
|
def native_pressure_unit(self):
|
||||||
pressure = round(pressure)
|
"""Return the current pressure unit."""
|
||||||
else:
|
return PRESSURE_PA
|
||||||
pressure = convert_pressure(pressure_pa, PRESSURE_PA, PRESSURE_INHG)
|
|
||||||
pressure = round(pressure, 2)
|
|
||||||
return pressure
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def humidity(self):
|
def humidity(self):
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
humidity = None
|
|
||||||
if self.observation:
|
if self.observation:
|
||||||
humidity = self.observation.get("relativeHumidity")
|
return self.observation.get("relativeHumidity")
|
||||||
return humidity
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def wind_speed(self):
|
def native_wind_speed(self):
|
||||||
"""Return the current windspeed."""
|
"""Return the current windspeed."""
|
||||||
wind_km_hr = None
|
|
||||||
if self.observation:
|
if self.observation:
|
||||||
wind_km_hr = self.observation.get("windSpeed")
|
return self.observation.get("windSpeed")
|
||||||
if wind_km_hr is None:
|
return None
|
||||||
return None
|
|
||||||
|
|
||||||
if self.is_metric:
|
@property
|
||||||
wind = wind_km_hr
|
def native_wind_speed_unit(self):
|
||||||
else:
|
"""Return the current windspeed."""
|
||||||
wind = convert_speed(
|
return SPEED_KILOMETERS_PER_HOUR
|
||||||
wind_km_hr, SPEED_KILOMETERS_PER_HOUR, SPEED_MILES_PER_HOUR
|
|
||||||
)
|
|
||||||
return round(wind)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def wind_bearing(self):
|
def wind_bearing(self):
|
||||||
"""Return the current wind bearing (degrees)."""
|
"""Return the current wind bearing (degrees)."""
|
||||||
wind_bearing = None
|
|
||||||
if self.observation:
|
if self.observation:
|
||||||
wind_bearing = self.observation.get("windDirection")
|
return self.observation.get("windDirection")
|
||||||
return wind_bearing
|
return None
|
||||||
|
|
||||||
@property
|
|
||||||
def temperature_unit(self):
|
|
||||||
"""Return the unit of measurement."""
|
|
||||||
return TEMP_FAHRENHEIT
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def condition(self):
|
def condition(self):
|
||||||
@ -232,19 +212,16 @@ class NWSWeather(WeatherEntity):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def visibility(self):
|
def native_visibility(self):
|
||||||
"""Return visibility."""
|
"""Return visibility."""
|
||||||
vis_m = None
|
|
||||||
if self.observation:
|
if self.observation:
|
||||||
vis_m = self.observation.get("visibility")
|
return self.observation.get("visibility")
|
||||||
if vis_m is None:
|
return None
|
||||||
return None
|
|
||||||
|
|
||||||
if self.is_metric:
|
@property
|
||||||
vis = convert_distance(vis_m, LENGTH_METERS, LENGTH_KILOMETERS)
|
def native_visibility_unit(self):
|
||||||
else:
|
"""Return visibility unit."""
|
||||||
vis = convert_distance(vis_m, LENGTH_METERS, LENGTH_MILES)
|
return LENGTH_METERS
|
||||||
return round(vis, 0)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def forecast(self):
|
def forecast(self):
|
||||||
@ -257,10 +234,16 @@ class NWSWeather(WeatherEntity):
|
|||||||
ATTR_FORECAST_DETAILED_DESCRIPTION: forecast_entry.get(
|
ATTR_FORECAST_DETAILED_DESCRIPTION: forecast_entry.get(
|
||||||
"detailedForecast"
|
"detailedForecast"
|
||||||
),
|
),
|
||||||
ATTR_FORECAST_TEMP: forecast_entry.get("temperature"),
|
|
||||||
ATTR_FORECAST_TIME: forecast_entry.get("startTime"),
|
ATTR_FORECAST_TIME: forecast_entry.get("startTime"),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (temp := forecast_entry.get("temperature")) is not None:
|
||||||
|
data[ATTR_FORECAST_NATIVE_TEMP] = convert_temperature(
|
||||||
|
temp, TEMP_FAHRENHEIT, TEMP_CELSIUS
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
data[ATTR_FORECAST_NATIVE_TEMP] = None
|
||||||
|
|
||||||
if self.mode == DAYNIGHT:
|
if self.mode == DAYNIGHT:
|
||||||
data[ATTR_FORECAST_DAYTIME] = forecast_entry.get("isDaytime")
|
data[ATTR_FORECAST_DAYTIME] = forecast_entry.get("isDaytime")
|
||||||
time = forecast_entry.get("iconTime")
|
time = forecast_entry.get("iconTime")
|
||||||
@ -275,16 +258,11 @@ class NWSWeather(WeatherEntity):
|
|||||||
data[ATTR_FORECAST_WIND_BEARING] = forecast_entry.get("windBearing")
|
data[ATTR_FORECAST_WIND_BEARING] = forecast_entry.get("windBearing")
|
||||||
wind_speed = forecast_entry.get("windSpeedAvg")
|
wind_speed = forecast_entry.get("windSpeedAvg")
|
||||||
if wind_speed is not None:
|
if wind_speed is not None:
|
||||||
if self.is_metric:
|
data[ATTR_FORECAST_NATIVE_WIND_SPEED] = convert_speed(
|
||||||
data[ATTR_FORECAST_WIND_SPEED] = round(
|
wind_speed, SPEED_MILES_PER_HOUR, SPEED_KILOMETERS_PER_HOUR
|
||||||
convert_speed(
|
)
|
||||||
wind_speed, SPEED_MILES_PER_HOUR, SPEED_KILOMETERS_PER_HOUR
|
|
||||||
)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
data[ATTR_FORECAST_WIND_SPEED] = round(wind_speed)
|
|
||||||
else:
|
else:
|
||||||
data[ATTR_FORECAST_WIND_SPEED] = None
|
data[ATTR_FORECAST_NATIVE_WIND_SPEED] = None
|
||||||
forecast.append(data)
|
forecast.append(data)
|
||||||
return forecast
|
return forecast
|
||||||
|
|
||||||
|
@ -105,13 +105,13 @@ WEATHER_EXPECTED_OBSERVATION_IMPERIAL = {
|
|||||||
),
|
),
|
||||||
ATTR_WEATHER_WIND_BEARING: 180,
|
ATTR_WEATHER_WIND_BEARING: 180,
|
||||||
ATTR_WEATHER_WIND_SPEED: round(
|
ATTR_WEATHER_WIND_SPEED: round(
|
||||||
convert_speed(10, SPEED_KILOMETERS_PER_HOUR, SPEED_MILES_PER_HOUR)
|
convert_speed(10, SPEED_KILOMETERS_PER_HOUR, SPEED_MILES_PER_HOUR), 2
|
||||||
),
|
),
|
||||||
ATTR_WEATHER_PRESSURE: round(
|
ATTR_WEATHER_PRESSURE: round(
|
||||||
convert_pressure(100000, PRESSURE_PA, PRESSURE_INHG), 2
|
convert_pressure(100000, PRESSURE_PA, PRESSURE_INHG), 2
|
||||||
),
|
),
|
||||||
ATTR_WEATHER_VISIBILITY: round(
|
ATTR_WEATHER_VISIBILITY: round(
|
||||||
convert_distance(10000, LENGTH_METERS, LENGTH_MILES)
|
convert_distance(10000, LENGTH_METERS, LENGTH_MILES), 2
|
||||||
),
|
),
|
||||||
ATTR_WEATHER_HUMIDITY: 10,
|
ATTR_WEATHER_HUMIDITY: 10,
|
||||||
}
|
}
|
||||||
@ -161,7 +161,7 @@ EXPECTED_FORECAST_METRIC = {
|
|||||||
convert_temperature(10, TEMP_FAHRENHEIT, TEMP_CELSIUS), 1
|
convert_temperature(10, TEMP_FAHRENHEIT, TEMP_CELSIUS), 1
|
||||||
),
|
),
|
||||||
ATTR_FORECAST_WIND_SPEED: round(
|
ATTR_FORECAST_WIND_SPEED: round(
|
||||||
convert_speed(10, SPEED_MILES_PER_HOUR, SPEED_KILOMETERS_PER_HOUR)
|
convert_speed(10, SPEED_MILES_PER_HOUR, SPEED_KILOMETERS_PER_HOUR), 2
|
||||||
),
|
),
|
||||||
ATTR_FORECAST_WIND_BEARING: 180,
|
ATTR_FORECAST_WIND_BEARING: 180,
|
||||||
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 90,
|
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 90,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user