From c407a20b2a3bccd67b9407013f8ff83723fa4ed3 Mon Sep 17 00:00:00 2001 From: Nathan Spencer Date: Wed, 6 Dec 2023 12:22:54 -0700 Subject: [PATCH] Fix error on numeric entities when no value is available --- custom_components/rivian/sensor.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/custom_components/rivian/sensor.py b/custom_components/rivian/sensor.py index 4755bde..08ed9b3 100644 --- a/custom_components/rivian/sensor.py +++ b/custom_components/rivian/sensor.py @@ -115,19 +115,20 @@ def native_value(self) -> str | None: if _fn := self.entity_description.value_fn: return _fn(self.coordinator) - if (val := self._get_value(self.entity_description.field)) is not None: - rval = _fn(val) if (_fn := self.entity_description.value_lambda) else val - if self.device_class == SensorDeviceClass.ENUM and rval not in self.options: - _LOGGER.error( - "Sensor %s provides state value '%s', which is not in the list of known options. Please consider opening an issue at https://github.com/bretterer/home-assistant-rivian/issues with the following info: 'field: \"%s\" / value: \"%s\"'", - self.name, - rval, - self.entity_description.field, - val, - ) - self.options.append(rval) - return rval - return STATE_UNAVAILABLE + if (val := self._get_value(self.entity_description.field)) is None: + return STATE_UNAVAILABLE if not self.native_unit_of_measurement else None + + rval = _fn(val) if (_fn := self.entity_description.value_lambda) else val + if self.device_class == SensorDeviceClass.ENUM and rval not in self.options: + _LOGGER.error( + "Sensor %s provides state value '%s', which is not in the list of known options. Please consider opening an issue at https://github.com/bretterer/home-assistant-rivian/issues with the following info: 'field: \"%s\" / value: \"%s\"'", + self.name, + rval, + self.entity_description.field, + val, + ) + self.options.append(rval) + return rval @property def extra_state_attributes(self) -> Mapping[str, Any] | None: