Use FREQUENCY device class in integrations (#83916)

* Use FREQUENCY device class in integrations

* Adjust tesla

* Undo goodwe

* Adjust cpuspeed tests
This commit is contained in:
epenet 2022-12-13 14:14:42 +01:00 committed by GitHub
parent fc43fb17a9
commit 85235ea5f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 35 additions and 24 deletions

View File

@ -3,9 +3,9 @@ from __future__ import annotations
from cpuinfo import cpuinfo from cpuinfo import cpuinfo
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import FREQUENCY_GIGAHERTZ from homeassistant.const import UnitOfFrequency
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -32,9 +32,10 @@ async def async_setup_entry(
class CPUSpeedSensor(SensorEntity): class CPUSpeedSensor(SensorEntity):
"""Representation of a CPU sensor.""" """Representation of a CPU sensor."""
_attr_device_class = SensorDeviceClass.FREQUENCY
_attr_icon = "mdi:pulse" _attr_icon = "mdi:pulse"
_attr_has_entity_name = True _attr_has_entity_name = True
_attr_native_unit_of_measurement = FREQUENCY_GIGAHERTZ _attr_native_unit_of_measurement = UnitOfFrequency.GIGAHERTZ
def __init__(self, entry: ConfigEntry) -> None: def __init__(self, entry: ConfigEntry) -> None:
"""Initialize the CPU sensor.""" """Initialize the CPU sensor."""

View File

@ -5,9 +5,9 @@ from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.const import ( from homeassistant.const import (
ELECTRIC_POTENTIAL_VOLT, ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR, ENERGY_KILO_WATT_HOUR,
FREQUENCY_HERTZ,
TEMP_CELSIUS, TEMP_CELSIUS,
UnitOfElectricCurrent, UnitOfElectricCurrent,
UnitOfFrequency,
UnitOfPower, UnitOfPower,
) )
@ -131,7 +131,8 @@ INVERTER_SENSOR_TYPES: tuple[GrowattSensorEntityDescription, ...] = (
key="inverter_frequency", key="inverter_frequency",
name="AC frequency", name="AC frequency",
api_key="fac", api_key="fac",
native_unit_of_measurement=FREQUENCY_HERTZ, native_unit_of_measurement=UnitOfFrequency.HERTZ,
device_class=SensorDeviceClass.FREQUENCY,
precision=1, precision=1,
), ),
GrowattSensorEntityDescription( GrowattSensorEntityDescription(

View File

@ -5,9 +5,9 @@ from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.const import ( from homeassistant.const import (
ELECTRIC_POTENTIAL_VOLT, ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR, ENERGY_KILO_WATT_HOUR,
FREQUENCY_HERTZ,
PERCENTAGE, PERCENTAGE,
UnitOfElectricCurrent, UnitOfElectricCurrent,
UnitOfFrequency,
UnitOfPower, UnitOfPower,
) )
@ -149,7 +149,8 @@ STORAGE_SENSOR_TYPES: tuple[GrowattSensorEntityDescription, ...] = (
key="storage_ac_input_frequency_out", key="storage_ac_input_frequency_out",
name="AC input frequency", name="AC input frequency",
api_key="freqOutPut", api_key="freqOutPut",
native_unit_of_measurement=FREQUENCY_HERTZ, native_unit_of_measurement=UnitOfFrequency.HERTZ,
device_class=SensorDeviceClass.FREQUENCY,
precision=2, precision=2,
), ),
GrowattSensorEntityDescription( GrowattSensorEntityDescription(
@ -164,7 +165,8 @@ STORAGE_SENSOR_TYPES: tuple[GrowattSensorEntityDescription, ...] = (
key="storage_ac_output_frequency", key="storage_ac_output_frequency",
name="Ac output frequency", name="Ac output frequency",
api_key="freqGrid", api_key="freqGrid",
native_unit_of_measurement=FREQUENCY_HERTZ, native_unit_of_measurement=UnitOfFrequency.HERTZ,
device_class=SensorDeviceClass.FREQUENCY,
precision=2, precision=2,
), ),
GrowattSensorEntityDescription( GrowattSensorEntityDescription(

View File

@ -5,9 +5,9 @@ from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.const import ( from homeassistant.const import (
ELECTRIC_POTENTIAL_VOLT, ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR, ENERGY_KILO_WATT_HOUR,
FREQUENCY_HERTZ,
TEMP_CELSIUS, TEMP_CELSIUS,
UnitOfElectricCurrent, UnitOfElectricCurrent,
UnitOfFrequency,
UnitOfPower, UnitOfPower,
) )
@ -136,7 +136,8 @@ TLX_SENSOR_TYPES: tuple[GrowattSensorEntityDescription, ...] = (
key="tlx_frequency", key="tlx_frequency",
name="AC frequency", name="AC frequency",
api_key="fac", api_key="fac",
native_unit_of_measurement=FREQUENCY_HERTZ, native_unit_of_measurement=UnitOfFrequency.HERTZ,
device_class=SensorDeviceClass.FREQUENCY,
precision=1, precision=1,
), ),
GrowattSensorEntityDescription( GrowattSensorEntityDescription(

View File

@ -18,12 +18,12 @@ from homeassistant.const import (
ELECTRIC_POTENTIAL_VOLT, ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR, ENERGY_KILO_WATT_HOUR,
ENERGY_WATT_HOUR, ENERGY_WATT_HOUR,
FREQUENCY_HERTZ,
LIGHT_LUX, LIGHT_LUX,
PERCENTAGE, PERCENTAGE,
PRESSURE_HPA, PRESSURE_HPA,
TEMP_CELSIUS, TEMP_CELSIUS,
UnitOfElectricCurrent, UnitOfElectricCurrent,
UnitOfFrequency,
UnitOfPower, UnitOfPower,
UnitOfPrecipitationDepth, UnitOfPrecipitationDepth,
UnitOfSpeed, UnitOfSpeed,
@ -195,7 +195,8 @@ SENSOR_DESCRIPTIONS: dict[str, SensorEntityDescription] = {
), ),
"FREQUENCY": SensorEntityDescription( "FREQUENCY": SensorEntityDescription(
key="FREQUENCY", key="FREQUENCY",
native_unit_of_measurement=FREQUENCY_HERTZ, native_unit_of_measurement=UnitOfFrequency.HERTZ,
device_class=SensorDeviceClass.FREQUENCY,
), ),
"VALUE": SensorEntityDescription( "VALUE": SensorEntityDescription(
key="VALUE", key="VALUE",

View File

@ -17,10 +17,10 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
ELECTRIC_POTENTIAL_VOLT, ELECTRIC_POTENTIAL_VOLT,
ENERGY_WATT_HOUR, ENERGY_WATT_HOUR,
FREQUENCY_HERTZ,
PERCENTAGE, PERCENTAGE,
UnitOfApparentPower, UnitOfApparentPower,
UnitOfElectricCurrent, UnitOfElectricCurrent,
UnitOfFrequency,
UnitOfPower, UnitOfPower,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
@ -60,8 +60,9 @@ ENTITY_DESCRIPTION_KEY_MAP: dict[str, IotaWattSensorEntityDescription] = {
), ),
"Hz": IotaWattSensorEntityDescription( "Hz": IotaWattSensorEntityDescription(
"Hz", "Hz",
native_unit_of_measurement=FREQUENCY_HERTZ, native_unit_of_measurement=UnitOfFrequency.HERTZ,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.FREQUENCY,
icon="mdi:flash", icon="mdi:flash",
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
), ),

View File

@ -16,12 +16,10 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONF_PORT, CONF_PORT,
ELECTRIC_POTENTIAL_VOLT, ELECTRIC_POTENTIAL_VOLT,
FREQUENCY_GIGAHERTZ,
FREQUENCY_HERTZ,
FREQUENCY_MEGAHERTZ,
PERCENTAGE, PERCENTAGE,
REVOLUTIONS_PER_MINUTE, REVOLUTIONS_PER_MINUTE,
TEMP_CELSIUS, TEMP_CELSIUS,
UnitOfFrequency,
UnitOfInformation, UnitOfInformation,
UnitOfPower, UnitOfPower,
) )
@ -138,7 +136,8 @@ BASE_SENSOR_TYPES: tuple[SystemBridgeSensorEntityDescription, ...] = (
key="cpu_speed", key="cpu_speed",
name="CPU Speed", name="CPU Speed",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=FREQUENCY_GIGAHERTZ, native_unit_of_measurement=UnitOfFrequency.GIGAHERTZ,
device_class=SensorDeviceClass.FREQUENCY,
icon="mdi:speedometer", icon="mdi:speedometer",
value=cpu_speed, value=cpu_speed,
), ),
@ -347,7 +346,8 @@ async def async_setup_entry(
key=f"display_{display['name']}_refresh_rate", key=f"display_{display['name']}_refresh_rate",
name=f"Display {display['name']} Refresh Rate", name=f"Display {display['name']} Refresh Rate",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=FREQUENCY_HERTZ, native_unit_of_measurement=UnitOfFrequency.HERTZ,
device_class=SensorDeviceClass.FREQUENCY,
icon="mdi:monitor", icon="mdi:monitor",
value=lambda data, k=display["key"]: getattr( value=lambda data, k=display["key"]: getattr(
data.display, f"{k}_refresh_rate" data.display, f"{k}_refresh_rate"
@ -376,7 +376,8 @@ async def async_setup_entry(
name=f"{gpu['name']} Clock Speed", name=f"{gpu['name']} Clock Speed",
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=FREQUENCY_MEGAHERTZ, native_unit_of_measurement=UnitOfFrequency.MEGAHERTZ,
device_class=SensorDeviceClass.FREQUENCY,
icon="mdi:speedometer", icon="mdi:speedometer",
value=lambda data, k=gpu["key"]: gpu_core_clock_speed(data, k), value=lambda data, k=gpu["key"]: gpu_core_clock_speed(data, k),
), ),
@ -389,7 +390,8 @@ async def async_setup_entry(
name=f"{gpu['name']} Memory Clock Speed", name=f"{gpu['name']} Memory Clock Speed",
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=FREQUENCY_MEGAHERTZ, native_unit_of_measurement=UnitOfFrequency.MEGAHERTZ,
device_class=SensorDeviceClass.FREQUENCY,
icon="mdi:speedometer", icon="mdi:speedometer",
value=lambda data, k=gpu["key"]: gpu_memory_clock_speed(data, k), value=lambda data, k=gpu["key"]: gpu_memory_clock_speed(data, k),
), ),

View File

@ -13,8 +13,8 @@ from homeassistant.const import (
ELECTRIC_CURRENT_AMPERE, ELECTRIC_CURRENT_AMPERE,
ELECTRIC_POTENTIAL_VOLT, ELECTRIC_POTENTIAL_VOLT,
ENERGY_WATT_HOUR, ENERGY_WATT_HOUR,
FREQUENCY_HERTZ,
TEMP_CELSIUS, TEMP_CELSIUS,
UnitOfFrequency,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
@ -66,8 +66,9 @@ WALL_CONNECTOR_SENSORS = [
WallConnectorSensorDescription( WallConnectorSensorDescription(
key="grid_hz", key="grid_hz",
name=prefix_entity_name("Grid Frequency"), name=prefix_entity_name("Grid Frequency"),
native_unit_of_measurement=FREQUENCY_HERTZ, native_unit_of_measurement=UnitOfFrequency.HERTZ,
value_fn=lambda data: round(data[WALLCONNECTOR_DATA_VITALS].grid_hz, 3), value_fn=lambda data: round(data[WALLCONNECTOR_DATA_VITALS].grid_hz, 3),
device_class=SensorDeviceClass.FREQUENCY,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),

View File

@ -8,6 +8,7 @@ from homeassistant.components.homeassistant import (
DOMAIN as HOME_ASSISTANT_DOMAIN, DOMAIN as HOME_ASSISTANT_DOMAIN,
SERVICE_UPDATE_ENTITY, SERVICE_UPDATE_ENTITY,
) )
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.const import ( from homeassistant.const import (
ATTR_DEVICE_CLASS, ATTR_DEVICE_CLASS,
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
@ -42,7 +43,7 @@ async def test_sensor(
assert state.state == "3.2" assert state.state == "3.2"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "CPU Speed" assert state.attributes.get(ATTR_FRIENDLY_NAME) == "CPU Speed"
assert state.attributes.get(ATTR_ICON) == "mdi:pulse" assert state.attributes.get(ATTR_ICON) == "mdi:pulse"
assert ATTR_DEVICE_CLASS not in state.attributes assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.FREQUENCY
assert state.attributes.get(ATTR_ARCH) == "aargh" assert state.attributes.get(ATTR_ARCH) == "aargh"
assert state.attributes.get(ATTR_BRAND) == "Intel Ryzen 7" assert state.attributes.get(ATTR_BRAND) == "Intel Ryzen 7"