Do not call update() in constructor (#8849)
* Do not call update() in constructor * Fix pylint issues
This commit is contained in:
parent
6832a2e642
commit
cd36a71f64
1 changed files with 43 additions and 46 deletions
|
@ -7,16 +7,16 @@ https://home-assistant.io/components/sensor.synologydsm/
|
||||||
import logging
|
import logging
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
||||||
from homeassistant.helpers.entity import Entity
|
|
||||||
from homeassistant.const import (
|
|
||||||
CONF_HOST, CONF_USERNAME, CONF_PASSWORD, CONF_PORT,
|
|
||||||
CONF_MONITORED_CONDITIONS, TEMP_CELSIUS, EVENT_HOMEASSISTANT_START)
|
|
||||||
from homeassistant.util import Throttle
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
|
from homeassistant.const import (
|
||||||
|
CONF_HOST, CONF_USERNAME, CONF_PASSWORD, CONF_PORT, TEMP_CELSIUS,
|
||||||
|
CONF_MONITORED_CONDITIONS, EVENT_HOMEASSISTANT_START)
|
||||||
|
from homeassistant.helpers.entity import Entity
|
||||||
|
from homeassistant.util import Throttle
|
||||||
|
|
||||||
REQUIREMENTS = ['python-synology==0.1.0']
|
REQUIREMENTS = ['python-synology==0.1.0']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -84,73 +84,71 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Set up the Synology NAS Sensor."""
|
"""Set up the Synology NAS Sensor."""
|
||||||
# pylint: disable=too-many-locals
|
|
||||||
def run_setup(event):
|
def run_setup(event):
|
||||||
"""Wait until HASS is fully initialized before creating.
|
"""Wait until Home Assistant is fully initialized before creating.
|
||||||
|
|
||||||
Delay the setup until Home Assistant is fully initialized.
|
Delay the setup until Home Assistant is fully initialized.
|
||||||
This allows any entities to be created already
|
This allows any entities to be created already
|
||||||
"""
|
"""
|
||||||
# Setup API
|
host = config.get(CONF_HOST)
|
||||||
api = SynoApi(config.get(CONF_HOST), config.get(CONF_PORT),
|
port = config.get(CONF_PORT)
|
||||||
config.get(CONF_USERNAME), config.get(CONF_PASSWORD),
|
username = config.get(CONF_USERNAME)
|
||||||
hass.config.units.temperature_unit)
|
password = config.get(CONF_PASSWORD)
|
||||||
|
unit = hass.config.units.temperature_unit
|
||||||
|
monitored_conditions = config.get(CONF_MONITORED_CONDITIONS)
|
||||||
|
|
||||||
sensors = [SynoNasUtilSensor(api, variable,
|
api = SynoApi(host, port, username, password, unit)
|
||||||
_UTILISATION_MON_COND[variable])
|
|
||||||
for variable in config[CONF_MONITORED_CONDITIONS]
|
sensors = [SynoNasUtilSensor(
|
||||||
|
api, variable, _UTILISATION_MON_COND[variable])
|
||||||
|
for variable in monitored_conditions
|
||||||
if variable in _UTILISATION_MON_COND]
|
if variable in _UTILISATION_MON_COND]
|
||||||
|
|
||||||
# Handle all Volumes
|
# Handle all volumes
|
||||||
volumes = config['volumes']
|
volumes = config['volumes']
|
||||||
if volumes is None:
|
if volumes is None:
|
||||||
volumes = api.storage.volumes
|
volumes = api.storage.volumes
|
||||||
|
|
||||||
for volume in volumes:
|
for volume in volumes:
|
||||||
sensors += [SynoNasStorageSensor(api, variable,
|
sensors += [SynoNasStorageSensor(
|
||||||
_STORAGE_VOL_MON_COND[variable],
|
api, variable, _STORAGE_VOL_MON_COND[variable], volume)
|
||||||
volume)
|
for variable in monitored_conditions
|
||||||
for variable in config[CONF_MONITORED_CONDITIONS]
|
|
||||||
if variable in _STORAGE_VOL_MON_COND]
|
if variable in _STORAGE_VOL_MON_COND]
|
||||||
|
|
||||||
# Handle all Disks
|
# Handle all disks
|
||||||
disks = config['disks']
|
disks = config['disks']
|
||||||
if disks is None:
|
if disks is None:
|
||||||
disks = api.storage.disks
|
disks = api.storage.disks
|
||||||
|
|
||||||
for disk in disks:
|
for disk in disks:
|
||||||
sensors += [SynoNasStorageSensor(api, variable,
|
sensors += [SynoNasStorageSensor(
|
||||||
_STORAGE_DSK_MON_COND[variable],
|
api, variable, _STORAGE_DSK_MON_COND[variable], disk)
|
||||||
disk)
|
for variable in monitored_conditions
|
||||||
for variable in config[CONF_MONITORED_CONDITIONS]
|
|
||||||
if variable in _STORAGE_DSK_MON_COND]
|
if variable in _STORAGE_DSK_MON_COND]
|
||||||
|
|
||||||
add_devices_callback(sensors)
|
add_devices(sensors, True)
|
||||||
|
|
||||||
# Wait until start event is sent to load this component.
|
# Wait until start event is sent to load this component.
|
||||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, run_setup)
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, run_setup)
|
||||||
|
|
||||||
|
|
||||||
class SynoApi():
|
class SynoApi(object):
|
||||||
"""Class to interface with API."""
|
"""Class to interface with Synology DSM API."""
|
||||||
|
|
||||||
# pylint: disable=too-many-arguments, bare-except
|
# pylint: disable=bare-except
|
||||||
def __init__(self, host, port, username, password, temp_unit):
|
def __init__(self, host, port, username, password, temp_unit):
|
||||||
"""Initialize the API wrapper class."""
|
"""Initialize the API wrapper class."""
|
||||||
from SynologyDSM import SynologyDSM
|
from SynologyDSM import SynologyDSM
|
||||||
self.temp_unit = temp_unit
|
self.temp_unit = temp_unit
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._api = SynologyDSM(host,
|
self._api = SynologyDSM(host, port, username, password)
|
||||||
port,
|
|
||||||
username,
|
|
||||||
password)
|
|
||||||
except:
|
except:
|
||||||
_LOGGER.error("Error setting up Synology DSM")
|
_LOGGER.error("Error setting up Synology DSM")
|
||||||
|
|
||||||
# Will be updated when `update` gets called.
|
# Will be updated when update() gets called.
|
||||||
self.utilisation = self._api.utilisation
|
self.utilisation = self._api.utilisation
|
||||||
self.storage = self._api.storage
|
self.storage = self._api.storage
|
||||||
|
|
||||||
|
@ -161,14 +159,14 @@ class SynoApi():
|
||||||
|
|
||||||
|
|
||||||
class SynoNasSensor(Entity):
|
class SynoNasSensor(Entity):
|
||||||
"""Representation of a Synology Nas Sensor."""
|
"""Representation of a Synology NAS Sensor."""
|
||||||
|
|
||||||
def __init__(self, api, variable, variableInfo, monitor_device=None):
|
def __init__(self, api, variable, variable_info, monitor_device=None):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self.var_id = variable
|
self.var_id = variable
|
||||||
self.var_name = variableInfo[0]
|
self.var_name = variable_info[0]
|
||||||
self.var_units = variableInfo[1]
|
self.var_units = variable_info[1]
|
||||||
self.var_icon = variableInfo[2]
|
self.var_icon = variable_info[2]
|
||||||
self.monitor_device = monitor_device
|
self.monitor_device = monitor_device
|
||||||
self._api = api
|
self._api = api
|
||||||
|
|
||||||
|
@ -231,13 +229,12 @@ class SynoNasStorageSensor(SynoNasSensor):
|
||||||
|
|
||||||
if self.monitor_device is not None:
|
if self.monitor_device is not None:
|
||||||
if self.var_id in temp_sensors:
|
if self.var_id in temp_sensors:
|
||||||
attr = getattr(self._api.storage,
|
attr = getattr(
|
||||||
self.var_id)(self.monitor_device)
|
self._api.storage, self.var_id)(self.monitor_device)
|
||||||
|
|
||||||
if self._api.temp_unit == TEMP_CELSIUS:
|
if self._api.temp_unit == TEMP_CELSIUS:
|
||||||
return attr
|
return attr
|
||||||
|
|
||||||
return round(attr * 1.8 + 32.0, 1)
|
return round(attr * 1.8 + 32.0, 1)
|
||||||
|
|
||||||
return getattr(self._api.storage,
|
return getattr(self._api.storage, self.var_id)(self.monitor_device)
|
||||||
self.var_id)(self.monitor_device)
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue