Skip to content

Commit 5954cea

Browse files
authored
Merge branch 'zigpy:dev' into cover-correctly-handle-restore-of-movement-state
2 parents e97e705 + 1b6b5df commit 5954cea

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

tests/test_sensor.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ async def test_unsupported_attributes_sensor(
954954
(
955955
1,
956956
1232000,
957-
123.2,
957+
123.2000,
958958
UnitOfVolume.CUBIC_METERS,
959959
),
960960
(
@@ -978,43 +978,43 @@ async def test_unsupported_attributes_sensor(
978978
(
979979
0,
980980
9366,
981-
0.937,
981+
0.9366,
982982
UnitOfEnergy.KILO_WATT_HOUR,
983983
),
984984
(
985985
0,
986986
999,
987-
0.1,
987+
0.0999,
988988
UnitOfEnergy.KILO_WATT_HOUR,
989989
),
990990
(
991991
0,
992992
10091,
993-
1.009,
993+
1.0091,
994994
UnitOfEnergy.KILO_WATT_HOUR,
995995
),
996996
(
997997
0,
998998
10099,
999-
1.01,
999+
1.0099,
10001000
UnitOfEnergy.KILO_WATT_HOUR,
10011001
),
10021002
(
10031003
0,
10041004
100999,
1005-
10.1,
1005+
10.0999,
10061006
UnitOfEnergy.KILO_WATT_HOUR,
10071007
),
10081008
(
10091009
0,
10101010
100023,
1011-
10.002,
1011+
10.0023,
10121012
UnitOfEnergy.KILO_WATT_HOUR,
10131013
),
10141014
(
10151015
0,
10161016
102456,
1017-
10.246,
1017+
10.2456,
10181018
UnitOfEnergy.KILO_WATT_HOUR,
10191019
),
10201020
(

zha/application/platforms/sensor/__init__.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class SensorEntityInfo(BaseEntityInfo):
124124
"""Sensor entity info."""
125125

126126
attribute: str
127-
decimals: int
127+
suggested_display_precision: int | None = None
128128
divisor: int
129129
multiplier: int
130130
unit: str | None = None
@@ -137,6 +137,7 @@ class DeviceCounterEntityInfo(BaseEntityInfo):
137137
"""Device counter entity info."""
138138

139139
device_ieee: str
140+
suggested_display_precision: int
140141
available: bool
141142
counter: str
142143
counter_value: int
@@ -157,9 +158,9 @@ class Sensor(PlatformEntity):
157158
PLATFORM = Platform.SENSOR
158159
_attribute_name: int | str | None = None
159160
_attribute_converter: typing.Callable[[typing.Any], typing.Any] | None = None
160-
_decimals: int = 1
161161
_divisor: int = 1
162162
_multiplier: int | float = 1
163+
_attr_suggested_display_precision: int | None = None
163164
_attr_native_unit_of_measurement: str | None = None
164165
_attr_device_class: SensorDeviceClass | None = None
165166
_attr_state_class: SensorStateClass | None = None
@@ -263,7 +264,7 @@ def info_object(self) -> SensorEntityInfo:
263264
return SensorEntityInfo(
264265
**super().info_object.__dict__,
265266
attribute=self._attribute_name,
266-
decimals=self._decimals,
267+
suggested_display_precision=self._attr_suggested_display_precision,
267268
divisor=self._divisor,
268269
multiplier=self._multiplier,
269270
unit=(
@@ -323,11 +324,7 @@ def formatter(
323324
if self._is_non_value(value):
324325
return None
325326

326-
if self._decimals > 0:
327-
return round(
328-
float(value * self._multiplier) / self._divisor, self._decimals
329-
)
330-
return round(float(value * self._multiplier) / self._divisor)
327+
return float(value * self._multiplier) / self._divisor
331328

332329

333330
class TimestampSensor(Sensor):
@@ -416,6 +413,7 @@ class DeviceCounterSensor(BaseEntity):
416413
_attr_state_class: SensorStateClass = SensorStateClass.TOTAL
417414
_attr_entity_category = EntityCategory.DIAGNOSTIC
418415
_attr_entity_registry_enabled_default = False
416+
_attr_suggested_display_precision = 0
419417

420418
def __init__(
421419
self,
@@ -470,6 +468,7 @@ def info_object(self) -> DeviceCounterEntityInfo:
470468
"""Return a representation of the platform entity."""
471469
return DeviceCounterEntityInfo(
472470
**super().info_object.__dict__,
471+
suggested_display_precision=self._attr_suggested_display_precision,
473472
counter=self._zigpy_counter.name,
474473
counter_value=self._zigpy_counter.value,
475474
counter_groups=self._zigpy_counter_groups,
@@ -575,6 +574,7 @@ class Battery(Sensor):
575574
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
576575
_attr_entity_category = EntityCategory.DIAGNOSTIC
577576
_attr_native_unit_of_measurement = PERCENTAGE
577+
_attr_suggested_display_precision: int = 0
578578
_attr_extra_state_attribute_names: set[str] = {
579579
"battery_size",
580580
"battery_quantity",
@@ -586,13 +586,12 @@ def _is_supported(self) -> bool:
586586
return PlatformEntity._is_supported(self) and not self.device.is_mains_powered
587587

588588
@staticmethod
589-
def formatter(value: int) -> int | None: # pylint: disable=arguments-differ
589+
def formatter(value: int) -> float | None: # pylint: disable=arguments-differ
590590
"""Return the state of the entity."""
591591
# per zcl specs battery percent is reported at 200% ¯\_(ツ)_/¯
592592
if not isinstance(value, numbers.Number) or value in (-1, 255):
593593
return None
594-
value = round(value / 2)
595-
return value
594+
return value / 2
596595

597596
@property
598597
def state(self) -> dict[str, Any]:
@@ -615,6 +614,7 @@ class BaseElectricalMeasurement(PollableSensor):
615614

616615
_use_custom_polling: bool = False
617616
_attribute_name = "active_power"
617+
_attr_suggested_display_precision = 1
618618
_attr_device_class: SensorDeviceClass = SensorDeviceClass.POWER
619619
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
620620
_attr_native_unit_of_measurement: str = UnitOfPower.WATT
@@ -671,7 +671,7 @@ def formatter(self, value: int) -> int | float:
671671

672672
value = float(value * multiplier) / divisor
673673
if value < 100 and divisor > 1:
674-
return round(value, self._decimals)
674+
return round(value, self._attr_suggested_display_precision)
675675
return round(value)
676676

677677

@@ -1064,6 +1064,7 @@ class SmartEnergySummation(SmartEnergyMetering):
10641064
_attribute_name = "current_summ_delivered"
10651065
_unique_id_suffix = "summation_delivered"
10661066
_attr_translation_key: str = "summation_delivered"
1067+
_attr_suggested_display_precision: int = 3
10671068

10681069
_ENTITY_DESCRIPTION_MAP = {
10691070
0x00: SmartEnergySummationEntityDescription(
@@ -1127,11 +1128,10 @@ def formatter(self, value: int) -> int | float:
11271128
if self._cluster_handler.unit_of_measurement != 0:
11281129
return self._cluster_handler.summa_formatter(value)
11291130

1130-
cooked = (
1131+
return (
11311132
float(self._cluster_handler.multiplier * value)
11321133
/ self._cluster_handler.divisor
11331134
)
1134-
return round(cooked, 3)
11351135

11361136

11371137
@MULTI_MATCH(
@@ -1251,7 +1251,7 @@ class Pressure(Sensor):
12511251
_attribute_name = "measured_value"
12521252
_attr_device_class: SensorDeviceClass = SensorDeviceClass.PRESSURE
12531253
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
1254-
_decimals = 0
1254+
_attr_suggested_display_precision: int = 0
12551255
_attr_native_unit_of_measurement = UnitOfPressure.HPA
12561256
_attr_primary_weight = 1
12571257

@@ -1331,7 +1331,7 @@ class CarbonDioxideConcentration(Sensor):
13311331
_attribute_name = "measured_value"
13321332
_attr_device_class: SensorDeviceClass = SensorDeviceClass.CO2
13331333
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
1334-
_decimals = 0
1334+
_attr_suggested_display_precision = 0
13351335
_multiplier = 1e6
13361336
_attr_native_unit_of_measurement = CONCENTRATION_PARTS_PER_MILLION
13371337
_attr_primary_weight = 1
@@ -1344,7 +1344,7 @@ class CarbonMonoxideConcentration(Sensor):
13441344
_attribute_name = "measured_value"
13451345
_attr_device_class: SensorDeviceClass = SensorDeviceClass.CO
13461346
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
1347-
_decimals = 0
1347+
_attr_suggested_display_precision = 0
13481348
_multiplier = 1e6
13491349
_attr_native_unit_of_measurement = CONCENTRATION_PARTS_PER_MILLION
13501350
_attr_primary_weight = 1
@@ -1358,7 +1358,7 @@ class VOCLevel(Sensor):
13581358
_attribute_name = "measured_value"
13591359
_attr_device_class: SensorDeviceClass = SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS
13601360
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
1361-
_decimals = 0
1361+
_attr_suggested_display_precision = 0
13621362
_multiplier = 1e6
13631363
_attr_native_unit_of_measurement = CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
13641364
_attr_primary_weight = 1
@@ -1377,7 +1377,7 @@ class PPBVOCLevel(Sensor):
13771377
SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS_PARTS
13781378
)
13791379
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
1380-
_decimals = 0
1380+
_attr_suggested_display_precision = 0
13811381
_multiplier = 1
13821382
_attr_native_unit_of_measurement = CONCENTRATION_PARTS_PER_BILLION
13831383
_attr_primary_weight = 1
@@ -1390,7 +1390,6 @@ class PM25(Sensor):
13901390
_attribute_name = "measured_value"
13911391
_attr_device_class: SensorDeviceClass = SensorDeviceClass.PM25
13921392
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
1393-
_decimals = 0
13941393
_multiplier = 1
13951394
_attr_native_unit_of_measurement = CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
13961395
_attr_primary_weight = 1
@@ -1413,7 +1412,7 @@ class FormaldehydeConcentration(Sensor):
14131412
_attribute_name = "measured_value"
14141413
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
14151414
_attr_translation_key: str = "formaldehyde"
1416-
_decimals = 0
1415+
_attr_suggested_display_precision = 0
14171416
_multiplier = 1e6
14181417
_attr_native_unit_of_measurement = CONCENTRATION_PARTS_PER_MILLION
14191418
_attr_primary_weight = 1
@@ -1738,7 +1737,7 @@ class AqaraSmokeDensityDbm(Sensor):
17381737
_attr_translation_key: str = "smoke_density"
17391738
_attr_native_unit_of_measurement = "dB/m"
17401739
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
1741-
_attr_suggested_display_precision: int = 3
1740+
_attr_suggested_display_precision = 3
17421741

17431742

17441743
class SonoffIlluminationStates(types.enum8):
@@ -1769,7 +1768,7 @@ class PiHeatingDemand(Sensor):
17691768
_attribute_name = "pi_heating_demand"
17701769
_attr_translation_key: str = "pi_heating_demand"
17711770
_attr_native_unit_of_measurement = PERCENTAGE
1772-
_decimals = 0
1771+
_attr_suggested_display_precision = 0
17731772
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
17741773
_attr_entity_category = EntityCategory.DIAGNOSTIC
17751774

0 commit comments

Comments
 (0)