Skip to content

Commit

Permalink
Round numeric state values to one decimal place (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
weltenwort authored Aug 28, 2021
1 parent 69d30a2 commit 1950f07
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 2 additions & 0 deletions custom_components/rct_power/lib/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
-------------------------------------------------------------------
"""

NUMERIC_STATE_DECIMAL_DIGITS = 1


class EntityUpdatePriority(Enum):
FREQUENT = auto()
Expand Down
9 changes: 6 additions & 3 deletions custom_components/rct_power/lib/entity.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,6 +19,7 @@
ICON,
INVERTER_MODEL,
NAME,
NUMERIC_STATE_DECIMAL_DIGITS,
EntityUpdatePriority,
MeteredResetFrequency,
)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 1950f07

Please sign in to comment.