From 6633844a0e586f3aebc643f4cad4ac60bb240d1a Mon Sep 17 00:00:00 2001 From: Quentin POLLET Date: Sat, 2 May 2020 00:19:25 +0200 Subject: [PATCH 1/2] Fix Synology disk space incorrect sensor type --- .../components/synology_dsm/const.py | 5 +- .../components/synology_dsm/manifest.json | 2 +- .../components/synology_dsm/sensor.py | 55 ++++++++++--------- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 5 files changed, 35 insertions(+), 31 deletions(-) diff --git a/homeassistant/components/synology_dsm/const.py b/homeassistant/components/synology_dsm/const.py index 8c17de5e99737d..b3c9f66c8da272 100644 --- a/homeassistant/components/synology_dsm/const.py +++ b/homeassistant/components/synology_dsm/const.py @@ -2,6 +2,7 @@ from homeassistant.const import ( DATA_MEGABYTES, DATA_RATE_KILOBYTES_PER_SECOND, + DATA_TERABYTES, UNIT_PERCENTAGE, ) @@ -34,8 +35,8 @@ STORAGE_VOL_SENSORS = { "volume_status": ["Status", None, "mdi:checkbox-marked-circle-outline"], "volume_device_type": ["Type", None, "mdi:harddisk"], - "volume_size_total": ["Total Size", None, "mdi:chart-pie"], - "volume_size_used": ["Used Space", None, "mdi:chart-pie"], + "volume_size_total": ["Total Size", DATA_TERABYTES, "mdi:chart-pie"], + "volume_size_used": ["Used Space", DATA_TERABYTES, "mdi:chart-pie"], "volume_percentage_used": ["Volume Used", UNIT_PERCENTAGE, "mdi:chart-pie"], "volume_disk_temp_avg": ["Average Disk Temp", None, "mdi:thermometer"], "volume_disk_temp_max": ["Maximum Disk Temp", None, "mdi:thermometer"], diff --git a/homeassistant/components/synology_dsm/manifest.json b/homeassistant/components/synology_dsm/manifest.json index 4a538606ecba4d..f57f1843f45fcf 100644 --- a/homeassistant/components/synology_dsm/manifest.json +++ b/homeassistant/components/synology_dsm/manifest.json @@ -2,7 +2,7 @@ "domain": "synology_dsm", "name": "Synology DSM", "documentation": "https://www.home-assistant.io/integrations/synology_dsm", - "requirements": ["python-synology==0.7.4"], + "requirements": ["python-synology==0.8.0"], "codeowners": ["@ProtoThis", "@Quentame"], "config_flow": true, "ssdp": [ diff --git a/homeassistant/components/synology_dsm/sensor.py b/homeassistant/components/synology_dsm/sensor.py index 6e5a486ab8900f..eecbe2ad90fe32 100644 --- a/homeassistant/components/synology_dsm/sensor.py +++ b/homeassistant/components/synology_dsm/sensor.py @@ -7,6 +7,7 @@ CONF_DISKS, DATA_MEGABYTES, DATA_RATE_KILOBYTES_PER_SECOND, + DATA_TERABYTES, TEMP_CELSIUS, ) from homeassistant.helpers.dispatcher import async_dispatcher_connect @@ -63,7 +64,7 @@ async def async_setup_entry( class SynoNasSensor(Entity): - """Representation of a Synology NAS Sensor.""" + """Representation of a Synology NAS sensor.""" def __init__( self, @@ -142,47 +143,49 @@ async def async_will_remove_from_hass(self): class SynoNasUtilSensor(SynoNasSensor): - """Representation a Synology Utilisation Sensor.""" + """Representation a Synology Utilisation sensor.""" @property def state(self): """Return the state.""" - if self._unit == DATA_RATE_KILOBYTES_PER_SECOND or self._unit == DATA_MEGABYTES: - attr = getattr(self._api.utilisation, self.sensor_type)(False) + attr = getattr(self._api.utilisation, self.sensor_type) + if callable(attr): + attr = attr() + if not attr: + return None - if attr is None: - return None + # Data (RAM) + if self._unit == DATA_MEGABYTES: + return round(attr / 1024.0 / 1024.0, 1) - if self._unit == DATA_RATE_KILOBYTES_PER_SECOND: - return round(attr / 1024.0, 1) - if self._unit == DATA_MEGABYTES: - return round(attr / 1024.0 / 1024.0, 1) - else: - return getattr(self._api.utilisation, self.sensor_type) + # Network + if self._unit == DATA_RATE_KILOBYTES_PER_SECOND: + return round(attr / 1024.0, 1) + + return attr class SynoNasStorageSensor(SynoNasSensor): - """Representation a Synology Storage Sensor.""" + """Representation a Synology Storage sensor.""" @property def state(self): """Return the state.""" - if self.monitored_device: - if self.sensor_type in TEMP_SENSORS_KEYS: - attr = getattr(self._api.storage, self.sensor_type)( - self.monitored_device - ) + attr = getattr(self._api.storage, self.sensor_type)(self.monitored_device) + if not attr: + return None - if attr is None: - return None + # Data (disk space) + if self._unit == DATA_TERABYTES: + return round(attr / 1024.0 / 1024.0 / 1024.0 / 1024.0, 2) - if self._api.temp_unit == TEMP_CELSIUS: - return attr - - return round(attr * 1.8 + 32.0, 1) + # Temperature + if self._api.temp_unit == TEMP_CELSIUS: + return attr + if self.sensor_type in TEMP_SENSORS_KEYS: + return round(attr * 1.8 + 32.0, 1) - return getattr(self._api.storage, self.sensor_type)(self.monitored_device) - return None + return attr @property def device_info(self) -> Dict[str, any]: diff --git a/requirements_all.txt b/requirements_all.txt index fe439d5c73da0d..014bdfe100cc9b 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1692,7 +1692,7 @@ python-sochain-api==0.0.2 python-songpal==0.11.2 # homeassistant.components.synology_dsm -python-synology==0.7.4 +python-synology==0.8.0 # homeassistant.components.tado python-tado==0.8.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index f1d80a3c9f0477..c7333f8306a041 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -662,7 +662,7 @@ python-miio==0.5.0.1 python-nest==4.1.0 # homeassistant.components.synology_dsm -python-synology==0.7.4 +python-synology==0.8.0 # homeassistant.components.tado python-tado==0.8.1 From a0f869a7989d984e1607fc7d774c71b1297b104b Mon Sep 17 00:00:00 2001 From: Quentin POLLET Date: Sat, 2 May 2020 19:38:48 +0200 Subject: [PATCH 2/2] Review 1 --- homeassistant/components/synology_dsm/sensor.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/synology_dsm/sensor.py b/homeassistant/components/synology_dsm/sensor.py index eecbe2ad90fe32..b6a88fe5a5a948 100644 --- a/homeassistant/components/synology_dsm/sensor.py +++ b/homeassistant/components/synology_dsm/sensor.py @@ -13,6 +13,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import Entity from homeassistant.helpers.typing import HomeAssistantType +from homeassistant.util.temperature import celsius_to_fahrenheit from . import SynoApi from .const import ( @@ -156,7 +157,7 @@ def state(self): # Data (RAM) if self._unit == DATA_MEGABYTES: - return round(attr / 1024.0 / 1024.0, 1) + return round(attr / 1024.0 ** 2, 1) # Network if self._unit == DATA_RATE_KILOBYTES_PER_SECOND: @@ -177,13 +178,15 @@ def state(self): # Data (disk space) if self._unit == DATA_TERABYTES: - return round(attr / 1024.0 / 1024.0 / 1024.0 / 1024.0, 2) + return round(attr / 1024.0 ** 4, 2) # Temperature if self._api.temp_unit == TEMP_CELSIUS: + # Celsius return attr if self.sensor_type in TEMP_SENSORS_KEYS: - return round(attr * 1.8 + 32.0, 1) + # Fahrenheit + return celsius_to_fahrenheit(attr) return attr