From 1950f0757cb57c973a671028b5af3645d39e3e47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20St=C3=BCrmer?= Date: Sat, 28 Aug 2021 18:01:57 +0200 Subject: [PATCH] Round numeric state values to one decimal place (#111) --- custom_components/rct_power/lib/const.py | 2 ++ custom_components/rct_power/lib/entity.py | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) 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