Add support for OpenWeatherMap's visibility (#71013)

* Add support for visibility

* Add docstrings
This commit is contained in:
Fabian Affolter 2022-04-28 20:39:50 +02:00 committed by GitHub
parent caf71c854f
commit 805aa3375a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View File

@ -31,6 +31,7 @@ from homeassistant.components.weather import (
) )
from homeassistant.const import ( from homeassistant.const import (
DEGREE, DEGREE,
LENGTH_KILOMETERS,
LENGTH_MILLIMETERS, LENGTH_MILLIMETERS,
PERCENTAGE, PERCENTAGE,
PRESSURE_HPA, PRESSURE_HPA,
@ -65,6 +66,7 @@ ATTR_API_CLOUDS = "clouds"
ATTR_API_RAIN = "rain" ATTR_API_RAIN = "rain"
ATTR_API_SNOW = "snow" ATTR_API_SNOW = "snow"
ATTR_API_UV_INDEX = "uv_index" ATTR_API_UV_INDEX = "uv_index"
ATTR_API_VISIBILITY_DISTANCE = "visibility_distance"
ATTR_API_WEATHER_CODE = "weather_code" ATTR_API_WEATHER_CODE = "weather_code"
ATTR_API_FORECAST = "forecast" ATTR_API_FORECAST = "forecast"
UPDATE_LISTENER = "update_listener" UPDATE_LISTENER = "update_listener"
@ -243,6 +245,12 @@ WEATHER_SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
native_unit_of_measurement=UV_INDEX, native_unit_of_measurement=UV_INDEX,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SensorEntityDescription(
key=ATTR_API_VISIBILITY_DISTANCE,
name="Visibility",
native_unit_of_measurement=LENGTH_KILOMETERS,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription( SensorEntityDescription(
key=ATTR_API_CONDITION, key=ATTR_API_CONDITION,
name="Condition", name="Condition",

View File

@ -36,6 +36,7 @@ from .const import (
ATTR_API_SNOW, ATTR_API_SNOW,
ATTR_API_TEMPERATURE, ATTR_API_TEMPERATURE,
ATTR_API_UV_INDEX, ATTR_API_UV_INDEX,
ATTR_API_VISIBILITY_DISTANCE,
ATTR_API_WEATHER, ATTR_API_WEATHER,
ATTR_API_WEATHER_CODE, ATTR_API_WEATHER_CODE,
ATTR_API_WIND_BEARING, ATTR_API_WIND_BEARING,
@ -72,6 +73,7 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator):
) )
async def _async_update_data(self): async def _async_update_data(self):
"""Update the data."""
data = {} data = {}
async with async_timeout.timeout(20): async with async_timeout.timeout(20):
try: try:
@ -137,11 +139,13 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator):
ATTR_API_WEATHER: current_weather.detailed_status, ATTR_API_WEATHER: current_weather.detailed_status,
ATTR_API_CONDITION: self._get_condition(current_weather.weather_code), ATTR_API_CONDITION: self._get_condition(current_weather.weather_code),
ATTR_API_UV_INDEX: current_weather.uvi, ATTR_API_UV_INDEX: current_weather.uvi,
ATTR_API_VISIBILITY_DISTANCE: current_weather.visibility_distance,
ATTR_API_WEATHER_CODE: current_weather.weather_code, ATTR_API_WEATHER_CODE: current_weather.weather_code,
ATTR_API_FORECAST: forecast_weather, ATTR_API_FORECAST: forecast_weather,
} }
def _get_forecast_from_weather_response(self, weather_response): def _get_forecast_from_weather_response(self, weather_response):
"""Extract the forecast data from the weather response."""
forecast_arg = "forecast" forecast_arg = "forecast"
if self._forecast_mode == FORECAST_MODE_ONECALL_HOURLY: if self._forecast_mode == FORECAST_MODE_ONECALL_HOURLY:
forecast_arg = "forecast_hourly" forecast_arg = "forecast_hourly"
@ -152,6 +156,7 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator):
] ]
def _convert_forecast(self, entry): def _convert_forecast(self, entry):
"""Convert the forecast data."""
forecast = { forecast = {
ATTR_FORECAST_TIME: dt.utc_from_timestamp( ATTR_FORECAST_TIME: dt.utc_from_timestamp(
entry.reference_time("unix") entry.reference_time("unix")
@ -182,6 +187,7 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator):
@staticmethod @staticmethod
def _fmt_dewpoint(dewpoint): def _fmt_dewpoint(dewpoint):
"""Format the dewpoint data."""
if dewpoint is not None: if dewpoint is not None:
return round(kelvin_to_celsius(dewpoint), 1) return round(kelvin_to_celsius(dewpoint), 1)
return None return None