Set device_class on temperature sensors L-Q (#52919)

This commit is contained in:
Erik Montnemery 2021-07-12 20:41:45 +02:00 committed by GitHub
parent 0a3aab935a
commit d1f3c20079
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 143 additions and 64 deletions

View file

@ -17,6 +17,7 @@ from homeassistant.const import (
CONF_NAME, CONF_NAME,
CONF_SENSORS, CONF_SENSORS,
CONF_TYPE, CONF_TYPE,
DEVICE_CLASS_TEMPERATURE,
EVENT_HOMEASSISTANT_STOP, EVENT_HOMEASSISTANT_STOP,
PERCENTAGE, PERCENTAGE,
TEMP_CELSIUS, TEMP_CELSIUS,
@ -174,6 +175,7 @@ class LaCrosseSensor(SensorEntity):
class LaCrosseTemperature(LaCrosseSensor): class LaCrosseTemperature(LaCrosseSensor):
"""Implementation of a Lacrosse temperature sensor.""" """Implementation of a Lacrosse temperature sensor."""
_attr_device_class = DEVICE_CLASS_TEMPERATURE
_attr_unit_of_measurement = TEMP_CELSIUS _attr_unit_of_measurement = TEMP_CELSIUS
@property @property

View file

@ -12,6 +12,9 @@ from homeassistant.const import (
CONF_SCAN_INTERVAL, CONF_SCAN_INTERVAL,
CONF_SENSORS, CONF_SENSORS,
CONF_SHOW_ON_MAP, CONF_SHOW_ON_MAP,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_PRESSURE,
DEVICE_CLASS_TEMPERATURE,
PERCENTAGE, PERCENTAGE,
PRESSURE_HPA, PRESSURE_HPA,
TEMP_CELSIUS, TEMP_CELSIUS,
@ -45,19 +48,41 @@ SENSOR_TEMPERATURE = "temperature"
TOPIC_UPDATE = f"{DOMAIN}_data_update" TOPIC_UPDATE = f"{DOMAIN}_data_update"
SENSORS = { SENSORS = {
SENSOR_TEMPERATURE: ["Temperature", "mdi:thermometer", TEMP_CELSIUS], SENSOR_TEMPERATURE: [
SENSOR_HUMIDITY: ["Humidity", "mdi:water-percent", PERCENTAGE], "Temperature",
SENSOR_PRESSURE: ["Pressure", "mdi:arrow-down-bold", PRESSURE_HPA], "mdi:thermometer",
SENSOR_PRESSURE_AT_SEALEVEL: ["Pressure at sealevel", "mdi:download", PRESSURE_HPA], TEMP_CELSIUS,
DEVICE_CLASS_TEMPERATURE,
],
SENSOR_HUMIDITY: [
"Humidity",
"mdi:water-percent",
PERCENTAGE,
DEVICE_CLASS_HUMIDITY,
],
SENSOR_PRESSURE: [
"Pressure",
"mdi:arrow-down-bold",
PRESSURE_HPA,
DEVICE_CLASS_PRESSURE,
],
SENSOR_PRESSURE_AT_SEALEVEL: [
"Pressure at sealevel",
"mdi:download",
PRESSURE_HPA,
DEVICE_CLASS_PRESSURE,
],
SENSOR_PM10: [ SENSOR_PM10: [
"PM10", "PM10",
"mdi:thought-bubble", "mdi:thought-bubble",
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
None,
], ],
SENSOR_PM2_5: [ SENSOR_PM2_5: [
"PM2.5", "PM2.5",
"mdi:thought-bubble-outline", "mdi:thought-bubble-outline",
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
None,
], ],
} }

View file

@ -31,14 +31,20 @@ async def async_setup_entry(hass, entry, async_add_entities):
sensors = [] sensors = []
for sensor_type in luftdaten.sensor_conditions: for sensor_type in luftdaten.sensor_conditions:
try: try:
name, icon, unit = SENSORS[sensor_type] name, icon, unit, device_class = SENSORS[sensor_type]
except KeyError: except KeyError:
_LOGGER.debug("Unknown sensor value type: %s", sensor_type) _LOGGER.debug("Unknown sensor value type: %s", sensor_type)
continue continue
sensors.append( sensors.append(
LuftdatenSensor( LuftdatenSensor(
luftdaten, sensor_type, name, icon, unit, entry.data[CONF_SHOW_ON_MAP] luftdaten,
sensor_type,
name,
icon,
unit,
device_class,
entry.data[CONF_SHOW_ON_MAP],
) )
) )
@ -48,7 +54,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
class LuftdatenSensor(SensorEntity): class LuftdatenSensor(SensorEntity):
"""Implementation of a Luftdaten sensor.""" """Implementation of a Luftdaten sensor."""
def __init__(self, luftdaten, sensor_type, name, icon, unit, show): def __init__(self, luftdaten, sensor_type, name, icon, unit, device_class, show):
"""Initialize the Luftdaten sensor.""" """Initialize the Luftdaten sensor."""
self._async_unsub_dispatcher_connect = None self._async_unsub_dispatcher_connect = None
self.luftdaten = luftdaten self.luftdaten = luftdaten
@ -59,6 +65,7 @@ class LuftdatenSensor(SensorEntity):
self._unit_of_measurement = unit self._unit_of_measurement = unit
self._show_on_map = show self._show_on_map = show
self._attrs = {} self._attrs = {}
self._attr_device_class = device_class
@property @property
def icon(self): def icon(self):

View file

@ -13,6 +13,7 @@ from homeassistant.const import (
CONF_SSL, CONF_SSL,
CONF_USERNAME, CONF_USERNAME,
CONF_VERIFY_SSL, CONF_VERIFY_SSL,
DEVICE_CLASS_TEMPERATURE,
STATE_OFF, STATE_OFF,
STATE_ON, STATE_ON,
TEMP_CELSIUS, TEMP_CELSIUS,
@ -83,7 +84,7 @@ class MfiSensor(SensorEntity):
@property @property
def name(self): def name(self):
"""Return the name of th sensor.""" """Return the name of the sensor."""
return self._port.label return self._port.label
@property @property
@ -100,6 +101,19 @@ class MfiSensor(SensorEntity):
digits = DIGITS.get(self._port.tag, 0) digits = DIGITS.get(self._port.tag, 0)
return round(self._port.value, digits) return round(self._port.value, digits)
@property
def device_class(self):
"""Return the device class of the sensor."""
try:
tag = self._port.tag
except ValueError:
return None
if tag == "temperature":
return DEVICE_CLASS_TEMPERATURE
return None
@property @property
def unit_of_measurement(self): def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any.""" """Return the unit of measurement of this entity, if any."""

View file

@ -9,6 +9,8 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONDUCTIVITY, CONDUCTIVITY,
DEGREE, DEGREE,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_TEMPERATURE,
ELECTRICAL_CURRENT_AMPERE, ELECTRICAL_CURRENT_AMPERE,
ELECTRICAL_VOLT_AMPERE, ELECTRICAL_VOLT_AMPERE,
ENERGY_KILO_WATT_HOUR, ENERGY_KILO_WATT_HOUR,
@ -31,37 +33,37 @@ from .const import MYSENSORS_DISCOVERY, DiscoveryInfo
from .helpers import on_unload from .helpers import on_unload
SENSORS: dict[str, list[str | None] | dict[str, list[str | None]]] = { SENSORS: dict[str, list[str | None] | dict[str, list[str | None]]] = {
"V_TEMP": [None, "mdi:thermometer"], "V_TEMP": [None, None, DEVICE_CLASS_TEMPERATURE],
"V_HUM": [PERCENTAGE, "mdi:water-percent"], "V_HUM": [PERCENTAGE, "mdi:water-percent", DEVICE_CLASS_HUMIDITY],
"V_DIMMER": [PERCENTAGE, "mdi:percent"], "V_DIMMER": [PERCENTAGE, "mdi:percent", None],
"V_PERCENTAGE": [PERCENTAGE, "mdi:percent"], "V_PERCENTAGE": [PERCENTAGE, "mdi:percent", None],
"V_PRESSURE": [None, "mdi:gauge"], "V_PRESSURE": [None, "mdi:gauge", None],
"V_FORECAST": [None, "mdi:weather-partly-cloudy"], "V_FORECAST": [None, "mdi:weather-partly-cloudy", None],
"V_RAIN": [None, "mdi:weather-rainy"], "V_RAIN": [None, "mdi:weather-rainy", None],
"V_RAINRATE": [None, "mdi:weather-rainy"], "V_RAINRATE": [None, "mdi:weather-rainy", None],
"V_WIND": [None, "mdi:weather-windy"], "V_WIND": [None, "mdi:weather-windy", None],
"V_GUST": [None, "mdi:weather-windy"], "V_GUST": [None, "mdi:weather-windy", None],
"V_DIRECTION": [DEGREE, "mdi:compass"], "V_DIRECTION": [DEGREE, "mdi:compass", None],
"V_WEIGHT": [MASS_KILOGRAMS, "mdi:weight-kilogram"], "V_WEIGHT": [MASS_KILOGRAMS, "mdi:weight-kilogram", None],
"V_DISTANCE": [LENGTH_METERS, "mdi:ruler"], "V_DISTANCE": [LENGTH_METERS, "mdi:ruler", None],
"V_IMPEDANCE": ["ohm", None], "V_IMPEDANCE": ["ohm", None, None],
"V_WATT": [POWER_WATT, None], "V_WATT": [POWER_WATT, None, None],
"V_KWH": [ENERGY_KILO_WATT_HOUR, None], "V_KWH": [ENERGY_KILO_WATT_HOUR, None, None],
"V_LIGHT_LEVEL": [PERCENTAGE, "mdi:white-balance-sunny"], "V_LIGHT_LEVEL": [PERCENTAGE, "mdi:white-balance-sunny", None],
"V_FLOW": [LENGTH_METERS, "mdi:gauge"], "V_FLOW": [LENGTH_METERS, "mdi:gauge", None],
"V_VOLUME": [f"{VOLUME_CUBIC_METERS}", None], "V_VOLUME": [f"{VOLUME_CUBIC_METERS}", None, None],
"V_LEVEL": { "V_LEVEL": {
"S_SOUND": ["dB", "mdi:volume-high"], "S_SOUND": ["dB", "mdi:volume-high", None],
"S_VIBRATION": [FREQUENCY_HERTZ, None], "S_VIBRATION": [FREQUENCY_HERTZ, None, None],
"S_LIGHT_LEVEL": [LIGHT_LUX, "mdi:white-balance-sunny"], "S_LIGHT_LEVEL": [LIGHT_LUX, "mdi:white-balance-sunny", None],
}, },
"V_VOLTAGE": [VOLT, "mdi:flash"], "V_VOLTAGE": [VOLT, "mdi:flash", None],
"V_CURRENT": [ELECTRICAL_CURRENT_AMPERE, "mdi:flash-auto"], "V_CURRENT": [ELECTRICAL_CURRENT_AMPERE, "mdi:flash-auto", None],
"V_PH": ["pH", None], "V_PH": ["pH", None, None],
"V_ORP": ["mV", None], "V_ORP": ["mV", None, None],
"V_EC": [CONDUCTIVITY, None], "V_EC": [CONDUCTIVITY, None, None],
"V_VAR": ["var", None], "V_VAR": ["var", None, None],
"V_VA": [ELECTRICAL_VOLT_AMPERE, None], "V_VA": [ELECTRICAL_VOLT_AMPERE, None, None],
} }
@ -107,9 +109,15 @@ class MySensorsSensor(mysensors.device.MySensorsEntity, SensorEntity):
@property @property
def state(self) -> str | None: def state(self) -> str | None:
"""Return the state of the device.""" """Return the state of this entity."""
return self._values.get(self.value_type) return self._values.get(self.value_type)
@property
def device_class(self) -> str | None:
"""Return the device class of this entity."""
icon = self._get_sensor_type()[2]
return icon
@property @property
def icon(self) -> str | None: def icon(self) -> str | None:
"""Return the icon to use in the frontend, if any.""" """Return the icon to use in the frontend, if any."""
@ -140,9 +148,11 @@ class MySensorsSensor(mysensors.device.MySensorsEntity, SensorEntity):
pres = self.gateway.const.Presentation pres = self.gateway.const.Presentation
set_req = self.gateway.const.SetReq set_req = self.gateway.const.SetReq
_sensor_type = SENSORS.get(set_req(self.value_type).name, [None, None]) _sensor_type = SENSORS.get(set_req(self.value_type).name, [None, None, None])
if isinstance(_sensor_type, dict): if isinstance(_sensor_type, dict):
sensor_type = _sensor_type.get(pres(self.child_type).name, [None, None]) sensor_type = _sensor_type.get(
pres(self.child_type).name, [None, None, None]
)
else: else:
sensor_type = _sensor_type sensor_type = _sensor_type
return sensor_type return sensor_type

View file

@ -1,7 +1,7 @@
"""Support for Notion sensors.""" """Support for Notion sensors."""
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import TEMP_CELSIUS from homeassistant.const import DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
@ -9,7 +9,9 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from . import NotionEntity from . import NotionEntity
from .const import DATA_COORDINATOR, DOMAIN, LOGGER, SENSOR_TEMPERATURE from .const import DATA_COORDINATOR, DOMAIN, LOGGER, SENSOR_TEMPERATURE
SENSOR_TYPES = {SENSOR_TEMPERATURE: ("Temperature", "temperature", TEMP_CELSIUS)} SENSOR_TYPES = {
SENSOR_TEMPERATURE: ("Temperature", DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS)
}
async def async_setup_entry( async def async_setup_entry(

View file

@ -9,6 +9,7 @@ from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import ( from homeassistant.const import (
CONF_HOST, CONF_HOST,
CONF_MONITORED_VARIABLES, CONF_MONITORED_VARIABLES,
DEVICE_CLASS_TEMPERATURE,
ENERGY_KILO_WATT_HOUR, ENERGY_KILO_WATT_HOUR,
TEMP_CELSIUS, TEMP_CELSIUS,
TIME_MINUTES, TIME_MINUTES,
@ -18,13 +19,13 @@ import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
SENSOR_TYPES = { SENSOR_TYPES = {
"status": ["Charging Status", None], "status": ["Charging Status", None, None],
"charge_time": ["Charge Time Elapsed", TIME_MINUTES], "charge_time": ["Charge Time Elapsed", TIME_MINUTES, None],
"ambient_temp": ["Ambient Temperature", TEMP_CELSIUS], "ambient_temp": ["Ambient Temperature", TEMP_CELSIUS, DEVICE_CLASS_TEMPERATURE],
"ir_temp": ["IR Temperature", TEMP_CELSIUS], "ir_temp": ["IR Temperature", TEMP_CELSIUS, DEVICE_CLASS_TEMPERATURE],
"rtc_temp": ["RTC Temperature", TEMP_CELSIUS], "rtc_temp": ["RTC Temperature", TEMP_CELSIUS, DEVICE_CLASS_TEMPERATURE],
"usage_session": ["Usage this Session", ENERGY_KILO_WATT_HOUR], "usage_session": ["Usage this Session", ENERGY_KILO_WATT_HOUR, None],
"usage_total": ["Total Usage", ENERGY_KILO_WATT_HOUR], "usage_total": ["Total Usage", ENERGY_KILO_WATT_HOUR, None],
} }
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
@ -61,6 +62,7 @@ class OpenEVSESensor(SensorEntity):
self._state = None self._state = None
self.charger = charger self.charger = charger
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1] self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
self._attr_device_class = SENSOR_TYPES[sensor_type][2]
@property @property
def name(self): def name(self):

View file

@ -18,6 +18,7 @@ from homeassistant.const import (
CONF_VERIFY_SSL, CONF_VERIFY_SSL,
DATA_GIBIBYTES, DATA_GIBIBYTES,
DATA_RATE_MEBIBYTES_PER_SECOND, DATA_RATE_MEBIBYTES_PER_SECOND,
DEVICE_CLASS_TEMPERATURE,
PERCENTAGE, PERCENTAGE,
TEMP_CELSIUS, TEMP_CELSIUS,
) )
@ -56,31 +57,46 @@ NOTIFICATION_ID = "qnap_notification"
NOTIFICATION_TITLE = "QNAP Sensor Setup" NOTIFICATION_TITLE = "QNAP Sensor Setup"
_SYSTEM_MON_COND = { _SYSTEM_MON_COND = {
"status": ["Status", None, "mdi:checkbox-marked-circle-outline"], "status": ["Status", None, "mdi:checkbox-marked-circle-outline", None],
"system_temp": ["System Temperature", TEMP_CELSIUS, "mdi:thermometer"], "system_temp": ["System Temperature", TEMP_CELSIUS, None, DEVICE_CLASS_TEMPERATURE],
} }
_CPU_MON_COND = { _CPU_MON_COND = {
"cpu_temp": ["CPU Temperature", TEMP_CELSIUS, "mdi:thermometer"], "cpu_temp": ["CPU Temperature", TEMP_CELSIUS, None, DEVICE_CLASS_TEMPERATURE],
"cpu_usage": ["CPU Usage", PERCENTAGE, "mdi:chip"], "cpu_usage": ["CPU Usage", PERCENTAGE, "mdi:chip", None],
} }
_MEMORY_MON_COND = { _MEMORY_MON_COND = {
"memory_free": ["Memory Available", DATA_GIBIBYTES, "mdi:memory"], "memory_free": ["Memory Available", DATA_GIBIBYTES, "mdi:memory", None],
"memory_used": ["Memory Used", DATA_GIBIBYTES, "mdi:memory"], "memory_used": ["Memory Used", DATA_GIBIBYTES, "mdi:memory", None],
"memory_percent_used": ["Memory Usage", PERCENTAGE, "mdi:memory"], "memory_percent_used": ["Memory Usage", PERCENTAGE, "mdi:memory", None],
} }
_NETWORK_MON_COND = { _NETWORK_MON_COND = {
"network_link_status": ["Network Link", None, "mdi:checkbox-marked-circle-outline"], "network_link_status": [
"network_tx": ["Network Up", DATA_RATE_MEBIBYTES_PER_SECOND, "mdi:upload"], "Network Link",
"network_rx": ["Network Down", DATA_RATE_MEBIBYTES_PER_SECOND, "mdi:download"], None,
"mdi:checkbox-marked-circle-outline",
None,
],
"network_tx": ["Network Up", DATA_RATE_MEBIBYTES_PER_SECOND, "mdi:upload", None],
"network_rx": [
"Network Down",
DATA_RATE_MEBIBYTES_PER_SECOND,
"mdi:download",
None,
],
} }
_DRIVE_MON_COND = { _DRIVE_MON_COND = {
"drive_smart_status": ["SMART Status", None, "mdi:checkbox-marked-circle-outline"], "drive_smart_status": [
"drive_temp": ["Temperature", TEMP_CELSIUS, "mdi:thermometer"], "SMART Status",
None,
"mdi:checkbox-marked-circle-outline",
None,
],
"drive_temp": ["Temperature", TEMP_CELSIUS, None, None, DEVICE_CLASS_TEMPERATURE],
} }
_VOLUME_MON_COND = { _VOLUME_MON_COND = {
"volume_size_used": ["Used Space", DATA_GIBIBYTES, "mdi:chart-pie"], "volume_size_used": ["Used Space", DATA_GIBIBYTES, "mdi:chart-pie", None],
"volume_size_free": ["Free Space", DATA_GIBIBYTES, "mdi:chart-pie"], "volume_size_free": ["Free Space", DATA_GIBIBYTES, "mdi:chart-pie", None],
"volume_percentage_used": ["Volume Used", PERCENTAGE, "mdi:chart-pie"], "volume_percentage_used": ["Volume Used", PERCENTAGE, "mdi:chart-pie", None],
} }
_MONITORED_CONDITIONS = ( _MONITORED_CONDITIONS = (
@ -210,6 +226,7 @@ class QNAPSensor(SensorEntity):
self.var_icon = variable_info[2] self.var_icon = variable_info[2]
self.monitor_device = monitor_device self.monitor_device = monitor_device
self._api = api self._api = api
self._attr_device_class = variable_info[3]
@property @property
def name(self): def name(self):