Fix zwave_js current and voltage meter sensor device class (#53723)

pull/53732/head
Martin Hjelmare 2021-07-29 23:26:57 +02:00 committed by GitHub
parent de7a885045
commit 6d30e596ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 4 deletions

View File

@ -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(

View File

@ -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"

View File

@ -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."""