Set device_class on temperature sensors F-K (#52918)

* Set device_class on temperature sensors F-K

* Fix juicenet sensor
pull/52970/head
Erik Montnemery 2021-07-12 20:32:55 +02:00 committed by GitHub
parent 646862ec96
commit 3e09787d85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 123 additions and 41 deletions

View File

@ -16,6 +16,7 @@ from homeassistant.const import (
CONCENTRATION_PARTS_PER_MILLION, CONCENTRATION_PARTS_PER_MILLION,
CONF_TOKEN, CONF_TOKEN,
CONF_USERNAME, CONF_USERNAME,
DEVICE_CLASS_TEMPERATURE,
PERCENTAGE, PERCENTAGE,
TEMP_CELSIUS, TEMP_CELSIUS,
TIME_SECONDS, TIME_SECONDS,
@ -36,17 +37,23 @@ ATTR_VOLATILE_ORGANIC_COMPOUNDS = "VOC"
ATTR_FOOBOT_INDEX = "index" ATTR_FOOBOT_INDEX = "index"
SENSOR_TYPES = { SENSOR_TYPES = {
"time": [ATTR_TIME, TIME_SECONDS], "time": [ATTR_TIME, TIME_SECONDS, None, None],
"pm": [ATTR_PM2_5, CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, "mdi:cloud"], "pm": [ATTR_PM2_5, CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, "mdi:cloud", None],
"tmp": [ATTR_TEMPERATURE, TEMP_CELSIUS, "mdi:thermometer"], "tmp": [ATTR_TEMPERATURE, TEMP_CELSIUS, None, DEVICE_CLASS_TEMPERATURE],
"hum": [ATTR_HUMIDITY, PERCENTAGE, "mdi:water-percent"], "hum": [ATTR_HUMIDITY, PERCENTAGE, "mdi:water-percent", None],
"co2": [ATTR_CARBON_DIOXIDE, CONCENTRATION_PARTS_PER_MILLION, "mdi:molecule-co2"], "co2": [
ATTR_CARBON_DIOXIDE,
CONCENTRATION_PARTS_PER_MILLION,
"mdi:molecule-co2",
None,
],
"voc": [ "voc": [
ATTR_VOLATILE_ORGANIC_COMPOUNDS, ATTR_VOLATILE_ORGANIC_COMPOUNDS,
CONCENTRATION_PARTS_PER_BILLION, CONCENTRATION_PARTS_PER_BILLION,
"mdi:cloud", "mdi:cloud",
None,
], ],
"allpollu": [ATTR_FOOBOT_INDEX, PERCENTAGE, "mdi:percent"], "allpollu": [ATTR_FOOBOT_INDEX, PERCENTAGE, "mdi:percent", None],
} }
SCAN_INTERVAL = timedelta(minutes=10) SCAN_INTERVAL = timedelta(minutes=10)
@ -108,6 +115,11 @@ class FoobotSensor(SensorEntity):
"""Return the name of the sensor.""" """Return the name of the sensor."""
return self._name return self._name
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return SENSOR_TYPES[self.type][3]
@property @property
def icon(self): def icon(self):
"""Icon to use in the frontend.""" """Icon to use in the frontend."""

View File

@ -9,6 +9,7 @@ from homeassistant.const import (
ATTR_NAME, ATTR_NAME,
ATTR_UNIT_OF_MEASUREMENT, ATTR_UNIT_OF_MEASUREMENT,
DEVICE_CLASS_BATTERY, DEVICE_CLASS_BATTERY,
DEVICE_CLASS_TEMPERATURE,
PERCENTAGE, PERCENTAGE,
TEMP_CELSIUS, TEMP_CELSIUS,
) )
@ -44,7 +45,7 @@ async def async_setup_entry(
ATTR_NAME: f"{device.name}", ATTR_NAME: f"{device.name}",
ATTR_ENTITY_ID: f"{device.ain}", ATTR_ENTITY_ID: f"{device.ain}",
ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS, ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS,
ATTR_DEVICE_CLASS: None, ATTR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE,
}, },
coordinator, coordinator,
ain, ain,

View File

@ -1,7 +1,13 @@
"""Constants for Glances component.""" """Constants for Glances component."""
import sys import sys
from homeassistant.const import DATA_GIBIBYTES, DATA_MEBIBYTES, PERCENTAGE, TEMP_CELSIUS from homeassistant.const import (
DATA_GIBIBYTES,
DATA_MEBIBYTES,
DEVICE_CLASS_TEMPERATURE,
PERCENTAGE,
TEMP_CELSIUS,
)
DOMAIN = "glances" DOMAIN = "glances"
CONF_VERSION = "version" CONF_VERSION = "version"
@ -21,31 +27,50 @@ else:
CPU_ICON = "mdi:cpu-32-bit" CPU_ICON = "mdi:cpu-32-bit"
SENSOR_TYPES = { SENSOR_TYPES = {
"disk_use_percent": ["fs", "used percent", PERCENTAGE, "mdi:harddisk"], "disk_use_percent": ["fs", "used percent", PERCENTAGE, "mdi:harddisk", None],
"disk_use": ["fs", "used", DATA_GIBIBYTES, "mdi:harddisk"], "disk_use": ["fs", "used", DATA_GIBIBYTES, "mdi:harddisk", None],
"disk_free": ["fs", "free", DATA_GIBIBYTES, "mdi:harddisk"], "disk_free": ["fs", "free", DATA_GIBIBYTES, "mdi:harddisk", None],
"memory_use_percent": ["mem", "RAM used percent", PERCENTAGE, "mdi:memory"], "memory_use_percent": ["mem", "RAM used percent", PERCENTAGE, "mdi:memory", None],
"memory_use": ["mem", "RAM used", DATA_MEBIBYTES, "mdi:memory"], "memory_use": ["mem", "RAM used", DATA_MEBIBYTES, "mdi:memory", None],
"memory_free": ["mem", "RAM free", DATA_MEBIBYTES, "mdi:memory"], "memory_free": ["mem", "RAM free", DATA_MEBIBYTES, "mdi:memory", None],
"swap_use_percent": ["memswap", "Swap used percent", PERCENTAGE, "mdi:memory"], "swap_use_percent": [
"swap_use": ["memswap", "Swap used", DATA_GIBIBYTES, "mdi:memory"], "memswap",
"swap_free": ["memswap", "Swap free", DATA_GIBIBYTES, "mdi:memory"], "Swap used percent",
"processor_load": ["load", "CPU load", "15 min", CPU_ICON], PERCENTAGE,
"process_running": ["processcount", "Running", "Count", CPU_ICON], "mdi:memory",
"process_total": ["processcount", "Total", "Count", CPU_ICON], None,
"process_thread": ["processcount", "Thread", "Count", CPU_ICON], ],
"process_sleeping": ["processcount", "Sleeping", "Count", CPU_ICON], "swap_use": ["memswap", "Swap used", DATA_GIBIBYTES, "mdi:memory", None],
"cpu_use_percent": ["cpu", "CPU used", PERCENTAGE, CPU_ICON], "swap_free": ["memswap", "Swap free", DATA_GIBIBYTES, "mdi:memory", None],
"temperature_core": ["sensors", "Temperature", TEMP_CELSIUS, "mdi:thermometer"], "processor_load": ["load", "CPU load", "15 min", CPU_ICON, None],
"temperature_hdd": ["sensors", "Temperature", TEMP_CELSIUS, "mdi:thermometer"], "process_running": ["processcount", "Running", "Count", CPU_ICON, None],
"fan_speed": ["sensors", "Fan speed", "RPM", "mdi:fan"], "process_total": ["processcount", "Total", "Count", CPU_ICON, None],
"battery": ["sensors", "Charge", PERCENTAGE, "mdi:battery"], "process_thread": ["processcount", "Thread", "Count", CPU_ICON, None],
"docker_active": ["docker", "Containers active", "", "mdi:docker"], "process_sleeping": ["processcount", "Sleeping", "Count", CPU_ICON, None],
"docker_cpu_use": ["docker", "Containers CPU used", PERCENTAGE, "mdi:docker"], "cpu_use_percent": ["cpu", "CPU used", PERCENTAGE, CPU_ICON, None],
"temperature_core": [
"sensors",
"Temperature",
TEMP_CELSIUS,
None,
DEVICE_CLASS_TEMPERATURE,
],
"temperature_hdd": [
"sensors",
"Temperature",
TEMP_CELSIUS,
None,
DEVICE_CLASS_TEMPERATURE,
],
"fan_speed": ["sensors", "Fan speed", "RPM", "mdi:fan", None],
"battery": ["sensors", "Charge", PERCENTAGE, "mdi:battery", None],
"docker_active": ["docker", "Containers active", "", "mdi:docker", None],
"docker_cpu_use": ["docker", "Containers CPU used", PERCENTAGE, "mdi:docker", None],
"docker_memory_use": [ "docker_memory_use": [
"docker", "docker",
"Containers RAM used", "Containers RAM used",
DATA_MEBIBYTES, DATA_MEBIBYTES,
"mdi:docker", "mdi:docker",
None,
], ],
} }

View File

@ -91,6 +91,11 @@ class GlancesSensor(SensorEntity):
"""Set unique_id for sensor.""" """Set unique_id for sensor."""
return f"{self.glances_data.host}-{self.name}" return f"{self.glances_data.host}-{self.name}"
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return self.sensor_details[4]
@property @property
def icon(self): def icon(self):
"""Icon to use in the frontend, if any.""" """Icon to use in the frontend, if any."""

View File

@ -12,6 +12,7 @@ from homeassistant.const import (
CONF_HOST, CONF_HOST,
CONF_NAME, CONF_NAME,
CONF_PORT, CONF_PORT,
DEVICE_CLASS_TEMPERATURE,
TEMP_CELSIUS, TEMP_CELSIUS,
TEMP_FAHRENHEIT, TEMP_FAHRENHEIT,
) )
@ -81,6 +82,11 @@ class HddTempSensor(SensorEntity):
"""Return the state of the device.""" """Return the state of the device."""
return self._state return self._state
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return DEVICE_CLASS_TEMPERATURE
@property @property
def unit_of_measurement(self): def unit_of_measurement(self):
"""Return the unit the value is expressed in.""" """Return the unit the value is expressed in."""

View File

@ -1,6 +1,7 @@
"""Support for IHC sensors.""" """Support for IHC sensors."""
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorEntity
from homeassistant.const import CONF_UNIT_OF_MEASUREMENT from homeassistant.const import CONF_UNIT_OF_MEASUREMENT, DEVICE_CLASS_TEMPERATURE
from homeassistant.util.unit_system import TEMPERATURE_UNITS
from . import IHC_CONTROLLER, IHC_INFO from . import IHC_CONTROLLER, IHC_INFO
from .ihcdevice import IHCDevice from .ihcdevice import IHCDevice
@ -37,6 +38,15 @@ class IHCSensor(IHCDevice, SensorEntity):
self._state = None self._state = None
self._unit_of_measurement = unit self._unit_of_measurement = unit
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return (
DEVICE_CLASS_TEMPERATURE
if self._unit_of_measurement in TEMPERATURE_UNITS
else None
)
@property @property
def state(self): def state(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""

View File

@ -1,6 +1,11 @@
"""Support for monitoring juicenet/juicepoint/juicebox based EVSE sensors.""" """Support for monitoring juicenet/juicepoint/juicebox based EVSE sensors."""
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
from homeassistant.const import ( from homeassistant.const import (
DEVICE_CLASS_CURRENT,
DEVICE_CLASS_ENERGY,
DEVICE_CLASS_POWER,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_VOLTAGE,
ELECTRICAL_CURRENT_AMPERE, ELECTRICAL_CURRENT_AMPERE,
ENERGY_WATT_HOUR, ENERGY_WATT_HOUR,
POWER_WATT, POWER_WATT,
@ -13,13 +18,23 @@ from .const import DOMAIN, JUICENET_API, JUICENET_COORDINATOR
from .entity import JuiceNetDevice from .entity import JuiceNetDevice
SENSOR_TYPES = { SENSOR_TYPES = {
"status": ["Charging Status", None, None], "status": ["Charging Status", None, None, None],
"temperature": ["Temperature", TEMP_CELSIUS, STATE_CLASS_MEASUREMENT], "temperature": [
"voltage": ["Voltage", VOLT, None], "Temperature",
"amps": ["Amps", ELECTRICAL_CURRENT_AMPERE, STATE_CLASS_MEASUREMENT], TEMP_CELSIUS,
"watts": ["Watts", POWER_WATT, STATE_CLASS_MEASUREMENT], DEVICE_CLASS_TEMPERATURE,
"charge_time": ["Charge time", TIME_SECONDS, None], STATE_CLASS_MEASUREMENT,
"energy_added": ["Energy added", ENERGY_WATT_HOUR, None], ],
"voltage": ["Voltage", VOLT, DEVICE_CLASS_VOLTAGE, None],
"amps": [
"Amps",
ELECTRICAL_CURRENT_AMPERE,
DEVICE_CLASS_CURRENT,
STATE_CLASS_MEASUREMENT,
],
"watts": ["Watts", POWER_WATT, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT],
"charge_time": ["Charge time", TIME_SECONDS, None, None],
"energy_added": ["Energy added", ENERGY_WATT_HOUR, DEVICE_CLASS_ENERGY, None],
} }
@ -44,7 +59,8 @@ class JuiceNetSensorDevice(JuiceNetDevice, SensorEntity):
super().__init__(device, sensor_type, coordinator) super().__init__(device, sensor_type, coordinator)
self._name = SENSOR_TYPES[sensor_type][0] self._name = SENSOR_TYPES[sensor_type][0]
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1] self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
self._attr_state_class = SENSOR_TYPES[sensor_type][2] self._attr_device_class = SENSOR_TYPES[sensor_type][2]
self._attr_state_class = SENSOR_TYPES[sensor_type][3]
@property @property
def name(self): def name(self):

View File

@ -1,13 +1,20 @@
"""Support for Kaiterra Temperature ahn Humidity Sensors.""" """Support for Kaiterra Temperature ahn Humidity Sensors."""
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorEntity
from homeassistant.const import CONF_DEVICE_ID, CONF_NAME, TEMP_CELSIUS, TEMP_FAHRENHEIT from homeassistant.const import (
CONF_DEVICE_ID,
CONF_NAME,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_TEMPERATURE,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
)
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .const import DISPATCHER_KAITERRA, DOMAIN from .const import DISPATCHER_KAITERRA, DOMAIN
SENSORS = [ SENSORS = [
{"name": "Temperature", "prop": "rtemp", "device_class": "temperature"}, {"name": "Temperature", "prop": "rtemp", "device_class": DEVICE_CLASS_TEMPERATURE},
{"name": "Humidity", "prop": "rhumid", "device_class": "humidity"}, {"name": "Humidity", "prop": "rhumid", "device_class": DEVICE_CLASS_HUMIDITY},
] ]