From 805aa3375a0be2f491ed49487ad7ead592aaec98 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Thu, 28 Apr 2022 20:39:50 +0200 Subject: [PATCH] Add support for OpenWeatherMap's visibility (#71013) * Add support for visibility * Add docstrings --- homeassistant/components/openweathermap/const.py | 8 ++++++++ .../openweathermap/weather_update_coordinator.py | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/homeassistant/components/openweathermap/const.py b/homeassistant/components/openweathermap/const.py index b623ed86c3a..8d673507929 100644 --- a/homeassistant/components/openweathermap/const.py +++ b/homeassistant/components/openweathermap/const.py @@ -31,6 +31,7 @@ from homeassistant.components.weather import ( ) from homeassistant.const import ( DEGREE, + LENGTH_KILOMETERS, LENGTH_MILLIMETERS, PERCENTAGE, PRESSURE_HPA, @@ -65,6 +66,7 @@ ATTR_API_CLOUDS = "clouds" ATTR_API_RAIN = "rain" ATTR_API_SNOW = "snow" ATTR_API_UV_INDEX = "uv_index" +ATTR_API_VISIBILITY_DISTANCE = "visibility_distance" ATTR_API_WEATHER_CODE = "weather_code" ATTR_API_FORECAST = "forecast" UPDATE_LISTENER = "update_listener" @@ -243,6 +245,12 @@ WEATHER_SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( native_unit_of_measurement=UV_INDEX, state_class=SensorStateClass.MEASUREMENT, ), + SensorEntityDescription( + key=ATTR_API_VISIBILITY_DISTANCE, + name="Visibility", + native_unit_of_measurement=LENGTH_KILOMETERS, + state_class=SensorStateClass.MEASUREMENT, + ), SensorEntityDescription( key=ATTR_API_CONDITION, name="Condition", diff --git a/homeassistant/components/openweathermap/weather_update_coordinator.py b/homeassistant/components/openweathermap/weather_update_coordinator.py index f4814e64d9a..26341621051 100644 --- a/homeassistant/components/openweathermap/weather_update_coordinator.py +++ b/homeassistant/components/openweathermap/weather_update_coordinator.py @@ -36,6 +36,7 @@ from .const import ( ATTR_API_SNOW, ATTR_API_TEMPERATURE, ATTR_API_UV_INDEX, + ATTR_API_VISIBILITY_DISTANCE, ATTR_API_WEATHER, ATTR_API_WEATHER_CODE, ATTR_API_WIND_BEARING, @@ -72,6 +73,7 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator): ) async def _async_update_data(self): + """Update the data.""" data = {} async with async_timeout.timeout(20): try: @@ -137,11 +139,13 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator): ATTR_API_WEATHER: current_weather.detailed_status, ATTR_API_CONDITION: self._get_condition(current_weather.weather_code), 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_FORECAST: forecast_weather, } def _get_forecast_from_weather_response(self, weather_response): + """Extract the forecast data from the weather response.""" forecast_arg = "forecast" if self._forecast_mode == FORECAST_MODE_ONECALL_HOURLY: forecast_arg = "forecast_hourly" @@ -152,6 +156,7 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator): ] def _convert_forecast(self, entry): + """Convert the forecast data.""" forecast = { ATTR_FORECAST_TIME: dt.utc_from_timestamp( entry.reference_time("unix") @@ -182,6 +187,7 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator): @staticmethod def _fmt_dewpoint(dewpoint): + """Format the dewpoint data.""" if dewpoint is not None: return round(kelvin_to_celsius(dewpoint), 1) return None