Remove data rate converting code from NZBGet (#98806)

pull/98854/head
Joost Lekkerkerker 2023-08-22 23:23:13 +02:00 committed by GitHub
parent 30628766ae
commit c4ae9ae430
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 23 deletions

View File

@ -13,6 +13,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, UnitOfDataRate, UnitOfInformation from homeassistant.const import CONF_NAME, UnitOfDataRate, UnitOfInformation
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
from . import NZBGetEntity from . import NZBGetEntity
@ -32,7 +33,8 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
key="AverageDownloadRate", key="AverageDownloadRate",
name="Average Speed", name="Average Speed",
device_class=SensorDeviceClass.DATA_RATE, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND,
suggested_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
), ),
SensorEntityDescription( SensorEntityDescription(
key="DownloadPaused", key="DownloadPaused",
@ -42,7 +44,8 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
key="DownloadRate", key="DownloadRate",
name="Speed", name="Speed",
device_class=SensorDeviceClass.DATA_RATE, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND,
suggested_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
), ),
SensorEntityDescription( SensorEntityDescription(
key="DownloadedSizeMB", key="DownloadedSizeMB",
@ -80,7 +83,8 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
key="DownloadLimit", key="DownloadLimit",
name="Speed Limit", name="Speed Limit",
device_class=SensorDeviceClass.DATA_RATE, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND,
suggested_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
), ),
) )
@ -121,30 +125,17 @@ class NZBGetSensor(NZBGetEntity, SensorEntity):
self.entity_description = description self.entity_description = description
self._attr_unique_id = f"{entry_id}_{description.key}" self._attr_unique_id = f"{entry_id}_{description.key}"
self._native_value: datetime | None = None
@property @property
def native_value(self): def native_value(self) -> StateType | datetime:
"""Return the state of the sensor.""" """Return the state of the sensor."""
sensor_type = self.entity_description.key sensor_type = self.entity_description.key
value = self.coordinator.data["status"].get(sensor_type) value = self.coordinator.data["status"].get(sensor_type)
if value is None: if value is not None and "UpTimeSec" in sensor_type and value > 0:
_LOGGER.warning("Unable to locate value for %s", sensor_type)
self._native_value = None
elif "DownloadRate" in sensor_type and value > 0:
# Convert download rate from Bytes/s to MBytes/s
self._native_value = round(value / 2**20, 2)
elif "DownloadLimit" in sensor_type and value > 0:
# Convert download rate from Bytes/s to MBytes/s
self._native_value = round(value / 2**20, 2)
elif "UpTimeSec" in sensor_type and value > 0:
uptime = utcnow().replace(microsecond=0) - timedelta(seconds=value) uptime = utcnow().replace(microsecond=0) - timedelta(seconds=value)
if not isinstance(self._attr_native_value, datetime) or abs( if not isinstance(self._attr_native_value, datetime) or abs(
uptime - self._attr_native_value uptime - self._attr_native_value
) > timedelta(seconds=5): ) > timedelta(seconds=5):
self._native_value = uptime return uptime
else: return value
self._native_value = value
return self._native_value

View File

@ -34,14 +34,14 @@ async def test_sensors(hass: HomeAssistant, nzbget_api) -> None:
), ),
"average_speed": ( "average_speed": (
"AverageDownloadRate", "AverageDownloadRate",
"1.19", "1.250000",
UnitOfDataRate.MEGABYTES_PER_SECOND, UnitOfDataRate.MEGABYTES_PER_SECOND,
SensorDeviceClass.DATA_RATE, SensorDeviceClass.DATA_RATE,
), ),
"download_paused": ("DownloadPaused", "False", None, None), "download_paused": ("DownloadPaused", "False", None, None),
"speed": ( "speed": (
"DownloadRate", "DownloadRate",
"2.38", "2.500000",
UnitOfDataRate.MEGABYTES_PER_SECOND, UnitOfDataRate.MEGABYTES_PER_SECOND,
SensorDeviceClass.DATA_RATE, SensorDeviceClass.DATA_RATE,
), ),
@ -68,7 +68,7 @@ async def test_sensors(hass: HomeAssistant, nzbget_api) -> None:
"uptime": ("UpTimeSec", uptime.isoformat(), None, SensorDeviceClass.TIMESTAMP), "uptime": ("UpTimeSec", uptime.isoformat(), None, SensorDeviceClass.TIMESTAMP),
"speed_limit": ( "speed_limit": (
"DownloadLimit", "DownloadLimit",
"0.95", "1.000000",
UnitOfDataRate.MEGABYTES_PER_SECOND, UnitOfDataRate.MEGABYTES_PER_SECOND,
SensorDeviceClass.DATA_RATE, SensorDeviceClass.DATA_RATE,
), ),