Determine zwave_js sensor device class during initialization (#48304)

pull/48343/head
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
1 changed files with 13 additions and 3 deletions

View File

@ -80,10 +80,15 @@ class ZwaveSensorBase(ZWaveBaseEntity, SensorEntity):
"""Initialize a ZWaveSensorBase entity."""
super().__init__(config_entry, client, info)
self._name = self.generate_name(include_value_name=True)
self._device_class = self._get_device_class()
@property
def device_class(self) -> str | None:
"""Return the device class of the sensor."""
def _get_device_class(self) -> str | None:
"""
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:
return DEVICE_CLASS_BATTERY
if self.info.primary_value.command_class == CommandClass.METER:
@ -102,6 +107,11 @@ class ZwaveSensorBase(ZWaveBaseEntity, SensorEntity):
return DEVICE_CLASS_ILLUMINANCE
return None
@property
def device_class(self) -> str | None:
"""Return the device class of the sensor."""
return self._device_class
@property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry."""