From 058d232c57856c9baae402c1fb6d8b79bf710eed Mon Sep 17 00:00:00 2001 From: Raman Gupta <7243222+raman325@users.noreply.github.com> Date: Wed, 24 Mar 2021 20:08:16 -0400 Subject: [PATCH] Determine zwave_js sensor device class during initialization (#48304) --- homeassistant/components/zwave_js/sensor.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/zwave_js/sensor.py b/homeassistant/components/zwave_js/sensor.py index 574e1af658c..52a81a26eb9 100644 --- a/homeassistant/components/zwave_js/sensor.py +++ b/homeassistant/components/zwave_js/sensor.py @@ -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."""