Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
HomeAssistant
Core
Commits
cd36a71f
Commit
cd36a71f
authored
7 years ago
by
Fabian Affolter
Committed by
GitHub
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Do not call update() in constructor (#8849)
* Do not call update() in constructor * Fix pylint issues
parent
6832a2e6
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
homeassistant/components/sensor/synologydsm.py
+42
-45
42 additions, 45 deletions
homeassistant/components/sensor/synologydsm.py
with
42 additions
and
45 deletions
homeassistant/components/sensor/synologydsm.py
+
42
−
45
View file @
cd36a71f
...
@@ -7,15 +7,15 @@ https://home-assistant.io/components/sensor.synologydsm/
...
@@ -7,15 +7,15 @@ https://home-assistant.io/components/sensor.synologydsm/
import
logging
import
logging
from
datetime
import
timedelta
from
datetime
import
timedelta
import
voluptuous
as
vol
import
homeassistant.helpers.config_validation
as
cv
from
homeassistant.components.sensor
import
PLATFORM_SCHEMA
from
homeassistant.components.sensor
import
PLATFORM_SCHEMA
from
homeassistant.helpers.entity
import
Entity
from
homeassistant.const
import
(
from
homeassistant.const
import
(
CONF_HOST
,
CONF_USERNAME
,
CONF_PASSWORD
,
CONF_PORT
,
CONF_HOST
,
CONF_USERNAME
,
CONF_PASSWORD
,
CONF_PORT
,
TEMP_CELSIUS
,
CONF_MONITORED_CONDITIONS
,
TEMP_CELSIUS
,
EVENT_HOMEASSISTANT_START
)
CONF_MONITORED_CONDITIONS
,
EVENT_HOMEASSISTANT_START
)
from
homeassistant.helpers.entity
import
Entity
from
homeassistant.util
import
Throttle
from
homeassistant.util
import
Throttle
import
homeassistant.helpers.config_validation
as
cv
import
voluptuous
as
vol
REQUIREMENTS
=
[
'
python-synology==0.1.0
'
]
REQUIREMENTS
=
[
'
python-synology==0.1.0
'
]
...
@@ -84,73 +84,71 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
...
@@ -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 H
ASS
is fully initialized before creating.
"""
Wait until H
ome 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
sensors
=
[
SynoNasUtilSensor
(
api
,
variable
,
monitored_conditions
=
config
.
get
(
CONF_MONITORED_CONDITIONS
)
_UTILISATION_MON_COND
[
variable
])
for
variable
in
config
[
CONF_MONITORED_CONDITIONS
]
api
=
SynoApi
(
host
,
port
,
username
,
password
,
unit
)
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
V
olumes
# Handle all
v
olumes
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
D
isks
# Handle all
d
isks
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():
...
@@ -161,14 +159,14 @@ class SynoApi():
class
SynoNasSensor
(
Entity
):
class
SynoNasSensor
(
Entity
):
"""
Representation of a Synology N
as
Sensor.
"""
"""
Representation of a Synology N
AS
Sensor.
"""
def
__init__
(
self
,
api
,
variable
,
variable
I
nfo
,
monitor_device
=
None
):
def
__init__
(
self
,
api
,
variable
,
variable
_i
nfo
,
monitor_device
=
None
):
"""
Initialize the sensor.
"""
"""
Initialize the sensor.
"""
self
.
var_id
=
variable
self
.
var_id
=
variable
self
.
var_name
=
variable
I
nfo
[
0
]
self
.
var_name
=
variable
_i
nfo
[
0
]
self
.
var_units
=
variable
I
nfo
[
1
]
self
.
var_units
=
variable
_i
nfo
[
1
]
self
.
var_icon
=
variable
I
nfo
[
2
]
self
.
var_icon
=
variable
_i
nfo
[
2
]
self
.
monitor_device
=
monitor_device
self
.
monitor_device
=
monitor_device
self
.
_api
=
api
self
.
_api
=
api
...
@@ -231,13 +229,12 @@ class SynoNasStorageSensor(SynoNasSensor):
...
@@ -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
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment