diff --git a/custom_components/rct_power/lib/const.py b/custom_components/rct_power/lib/const.py index 28b89fe..e8a566f 100644 --- a/custom_components/rct_power/lib/const.py +++ b/custom_components/rct_power/lib/const.py @@ -43,6 +43,8 @@ ------------------------------------------------------------------- """ +NUMERIC_STATE_DECIMAL_DIGITS = 1 + class EntityUpdatePriority(Enum): FREQUENT = auto() diff --git a/custom_components/rct_power/lib/entity.py b/custom_components/rct_power/lib/entity.py index 57e850f..e75155f 100644 --- a/custom_components/rct_power/lib/entity.py +++ b/custom_components/rct_power/lib/entity.py @@ -1,5 +1,4 @@ from dataclasses import dataclass, field -from numbers import Number from typing import Any, Dict, List, Optional from homeassistant.components.sensor import SensorEntity, SensorEntityDescription @@ -20,6 +19,7 @@ ICON, INVERTER_MODEL, NAME, + NUMERIC_STATE_DECIMAL_DIGITS, EntityUpdatePriority, MeteredResetFrequency, ) @@ -118,8 +118,11 @@ def state(self): if isinstance(value, tuple): return None - if self.unit_of_measurement == "%" and isinstance(value, Number): - return value * 100 + if isinstance(value, (int, float)) and self.unit_of_measurement == "%": + return round(value * 100, NUMERIC_STATE_DECIMAL_DIGITS) + + if isinstance(value, (int, float)): + return round(value, NUMERIC_STATE_DECIMAL_DIGITS) return value