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 homeassistant.components.sensor import SensorEntity
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import FREQUENCY_GIGAHERTZ
from homeassistant.const import UnitOfFrequency
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -32,9 +32,10 @@ async def async_setup_entry(
class CPUSpeedSensor(SensorEntity):
"""Representation of a CPU sensor."""
_attr_device_class = SensorDeviceClass.FREQUENCY
_attr_icon = "mdi:pulse"
_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:
"""Initialize the CPU sensor."""

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -16,12 +16,10 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_PORT,
ELECTRIC_POTENTIAL_VOLT,
FREQUENCY_GIGAHERTZ,
FREQUENCY_HERTZ,
FREQUENCY_MEGAHERTZ,
PERCENTAGE,
REVOLUTIONS_PER_MINUTE,
TEMP_CELSIUS,
UnitOfFrequency,
UnitOfInformation,
UnitOfPower,
)
@ -138,7 +136,8 @@ BASE_SENSOR_TYPES: tuple[SystemBridgeSensorEntityDescription, ...] = (
key="cpu_speed",
name="CPU Speed",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=FREQUENCY_GIGAHERTZ,
native_unit_of_measurement=UnitOfFrequency.GIGAHERTZ,
device_class=SensorDeviceClass.FREQUENCY,
icon="mdi:speedometer",
value=cpu_speed,
),
@ -347,7 +346,8 @@ async def async_setup_entry(
key=f"display_{display['name']}_refresh_rate",
name=f"Display {display['name']} Refresh Rate",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=FREQUENCY_HERTZ,
native_unit_of_measurement=UnitOfFrequency.HERTZ,
device_class=SensorDeviceClass.FREQUENCY,
icon="mdi:monitor",
value=lambda data, k=display["key"]: getattr(
data.display, f"{k}_refresh_rate"
@ -376,7 +376,8 @@ async def async_setup_entry(
name=f"{gpu['name']} Clock Speed",
entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=FREQUENCY_MEGAHERTZ,
native_unit_of_measurement=UnitOfFrequency.MEGAHERTZ,
device_class=SensorDeviceClass.FREQUENCY,
icon="mdi:speedometer",
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",
entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=FREQUENCY_MEGAHERTZ,
native_unit_of_measurement=UnitOfFrequency.MEGAHERTZ,
device_class=SensorDeviceClass.FREQUENCY,
icon="mdi:speedometer",
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_POTENTIAL_VOLT,
ENERGY_WATT_HOUR,
FREQUENCY_HERTZ,
TEMP_CELSIUS,
UnitOfFrequency,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
@ -66,8 +66,9 @@ WALL_CONNECTOR_SENSORS = [
WallConnectorSensorDescription(
key="grid_hz",
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),
device_class=SensorDeviceClass.FREQUENCY,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),

View File

@ -8,6 +8,7 @@ from homeassistant.components.homeassistant import (
DOMAIN as HOME_ASSISTANT_DOMAIN,
SERVICE_UPDATE_ENTITY,
)
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_ENTITY_ID,
@ -42,7 +43,7 @@ async def test_sensor(
assert state.state == "3.2"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "CPU Speed"
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_BRAND) == "Intel Ryzen 7"