Add temperature and uptime to Synology DSM (#39419)

Co-authored-by: Quentame <polletquentin74@me.com>
This commit is contained in:
Thomas Germain 2020-09-14 09:42:37 +02:00 committed by GitHub
parent eb0af3752c
commit 3881e0cb23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 10 deletions

View file

@ -234,6 +234,7 @@ class SynoApi:
self._with_security = True
self._with_storage = True
self._with_utilisation = True
self._with_information = True
self._with_surveillance_station = True
self._unsub_dispatcher = None
@ -304,11 +305,14 @@ class SynoApi:
self._with_utilisation = bool(
self._fetching_entities.get(SynoCoreUtilization.API_KEY)
)
self._with_information = bool(
self._fetching_entities.get(SynoDSMInformation.API_KEY)
)
self._with_surveillance_station = bool(
self._fetching_entities.get(SynoSurveillanceStation.CAMERA_API_KEY)
)
# Reset not used API
# Reset not used API, information is not reset since it's used in device_info
if not self._with_security:
self.dsm.reset(self.security)
self.security = None
@ -351,7 +355,7 @@ class SynoApi:
async def async_update(self, now=None):
"""Update function for updating API information."""
self._async_setup_api_requests()
await self._hass.async_add_executor_job(self.dsm.update)
await self._hass.async_add_executor_job(self.dsm.update, self._with_information)
async_dispatcher_send(self._hass, self.signal_sensor_update)

View file

@ -2,6 +2,7 @@
from synology_dsm.api.core.security import SynoCoreSecurity
from synology_dsm.api.core.utilization import SynoCoreUtilization
from synology_dsm.api.dsm.information import SynoDSMInformation
from synology_dsm.api.storage.storage import SynoStorage
from homeassistant.components.binary_sensor import DEVICE_CLASS_SAFETY
@ -9,6 +10,8 @@ from homeassistant.const import (
DATA_MEGABYTES,
DATA_RATE_KILOBYTES_PER_SECOND,
DATA_TERABYTES,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_TIMESTAMP,
PERCENTAGE,
)
@ -213,15 +216,15 @@ STORAGE_VOL_SENSORS = {
f"{SynoStorage.API_KEY}:volume_disk_temp_avg": {
ENTITY_NAME: "Average Disk Temp",
ENTITY_UNIT: None,
ENTITY_ICON: "mdi:thermometer",
ENTITY_CLASS: "temperature",
ENTITY_ICON: None,
ENTITY_CLASS: DEVICE_CLASS_TEMPERATURE,
ENTITY_ENABLE: True,
},
f"{SynoStorage.API_KEY}:volume_disk_temp_max": {
ENTITY_NAME: "Maximum Disk Temp",
ENTITY_UNIT: None,
ENTITY_ICON: "mdi:thermometer",
ENTITY_CLASS: "temperature",
ENTITY_ICON: None,
ENTITY_CLASS: DEVICE_CLASS_TEMPERATURE,
ENTITY_ENABLE: False,
},
}
@ -243,11 +246,33 @@ STORAGE_DISK_SENSORS = {
f"{SynoStorage.API_KEY}:disk_temp": {
ENTITY_NAME: "Temperature",
ENTITY_UNIT: None,
ENTITY_ICON: "mdi:thermometer",
ENTITY_CLASS: "temperature",
ENTITY_ICON: None,
ENTITY_CLASS: DEVICE_CLASS_TEMPERATURE,
ENTITY_ENABLE: True,
},
}
INFORMATION_SENSORS = {
f"{SynoDSMInformation.API_KEY}:temperature": {
ENTITY_NAME: "temperature",
ENTITY_UNIT: None,
ENTITY_ICON: None,
ENTITY_CLASS: DEVICE_CLASS_TEMPERATURE,
ENTITY_ENABLE: True,
},
f"{SynoDSMInformation.API_KEY}:uptime": {
ENTITY_NAME: "last boot",
ENTITY_UNIT: None,
ENTITY_ICON: None,
ENTITY_CLASS: DEVICE_CLASS_TIMESTAMP,
ENTITY_ENABLE: False,
},
}
TEMP_SENSORS_KEYS = ["volume_disk_temp_avg", "volume_disk_temp_max", "disk_temp"]
TEMP_SENSORS_KEYS = [
"volume_disk_temp_avg",
"volume_disk_temp_max",
"disk_temp",
"temperature",
]

View file

@ -1,4 +1,7 @@
"""Support for Synology DSM sensors."""
from datetime import timedelta
from typing import Dict
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_DISKS,
@ -10,11 +13,13 @@ from homeassistant.const import (
)
from homeassistant.helpers.temperature import display_temp
from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.util.dt import utcnow
from . import SynologyDSMDeviceEntity, SynologyDSMEntity
from . import SynoApi, SynologyDSMDeviceEntity, SynologyDSMEntity
from .const import (
CONF_VOLUMES,
DOMAIN,
INFORMATION_SENSORS,
STORAGE_DISK_SENSORS,
STORAGE_VOL_SENSORS,
SYNO_API,
@ -55,6 +60,11 @@ async def async_setup_entry(
for sensor_type in STORAGE_DISK_SENSORS
]
entities += [
SynoDSMInfoSensor(api, sensor_type, INFORMATION_SENSORS[sensor_type])
for sensor_type in INFORMATION_SENSORS
]
async_add_entities(entities)
@ -105,3 +115,34 @@ class SynoDSMStorageSensor(SynologyDSMDeviceEntity):
return display_temp(self.hass, attr, TEMP_CELSIUS, PRECISION_TENTHS)
return attr
class SynoDSMInfoSensor(SynologyDSMEntity):
"""Representation a Synology information sensor."""
def __init__(self, api: SynoApi, entity_type: str, entity_info: Dict[str, str]):
"""Initialize the Synology SynoDSMInfoSensor entity."""
super().__init__(api, entity_type, entity_info)
self._previous_uptime = None
self._last_boot = None
@property
def state(self):
"""Return the state."""
attr = getattr(self._api.information, self.entity_type)
if attr is None:
return None
# Temperature
if self.entity_type in TEMP_SENSORS_KEYS:
return display_temp(self.hass, attr, TEMP_CELSIUS, PRECISION_TENTHS)
if self.entity_type == "uptime":
# reboot happened or entity creation
if self._previous_uptime is None or self._previous_uptime > attr:
last_boot = utcnow() - timedelta(seconds=attr)
self._last_boot = last_boot.replace(microsecond=0).isoformat()
self._previous_uptime = attr
return self._last_boot
return attr