diff --git a/homeassistant/components/zwave_js/sensor.py b/homeassistant/components/zwave_js/sensor.py index f4b303aa1af..304a80f7940 100644 --- a/homeassistant/components/zwave_js/sensor.py +++ b/homeassistant/components/zwave_js/sensor.py @@ -22,8 +22,10 @@ from homeassistant.components.sensor import ( ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( + DEVICE_CLASS_CURRENT, DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE, + DEVICE_CLASS_VOLTAGE, TEMP_CELSIUS, TEMP_FAHRENHEIT, ) @@ -142,8 +144,14 @@ class ZwaveSensorBase(ZWaveBaseEntity, SensorEntity): return DEVICE_CLASS_HUMIDITY if "temperature" in property_lower: return DEVICE_CLASS_TEMPERATURE + if self.info.primary_value.metadata.unit == "A": + return DEVICE_CLASS_CURRENT if self.info.primary_value.metadata.unit == "W": return DEVICE_CLASS_POWER + if self.info.primary_value.metadata.unit == "kWh": + return DEVICE_CLASS_ENERGY + if self.info.primary_value.metadata.unit == "V": + return DEVICE_CLASS_VOLTAGE if self.info.primary_value.metadata.unit == "Lux": return DEVICE_CLASS_ILLUMINANCE return None @@ -241,9 +249,6 @@ class ZWaveMeterSensor(ZWaveNumericSensor, RestoreEntity): # Entity class attributes self._attr_state_class = STATE_CLASS_MEASUREMENT self._attr_last_reset = dt.utc_from_timestamp(0) - self._attr_device_class = DEVICE_CLASS_POWER - if self.info.primary_value.metadata.unit == "kWh": - self._attr_device_class = DEVICE_CLASS_ENERGY @callback def async_update_last_reset( diff --git a/tests/components/zwave_js/common.py b/tests/components/zwave_js/common.py index c7100b22bd5..8c8a3f2e576 100644 --- a/tests/components/zwave_js/common.py +++ b/tests/components/zwave_js/common.py @@ -3,8 +3,10 @@ from datetime import datetime, timezone AIR_TEMPERATURE_SENSOR = "sensor.multisensor_6_air_temperature" HUMIDITY_SENSOR = "sensor.multisensor_6_humidity" -ENERGY_SENSOR = "sensor.smart_plug_with_two_usb_ports_value_electric_consumed_2" POWER_SENSOR = "sensor.smart_plug_with_two_usb_ports_value_electric_consumed" +ENERGY_SENSOR = "sensor.smart_plug_with_two_usb_ports_value_electric_consumed_2" +VOLTAGE_SENSOR = "sensor.smart_plug_with_two_usb_ports_value_electric_consumed_3" +CURRENT_SENSOR = "sensor.smart_plug_with_two_usb_ports_value_electric_consumed_4" SWITCH_ENTITY = "switch.smart_plug_with_two_usb_ports" LOW_BATTERY_BINARY_SENSOR = "binary_sensor.multisensor_6_low_battery_level" ENABLED_LEGACY_BINARY_SENSOR = "binary_sensor.z_wave_door_window_sensor_any" diff --git a/tests/components/zwave_js/test_sensor.py b/tests/components/zwave_js/test_sensor.py index ebe406fb951..9fa4152ad6b 100644 --- a/tests/components/zwave_js/test_sensor.py +++ b/tests/components/zwave_js/test_sensor.py @@ -12,10 +12,14 @@ from homeassistant.components.zwave_js.const import ( ) from homeassistant.const import ( ATTR_ENTITY_ID, + DEVICE_CLASS_CURRENT, DEVICE_CLASS_ENERGY, DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_POWER, DEVICE_CLASS_TEMPERATURE, + DEVICE_CLASS_VOLTAGE, + ELECTRIC_CURRENT_AMPERE, + ELECTRIC_POTENTIAL_VOLT, ENERGY_KILO_WATT_HOUR, POWER_WATT, TEMP_CELSIUS, @@ -25,6 +29,7 @@ from homeassistant.helpers import entity_registry as er from .common import ( AIR_TEMPERATURE_SENSOR, BASIC_SENSOR, + CURRENT_SENSOR, DATETIME_LAST_RESET, DATETIME_ZERO, ENERGY_SENSOR, @@ -34,6 +39,7 @@ from .common import ( METER_SENSOR, NOTIFICATION_MOTION_SENSOR, POWER_SENSOR, + VOLTAGE_SENSOR, ) @@ -72,6 +78,20 @@ async def test_energy_sensors(hass, hank_binary_switch, integration): assert state.attributes["device_class"] == DEVICE_CLASS_ENERGY assert state.attributes["state_class"] == STATE_CLASS_MEASUREMENT + state = hass.states.get(VOLTAGE_SENSOR) + + assert state + assert state.state == "122.96" + assert state.attributes["unit_of_measurement"] == ELECTRIC_POTENTIAL_VOLT + assert state.attributes["device_class"] == DEVICE_CLASS_VOLTAGE + + state = hass.states.get(CURRENT_SENSOR) + + assert state + assert state.state == "0.0" + assert state.attributes["unit_of_measurement"] == ELECTRIC_CURRENT_AMPERE + assert state.attributes["device_class"] == DEVICE_CLASS_CURRENT + async def test_disabled_notification_sensor(hass, multisensor_6, integration): """Test sensor is created from Notification CC and is disabled."""