Skip to content

Commit c814348

Browse files
author
Marcus Terasa
committed
Round values
1 parent 4eb18ed commit c814348

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

custom_components/foxess_api/sensor.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,18 @@ def native_value(self) -> datetime | StateType:
8686
return None
8787
else:
8888
try:
89-
return self.entity_description.state(self.coordinator.data)
89+
value = self.entity_description.state(self.coordinator.data)
90+
if self.entity_description.process is not None:
91+
value = self.entity_description.process(value)
92+
return value
9093
except KeyError:
9194
LOGGER.warning("Could not get state for %s. If this error persists, "
9295
"check API for changes. (Current version is %s)",
9396
self.entity_description.key,
9497
API_VERSION)
9598
return None
96-
except TypeError:
99+
except TypeError as te:
100+
LOGGER.error(te)
97101
LOGGER.warning("Type error for %s. If this error persists, "
98102
"check API for changes. (Current version is %s)",
99103
self.entity_description.key,

custom_components/foxess_api/sensor_descriptions.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@
88
UnitOfTemperature, UnitOfFrequency
99

1010

11+
def round_if_numeric(value: Any, decimals: int):
12+
if type(value) in (int, float):
13+
return round(value, decimals)
14+
else:
15+
return value
16+
17+
1118
@dataclass(frozen=True)
1219
class FoxEssEntityDescription(SensorEntityDescription):
1320
variable: str | None = None
1421
state: Callable[[defaultdict], Any] | None = None
22+
process: Callable[[Any], Any] | None = lambda value: round_if_numeric(value, 3)
1523
realtime: bool = True
1624

1725

@@ -24,6 +32,7 @@ class FoxEssEntityDescription(SensorEntityDescription):
2432
device_class=SensorDeviceClass.TIMESTAMP,
2533
variable="",
2634
state=lambda data: data["last_update"],
35+
process=None
2736
),
2837
FoxEssEntityDescription(
2938
key="pv_power",
@@ -535,6 +544,7 @@ class FoxEssEntityDescription(SensorEntityDescription):
535544
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
536545
variable="batTemperature",
537546
state=lambda data: data["realtime"]["batTemperature"],
547+
process=lambda value: round_if_numeric(value, 1),
538548
),
539549
FoxEssEntityDescription(
540550
key="ambient_temperature",
@@ -545,6 +555,7 @@ class FoxEssEntityDescription(SensorEntityDescription):
545555
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
546556
variable="ambientTemperation",
547557
state=lambda data: data["realtime"]["ambientTemperation"],
558+
process=lambda value: round_if_numeric(value, 1),
548559
),
549560
FoxEssEntityDescription(
550561
key="inv_temperature",
@@ -555,6 +566,7 @@ class FoxEssEntityDescription(SensorEntityDescription):
555566
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
556567
variable="invTemperation",
557568
state=lambda data: data["realtime"]["invTemperation"],
569+
process=lambda value: round_if_numeric(value, 1),
558570
),
559571
FoxEssEntityDescription(
560572
key="boost_temperature",
@@ -565,6 +577,7 @@ class FoxEssEntityDescription(SensorEntityDescription):
565577
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
566578
variable="boostTemperation",
567579
state=lambda data: data["realtime"]["boostTemperation"],
580+
process=lambda value: round_if_numeric(value, 1),
568581
),
569582
FoxEssEntityDescription(
570583
key="charge_temperature",
@@ -575,6 +588,7 @@ class FoxEssEntityDescription(SensorEntityDescription):
575588
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
576589
variable="chargeTemperature",
577590
state=lambda data: data["realtime"]["chargeTemperature"],
591+
process=lambda value: round_if_numeric(value, 1),
578592
),
579593
FoxEssEntityDescription(
580594
key="dsp_temperature",
@@ -585,6 +599,7 @@ class FoxEssEntityDescription(SensorEntityDescription):
585599
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
586600
variable="dspTemperature",
587601
state=lambda data: data["realtime"]["dspTemperature"],
602+
process=lambda value: round_if_numeric(value, 1),
588603
),
589604
# end of temperatures
590605

@@ -598,6 +613,7 @@ class FoxEssEntityDescription(SensorEntityDescription):
598613
native_unit_of_measurement=UnitOfFrequency.HERTZ,
599614
variable="RFreq",
600615
state=lambda data: data["realtime"]["RFreq"],
616+
process=lambda value: round_if_numeric(value, 1),
601617
),
602618
FoxEssEntityDescription(
603619
key="s_frequency",
@@ -608,6 +624,7 @@ class FoxEssEntityDescription(SensorEntityDescription):
608624
native_unit_of_measurement=UnitOfFrequency.HERTZ,
609625
variable="SFreq",
610626
state=lambda data: data["realtime"]["SFreq"],
627+
process=lambda value: round_if_numeric(value, 1),
611628
),
612629
FoxEssEntityDescription(
613630
key="t_frequency",
@@ -618,6 +635,7 @@ class FoxEssEntityDescription(SensorEntityDescription):
618635
native_unit_of_measurement=UnitOfFrequency.HERTZ,
619636
variable="TFreq",
620637
state=lambda data: data["realtime"]["TFreq"],
638+
process=lambda value: round_if_numeric(value, 1),
621639
),
622640
# end of frequencies
623641

@@ -683,6 +701,7 @@ class FoxEssEntityDescription(SensorEntityDescription):
683701
native_unit_of_measurement=PERCENTAGE,
684702
variable="SoC",
685703
state=lambda data: data["realtime"]["SoC"],
704+
process=lambda value: round_if_numeric(value, 0),
686705
),
687706
FoxEssEntityDescription(
688707
key="residual_energy",
@@ -732,7 +751,7 @@ class FoxEssEntityDescription(SensorEntityDescription):
732751
),
733752
FoxEssEntityDescription(
734753
key="charged_energy_total",
735-
name="Charged Energy Total",
754+
name="Daily Charged Energy Total",
736755
icon="mdi:battery-plus-outline",
737756
device_class=SensorDeviceClass.ENERGY,
738757
state_class=SensorStateClass.TOTAL_INCREASING,
@@ -743,7 +762,7 @@ class FoxEssEntityDescription(SensorEntityDescription):
743762
),
744763
FoxEssEntityDescription(
745764
key="discharged_energy_total",
746-
name="Discharged Energy Total",
765+
name="Daily Discharged Energy Total",
747766
icon="mdi:battery-minus-outline",
748767
device_class=SensorDeviceClass.ENERGY,
749768
state_class=SensorStateClass.TOTAL_INCREASING,

0 commit comments

Comments
 (0)