Add update available binary sensor to synology_dsm (#42378)

* 'update available' binary-sensor

* make black compliant

* add available() to SynoDSMUpgradeBinarySensor

* correct spacing

* Correct usage of _with_upgrade

Co-authored-by: Quentame <polletquentin74@me.com>
pull/42523/head
Michael 2020-10-28 20:56:18 +01:00 committed by GitHub
parent c480284861
commit 36b1756816
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 0 deletions

View File

@ -6,6 +6,7 @@ from typing import Dict
from synology_dsm import SynologyDSM
from synology_dsm.api.core.security import SynoCoreSecurity
from synology_dsm.api.core.upgrade import SynoCoreUpgrade
from synology_dsm.api.core.utilization import SynoCoreUtilization
from synology_dsm.api.dsm.information import SynoDSMInformation
from synology_dsm.api.dsm.network import SynoDSMNetwork
@ -230,6 +231,7 @@ class SynoApi:
self.information: SynoDSMInformation = None
self.network: SynoDSMNetwork = None
self.security: SynoCoreSecurity = None
self.upgrade: SynoCoreUpgrade = None
self.storage: SynoStorage = None
self.utilisation: SynoCoreUtilization = None
self.surveillance_station: SynoSurveillanceStation = None
@ -238,6 +240,7 @@ class SynoApi:
self._fetching_entities = {}
self._with_security = True
self._with_storage = True
self._with_upgrade = True
self._with_utilisation = True
self._with_information = True
self._with_surveillance_station = True
@ -308,6 +311,7 @@ class SynoApi:
self._fetching_entities.get(SynoCoreSecurity.API_KEY)
)
self._with_storage = bool(self._fetching_entities.get(SynoStorage.API_KEY))
self._with_upgrade = bool(self._fetching_entities.get(SynoCoreUpgrade.API_KEY))
self._with_utilisation = bool(
self._fetching_entities.get(SynoCoreUtilization.API_KEY)
)
@ -329,6 +333,10 @@ class SynoApi:
self.dsm.reset(self.storage)
self.storage = None
if not self._with_upgrade:
self.dsm.reset(self.upgrade)
self.upgrade = None
if not self._with_utilisation:
self.dsm.reset(self.utilisation)
self.utilisation = None
@ -349,6 +357,9 @@ class SynoApi:
if self._with_storage:
self.storage = self.dsm.storage
if self._with_upgrade:
self.upgrade = self.dsm.upgrade
if self._with_utilisation:
self.utilisation = self.dsm.utilisation

View File

@ -12,6 +12,7 @@ from .const import (
SECURITY_BINARY_SENSORS,
STORAGE_DISK_BINARY_SENSORS,
SYNO_API,
UPGRADE_BINARY_SENSORS,
)
@ -29,6 +30,13 @@ async def async_setup_entry(
for sensor_type in SECURITY_BINARY_SENSORS
]
entities += [
SynoDSMUpgradeBinarySensor(
api, sensor_type, UPGRADE_BINARY_SENSORS[sensor_type]
)
for sensor_type in UPGRADE_BINARY_SENSORS
]
# Handle all disks
if api.storage.disks_ids:
for disk in entry.data.get(CONF_DISKS, api.storage.disks_ids):
@ -68,3 +76,17 @@ class SynoDSMStorageBinarySensor(SynologyDSMDeviceEntity, BinarySensorEntity):
def is_on(self) -> bool:
"""Return the state."""
return getattr(self._api.storage, self.entity_type)(self._device_id)
class SynoDSMUpgradeBinarySensor(SynologyDSMEntity, BinarySensorEntity):
"""Representation a Synology Upgrade binary sensor."""
@property
def is_on(self) -> bool:
"""Return the state."""
return getattr(self._api.upgrade, self.entity_type)
@property
def available(self) -> bool:
"""Return True if entity is available."""
return bool(self._api.upgrade)

View File

@ -1,6 +1,7 @@
"""Constants for Synology DSM."""
from synology_dsm.api.core.security import SynoCoreSecurity
from synology_dsm.api.core.upgrade import SynoCoreUpgrade
from synology_dsm.api.core.utilization import SynoCoreUtilization
from synology_dsm.api.dsm.information import SynoDSMInformation
from synology_dsm.api.storage.storage import SynoStorage
@ -43,6 +44,16 @@ ENTITY_ENABLE = "enable"
# Entity keys should start with the API_KEY to fetch
# Binary sensors
UPGRADE_BINARY_SENSORS = {
f"{SynoCoreUpgrade.API_KEY}:update_available": {
ENTITY_NAME: "Update available",
ENTITY_UNIT: None,
ENTITY_ICON: "mdi:update",
ENTITY_CLASS: None,
ENTITY_ENABLE: True,
},
}
SECURITY_BINARY_SENSORS = {
f"{SynoCoreSecurity.API_KEY}:status": {
ENTITY_NAME: "Security status",