From 7b98656bcd51b2beab81b040ae6aef51b477d049 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 5 Dec 2022 17:04:02 +0100 Subject: [PATCH] Use new device class in openweathermap (#83331) --- .../components/openweathermap/const.py | 166 +-------------- .../components/openweathermap/sensor.py | 192 +++++++++++++++++- 2 files changed, 191 insertions(+), 167 deletions(-) diff --git a/homeassistant/components/openweathermap/const.py b/homeassistant/components/openweathermap/const.py index 5a370604e1f..4af068f2b84 100644 --- a/homeassistant/components/openweathermap/const.py +++ b/homeassistant/components/openweathermap/const.py @@ -1,11 +1,6 @@ """Consts for the OpenWeatherMap.""" from __future__ import annotations -from homeassistant.components.sensor import ( - SensorDeviceClass, - SensorEntityDescription, - SensorStateClass, -) from homeassistant.components.weather import ( ATTR_CONDITION_CLOUDY, ATTR_CONDITION_EXCEPTIONAL, @@ -22,17 +17,7 @@ from homeassistant.components.weather import ( ATTR_CONDITION_WINDY, ATTR_CONDITION_WINDY_VARIANT, ) -from homeassistant.const import ( - DEGREE, - LENGTH_METERS, - LENGTH_MILLIMETERS, - PERCENTAGE, - PRESSURE_HPA, - SPEED_METERS_PER_SECOND, - TEMP_CELSIUS, - UV_INDEX, - Platform, -) +from homeassistant.const import Platform DOMAIN = "openweathermap" DEFAULT_NAME = "OpenWeatherMap" @@ -168,152 +153,3 @@ CONDITION_CLASSES = { 904, ], } -WEATHER_SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( - SensorEntityDescription( - key=ATTR_API_WEATHER, - name="Weather", - ), - SensorEntityDescription( - key=ATTR_API_DEW_POINT, - name="Dew Point", - native_unit_of_measurement=TEMP_CELSIUS, - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - ), - SensorEntityDescription( - key=ATTR_API_TEMPERATURE, - name="Temperature", - native_unit_of_measurement=TEMP_CELSIUS, - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - ), - SensorEntityDescription( - key=ATTR_API_FEELS_LIKE_TEMPERATURE, - name="Feels like temperature", - native_unit_of_measurement=TEMP_CELSIUS, - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - ), - SensorEntityDescription( - key=ATTR_API_WIND_SPEED, - name="Wind speed", - native_unit_of_measurement=SPEED_METERS_PER_SECOND, - state_class=SensorStateClass.MEASUREMENT, - ), - SensorEntityDescription( - key=ATTR_API_WIND_BEARING, - name="Wind bearing", - native_unit_of_measurement=DEGREE, - state_class=SensorStateClass.MEASUREMENT, - ), - SensorEntityDescription( - key=ATTR_API_HUMIDITY, - name="Humidity", - native_unit_of_measurement=PERCENTAGE, - device_class=SensorDeviceClass.HUMIDITY, - state_class=SensorStateClass.MEASUREMENT, - ), - SensorEntityDescription( - key=ATTR_API_PRESSURE, - name="Pressure", - native_unit_of_measurement=PRESSURE_HPA, - device_class=SensorDeviceClass.PRESSURE, - state_class=SensorStateClass.MEASUREMENT, - ), - SensorEntityDescription( - key=ATTR_API_CLOUDS, - name="Cloud coverage", - native_unit_of_measurement=PERCENTAGE, - state_class=SensorStateClass.MEASUREMENT, - ), - SensorEntityDescription( - key=ATTR_API_RAIN, - name="Rain", - native_unit_of_measurement=LENGTH_MILLIMETERS, - state_class=SensorStateClass.MEASUREMENT, - ), - SensorEntityDescription( - key=ATTR_API_SNOW, - name="Snow", - native_unit_of_measurement=LENGTH_MILLIMETERS, - state_class=SensorStateClass.MEASUREMENT, - ), - SensorEntityDescription( - key=ATTR_API_PRECIPITATION_KIND, - name="Precipitation kind", - ), - SensorEntityDescription( - key=ATTR_API_UV_INDEX, - name="UV Index", - native_unit_of_measurement=UV_INDEX, - state_class=SensorStateClass.MEASUREMENT, - ), - SensorEntityDescription( - key=ATTR_API_VISIBILITY_DISTANCE, - name="Visibility", - native_unit_of_measurement=LENGTH_METERS, - state_class=SensorStateClass.MEASUREMENT, - ), - SensorEntityDescription( - key=ATTR_API_CONDITION, - name="Condition", - ), - SensorEntityDescription( - key=ATTR_API_WEATHER_CODE, - name="Weather Code", - ), -) -FORECAST_SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( - SensorEntityDescription( - key=ATTR_API_FORECAST_CONDITION, - name="Condition", - ), - SensorEntityDescription( - key=ATTR_API_FORECAST_PRECIPITATION, - name="Precipitation", - native_unit_of_measurement=LENGTH_MILLIMETERS, - ), - SensorEntityDescription( - key=ATTR_API_FORECAST_PRECIPITATION_PROBABILITY, - name="Precipitation probability", - native_unit_of_measurement=PERCENTAGE, - ), - SensorEntityDescription( - key=ATTR_API_FORECAST_PRESSURE, - name="Pressure", - native_unit_of_measurement=PRESSURE_HPA, - device_class=SensorDeviceClass.PRESSURE, - ), - SensorEntityDescription( - key=ATTR_API_FORECAST_TEMP, - name="Temperature", - native_unit_of_measurement=TEMP_CELSIUS, - device_class=SensorDeviceClass.TEMPERATURE, - ), - SensorEntityDescription( - key=ATTR_API_FORECAST_TEMP_LOW, - name="Temperature Low", - native_unit_of_measurement=TEMP_CELSIUS, - device_class=SensorDeviceClass.TEMPERATURE, - ), - SensorEntityDescription( - key=ATTR_API_FORECAST_TIME, - name="Time", - device_class=SensorDeviceClass.TIMESTAMP, - ), - SensorEntityDescription( - key=ATTR_API_WIND_BEARING, - name="Wind bearing", - native_unit_of_measurement=DEGREE, - ), - SensorEntityDescription( - key=ATTR_API_WIND_SPEED, - name="Wind speed", - native_unit_of_measurement=SPEED_METERS_PER_SECOND, - ), - SensorEntityDescription( - key=ATTR_API_CLOUDS, - name="Cloud coverage", - native_unit_of_measurement=PERCENTAGE, - ), -) diff --git a/homeassistant/components/openweathermap/sensor.py b/homeassistant/components/openweathermap/sensor.py index 20ee621b9dc..2cfdd2456ab 100644 --- a/homeassistant/components/openweathermap/sensor.py +++ b/homeassistant/components/openweathermap/sensor.py @@ -7,8 +7,19 @@ from homeassistant.components.sensor import ( SensorDeviceClass, SensorEntity, SensorEntityDescription, + SensorStateClass, ) from homeassistant.config_entries import ConfigEntry +from homeassistant.const import ( + DEGREE, + PERCENTAGE, + UV_INDEX, + UnitOfLength, + UnitOfPrecipitationDepth, + UnitOfPressure, + UnitOfSpeed, + UnitOfTemperature, +) from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.entity import DeviceInfo @@ -18,18 +29,195 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from homeassistant.util import dt as dt_util from .const import ( + ATTR_API_CLOUDS, + ATTR_API_CONDITION, + ATTR_API_DEW_POINT, + ATTR_API_FEELS_LIKE_TEMPERATURE, ATTR_API_FORECAST, + ATTR_API_FORECAST_CONDITION, + ATTR_API_FORECAST_PRECIPITATION, + ATTR_API_FORECAST_PRECIPITATION_PROBABILITY, + ATTR_API_FORECAST_PRESSURE, + ATTR_API_FORECAST_TEMP, + ATTR_API_FORECAST_TEMP_LOW, + ATTR_API_FORECAST_TIME, + ATTR_API_HUMIDITY, + ATTR_API_PRECIPITATION_KIND, + ATTR_API_PRESSURE, + ATTR_API_RAIN, + 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, + ATTR_API_WIND_SPEED, ATTRIBUTION, DEFAULT_NAME, DOMAIN, ENTRY_NAME, ENTRY_WEATHER_COORDINATOR, - FORECAST_SENSOR_TYPES, MANUFACTURER, - WEATHER_SENSOR_TYPES, ) from .weather_update_coordinator import WeatherUpdateCoordinator +WEATHER_SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( + SensorEntityDescription( + key=ATTR_API_WEATHER, + name="Weather", + ), + SensorEntityDescription( + key=ATTR_API_DEW_POINT, + name="Dew Point", + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_API_TEMPERATURE, + name="Temperature", + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_API_FEELS_LIKE_TEMPERATURE, + name="Feels like temperature", + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_API_WIND_SPEED, + name="Wind speed", + native_unit_of_measurement=UnitOfSpeed.METERS_PER_SECOND, + device_class=SensorDeviceClass.WIND_SPEED, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_API_WIND_BEARING, + name="Wind bearing", + native_unit_of_measurement=DEGREE, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_API_HUMIDITY, + name="Humidity", + native_unit_of_measurement=PERCENTAGE, + device_class=SensorDeviceClass.HUMIDITY, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_API_PRESSURE, + name="Pressure", + native_unit_of_measurement=UnitOfPressure.HPA, + device_class=SensorDeviceClass.PRESSURE, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_API_CLOUDS, + name="Cloud coverage", + native_unit_of_measurement=PERCENTAGE, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_API_RAIN, + name="Rain", + native_unit_of_measurement=UnitOfPrecipitationDepth.MILLIMETERS, + device_class=SensorDeviceClass.PRECIPITATION, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_API_SNOW, + name="Snow", + native_unit_of_measurement=UnitOfPrecipitationDepth.MILLIMETERS, + device_class=SensorDeviceClass.PRECIPITATION, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_API_PRECIPITATION_KIND, + name="Precipitation kind", + ), + SensorEntityDescription( + key=ATTR_API_UV_INDEX, + name="UV Index", + native_unit_of_measurement=UV_INDEX, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_API_VISIBILITY_DISTANCE, + name="Visibility", + native_unit_of_measurement=UnitOfLength.METERS, + device_class=SensorDeviceClass.DISTANCE, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_API_CONDITION, + name="Condition", + ), + SensorEntityDescription( + key=ATTR_API_WEATHER_CODE, + name="Weather Code", + ), +) +FORECAST_SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( + SensorEntityDescription( + key=ATTR_API_FORECAST_CONDITION, + name="Condition", + ), + SensorEntityDescription( + key=ATTR_API_FORECAST_PRECIPITATION, + name="Precipitation", + device_class=SensorDeviceClass.PRECIPITATION, + native_unit_of_measurement=UnitOfPrecipitationDepth.MILLIMETERS, + ), + SensorEntityDescription( + key=ATTR_API_FORECAST_PRECIPITATION_PROBABILITY, + name="Precipitation probability", + native_unit_of_measurement=PERCENTAGE, + ), + SensorEntityDescription( + key=ATTR_API_FORECAST_PRESSURE, + name="Pressure", + native_unit_of_measurement=UnitOfPressure.HPA, + device_class=SensorDeviceClass.PRESSURE, + ), + SensorEntityDescription( + key=ATTR_API_FORECAST_TEMP, + name="Temperature", + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_class=SensorDeviceClass.TEMPERATURE, + ), + SensorEntityDescription( + key=ATTR_API_FORECAST_TEMP_LOW, + name="Temperature Low", + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_class=SensorDeviceClass.TEMPERATURE, + ), + SensorEntityDescription( + key=ATTR_API_FORECAST_TIME, + name="Time", + device_class=SensorDeviceClass.TIMESTAMP, + ), + SensorEntityDescription( + key=ATTR_API_WIND_BEARING, + name="Wind bearing", + native_unit_of_measurement=DEGREE, + ), + SensorEntityDescription( + key=ATTR_API_WIND_SPEED, + name="Wind speed", + native_unit_of_measurement=UnitOfSpeed.METERS_PER_SECOND, + device_class=SensorDeviceClass.WIND_SPEED, + ), + SensorEntityDescription( + key=ATTR_API_CLOUDS, + name="Cloud coverage", + native_unit_of_measurement=PERCENTAGE, + ), +) + async def async_setup_entry( hass: HomeAssistant,