mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Change explicit rounding to suggested_display_precision (#95773)
This commit is contained in:
parent
907c667859
commit
5f6ddedd67
@ -95,26 +95,31 @@ _CPU_MON_COND: tuple[SensorEntityDescription, ...] = (
|
|||||||
native_unit_of_measurement=PERCENTAGE,
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
icon="mdi:chip",
|
icon="mdi:chip",
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
suggested_display_precision=0,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
_MEMORY_MON_COND: tuple[SensorEntityDescription, ...] = (
|
_MEMORY_MON_COND: tuple[SensorEntityDescription, ...] = (
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
key="memory_free",
|
key="memory_free",
|
||||||
name="Memory Available",
|
name="Memory Available",
|
||||||
native_unit_of_measurement=UnitOfInformation.GIBIBYTES,
|
native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
|
||||||
device_class=SensorDeviceClass.DATA_SIZE,
|
device_class=SensorDeviceClass.DATA_SIZE,
|
||||||
icon="mdi:memory",
|
icon="mdi:memory",
|
||||||
entity_registry_enabled_default=False,
|
entity_registry_enabled_default=False,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
suggested_display_precision=1,
|
||||||
|
suggested_unit_of_measurement=UnitOfInformation.GIBIBYTES,
|
||||||
),
|
),
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
key="memory_used",
|
key="memory_used",
|
||||||
name="Memory Used",
|
name="Memory Used",
|
||||||
native_unit_of_measurement=UnitOfInformation.GIBIBYTES,
|
native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
|
||||||
device_class=SensorDeviceClass.DATA_SIZE,
|
device_class=SensorDeviceClass.DATA_SIZE,
|
||||||
icon="mdi:memory",
|
icon="mdi:memory",
|
||||||
entity_registry_enabled_default=False,
|
entity_registry_enabled_default=False,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
suggested_display_precision=1,
|
||||||
|
suggested_unit_of_measurement=UnitOfInformation.GIBIBYTES,
|
||||||
),
|
),
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
key="memory_percent_used",
|
key="memory_percent_used",
|
||||||
@ -122,6 +127,7 @@ _MEMORY_MON_COND: tuple[SensorEntityDescription, ...] = (
|
|||||||
native_unit_of_measurement=PERCENTAGE,
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
icon="mdi:memory",
|
icon="mdi:memory",
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
suggested_display_precision=0,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
_NETWORK_MON_COND: tuple[SensorEntityDescription, ...] = (
|
_NETWORK_MON_COND: tuple[SensorEntityDescription, ...] = (
|
||||||
@ -133,20 +139,24 @@ _NETWORK_MON_COND: tuple[SensorEntityDescription, ...] = (
|
|||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
key="network_tx",
|
key="network_tx",
|
||||||
name="Network Up",
|
name="Network Up",
|
||||||
native_unit_of_measurement=UnitOfDataRate.MEBIBYTES_PER_SECOND,
|
native_unit_of_measurement=UnitOfDataRate.BITS_PER_SECOND,
|
||||||
device_class=SensorDeviceClass.DATA_RATE,
|
device_class=SensorDeviceClass.DATA_RATE,
|
||||||
icon="mdi:upload",
|
icon="mdi:upload",
|
||||||
entity_registry_enabled_default=False,
|
entity_registry_enabled_default=False,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
suggested_display_precision=1,
|
||||||
|
suggested_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
|
||||||
),
|
),
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
key="network_rx",
|
key="network_rx",
|
||||||
name="Network Down",
|
name="Network Down",
|
||||||
native_unit_of_measurement=UnitOfDataRate.MEBIBYTES_PER_SECOND,
|
native_unit_of_measurement=UnitOfDataRate.BITS_PER_SECOND,
|
||||||
device_class=SensorDeviceClass.DATA_RATE,
|
device_class=SensorDeviceClass.DATA_RATE,
|
||||||
icon="mdi:download",
|
icon="mdi:download",
|
||||||
entity_registry_enabled_default=False,
|
entity_registry_enabled_default=False,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
suggested_display_precision=1,
|
||||||
|
suggested_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
_DRIVE_MON_COND: tuple[SensorEntityDescription, ...] = (
|
_DRIVE_MON_COND: tuple[SensorEntityDescription, ...] = (
|
||||||
@ -170,20 +180,24 @@ _VOLUME_MON_COND: tuple[SensorEntityDescription, ...] = (
|
|||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
key="volume_size_used",
|
key="volume_size_used",
|
||||||
name="Used Space",
|
name="Used Space",
|
||||||
native_unit_of_measurement=UnitOfInformation.GIBIBYTES,
|
native_unit_of_measurement=UnitOfInformation.BYTES,
|
||||||
device_class=SensorDeviceClass.DATA_SIZE,
|
device_class=SensorDeviceClass.DATA_SIZE,
|
||||||
icon="mdi:chart-pie",
|
icon="mdi:chart-pie",
|
||||||
entity_registry_enabled_default=False,
|
entity_registry_enabled_default=False,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
suggested_display_precision=1,
|
||||||
|
suggested_unit_of_measurement=UnitOfInformation.GIBIBYTES,
|
||||||
),
|
),
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
key="volume_size_free",
|
key="volume_size_free",
|
||||||
name="Free Space",
|
name="Free Space",
|
||||||
native_unit_of_measurement=UnitOfInformation.GIBIBYTES,
|
native_unit_of_measurement=UnitOfInformation.BYTES,
|
||||||
device_class=SensorDeviceClass.DATA_SIZE,
|
device_class=SensorDeviceClass.DATA_SIZE,
|
||||||
icon="mdi:chart-pie",
|
icon="mdi:chart-pie",
|
||||||
entity_registry_enabled_default=False,
|
entity_registry_enabled_default=False,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
suggested_display_precision=1,
|
||||||
|
suggested_unit_of_measurement=UnitOfInformation.GIBIBYTES,
|
||||||
),
|
),
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
key="volume_percentage_used",
|
key="volume_percentage_used",
|
||||||
@ -191,6 +205,7 @@ _VOLUME_MON_COND: tuple[SensorEntityDescription, ...] = (
|
|||||||
native_unit_of_measurement=PERCENTAGE,
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
icon="mdi:chart-pie",
|
icon="mdi:chart-pie",
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
suggested_display_precision=0,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -316,16 +331,6 @@ async def async_setup_entry(
|
|||||||
async_add_entities(sensors)
|
async_add_entities(sensors)
|
||||||
|
|
||||||
|
|
||||||
def round_nicely(number):
|
|
||||||
"""Round a number based on its size (so it looks nice)."""
|
|
||||||
if number < 10:
|
|
||||||
return round(number, 2)
|
|
||||||
if number < 100:
|
|
||||||
return round(number, 1)
|
|
||||||
|
|
||||||
return round(number)
|
|
||||||
|
|
||||||
|
|
||||||
class QNAPSensor(CoordinatorEntity[QnapCoordinator], SensorEntity):
|
class QNAPSensor(CoordinatorEntity[QnapCoordinator], SensorEntity):
|
||||||
"""Base class for a QNAP sensor."""
|
"""Base class for a QNAP sensor."""
|
||||||
|
|
||||||
@ -378,25 +383,25 @@ class QNAPMemorySensor(QNAPSensor):
|
|||||||
@property
|
@property
|
||||||
def native_value(self):
|
def native_value(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
free = float(self.coordinator.data["system_stats"]["memory"]["free"]) / 1024
|
free = float(self.coordinator.data["system_stats"]["memory"]["free"])
|
||||||
if self.entity_description.key == "memory_free":
|
if self.entity_description.key == "memory_free":
|
||||||
return round_nicely(free)
|
return free
|
||||||
|
|
||||||
total = float(self.coordinator.data["system_stats"]["memory"]["total"]) / 1024
|
total = float(self.coordinator.data["system_stats"]["memory"]["total"])
|
||||||
|
|
||||||
used = total - free
|
used = total - free
|
||||||
if self.entity_description.key == "memory_used":
|
if self.entity_description.key == "memory_used":
|
||||||
return round_nicely(used)
|
return used
|
||||||
|
|
||||||
if self.entity_description.key == "memory_percent_used":
|
if self.entity_description.key == "memory_percent_used":
|
||||||
return round(used / total * 100)
|
return used / total * 100
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self):
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
if self.coordinator.data:
|
if self.coordinator.data:
|
||||||
data = self.coordinator.data["system_stats"]["memory"]
|
data = self.coordinator.data["system_stats"]["memory"]
|
||||||
size = round_nicely(float(data["total"]) / 1024)
|
size = round(float(data["total"]) / 1024, 2)
|
||||||
return {ATTR_MEMORY_SIZE: f"{size} {UnitOfInformation.GIBIBYTES}"}
|
return {ATTR_MEMORY_SIZE: f"{size} {UnitOfInformation.GIBIBYTES}"}
|
||||||
|
|
||||||
|
|
||||||
@ -412,10 +417,10 @@ class QNAPNetworkSensor(QNAPSensor):
|
|||||||
|
|
||||||
data = self.coordinator.data["bandwidth"][self.monitor_device]
|
data = self.coordinator.data["bandwidth"][self.monitor_device]
|
||||||
if self.entity_description.key == "network_tx":
|
if self.entity_description.key == "network_tx":
|
||||||
return round_nicely(data["tx"] / 1024 / 1024)
|
return data["tx"]
|
||||||
|
|
||||||
if self.entity_description.key == "network_rx":
|
if self.entity_description.key == "network_rx":
|
||||||
return round_nicely(data["rx"] / 1024 / 1024)
|
return data["rx"]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self):
|
||||||
@ -427,8 +432,6 @@ class QNAPNetworkSensor(QNAPSensor):
|
|||||||
ATTR_MASK: data["mask"],
|
ATTR_MASK: data["mask"],
|
||||||
ATTR_MAC: data["mac"],
|
ATTR_MAC: data["mac"],
|
||||||
ATTR_MAX_SPEED: data["max_speed"],
|
ATTR_MAX_SPEED: data["max_speed"],
|
||||||
ATTR_PACKETS_TX: data["tx_packets"],
|
|
||||||
ATTR_PACKETS_RX: data["rx_packets"],
|
|
||||||
ATTR_PACKETS_ERR: data["err_packets"],
|
ATTR_PACKETS_ERR: data["err_packets"],
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -507,18 +510,18 @@ class QNAPVolumeSensor(QNAPSensor):
|
|||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
data = self.coordinator.data["volumes"][self.monitor_device]
|
data = self.coordinator.data["volumes"][self.monitor_device]
|
||||||
|
|
||||||
free_gb = int(data["free_size"]) / 1024 / 1024 / 1024
|
free_gb = int(data["free_size"])
|
||||||
if self.entity_description.key == "volume_size_free":
|
if self.entity_description.key == "volume_size_free":
|
||||||
return round_nicely(free_gb)
|
return free_gb
|
||||||
|
|
||||||
total_gb = int(data["total_size"]) / 1024 / 1024 / 1024
|
total_gb = int(data["total_size"])
|
||||||
|
|
||||||
used_gb = total_gb - free_gb
|
used_gb = total_gb - free_gb
|
||||||
if self.entity_description.key == "volume_size_used":
|
if self.entity_description.key == "volume_size_used":
|
||||||
return round_nicely(used_gb)
|
return used_gb
|
||||||
|
|
||||||
if self.entity_description.key == "volume_percentage_used":
|
if self.entity_description.key == "volume_percentage_used":
|
||||||
return round(used_gb / total_gb * 100)
|
return used_gb / total_gb * 100
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self):
|
||||||
@ -528,5 +531,5 @@ class QNAPVolumeSensor(QNAPSensor):
|
|||||||
total_gb = int(data["total_size"]) / 1024 / 1024 / 1024
|
total_gb = int(data["total_size"]) / 1024 / 1024 / 1024
|
||||||
|
|
||||||
return {
|
return {
|
||||||
ATTR_VOLUME_SIZE: f"{round_nicely(total_gb)} {UnitOfInformation.GIBIBYTES}"
|
ATTR_VOLUME_SIZE: f"{round(total_gb, 1)} {UnitOfInformation.GIBIBYTES}"
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user