|
7 | 7 | CONF_DISKS, |
8 | 8 | DATA_MEGABYTES, |
9 | 9 | DATA_RATE_KILOBYTES_PER_SECOND, |
| 10 | + DATA_TERABYTES, |
10 | 11 | TEMP_CELSIUS, |
11 | 12 | ) |
12 | 13 | from homeassistant.helpers.dispatcher import async_dispatcher_connect |
13 | 14 | from homeassistant.helpers.entity import Entity |
14 | 15 | from homeassistant.helpers.typing import HomeAssistantType |
| 16 | +from homeassistant.util.temperature import celsius_to_fahrenheit |
15 | 17 |
|
16 | 18 | from . import SynoApi |
17 | 19 | from .const import ( |
@@ -63,7 +65,7 @@ async def async_setup_entry( |
63 | 65 |
|
64 | 66 |
|
65 | 67 | class SynoNasSensor(Entity): |
66 | | - """Representation of a Synology NAS Sensor.""" |
| 68 | + """Representation of a Synology NAS sensor.""" |
67 | 69 |
|
68 | 70 | def __init__( |
69 | 71 | self, |
@@ -142,47 +144,51 @@ async def async_will_remove_from_hass(self): |
142 | 144 |
|
143 | 145 |
|
144 | 146 | class SynoNasUtilSensor(SynoNasSensor): |
145 | | - """Representation a Synology Utilisation Sensor.""" |
| 147 | + """Representation a Synology Utilisation sensor.""" |
146 | 148 |
|
147 | 149 | @property |
148 | 150 | def state(self): |
149 | 151 | """Return the state.""" |
150 | | - if self._unit == DATA_RATE_KILOBYTES_PER_SECOND or self._unit == DATA_MEGABYTES: |
151 | | - attr = getattr(self._api.utilisation, self.sensor_type)(False) |
| 152 | + attr = getattr(self._api.utilisation, self.sensor_type) |
| 153 | + if callable(attr): |
| 154 | + attr = attr() |
| 155 | + if not attr: |
| 156 | + return None |
152 | 157 |
|
153 | | - if attr is None: |
154 | | - return None |
| 158 | + # Data (RAM) |
| 159 | + if self._unit == DATA_MEGABYTES: |
| 160 | + return round(attr / 1024.0 ** 2, 1) |
155 | 161 |
|
156 | | - if self._unit == DATA_RATE_KILOBYTES_PER_SECOND: |
157 | | - return round(attr / 1024.0, 1) |
158 | | - if self._unit == DATA_MEGABYTES: |
159 | | - return round(attr / 1024.0 / 1024.0, 1) |
160 | | - else: |
161 | | - return getattr(self._api.utilisation, self.sensor_type) |
| 162 | + # Network |
| 163 | + if self._unit == DATA_RATE_KILOBYTES_PER_SECOND: |
| 164 | + return round(attr / 1024.0, 1) |
| 165 | + |
| 166 | + return attr |
162 | 167 |
|
163 | 168 |
|
164 | 169 | class SynoNasStorageSensor(SynoNasSensor): |
165 | | - """Representation a Synology Storage Sensor.""" |
| 170 | + """Representation a Synology Storage sensor.""" |
166 | 171 |
|
167 | 172 | @property |
168 | 173 | def state(self): |
169 | 174 | """Return the state.""" |
170 | | - if self.monitored_device: |
171 | | - if self.sensor_type in TEMP_SENSORS_KEYS: |
172 | | - attr = getattr(self._api.storage, self.sensor_type)( |
173 | | - self.monitored_device |
174 | | - ) |
175 | | - |
176 | | - if attr is None: |
177 | | - return None |
178 | | - |
179 | | - if self._api.temp_unit == TEMP_CELSIUS: |
180 | | - return attr |
181 | | - |
182 | | - return round(attr * 1.8 + 32.0, 1) |
| 175 | + attr = getattr(self._api.storage, self.sensor_type)(self.monitored_device) |
| 176 | + if not attr: |
| 177 | + return None |
| 178 | + |
| 179 | + # Data (disk space) |
| 180 | + if self._unit == DATA_TERABYTES: |
| 181 | + return round(attr / 1024.0 ** 4, 2) |
| 182 | + |
| 183 | + # Temperature |
| 184 | + if self._api.temp_unit == TEMP_CELSIUS: |
| 185 | + # Celsius |
| 186 | + return attr |
| 187 | + if self.sensor_type in TEMP_SENSORS_KEYS: |
| 188 | + # Fahrenheit |
| 189 | + return celsius_to_fahrenheit(attr) |
183 | 190 |
|
184 | | - return getattr(self._api.storage, self.sensor_type)(self.monitored_device) |
185 | | - return None |
| 191 | + return attr |
186 | 192 |
|
187 | 193 | @property |
188 | 194 | def device_info(self) -> Dict[str, any]: |
|
0 commit comments