Determine zwave_js sensor device class during initialization (#48304)

This commit is contained in:
Raman Gupta 2021-03-24 20:08:16 -04:00 committed by GitHub
parent 6660fb7478
commit 058d232c57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -80,10 +80,15 @@ class ZwaveSensorBase(ZWaveBaseEntity, SensorEntity):
"""Initialize a ZWaveSensorBase entity.""" """Initialize a ZWaveSensorBase entity."""
super().__init__(config_entry, client, info) super().__init__(config_entry, client, info)
self._name = self.generate_name(include_value_name=True) self._name = self.generate_name(include_value_name=True)
self._device_class = self._get_device_class()
@property def _get_device_class(self) -> str | None:
def device_class(self) -> str | None: """
"""Return the device class of the sensor.""" Get the device class of the sensor.
This should be run once during initialization so we don't have to calculate
this value on every state update.
"""
if self.info.primary_value.command_class == CommandClass.BATTERY: if self.info.primary_value.command_class == CommandClass.BATTERY:
return DEVICE_CLASS_BATTERY return DEVICE_CLASS_BATTERY
if self.info.primary_value.command_class == CommandClass.METER: if self.info.primary_value.command_class == CommandClass.METER:
@ -102,6 +107,11 @@ class ZwaveSensorBase(ZWaveBaseEntity, SensorEntity):
return DEVICE_CLASS_ILLUMINANCE return DEVICE_CLASS_ILLUMINANCE
return None return None
@property
def device_class(self) -> str | None:
"""Return the device class of the sensor."""
return self._device_class
@property @property
def entity_registry_enabled_default(self) -> bool: def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry.""" """Return if the entity should be enabled when first added to the entity registry."""