hass-core/homeassistant/components/synology_dsm/sensor.py
Quentame 26cbca101a
Refactor Synology entries to allow not fetching the API when it's disabled + add security binary sensor (#35565)
- add Synology DSM Security binary sensor (enabled by default)
- use device name instead of id in names
- add device type to name
- show disk manufacturer, model and firmware version in devices
- some entries are disabled by default (`entity_registry_enabled_default`)
- binary sensor + sensor uses `device_class` when possible
- do not fetch a concerned API if all entries of it are disabled
- entity unique_id now uses key instead of label
- entity entity_id changes for disk and volume: example from `sensor.synology_status_sda` to `sensor.synology_drive_1_status`, or from `sensor.synology_average_disk_temp_volume_1` to `sensor.synology_volume_1_average_disk_temp`
- now binary sensor:
  - disk_exceed_bad_sector_thr
  - disk_below_remain_life_thr
- removed sensor:
  - volume type (RAID, SHR ...)
  - disk name (Drive [X])
  - disk device (/dev/sd[Y])
2020-06-02 18:22:51 +02:00

107 lines
3 KiB
Python

"""Support for Synology DSM sensors."""
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_DISKS,
DATA_MEGABYTES,
DATA_RATE_KILOBYTES_PER_SECOND,
DATA_TERABYTES,
PRECISION_TENTHS,
TEMP_CELSIUS,
)
from homeassistant.helpers.temperature import display_temp
from homeassistant.helpers.typing import HomeAssistantType
from . import SynologyDSMDeviceEntity, SynologyDSMEntity
from .const import (
CONF_VOLUMES,
DOMAIN,
STORAGE_DISK_SENSORS,
STORAGE_VOL_SENSORS,
SYNO_API,
TEMP_SENSORS_KEYS,
UTILISATION_SENSORS,
)
async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
) -> None:
"""Set up the Synology NAS Sensor."""
api = hass.data[DOMAIN][entry.unique_id][SYNO_API]
entities = [
SynoDSMUtilSensor(api, sensor_type, UTILISATION_SENSORS[sensor_type])
for sensor_type in UTILISATION_SENSORS
]
# Handle all volumes
if api.storage.volumes_ids:
for volume in entry.data.get(CONF_VOLUMES, api.storage.volumes_ids):
entities += [
SynoDSMStorageSensor(
api, sensor_type, STORAGE_VOL_SENSORS[sensor_type], volume
)
for sensor_type in STORAGE_VOL_SENSORS
]
# Handle all disks
if api.storage.disks_ids:
for disk in entry.data.get(CONF_DISKS, api.storage.disks_ids):
entities += [
SynoDSMStorageSensor(
api, sensor_type, STORAGE_DISK_SENSORS[sensor_type], disk
)
for sensor_type in STORAGE_DISK_SENSORS
]
async_add_entities(entities)
class SynoDSMUtilSensor(SynologyDSMEntity):
"""Representation a Synology Utilisation sensor."""
@property
def state(self):
"""Return the state."""
attr = getattr(self._api.utilisation, self.entity_type)
if callable(attr):
attr = attr()
if attr is None:
return None
# Data (RAM)
if self._unit == DATA_MEGABYTES:
return round(attr / 1024.0 ** 2, 1)
# Network
if self._unit == DATA_RATE_KILOBYTES_PER_SECOND:
return round(attr / 1024.0, 1)
return attr
@property
def available(self) -> bool:
"""Return True if entity is available."""
return bool(self._api.utilisation)
class SynoDSMStorageSensor(SynologyDSMDeviceEntity):
"""Representation a Synology Storage sensor."""
@property
def state(self):
"""Return the state."""
attr = getattr(self._api.storage, self.entity_type)(self._device_id)
if attr is None:
return None
# Data (disk space)
if self._unit == DATA_TERABYTES:
return round(attr / 1024.0 ** 4, 2)
# Temperature
if self.entity_type in TEMP_SENSORS_KEYS:
return display_temp(self.hass, attr, TEMP_CELSIUS, PRECISION_TENTHS)
return attr